Improve snippet sanitization
- Dominant language
- Haskell
- Stars
- 423
- Forks
- 105
- PR merge metrics
- No merged PRs in 30d
Description
Currently the constructor of `CompletionItem` expects a `Maybe Text` value in the `_insertText` field, but that leaves the possibility of outputting malformed snippets as is the case in [hls](https://github.com/haskell/haskell-language-server/issues/4363). The LSP specification defines the syntax for snippets and I think there should be a datatype to represent this syntax tree. This would be really useful for sanitization, where we could implement a function that converts this data type to a sanitized string.
What I'm suggesting is to add new data types:
```haskell
newtype Snippet = Snippet [SnippetAny]
data SnippetAny
= SText Text
| SPlaceholder Int SnippetAny
| ...
```
and then change the type of the `_insertText` field to `Snippet`. Then when `CompletionItem` is serialized to JSON, it applies a function `sanitizeSnippet :: Snippet -> Text` that converts this `Snippet` value to a valid LSP snippet. This `Snippet` type has a rich enough structure to make it easy to implement proper sanitization as outlined in the spec:
> With \ (backslash), you can escape $, } and \. Within choice elements, the backslash also escapes comma and pipe characters.
Contributor guide
Research direction
Start with the CompletionItem type and its _insertText field, then inspect how CompletionItem is serialized to JSON. Use the LSP snippet syntax cited in the issue to define the needed snippet structure and sanitization behavior; done means malformed snippet output is converted to valid LSP snippet text.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100