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

Language Pragmas Auto-Insert After Module Docs #1726

Closed
emilypi opened this issue Apr 14, 2021 · 2 comments · Fixed by #1731
Closed

Language Pragmas Auto-Insert After Module Docs #1726

emilypi opened this issue Apr 14, 2021 · 2 comments · Fixed by #1731
Labels
component: hls-pragmas-plugin type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..

Comments

@emilypi
Copy link
Member

emilypi commented Apr 14, 2021

Your environment

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

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

Which OS do you use:
Big Sur OSX

Which lsp-client do you use:
Emacs lsp-haskell
Describe your project (alternative: link to the project):
Cabal project
Contents of hie.yaml:

cradle:
  cabal:
    - path: "Cabal/src"
      component: "lib:Cabal"

    - path: "cabal-install/src"
      component: "lib:cabal-install"

    - path: "cabal-install/main/Main.hs"
      component: "cabal-install:exe:cabal"

    - path: "cabal-install/tests"
      component: "cabal-install:test:unit-tests"

    - path: "cabal-install/tests"
      component: "cabal-install:test:memory-usage-tests"

    - path: "cabal-install/tests"
      component: "cabal-install:test:integration-tests2"

    - path: "cabal-install/tests"
      component: "cabal-install:test:long-tests"

    - path: "cabal-install-solver/src"
      component: "lib:cabal-install-solver"

    - path: "cabal-install-solver/src-assertion"
      component: "lib:cabal-install-solver"

Steps to reproduce

On HLS 1.1, auto-insert any language pragma in a module with header documentation

Expected behaviour

Pragmas should be inserted before docs

Actual behaviour

Pragmas are inserted after docs:

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
-- | Cabal-like file AST types: 'Field', 'Section' etc,
--
-- This (intermediate) data type is used for pretty-printing.
--
-- @since 3.0.0.0
--
{-# LANGUAGE LambdaCase #-}
module Distribution.Fields.Pretty 
@jneira jneira added component: hls-pragmas-plugin type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. labels Apr 14, 2021
@OliverMadine
Copy link
Collaborator

OliverMadine commented Apr 15, 2021

I believe that the intended behaviour is to insert pragmas directly before the start of the first line of Haskell code, this is done using the following:

endOfModuleHeader :: ParsedModule -> Range
endOfModuleHeader pm =
  let mod = unLoc $ pm_parsed_source pm
      modNameLoc = getLoc <$> hsmodName mod
      firstImportLoc = getLoc <$> listToMaybe (hsmodImports mod)
      firstDeclLoc = getLoc <$> listToMaybe (hsmodDecls mod)
      line = maybe 0 (_line . _start) (modNameLoc <|> firstImportLoc <|> firstDeclLoc >>= srcSpanToRange)
      loc = Position line 0
   in Range loc loc

We could include an option to always insert pragmas at the top of the file? I will open a PR for this shortly.

@jneira
Copy link
Member

jneira commented Apr 15, 2021

I am afraid there was a pr that remove just that behaviour cause it put the pragma before she-bangs: #1340 which fixed #555
I think the ideal behaviour should be identify the code span of the last pragmas block (as we could have several pragmas blocks with comments between them) and insert the new one just after it

OliverMadine added a commit to OliverMadine/haskell-language-server that referenced this issue Apr 16, 2021
@mergify mergify bot closed this as completed in #1731 Apr 22, 2021
mergify bot pushed a commit that referenced this issue Apr 22, 2021
* added option to always insert pragmas at top of file

* Revert "added option to always insert pragmas at top of file"

This reverts commit 8cebdd5.

* Pragmas inserted before comments (#1726)

* Code style

* Code style

* Insert pragamas to existing pragma list

* added cpp macro to pragma test for ghc < 810

* Refactor: using file contents to find pragma insertion position

* Update getParsedModule action description

* Code style

Co-authored-by: Javier Neira <[email protected]>
Co-authored-by: Potato Hatsue <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: hls-pragmas-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