Skip to content

Commit 04c7bb9

Browse files
Fix diagnostics ranges
1 parent 27c89b1 commit 04c7bb9

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

server/diagnostics.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (s *server) getEvalDiags(doc *document) (diags []protocol.Diagnostic) {
153153
diag.Severity = protocol.SeverityError
154154
}
155155

156-
diag.Range = NewProtocolRange(line, col, endLine, endCol)
156+
diag.Range = NewProtocolRange(line-1, col-1, endLine-1, endCol-1)
157157
diags = append(diags, diag)
158158
}
159159

@@ -190,7 +190,7 @@ func (s *server) getLintDiags(doc *document) (diags []protocol.Diagnostic) {
190190

191191
diag.Message = match[9]
192192

193-
diag.Range = NewProtocolRange(line, col, endLine, endCol)
193+
diag.Range = NewProtocolRange(line-1, col-1, endLine-1, endCol-1)
194194
diags = append(diags, diag)
195195
}
196196
}

server/diagnostics_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package server
2+
3+
import (
4+
"testing"
5+
6+
"github.com/jdbaldry/go-language-server-protocol/lsp/protocol"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetLintDiags(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
fileContent string
14+
expected []protocol.Diagnostic
15+
}{
16+
{
17+
name: "unused variable",
18+
fileContent: `
19+
local unused = 'test';
20+
{}
21+
`,
22+
expected: []protocol.Diagnostic{
23+
{
24+
Range: protocol.Range{
25+
Start: protocol.Position{Line: 1, Character: 6},
26+
End: protocol.Position{Line: 1, Character: 21},
27+
},
28+
Severity: protocol.SeverityWarning,
29+
Source: "lint",
30+
Message: "Unused variable: unused",
31+
},
32+
},
33+
},
34+
}
35+
for _, tc := range testCases {
36+
t.Run(tc.name, func(t *testing.T) {
37+
s, fileURI := testServerWithFile(t, nil, tc.fileContent)
38+
doc, err := s.cache.get(fileURI)
39+
if err != nil {
40+
t.Fatalf("%s: %v", errorRetrievingDocument, err)
41+
}
42+
43+
diags := s.getLintDiags(doc)
44+
assert.Equal(t, tc.expected, diags)
45+
})
46+
}
47+
}

0 commit comments

Comments
 (0)