Skip to content

Commit

Permalink
Avoid returning negative positions
Browse files Browse the repository at this point in the history
According to the LSP spec, the line and character must both be unsigned
integers. The spec also specifically calls out that `-1` is not
supported: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position

With some code structures, it is possible to produce an error such as
the following from `golangci-lint run --out-format=json`:

```json
{
  "FromLinter": "typecheck",
  "Text": ": # github.com/my/package [github.com/my/package.test]\n./main.go:31:2: undefined: asdf",
  "Severity": "",
  "SourceLines": [
    "package main"
  ],
  "Replacement": null,
  "Pos": {
    "Filename": "main.go",
    "Offset": 0,
    "Line": 1,
    "Column": 0
  },
  "ExpectNoLint": false,
  "ExpectedNoLintLinter": ""
}
```

This ultimately does result in some issues with tooling compatibility,
for example: folke/trouble.nvim#224 (comment)

By preventing the number from dropping below zero, this class of error
should no longer be present.
  • Loading branch information
rliebz committed Oct 10, 2023
1 parent ed10182 commit 230cc3b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {

d := Diagnostic{
Range: Range{
Start: Position{Line: issue.Pos.Line - 1, Character: issue.Pos.Column - 1},
End: Position{Line: issue.Pos.Line - 1, Character: issue.Pos.Column - 1},
Start: Position{
Line: max(issue.Pos.Line-1, 0),
Character: max(issue.Pos.Column-1, 0),
},
End: Position{
Line: max(issue.Pos.Line-1, 0),
Character: max(issue.Pos.Column-1, 0),
},
},
Severity: issue.DiagSeverity(),
Source: &issue.FromLinter,
Expand All @@ -104,6 +110,13 @@ func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
return diagnostics, nil
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func (h *langHandler) diagnosticMessage(issue *Issue) string {
if h.noLinterName {
return issue.Text
Expand Down

0 comments on commit 230cc3b

Please # to comment.