Skip to content

Python - Truncate suffix based on relevant context

Bruno Cardoso requested to merge truncate-suffix-python into main

Implements #248 (closed).

Algorithm:

  1. Parse the whole prefix+suffix to get the full tree.
  2. Find the context node around the cursor using the algorithm described on !284 (merged).
  3. Convert the cursor point (which is relative to the whole prefix+suffix) to a relative point within the context node.
  4. Split the context node on this relative point and yield the truncated suffix.

Design choices:

This takes a conservative approach as it will keep the bigger context when available. Examples:

  1. Class within class: it will keep the outer class context, regardless of the cursor being inside the nested class or not.
  2. Function within function: it will keep the outer function context.
  3. Method within a class: it will keep the outer class context.

How it is being applied to the engine:

  1. The context truncation happens before the length-based truncation.
  2. There is not change to the prefix.

Possible Follow-ups:

  1. This approach is based on a prioritized list of known tree-sitter node-types. But some symbols should be at the same level (decorated_function and function_definition for example).
Edited by Bruno Cardoso

Merge request reports