Skip to content

Import Regression: Wrong Import Placement on first Import #2100

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

Closed
emilypi opened this issue Aug 16, 2021 · 5 comments · Fixed by #2116
Closed

Import Regression: Wrong Import Placement on first Import #2100

emilypi opened this issue Aug 16, 2021 · 5 comments · Fixed by #2116
Labels
component: imports plugin type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..

Comments

@emilypi
Copy link
Member

emilypi commented Aug 16, 2021

Here's a weird one: first imports are inserted in wrong place. Unsure why.

Your environment

Output of haskell-language-server --probe-tools or haskell-language-server-wrapper --probe-tools:

askell-language-server version: 1.3.0.0 (GHC: 8.10.4) (PATH: /Users/emilypi/.ghcup/bin/haskell-language-server-wrapper-1.3.0) (GIT hash: e7c5e90b6df5dff2760d76169eddaea3bdd6a831)
Tool versions found on the $PATH
cabal:		3.4.0.0
stack:		2.5.1
ghc:		8.10.4

Which OS do you use:
MacOS Big Sure

Which lsp-client do you use:

Emacs

Describe your project (alternative: link to the project):
*.cabal files, cabal.project

Contents of hie.yaml:

cradle:
  cabal:
    - path: "src"
      component: "lib:order"

    - path: "test"
      component: "order:test:doctests"

Steps to reproduce

  1. Make a first import with functions defined (along with comments)

Screen Shot 2021-08-16 at 11 44 07 AM

  1. Import goes in wrong place.

Screen Shot 2021-08-16 at 11 44 15 AM

  1. If importing another thing, imports are added below first import

Screen Shot 2021-08-16 at 11 46 12 AM

  1. However, when physically moved, to the right spot, imports work as expected.

Screen Shot 2021-08-16 at 11 44 40 AM

Screen Shot 2021-08-16 at 11 44 46 AM

Expected behaviour

Imports should be inserted in correct area of the module.

Actual behaviour

The first import is inserted above the first signicant decl, regardless of comments.

@jneira
Copy link
Member

jneira commented Aug 16, 2021

OMG this is the neverending history. A regression test is absolutely needed as first step.
@nini-faroux could you have the chance to take a look? thanks!

@jneira jneira added component: imports plugin status: regression test needed type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. labels Aug 16, 2021
@nini-faroux
Copy link
Contributor

sure! I can have a look tomorrow

@nini-faroux
Copy link
Contributor

hi, so I played around a little with this just now and it seems the issue is whenever the first (non-empty) line after the 'module ... where' is a comment, if you put anything above the comment the 'import' seems to be added above that correctly, as an example, with a data declaration above the comment the import gets added above that: 

module Test 
( SomeData(..)
) where

import Data.Monoid -- newly added
data Something = Something

-- | some comment
class Semigroup a => SomeData a
instance SomeData All

The problem seems to be with this function:

newImportInsertRange :: ParsedSource -> Maybe (Range, Int)

Where there are two cases, when the import list is empty, it finds the position from 'hsmodDecls', and if it matches this case and the first line is a comment, we have the problem with the import being added under the comment.

The second case is when there are imports, so the import list isn't empty, in which case it finds the position from 'hsmodImports', and will add the suggested import under the current last import in the file, which works fine once the existing imports are in the correct position, as mentioned already.

So I'd be happy to try to make a fix for this some evening later in the week if that suits, but it would be good to add a number of tests as well, so I was wondering where's the best place to add them? There's a 'test' folder in '/ghcide/...', but I didn't see any tests for this functionality there, and there's two import plugins (refine and explicit) but I don't think either are particularly related to this issue. Just to make sure I add them in a sensible place.

Thanks!

@jneira
Copy link
Member

jneira commented Aug 17, 2021

@nini-faroux many thanks for the head up, i think the place for such tests could be

suggestImportTests = testGroup "suggest import actions"

@nini-faroux
Copy link
Contributor

that's great, thanks @jneira

nini-faroux added a commit to nini-faroux/haskell-language-server that referenced this issue Aug 20, 2021
nini-faroux added a commit to nini-faroux/haskell-language-server that referenced this issue Aug 23, 2021
nini-faroux added a commit to nini-faroux/haskell-language-server that referenced this issue Aug 25, 2021
Remove HasSrcSpan import

Amend extendImportHandler' function

Amend old import tests
nini-faroux added a commit to nini-faroux/haskell-language-server that referenced this issue Aug 28, 2021
* Remove HasSrcSpan import

* Amend extendImportHandler' function

* Amend old import tests

* Adapt tests for windows newline issue
@jneira jneira linked a pull request Aug 28, 2021 that will close this issue
nini-faroux added a commit to nini-faroux/haskell-language-server that referenced this issue Sep 3, 2021
* Remove HasSrcSpan import

* Amend extendImportHandler' function

* Amend old import tests

* Adapt tests for windows newline issue
@mergify mergify bot closed this as completed in #2116 Sep 4, 2021
mergify bot pushed a commit that referenced this issue Sep 4, 2021
* Remove HasSrcSpan import

* Amend extendImportHandler' function

* Amend old import tests

* Adapt tests for windows newline issue

Co-authored-by: Javier Neira <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: imports plugin type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants