haskell / haskell-language-server

Official haskell ide support via language server (LSP). Successor of ghcide & haskell-ide-engine.
Apache License 2.0
2.71k stars 368 forks source link

Make code range work on comments #3005

Open sloorush opened 2 years ago

sloorush commented 2 years ago

Is your feature request related to a problem? Please describe.

@kokobd had pointed this out. When you expand selection(using shift+alt+right arrow) from within a comment, expand selection does not work on comments.

Describe the solution you'd like It is because comments are not included in HieAST, which we use for constructing ranges in the hls-selection-range-plugin. Annotated ParsedSource can be used to handle structures not in HieAST properly.

More information about it --> https://gitlab.haskell.org/ghc/ghc/-/wikis/api-annotations#comments

cc: @kokobd

sloorush commented 2 years ago

Can someone please point me to some documentation/explaination about how Annotated parse source is built/how it works? I am stuck at how it relates with the hieAST and how to add it to the CodeRange tree.

kokobd commented 2 years ago

Disclaimer: I'm not an expert in this field, below is what I have learned from some exact-print related issues. @wz1000 knows much more.

@sloorush Annotated ParsedSource is the original GHC AST plus some so-called "annotations". HieAST is the serializable AST containing the structure, type info, etc.

The main difference between them is that HieAST is easier to query, but it can not be used to modify the source code, as it doesn't contain the full "exact-print" information. Doc of HieFile might help you

By the way, HIE doesn't stands for "haskell ide engine", it means "hi file extended". If you have tried to compile source code with ghc command directly, you may have noticed some .hi files. So this is a GHC structure.

So what is Annotated ParsedSource? Annotated is defined in HLS, for compatible purposes. Before GHC 9.2, the GHC AST type ParsedSource doesn't contain "annotations" in it. Instead, you need to have Anns at hand. But in GHC 9.2, Anns is no longer required, it's built directly into the AST, ParsedSource. To make us not write too many C Pre Processor directives, we have Annotated ParsedSource to represent "AST plus exact print information"

Pepe Iborra's comments may also help you. https://github.com/haskell/haskell-language-server/pull/2565#issuecomment-1019453473

kokobd commented 2 years ago

I believe you don't need to remove the usage of HieAST, you just need to add new nodes to the tree. In ASTPreProcess.hs, we already have some "custom node" (nodes doesn't exist in the original HieAST). Maybe you can pull in Annotated ParsedSource, and inject comment nodes into HieAST