-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathDef.java
More file actions
162 lines (141 loc) · 3.73 KB
/
Def.java
File metadata and controls
162 lines (141 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright 2009, Google Inc. All rights reserved.
* Licensed to PSF under a Contributor Agreement.
*/
package org.python.indexer;
import org.python.indexer.ast.NName;
import org.python.indexer.ast.NNode;
import org.python.indexer.ast.NUrl;
/**
* Encapsulates information about a binding definition site.
*/
public class Def {
// Being frugal with fields here is good for memory usage.
private int start;
private int end;
private NBinding binding;
private String fileOrUrl;
private String name;
public Def(NNode node) {
this(node, null);
}
public Def(NNode node, NBinding b) {
if (node == null) {
throw new IllegalArgumentException("null 'node' param");
}
binding = b;
if (node instanceof NUrl) {
String url = ((NUrl)node).getURL();
if (url.startsWith("file://")) {
fileOrUrl = url.substring("file://".length());
} else {
fileOrUrl = url;
}
return;
}
// start/end offsets are invalid/bogus for NUrls
start = node.start();
end = node.end();
fileOrUrl = node.getFile();
if (fileOrUrl == null) {
throw new IllegalArgumentException("Non-URL nodes must have a non-null file");
}
if (node instanceof NName) {
name = ((NName)node).id;
}
}
/**
* Returns the name of the node. Only applies if the definition coincides
* with a {@link NName} node.
* @return the name, or null
*/
public String getName() {
return name;
}
/**
* Returns the file if this node is from a source file, else {@code null}.
*/
public String getFile() {
return isURL() ? null : fileOrUrl;
}
/**
* Returns the URL if this node is from a URL, else {@code null}.
*/
public String getURL() {
return isURL() ? fileOrUrl : null;
}
/**
* Returns the file if from a source file, else the URL.
*/
public String getFileOrUrl() {
return fileOrUrl;
}
/**
* Returns {@code true} if this node is from a URL.
*/
public boolean isURL() {
return fileOrUrl.startsWith("http://");
}
public boolean isModule() {
return binding != null && binding.kind == NBinding.Kind.MODULE;
}
public int start() {
return start;
}
public int end() {
return end;
}
public int length() {
return end - start;
}
public boolean isName() {
return name != null;
}
void setBinding(NBinding b) {
binding = b;
}
public NBinding getBinding() {
return binding;
}
@Override
public String toString() {
return "<Def:" + (name == null ? "" : name) +
":" + start + ":" + fileOrUrl + ">";
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Def)) {
return false;
}
Def def = (Def)obj;
if (start != def.start) {
return false;
}
if (end != def.end) {
return false;
}
if (name != null) {
if (!name.equals(def.name)) {
return false;
}
} else {
if (def.name != null) {
return false;
}
}
if (fileOrUrl != null) {
if (!fileOrUrl.equals(def.fileOrUrl)) {
return false;
}
} else {
if (def.fileOrUrl != null) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
return ("" + fileOrUrl + name + start + end).hashCode();
}
}