Skip to content

Commit 303635e

Browse files
committed
CppToIno range conversion now considers endline overlaps
1 parent af2c223 commit 303635e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

Diff for: handler/sourcemapper/ino.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,40 @@ func (s *InoMapper) CppToInoRange(cppRange lsp.Range) (string, lsp.Range) {
9090
return inoFile, inoRange
9191
}
9292

93+
// AdjustedRangeErr is returned if the range overlaps with a non-ino section by just the
94+
// last newline character.
95+
type AdjustedRangeErr struct{}
96+
97+
func (e AdjustedRangeErr) Error() string {
98+
return "the range has been adjusted to allow final newline"
99+
}
100+
93101
// CppToInoRangeOk converts a target (.cpp) lsp.Range into a source.ino:lsp.Range.
94102
// It returns an error if the range spans across multiple ino files.
103+
// If the range ends on the beginning of a new line in another .ino file, the range
104+
// is adjusted and AdjustedRangeErr is reported as err: the range may be still valid.
95105
func (s *InoMapper) CppToInoRangeOk(cppRange lsp.Range) (string, lsp.Range, error) {
96106
inoFile, startLine := s.CppToInoLine(cppRange.Start.Line)
97107
endInoFile, endLine := s.CppToInoLine(cppRange.End.Line)
98108
inoRange := cppRange
99109
inoRange.Start.Line = startLine
100110
inoRange.End.Line = endLine
101-
if inoFile != endInoFile {
102-
return "", lsp.Range{}, errors.Errorf("invalid range conversion %s -> %s:%d-%s:%d", cppRange, inoFile, startLine, endInoFile, endLine)
111+
if inoFile == endInoFile {
112+
// All done
113+
return inoFile, inoRange, nil
103114
}
104-
return inoFile, inoRange, nil
115+
116+
// Special case: the last line ends up in the "not-ino" area
117+
if inoRange.End.Character == 0 {
118+
if checkFile, checkLine := s.CppToInoLine(cppRange.End.Line - 1); checkFile == inoFile {
119+
// Adjust the range and return it with an AdjustedRange notification
120+
inoRange.End.Line = checkLine + 1
121+
return inoFile, inoRange, AdjustedRangeErr{}
122+
}
123+
}
124+
125+
// otherwise the range is not recoverable, just report error
126+
return inoFile, inoRange, errors.Errorf("invalid range conversion %s -> %s:%d-%s:%d", cppRange, inoFile, startLine, endInoFile, endLine)
105127
}
106128

107129
// CppToInoLineOk converts a target (.cpp) line into a source (.ino) line and

0 commit comments

Comments
 (0)