3434import java .util .ArrayList ;
3535import java .util .Arrays ;
3636import java .util .Deque ;
37+ import java .util .HashMap ;
3738import java .util .LinkedHashSet ;
3839import java .util .List ;
40+ import java .util .Map ;
3941import java .util .Set ;
4042import java .util .regex .Matcher ;
4143import java .util .regex .Pattern ;
@@ -91,18 +93,25 @@ public static final class Context {
9193 /** Names in scope at the caret, innermost first. */
9294 public final Set <String > names ;
9395
96+ /**
97+ * The types names in scope are annotated with, as written (e.g.
98+ * {@code numpy.ndarray} for {@code image: numpy.ndarray}), by name.
99+ */
100+ public final Map <String , String > types ;
101+
94102 /** For {@link Kind#ATTRIBUTE}: attributes used on the same receiver. */
95103 public final Set <String > attributesUsed ;
96104
97105 private Context (final Kind kind , final String prefix ,
98106 final int replaceStart , final String receiver , final Set <String > names ,
99- final Set <String > attributesUsed )
107+ final Map < String , String > types , final Set <String > attributesUsed )
100108 {
101109 this .kind = kind ;
102110 this .prefix = prefix ;
103111 this .replaceStart = replaceStart ;
104112 this .receiver = receiver ;
105113 this .names = names ;
114+ this .types = types ;
106115 this .attributesUsed = attributesUsed ;
107116 }
108117 }
@@ -177,18 +186,20 @@ else if (before.trim().endsWith(".") && !before.trim().endsWith("..")) {
177186 // Parse the script up to the caret, with the partial identifier replaced
178187 // by a placeholder, and any open brackets closed: a clean tree there.
179188 final String source = textBefore + CARET + closers ;
180- final Set <String > names = kind == Kind .NAME ? namesInScope (source ,
181- textBefore .length ()) : new LinkedHashSet <>();
189+ final Set <String > names = new LinkedHashSet <>();
190+ final Map <String , String > types = new HashMap <>();
191+ if (kind == Kind .NAME ) namesInScope (source , textBefore .length (), names ,
192+ types );
182193 final Set <String > used = kind == Kind .ATTRIBUTE && receiver != null
183194 ? attributesUsed (text , receiver , start ) : new LinkedHashSet <>();
184- return new Context (kind , prefix , start , receiver , names , used );
195+ return new Context (kind , prefix , start , receiver , names , types , used );
185196 }
186197
187198 // -- Helper methods --
188199
189200 private static Context none (final String prefix , final int start ) {
190201 return new Context (Kind .NONE , prefix , start , null , new LinkedHashSet <>(),
191- new LinkedHashSet <>());
202+ new HashMap <>(), new LinkedHashSet <>());
192203 }
193204
194205 /**
@@ -272,14 +283,16 @@ private static Set<String> attributesUsed(final String text,
272283 return used ;
273284 }
274285
275- /** Names in scope at the placeholder, which starts at {@code caret}. */
276- private static Set <String > namesInScope (final String source ,
277- final int caret )
286+ /**
287+ * Finds the names in scope at the placeholder, which starts at
288+ * {@code caret}, and the types they are annotated with.
289+ */
290+ private static void namesInScope (final String source , final int caret ,
291+ final Set <String > names , final Map <String , String > types )
278292 {
279293 final byte [] bytes = source .getBytes (StandardCharsets .UTF_8 );
280294 final int caretByte = source .substring (0 , caret ).getBytes (
281295 StandardCharsets .UTF_8 ).length ;
282- final Set <String > names = new LinkedHashSet <>();
283296 final TSParser parser = new TSParser ();
284297 try {
285298 parser .setLanguage (new TreeSitterPython ());
@@ -305,13 +318,13 @@ private static Set<String> namesInScope(final String source,
305318 if (type .endsWith ("comprehension" ) || type .equals (
306319 "generator_expression" ))
307320 {
308- collectBindings (node , bytes , names , true );
321+ collectBindings (node , bytes , names , types , true );
309322 }
310323 else if (body != null && !body .isNull () && !type .equals ("lambda" )) {
311- collectBindings (body , bytes , names , false );
324+ collectBindings (body , bytes , names , types , false );
312325 }
313326 }
314- collectBindings (root , bytes , names , false );
327+ collectBindings (root , bytes , names , types , false );
315328 }
316329 catch (final RuntimeException | LinkageError exc ) {
317330 // NB: Native parser unavailable or failed: no names, then.
@@ -320,15 +333,16 @@ else if (body != null && !body.isNull() && !type.equals("lambda")) {
320333 parser .close ();
321334 }
322335 names .remove (CARET );
323- return names ;
324336 }
325337
326338 /**
327339 * Collects the names bound by statements within {@code node}, without
328- * descending into nested scopes (whose own names are bound here, though).
340+ * descending into nested scopes (whose own names are bound here, though);
341+ * and the types annotated names are declared with.
329342 */
330343 private static void collectBindings (final TSNode node , final byte [] bytes ,
331- final Set <String > names , final boolean comprehension )
344+ final Set <String > names , final Map <String , String > types ,
345+ final boolean comprehension )
332346 {
333347 final int n = node .getNamedChildCount ();
334348 for (int i = 0 ; i < n ; i ++) {
@@ -347,6 +361,9 @@ private static void collectBindings(final TSNode node, final byte[] bytes,
347361 if (!comprehension ) continue ; // NB: A nested scope.
348362 break ;
349363 case "assignment" :
364+ addType (child , bytes , types );
365+ addTargets (child .getChildByFieldName ("left" ), bytes , names );
366+ break ;
350367 case "augmented_assignment" :
351368 case "for_statement" :
352369 case "for_in_clause" :
@@ -366,10 +383,21 @@ private static void collectBindings(final TSNode node, final byte[] bytes,
366383 continue ;
367384 default :
368385 }
369- collectBindings (child , bytes , names , comprehension );
386+ collectBindings (child , bytes , names , types , comprehension );
370387 }
371388 }
372389
390+ /** Adds the type of an annotated assignment, e.g. {@code x: int = 1}. */
391+ private static void addType (final TSNode assignment , final byte [] bytes ,
392+ final Map <String , String > types )
393+ {
394+ final TSNode left = assignment .getChildByFieldName ("left" );
395+ final TSNode type = assignment .getChildByFieldName ("type" );
396+ if (left == null || left .isNull () || !left .getType ().equals (
397+ "identifier" ) || type == null || type .isNull ()) return ;
398+ types .putIfAbsent (text (left , bytes ), text (type , bytes ));
399+ }
400+
373401 /** Adds the names an import statement binds. */
374402 private static void addImported (final TSNode statement , final byte [] bytes ,
375403 final Set <String > names , final boolean plainImport )
@@ -428,8 +456,12 @@ private static void addIdentifier(final TSNode node, final byte[] bytes,
428456 {
429457 if (node == null || node .isNull () || !node .getType ().equals (
430458 "identifier" )) return ;
431- names .add (new String (bytes , node .getStartByte (), node .getEndByte () - node
432- .getStartByte (), StandardCharsets .UTF_8 ));
459+ names .add (text (node , bytes ));
460+ }
461+
462+ private static String text (final TSNode node , final byte [] bytes ) {
463+ return new String (bytes , node .getStartByte (), node .getEndByte () - node
464+ .getStartByte (), StandardCharsets .UTF_8 );
433465 }
434466
435467 private static boolean isIdentifierPart (final char c ) {
0 commit comments