Skip to content

Completions for relative imports (., .., …) and partials #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gcv
Copy link

@gcv gcv commented Aug 9, 2025

This PR adds completion support for relative module imports with leading dots (., .., ...) and partials (e.g., .Sub, ..Sib, ...Gran), for both using and import.

Today, users typing relative imports get no completions for the next module segment, and EOL positions often don’t trigger the import completion path. This PR improves the editing experience without changing resolution behavior.

This code tries to:

  • Detect relative-dot depth by scanning the source string around the cursor, skipping trailing whitespace and newlines and ignoring dot runs preceded by identifier chars, so Base.M does not trigger.
  • Enter import completion when we detect leading dots, even if the expression under the cursor is nothing (e.g., at EOL). Guard all parent searches.
  • Compute the target ancestor module by:
    • finding the nearest import/using expression around the cursor; then
    • resolving the current/ancestor module EXPR via CST; and
    • listing immediate child modules by scanning the target module/file CST (no symbol server dependency).
  • Offer items for:
    • import . → children of the current module
    • import .. → children of the parent module
    • import ... → children of the grandparent module
    • import .Sub → Submodule, etc.
  • Applies symmetrically to using.

Implementation details

  • requests/completions.jl:
    • _relative_dot_depth_at(doc, offset): robust dot-run counter.
    • _current_module_expr/_module_ancestor_expr: walk module EXPR parents in CST.
    • _child_module_names: list child module names by scanning the module body (or file).
    • Trigger import_completions if either we’re in an import/using or relative-dot depth > 0; only call get_parent_fexpr on a safe EXPR and avoid calling it on nothing.
    • When relative branch is active, compute target scope and produce CompletionItems for children; fall back to existing logic for other branches.
  • Gate absolute-qualified misfires: dot-run preceded by id-char returns depth 0.

Tests

  • New test/requests/test_relative_imports.jl:
    • import . / .. / ... produce expected module child completions
    • partials (.Sub, ..Sib, ...Gran) complete
    • using forms mirror the same behavior
    • Base.M is not treated as relative

Compatibility and performance

  • Completions only; no changes to resolution, hover, goto, or diagnostics.
  • All code paths are gated; overhead is negligible.

Notes

- Add completion for leading-dot imports/using:
  import . / .. / … and partials (.Sub, ..Sib, …Gran).
- Detect dot runs by scanning source around the cursor, skipping whitespace;
  ignore dot runs preceded by identifier chars (so Base.M is not treated as relative).
- Trigger import completion when relative dots are present even at EOL,
  and guard get_parent_fexpr calls when the expr under the cursor is missing.
- Compute target ancestor module via CST and list child modules by scanning
  the target module/file body (no symbol server dependency).
- Applies symmetrically to using and import.

Tests:
- New relative import/using completion tests for ., .., … and partials.
- Ensure qualified names like Base.M don’t trigger relative completion.

Completions only; no changes to resolution, hover, goto, or diagnostics.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant