Skip to content

fix: add context code action with trailing comment #4649

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

guibou
Copy link
Collaborator

@guibou guibou commented Jul 6, 2025

For now it is just a repro for #4648

Tested with ghc 9.12.2, it seems to fail as expected:

[guillaume@gecko:~/srcs/haskell-language-server]$ cabal run hls-refactor-plugin-tests -- -p haddock
Configuration is affected by the following files:
- cabal.project
refactor
  code actions
    add function constraint
      preexisting constraint, with haddock comment in type signature: FAIL (0.84s)
        plugins/hls-refactor-plugin/test/Main.hs:3122:
        expected: "module Testing where\ndata Pair a b = Pair a b\neq \n    :: (Eq a, Eq b)\n    =>\n   -- This is the first argument\n   Pair a b ->\n   -- And this is the second\n   Pair a b ->\n   -- And this is the result\n   Bool\neq (Pair x y) (Pair x' y') = x == x' && y == y'\n"
         but got: "module Testing where\ndata Pair a b = Pair a b\neq \n    :: (Eq a,\n\n   -- This is the first argument Eq b)\n    =>\n\n   Pair a b ->\n   -- And this is the second\n   Pair a b ->\n   -- And this is the result\n   Bool\neq (Pair x y) (Pair x' y') = x == x' && y == y'\n"

1 out of 1 tests failed (0.84s)

(see how Eq b) is introduced inside the haddock comment This is the first argument.

I'm pushing the branch for reference and to test with other GHC version (I'm too lazy to build that locally, cabal cache was hot for ghc 9.12.2).

@guibou guibou requested a review from santiweight as a code owner July 6, 2025 08:16
@guibou guibou force-pushed the 4648_invalid_add_constraint_code_action branch from 24493db to bf75a82 Compare July 6, 2025 14:12
@guibou guibou changed the title feat(test): add a repro for 4648 fix: add context code action with trailing comment Jul 6, 2025
@guibou
Copy link
Collaborator Author

guibou commented Jul 6, 2025

The current code was tested with GHC 9.12 and does fix the broken generated code.

However it can:

  • drop comments
  • move all inline comments from the inside of the context block to the end of the context block.

The difficulty comes from the way comments are stored in the AST: they are not stored "inline", interleaved with the ast nodes. Instead, they are stored in a list of comment attached to an ast node and each comment is localised with it absolute position in the stream as well as the position of the latest taken (in order to introduce whitespaces).

Consider the following example:

bar :: 
  -- BEFORE
  {- yoto -} (Monad m {- yiti -}){- yutu -} => {- yete -} -- Trailing
  -- After
  m ()
  • BEFORE and yoto are attached to the bar node
  • yiti is attached to the Monad m node
  • yutu, yete, Trailing and After are attached to the (Monad m) node (e.g. the constraint) and the pretty printer layouts everything based on the anchor and position of the items.

In order to insert a value after Monad m while keeping everything correctly linked, we need to move the anchor and span for yutu, yete, Trailing and After.

Note that I'm discovering the annotation API right now and maybe there is a solution for that. Especially, there is https://hackage-content.haskell.org/package/ghc-9.8.1/docs/GHC-Parser-Annotation.html#t:AnchorOperation AnchorOperation which seems to be done especially for this.

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 this pull request may close these issues.

1 participant