Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit 34f9dd4

Browse files
authored
introduce new import node (#499)
* introduce new `import` node `import`s are both definitions (the introduction of a name to a syntax tree) and references (they refere to a name outside this syntax tree). this should help improve cross-file navigation using the following heuristics: - the definition of a name can exclude imports in cross-file navigation by default - if no definition exists for a name across the entire repo, this name is an externally definied name, we can mark its import node in the same syntax-tree (or file) as its definition node * allow navigation from an import node * improve rust & python scope-queries * refine js scope queries * remove reference to self in navigation results - do not search for definitions in other files if present locally * fixup * support template strings in js
1 parent b61b485 commit 34f9dd4

File tree

18 files changed

+548
-218
lines changed

18 files changed

+548
-218
lines changed

server/bleep/src/intelligence/code_navigation.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,32 @@ impl<'a> CurrentFileHandler<'a> {
3434
let Self {
3535
scope_graph, idx, ..
3636
} = self;
37-
let (defs, mut refs) = scope_graph
37+
let (defs, refs): (Vec<_>, Vec<_>) = scope_graph
3838
.definitions(*idx) // for every possible def...
39-
.map(|def| {
40-
// get its corresponding (range, refs)
39+
.chain(scope_graph.imports(*idx))
40+
.map(|node_idx| {
41+
// get its corresponding (node, refs)
4142
(
42-
scope_graph.graph[def].range(),
43+
&scope_graph.graph[node_idx],
4344
scope_graph
44-
.references(def)
45-
.map(|i| scope_graph.graph[i].range()),
45+
.references(node_idx)
46+
.map(|i| scope_graph.graph[i].range())
47+
.collect::<Vec<_>>(),
4648
)
4749
})
48-
.fold(
49-
// collect all of that into (all definitions, all references)
50-
//
51-
// we collect into a BTreeSet just to be absolutely sure that
52-
// there are no dupes.
53-
(BTreeSet::new(), BTreeSet::new()),
54-
|(mut defs, mut refs), (d, rs)| {
55-
defs.insert(d);
56-
for r in rs {
57-
refs.insert(r);
58-
}
59-
(defs, refs)
60-
},
61-
);
50+
.unzip();
6251

63-
// remove the currently hovered ref from the list
64-
refs.remove(&self.scope_graph.graph[self.idx].range());
52+
let defs = defs
53+
.into_iter()
54+
.filter(|d| matches!(d, NodeKind::Def(_)))
55+
.map(|d| d.range())
56+
.collect();
6557

66-
let defs = defs.into_iter().collect::<Vec<_>>();
58+
// remove self from the list of references
59+
let mut refs = refs.into_iter().flatten().collect::<BTreeSet<_>>();
60+
refs.remove(&self.scope_graph.graph[*idx].range());
6761
let refs = refs.into_iter().collect::<Vec<_>>();
62+
6863
(defs, refs)
6964
}
7065
}
@@ -102,14 +97,26 @@ impl<'a> RepoWideHandler<'a> {
10297
let (def_data, ref_data): (Vec<_>, Vec<_>) = graph
10398
.node_indices()
10499
.filter(|&node_idx| self.scope_graph.is_top_level(node_idx))
105-
.filter(|node_idx| matches!(&graph[*node_idx], NodeKind::Def(d) if d.name(src.as_bytes()) == self.token))
106-
.map(|node_idx| (graph[node_idx].range(), self.scope_graph.references(node_idx)))
100+
.filter(|node_idx| match &graph[*node_idx] {
101+
NodeKind::Def(d) => d.name(src.as_bytes()) == self.token,
102+
NodeKind::Import(i) => i.name(src.as_bytes()) == self.token,
103+
_ => false,
104+
})
105+
.map(|node_idx| (&graph[node_idx], self.scope_graph.references(node_idx)))
107106
.unzip();
107+
108108
let ref_data = ref_data
109109
.into_iter()
110110
.flatten()
111111
.map(|node_idx| graph[node_idx].range())
112112
.collect();
113+
114+
let def_data = def_data
115+
.into_iter()
116+
.filter(|node| matches!(node, NodeKind::Def(_)))
117+
.map(|node| node.range())
118+
.collect();
119+
113120
(def_data, ref_data)
114121
}
115122
}

server/bleep/src/intelligence/language/c_sharp/scopes.scm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,20 @@
168168

169169
;; using System.Text
170170
;;
171-
;; `Text` is a def
171+
;; `Text` is an import
172172
(using_directive
173173
.
174174
(qualified_name
175175
(_)
176176
.
177-
(identifier) @local.definition))
177+
(identifier) @local.import))
178178

179179
;; using Named = System.Text;
180180
;;
181181
;; `Named` is a def
182182
(using_directive
183183
(name_equals
184-
(identifier) @local.definition))
184+
(identifier) @local.import))
185185

186186

187187
;; refs

server/bleep/src/intelligence/language/go/mod.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod tests {
2727
const two, three = 2, 3
2828
"#;
2929

30-
let (_, d, _) = counts(src, "Go");
30+
let (_, d, _, _) = counts(src, "Go");
3131
assert_eq!(d, 3);
3232
}
3333

@@ -37,7 +37,7 @@ mod tests {
3737
const one uint64 = 1
3838
const two, three uint64 = 2, 3
3939
"#;
40-
let (_, d, _) = counts(src, "Go");
40+
let (_, d, _, _) = counts(src, "Go");
4141
assert_eq!(d, 3);
4242
}
4343

@@ -49,7 +49,7 @@ mod tests {
4949
one = 1
5050
)
5151
"#;
52-
let (_, d, _) = counts(src, "Go");
52+
let (_, d, _, _) = counts(src, "Go");
5353
assert_eq!(d, 2);
5454
}
5555

@@ -61,7 +61,7 @@ mod tests {
6161
one
6262
)
6363
"#;
64-
let (_, d, _) = counts(src, "Go");
64+
let (_, d, _, _) = counts(src, "Go");
6565
assert_eq!(d, 2);
6666
}
6767

@@ -74,7 +74,7 @@ mod tests {
7474
var one, two = 1, 2
7575
var three, four, five = 3, 4, 5
7676
"#;
77-
let (_, d, _) = counts(src, "Go");
77+
let (_, d, _, _) = counts(src, "Go");
7878
assert_eq!(d, 6);
7979
}
8080

@@ -86,7 +86,7 @@ mod tests {
8686
var zero uint64 = 0
8787
var one, two uint64 = 1, 2
8888
"#;
89-
let (_, d, _) = counts(src, "Go");
89+
let (_, d, _, _) = counts(src, "Go");
9090
assert_eq!(d, 3);
9191
}
9292

@@ -100,7 +100,7 @@ mod tests {
100100
one = 1
101101
)
102102
"#;
103-
let (_, d, _) = counts(src, "Go");
103+
let (_, d, _, _) = counts(src, "Go");
104104
assert_eq!(d, 2);
105105
}
106106

@@ -114,7 +114,7 @@ mod tests {
114114
"#;
115115

116116
// main, x, res, err
117-
let (_, d, _) = counts(src, "Go");
117+
let (_, d, _, _) = counts(src, "Go");
118118
assert_eq!(d, 4);
119119
}
120120

@@ -129,7 +129,7 @@ mod tests {
129129
func f4(result int, err error) {} // declares result, err
130130
func f5(x ... uint64, y ... uint64) {} // declares x, y
131131
"#;
132-
let (_, d, _) = counts(src, "Go");
132+
let (_, d, _, _) = counts(src, "Go");
133133

134134
// f1, f2, f3, f4, f5, result, err, x, y
135135
assert_eq!(d, 9);
@@ -148,7 +148,7 @@ mod tests {
148148
type s struct {}
149149
type i interface {}
150150
"#;
151-
let (_, d, _) = counts(src, "Go");
151+
let (_, d, _, _) = counts(src, "Go");
152152
assert_eq!(d, 5);
153153
}
154154

@@ -162,7 +162,7 @@ mod tests {
162162
b uint64
163163
)
164164
"#;
165-
let (_, d, _) = counts(src, "Go");
165+
let (_, d, _, _) = counts(src, "Go");
166166
assert_eq!(d, 2);
167167
}
168168

@@ -177,7 +177,7 @@ mod tests {
177177
"#;
178178

179179
// main, loop
180-
let (_, d, _) = counts(src, "Go");
180+
let (_, d, _, _) = counts(src, "Go");
181181
assert_eq!(d, 2);
182182
}
183183

@@ -190,7 +190,7 @@ mod tests {
190190
"#;
191191

192192
// main, t
193-
let (_, d, _) = counts(src, "Go");
193+
let (_, d, _, _) = counts(src, "Go");
194194
assert_eq!(d, 2);
195195
}
196196

@@ -205,7 +205,7 @@ mod tests {
205205
"#;
206206

207207
// 3 refs to a, 3 refs to b
208-
let (_, _, r) = counts(src, "Go");
208+
let (_, _, r, _) = counts(src, "Go");
209209
assert_eq!(r, 6);
210210
}
211211

@@ -220,7 +220,7 @@ mod tests {
220220
}
221221
"#;
222222

223-
let (_, _, r) = counts(src, "Go");
223+
let (_, _, r, _) = counts(src, "Go");
224224
assert_eq!(r, 2);
225225
}
226226

@@ -234,7 +234,7 @@ mod tests {
234234
}
235235
"#;
236236

237-
let (_, _, r) = counts(src, "Go");
237+
let (_, _, r, _) = counts(src, "Go");
238238
assert_eq!(r, 2);
239239
}
240240

@@ -247,7 +247,7 @@ mod tests {
247247
}
248248
"#;
249249

250-
let (_, _, r) = counts(src, "Go");
250+
let (_, _, r, _) = counts(src, "Go");
251251
assert_eq!(r, 1);
252252
}
253253

@@ -260,7 +260,7 @@ mod tests {
260260
}
261261
"#;
262262

263-
let (_, _, r) = counts(src, "Go");
263+
let (_, _, r, _) = counts(src, "Go");
264264
assert_eq!(r, 1);
265265
}
266266

@@ -277,7 +277,7 @@ mod tests {
277277
}
278278
"#;
279279

280-
let (_, _, r) = counts(src, "Go");
280+
let (_, _, r, _) = counts(src, "Go");
281281

282282
// p (variable ref), person (type ref)
283283
assert_eq!(r, 2);
@@ -292,7 +292,7 @@ mod tests {
292292
}
293293
"#;
294294

295-
let (_, _, r) = counts(src, "Go");
295+
let (_, _, r, _) = counts(src, "Go");
296296
assert_eq!(r, 1);
297297
}
298298

@@ -305,7 +305,7 @@ mod tests {
305305
}
306306
"#;
307307

308-
let (_, _, r) = counts(src, "Go");
308+
let (_, _, r, _) = counts(src, "Go");
309309
assert_eq!(r, 1);
310310
}
311311

@@ -332,7 +332,7 @@ mod tests {
332332
}
333333
"#;
334334

335-
let (_, _, r) = counts(src, "Go");
335+
let (_, _, r, _) = counts(src, "Go");
336336
assert_eq!(r, 10);
337337
}
338338

@@ -347,7 +347,7 @@ mod tests {
347347
}
348348
func f3() {}
349349
"#;
350-
let (_, d, r) = counts(src, "Go");
350+
let (_, d, r, _) = counts(src, "Go");
351351

352352
// f1, f1::a, f2, f3
353353
assert_eq!(d, 4);
@@ -580,13 +580,6 @@ mod tests {
580580
expect![[r#"
581581
scope {
582582
definitions: [
583-
x {
584-
kind: "module",
585-
context: "import §x§ \"github.com/golang/go/x\"",
586-
referenced in (1): [
587-
`var t §x§.Type := 2`,
588-
],
589-
},
590583
t {
591584
kind: "var",
592585
context: "var §t§ x.Type := 2",
@@ -595,6 +588,14 @@ mod tests {
595588
],
596589
},
597590
],
591+
imports: [
592+
x {
593+
context: "import §x§ \"github.com/golang/go/x\"",
594+
referenced in (1): [
595+
`var t §x§.Type := 2`,
596+
],
597+
},
598+
],
598599
child scopes: [],
599600
}
600601
"#]],

server/bleep/src/intelligence/language/go/scopes.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152

153153
;; imports
154154
(import_spec
155-
(package_identifier) @local.definition.module)
155+
(package_identifier) @local.import)
156156

157157
;; switch t := q.(type)
158158
(type_switch_statement

server/bleep/src/intelligence/language/java/scopes.scm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,19 @@
131131
(lambda_expression
132132
parameters: (identifier) @local.definition.local)
133133

134-
;; imports are defs
134+
;; imports
135135
;;
136136
;; import item;
137-
;; ^^^^ is a def
137+
;; ^^^^ is an import
138138
(import_declaration
139-
(identifier) @local.definition)
139+
(identifier) @local.import)
140140

141141
;; import java.util.Vector;
142-
;; ^^^^^^ is a def
142+
;; ^^^^^^ is an import
143143
(import_declaration
144144
(scoped_identifier
145145
(_)
146-
(identifier) @local.definition))
146+
(identifier) @local.import))
147147

148148
;; labels
149149
(labeled_statement

0 commit comments

Comments
 (0)