Skip to content
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

Extend import suggestions don't work when there's more than one option #838

Closed
pepeiborra opened this issue Sep 13, 2020 · 6 comments · Fixed by haskell/ghcide#913
Closed

Comments

@pepeiborra
Copy link
Collaborator

Given the following code:

import Data.Map ()

foo = fromList []

ghcide will produce a code action "Add fromList to the import list of Data.Map".

However, the code below will not produce an "Add fromList to the importList" suggestions at all:

import Data.Map ()
import Data.HashMap.Strict()

foo = fromList []
@gdevanla
Copy link
Contributor

I am using hls with emacs. I do see the following CodeLens:

foo = fromList []                                                                                                                                                                                                   
                                                                                                                                                        typecheck: • Variable not in scope: fromList :: [a0] -> t   
data TestSumType = TestSumType1 Int | TestSumType2 Bool                                                                                        • Perhaps you want to add ‘fromList’ to one of these import lists:   
                                                                                                                                           ‘Data.Map’ (/home/devanla/fsf/haskell-scratch/app/test-lsp.hs:21:1-18)   
                                                                                                                                           ‘Data.HashMap.Strict’ (/home/devanla/fsf/haskell-scratch/app/test-lsp.hs:22:1-29) 

And, when I do lsp-execute-code-actions, I see a lot of options listed in the menu I can choose from. I have pasted a few here:

import Data.List.NonEmpty.Extra (fromList)                                                                                                                                                                          
import Data.List.NonEmpty.Extra                                                                                                                                                                                     
import Data.IntMap.Strict (fromList)                                                                                                                                                                                
import Data.IntMap.Strict                                                                                                                                                                                           
import Data.Vector (fromList)                                                                                                                                                                                       
import Data.Vector                                                                                                                                                                                                  
import Data.List.NonEmpty.Compat (fromList)                                                                                                                                                                         
import Data.List.NonEmpty.Compat                                                                                                                                                                                    
import GHC.Exts (IsList(fromList))                                                                                                                                                                                  
import GHC.Exts                                                                                                                                                                                                     
import Data.Map.Lazy (fromList)                                                                                                                                                                                     
import Data.Map.Lazy                                                                                                                                                                                                
import Data.Map (fromList)                                                                                                                                                                                          
import Data.Map                                                                                                                                                                                                     
import Data.Set.Internal (fromList)                                                                                                                                                                                 
import Data.Set.Internal                                                                                                                                                                                            
import Data.Sequence.Internal (fromList)           

Am I checking this incorrectly?

@pepeiborra
Copy link
Collaborator Author

Yes. This ticket is for the "extend import" code action. You are missing the two import declarations in the original repro.

@gdka
Copy link

gdka commented Oct 10, 2020

Do you have more context? Files to modify? and how I can test the changes?:)

@pepeiborra
Copy link
Collaborator Author

The relevant code is in Development.IDE.Plugin.CodeAction, function suggestExtendImport.
To test the changes, extend the unit tests.
Good luck!

@gdka
Copy link

gdka commented Oct 11, 2020

Do you actually have unit test for this section? I can't find anything in /tst/src/development/ide

@gdevanla
Copy link
Contributor

So, here is the cause of the issue.

When we have one suggestion for 'Add imports', the complier error looks like this:

haskell-scratch-2/learn-lsp/app/testlsp.hs:7:7: error:
    • Variable not in scope: fromList :: [a0] -> t
    • Perhaps you want to add ‘fromList’ to the import list
      in the import of ‘Data.Map’ (app/testlsp.hs:4:1-18).
  |
7 | foo = fromList []
  |       ^^^^^^^^

The diagnostics string Perhaps you want to add ‘fromList’ to the import list in the import of ‘Data.Map’ (app/testlsp.hs:4:1-18). is captured by the the RegEx here: https://github.com/haskell/ghcide/blob/a345e11aa3f811240a8961fe08806c3b7862d12e/src/Development/IDE/Plugin/CodeAction.hs#L649.

But, when you have multiple suggestions, for code like this:

import Data.Map ()
import Data.HashMap.Strict()

then the `diagnostics from the compiler is

    • Perhaps you want to add ‘fromList’ to one of these import lists:
        ‘Data.Map’ (app/testlsp.hs:7:1-18)
        ‘Data.HashMap.Strict’ (app/testlsp.hs:8:1-29)

which does not match the current regex.

I have a fix for this here: haskell/ghcide#913

@pepeiborra pepeiborra transferred this issue from haskell/ghcide Jan 1, 2021
jneira added a commit to jneira/haskell-language-server that referenced this issue Jan 1, 2021
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 a pull request may close this issue.

3 participants