Skip to content

Commit

Permalink
cmd/govim: use visual positions for diagnostics
Browse files Browse the repository at this point in the history
When displaying diagnostics, the position must be visible to the user
within Vim. Some diagnostics have start/end positions that are beyond
the visible content, i.e. they are after the final \n. The user, however
will not be able to see/understand such a position. So we adjust such
positions to be before the final \n.
  • Loading branch information
myitcv committed Dec 2, 2019
1 parent 5a564be commit 6e8213f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/govim/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func (v *vimstate) diagnostics() []types.Diagnostic {
buf = types.NewBuffer(-1, fn, byts, false)
}
for _, d := range lspDiags {
s, err := types.PointFromPosition(buf, d.Range.Start)
s, err := types.VisualPointFromPosition(buf, d.Range.Start)
if err != nil {
v.Logf("redefineDiagnostics: failed to resolve start position: %v", err)
continue
}
e, err := types.PointFromPosition(buf, d.Range.End)
e, err := types.VisualPointFromPosition(buf, d.Range.End)
if err != nil {
v.Logf("redefineDiagnostics: failed to resolve end position: %v", err)
continue
Expand Down
19 changes: 19 additions & 0 deletions cmd/govim/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ func PointFromPosition(b *Buffer, pos protocol.Position) (Point, error) {
return res, nil
}

func VisualPointFromPosition(b *Buffer, pos protocol.Position) (Point, error) {
p, err := PointFromPosition(b, pos)
if err != nil {
return p, err
}
c := b.Contents()
l := len(c)
if p.Offset() == l && l > 0 && c[l-1] == '\n' {
cc := b.tokenConvertor()
var newLine, newCol int
newLine, newCol, err = cc.ToPosition(l - 1)
if err != nil {
return p, err
}
p, err = PointFromVim(b, newLine, newCol)
}
return p, err
}

// Line refers to the 1-indexed line in the buffer. This is how Vim refers to
// line numbers.
func (p Point) Line() int {
Expand Down
2 changes: 1 addition & 1 deletion cmd/govim/testdata/scenario_default/quickfix_eof.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ package main

var _ *
-- errors.golden --
main.go|4 col 1| expected ';', found 'EOF'
main.go|3 col 8| expected ';', found 'EOF'

0 comments on commit 6e8213f

Please sign in to comment.