sourcekit-lsp icon indicating copy to clipboard operation
sourcekit-lsp copied to clipboard

Add missing imports

Open PhantomInTheWire opened this issue 3 weeks ago • 0 comments

fixes: https://github.com/swiftlang/sourcekit-lsp/issues/1089

This PR implements a new code action, AddMissingImports, which provides quick fixes to automatically add import statements for unresolved types or values. It leverages the semantic index to identify defining modules and intelligently inserts the necessary imports into the source file.

Key Features

  • Automatically detects cannot find '...' in scope and cannot find type '...' in scope diagnostics.
  • Queries the semantic index to find which modules define the missing type or value.
  • Filtering strategy
    • Filters out modules that are already imported in the current file.
    • Excludes the current module to avoid self-imports.
  • Insertion Strategy:
    • Inserts new imports after the last existing import statement if present.
    • If no imports exist, inserts at the beginning of the file (after leading trivia/headers).
    • Handles spacing and newlines correctly to maintain file formatting.
  • Sorting: Provides multiple suggestions sorted alphabetically by module name when a type is defined in multiple accessible modules.

PhantomInTheWire avatar Jan 13 '26 21:01 PhantomInTheWire