Skip to content

Automatically add missing imports #1835

@anderseknert

Description

@anderseknert

For unknown reasons, it now happens quite often that I try to use something like ast.policy() in a test and get a compiler error on save, as I had forgotten to import the regal.ast package. Easily fixed, but it would be even easier if I didn't have to fix it :) I think we have enough data aggregated about refs in all modules that it should be fairly uncomplicated to guess which package ref to import. But we'll see.

Implementation-wise, this would be done in the completion provider by having relevant completion items return an additionalTextEdits attribute, which in addition to the completion itself would insert an import at an appropriate location, when applicable.

/**
 * An optional array of additional text edits that are applied when
 * selecting this completion. Edits must not overlap (including the same
 * insert position) with the main edit nor with themselves.
 *
 * Additional text edits should be used to change text unrelated to the
 * current cursor position (for example adding an import statement at the
 * top of the file if the completion item will insert an unqualified type).
 */
additionalTextEdits?: []TextEdit

I expect the biggest challenge to be to ensure that we get things right with regards to scope, so we don't e.g. add an import for data.regal.ast if the current position is a function body where one of the args may be named ast, and so on.

We could also add a Source Action to allow users to add all missing imports in a file in a single action.

Metadata

Metadata

Assignees

No one assigned

    Labels

    language serverIssues related to Regal's language server

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions