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 Nov 5, 2019
1 parent e36394b commit e239dc6
Show file tree
Hide file tree
Showing 3 changed files with 21 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 @@ -55,12 +55,12 @@ func (v *vimstate) redefineDiagnostics() error {
continue
}
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
18 changes: 18 additions & 0 deletions cmd/govim/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ 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()
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 e239dc6

Please sign in to comment.