Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.

Code action to suggest adding missing imports from pkg db #437

Merged
merged 2 commits into from
Feb 18, 2020

Conversation

pepeiborra
Copy link
Collaborator

@pepeiborra pepeiborra commented Feb 16, 2020

The implementation looks in modules loaded from the package database. It should
only look in packages declared as dependencies of the project. The package
modules are loaded lazily and are global to the HscEnv, so the success rate will
depend on what has been loaded so far in the env.

I intended to make this as a standalone plugin but we cannot have two plugins providing code actions, see haskell/haskell-language-server#25 (comment)

@pepeiborra pepeiborra force-pushed the guess-imports branch 2 times, most recently from 97fbbe2 to d496231 Compare February 16, 2020 23:16
Copy link
Contributor

@aherrmann-da aherrmann-da left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice feature, thank you!

Could you also add a test-case for this?

@@ -333,6 +347,37 @@ suggestSignature isQuickFix Diagnostic{_range=_range@Range{..},..}
| otherwise = nm <> ty
suggestSignature _ _ = []

suggestNewImport :: ExternalPackageState -> ParsedModule -> Diagnostic -> [(T.Text, [TextEdit])]
suggestNewImport eps ParsedModule {pm_parsed_source = L _ HsModule {..}} Diagnostic{_message}
| Just [name, _typ] <- matchRegex (unifySpaces _message) "Variable not in scope: ([^ ]+) :: ([^*•]+)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GHC's error message does not always contain the type. E.g. in

foo = fromJust

The error reads

Variable not in scope: fromJust

This suggestion doesn't use the type anyway. Could you change the regex to make the type annotation optional?

The implementation looks in modules loaded from the package database. It should
only look in packages declared as dependencies of the project. The package
modules are loaded lazily and are global to the HscEnv, so the success rate will
depend on what has been loaded so far in the env.
> import Data.Text (Text)
> foo = pack "foo"

Teach ghcide to suggest only:

"Add pack to the import list of Data.Text"

and avoid suggesting also:

"Import Data.Text (pack)"
Copy link
Contributor

@aherrmann-da aherrmann-da left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good!

@aherrmann-da aherrmann-da merged commit 5bea92f into haskell:master Feb 18, 2020
pepeiborra added a commit to pepeiborra/ghcide that referenced this pull request Feb 22, 2020
* Code action to suggest adding missing imports from pkg db

The implementation looks in modules loaded from the package database. It should
only look in packages declared as dependencies of the project. The package
modules are loaded lazily and are global to the HscEnv, so the success rate will
depend on what has been loaded so far in the env.

* Avoid overlapping with extend import suggestions

> import Data.Text (Text)
> foo = pack "foo"

Teach ghcide to suggest only:

"Add pack to the import list of Data.Text"

and avoid suggesting also:

"Import Data.Text (pack)"
pepeiborra added a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…cide#437)

* Code action to suggest adding missing imports from pkg db

The implementation looks in modules loaded from the package database. It should
only look in packages declared as dependencies of the project. The package
modules are loaded lazily and are global to the HscEnv, so the success rate will
depend on what has been loaded so far in the env.

* Avoid overlapping with extend import suggestions

> import Data.Text (Text)
> foo = pack "foo"

Teach ghcide to suggest only:

"Add pack to the import list of Data.Text"

and avoid suggesting also:

"Import Data.Text (pack)"
pepeiborra added a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…cide#437)

* Code action to suggest adding missing imports from pkg db

The implementation looks in modules loaded from the package database. It should
only look in packages declared as dependencies of the project. The package
modules are loaded lazily and are global to the HscEnv, so the success rate will
depend on what has been loaded so far in the env.

* Avoid overlapping with extend import suggestions

> import Data.Text (Text)
> foo = pack "foo"

Teach ghcide to suggest only:

"Add pack to the import list of Data.Text"

and avoid suggesting also:

"Import Data.Text (pack)"
pepeiborra added a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…cide#437)

* Code action to suggest adding missing imports from pkg db

The implementation looks in modules loaded from the package database. It should
only look in packages declared as dependencies of the project. The package
modules are loaded lazily and are global to the HscEnv, so the success rate will
depend on what has been loaded so far in the env.

* Avoid overlapping with extend import suggestions

> import Data.Text (Text)
> foo = pack "foo"

Teach ghcide to suggest only:

"Add pack to the import list of Data.Text"

and avoid suggesting also:

"Import Data.Text (pack)"
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants