@@ -90,18 +90,40 @@ func (s *InoMapper) CppToInoRange(cppRange lsp.Range) (string, lsp.Range) {
90
90
return inoFile , inoRange
91
91
}
92
92
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
+
93
101
// CppToInoRangeOk converts a target (.cpp) lsp.Range into a source.ino:lsp.Range.
94
102
// 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.
95
105
func (s * InoMapper ) CppToInoRangeOk (cppRange lsp.Range ) (string , lsp.Range , error ) {
96
106
inoFile , startLine := s .CppToInoLine (cppRange .Start .Line )
97
107
endInoFile , endLine := s .CppToInoLine (cppRange .End .Line )
98
108
inoRange := cppRange
99
109
inoRange .Start .Line = startLine
100
110
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
103
114
}
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 )
105
127
}
106
128
107
129
// CppToInoLineOk converts a target (.cpp) line into a source (.ino) line and
0 commit comments