Skip to content

Commit

Permalink
fix: LSP diagnostics that end at the newline not being remapped to te…
Browse files Browse the repository at this point in the history
…mpl files
  • Loading branch information
a-h committed Mar 5, 2023
1 parent 60f4799 commit a76a114
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/templ/lspcmd/proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p Client) PublishDiagnostics(ctx context.Context, params *lsp.PublishDiagn
end, ok := sourceMap.SourcePositionFromTarget(item.Range.End.Line, item.Range.End.Character)
if ok {
item.Range.End.Line = end.Line
item.Range.End.Line = end.Col
item.Range.End.Character = end.Col
}
params.Diagnostics[i] = item
}
Expand Down
35 changes: 26 additions & 9 deletions parser/v2/sourcemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,38 @@ func (sm *SourceMap) Add(src Expression, tgt Range) (updatedFrom Position) {
srcIndex := src.Range.From.Index
tgtIndex := tgt.From.Index

for lineIndex, line := range strings.Split(src.Value, "\n") {
lines := strings.Split(src.Value, "\n")
for lineIndex, line := range lines {
srcLine := src.Range.From.Line + uint32(lineIndex)
tgtLine := tgt.From.Line + uint32(lineIndex)

var srcCol, tgtCol uint32
if lineIndex == 0 {
// First line can have an offset.
srcCol += src.Range.From.Col
tgtCol += tgt.From.Col
}

// Process the cols.
for colIndex := 0; colIndex < len(line); colIndex++ {
var srcCol, tgtCol uint32 = uint32(colIndex), uint32(colIndex)
if lineIndex == 0 {
// First line can have an offset.
srcCol += src.Range.From.Col
tgtCol += tgt.From.Col
if _, ok := sm.SourceLinesToTarget[srcLine]; !ok {
sm.SourceLinesToTarget[srcLine] = make(map[uint32]Position)
}
sm.SourceLinesToTarget[srcLine][srcCol] = NewPositionFromValues(tgtIndex, tgtLine, tgtCol)

if _, ok := sm.TargetLinesToSource[tgtLine]; !ok {
sm.TargetLinesToSource[tgtLine] = make(map[uint32]Position)
}
sm.TargetLinesToSource[tgtLine][tgtCol] = NewPositionFromValues(srcIndex, srcLine, srcCol)

srcCol++
tgtCol++
srcIndex++
tgtIndex++
}

// LSPs include the newline char as a col.
if len(lines) > 1 {
if _, ok := sm.SourceLinesToTarget[srcLine]; !ok {
sm.SourceLinesToTarget[srcLine] = make(map[uint32]Position)
}
Expand All @@ -48,9 +68,6 @@ func (sm *SourceMap) Add(src Expression, tgt Range) (updatedFrom Position) {
srcIndex++
tgtIndex++
}
// Increment the index for the newline char.
srcIndex++
tgtIndex++
}
return src.Range.From
}
Expand Down

0 comments on commit a76a114

Please sign in to comment.