-
Notifications
You must be signed in to change notification settings - Fork 698
/
Def.java
149 lines (125 loc) · 3.41 KB
/
Def.java
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
package org.python.indexer;
import org.python.indexer.ast.Name;
import org.python.indexer.ast.Node;
import org.python.indexer.ast.Url;
/**
* 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 Binding binding;
private String fileOrUrl;
private String name;
private Node node;
public Def(Node node) {
this(node, null);
}
public Def(Node node, Binding b) {
if (node == null) {
throw new IllegalArgumentException("null 'node' param");
}
this.node = node;
binding = b;
if (node instanceof Url) {
String url = ((Url)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 Name) {
name = node.asName().getId();
}
}
/**
* Returns the name of the node. Only applies if the definition coincides
* with a {@link org.python.indexer.ast.Name} 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.getKind() == Binding.Kind.MODULE;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
public int getLength() {
return end - start;
}
public boolean isName() {
return name != null;
}
void setBinding(Binding b) {
binding = b;
}
public Binding getBinding() {
return binding;
}
public Node getNode() {
return node;
}
public void setNode(Node node) {
this.node = node;
}
@Override
public String toString() {
return "<Def:" + (name == null ? "" : name) +
":" + start + ":" + fileOrUrl + ">";
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Def)) {
return false;
} else {
Def def = (Def)obj;
return (start == def.start
&& end == def.end
&& (fileOrUrl == null && def.fileOrUrl == null
|| fileOrUrl == def.fileOrUrl));
}
}
@Override
public int hashCode() {
return ("" + fileOrUrl + start).hashCode();
}
}