@@ -10,6 +10,7 @@ import (
10
10
11
11
"github.com/bcmi-labs/arduino-language-server/handler/textutils"
12
12
"github.com/bcmi-labs/arduino-language-server/lsp"
13
+ "github.com/pkg/errors"
13
14
)
14
15
15
16
// InoMapper is a mapping between the .ino sketch and the preprocessed .cpp file
@@ -78,17 +79,28 @@ func (s *InoMapper) CppToInoLine(targetLine int) (string, int) {
78
79
return res .File , res .Line
79
80
}
80
81
81
- // CppToInoRange converts a target (.cpp) lsp.Range into a source.ino:lsp.Range
82
- func (s * InoMapper ) CppToInoRange (r lsp.Range ) (string , lsp.Range ) {
83
- startFile , startLine := s .CppToInoLine (r .Start .Line )
84
- endFile , endLine := s .CppToInoLine (r .End .Line )
85
- res := r
86
- res .Start .Line = startLine
87
- res .End .Line = endLine
88
- if startFile != endFile {
89
- panic ("invalid range conversion" )
82
+ // CppToInoRange converts a target (.cpp) lsp.Range into a source.ino:lsp.Range.
83
+ // It will panic if the range spans across multiple ino files.
84
+ func (s * InoMapper ) CppToInoRange (cppRange lsp.Range ) (string , lsp.Range ) {
85
+ inoFile , inoRange , err := s .CppToInoRangeOk (cppRange )
86
+ if err != nil {
87
+ panic (err .Error ())
88
+ }
89
+ return inoFile , inoRange
90
+ }
91
+
92
+ // CppToInoRangeOk converts a target (.cpp) lsp.Range into a source.ino:lsp.Range.
93
+ // It returns an error if the range spans across multiple ino files.
94
+ func (s * InoMapper ) CppToInoRangeOk (cppRange lsp.Range ) (string , lsp.Range , error ) {
95
+ inoFile , startLine := s .CppToInoLine (cppRange .Start .Line )
96
+ endInoFile , endLine := s .CppToInoLine (cppRange .End .Line )
97
+ inoRange := cppRange
98
+ inoRange .Start .Line = startLine
99
+ inoRange .End .Line = endLine
100
+ if inoFile != endInoFile {
101
+ return "" , lsp.Range {}, errors .Errorf ("invalid range conversion %s -> %s:%d-%s:%d" , cppRange , inoFile , startLine , endInoFile , endLine )
90
102
}
91
- return startFile , res
103
+ return inoFile , inoRange , nil
92
104
}
93
105
94
106
// CppToInoLineOk converts a target (.cpp) line into a source (.ino) line and
0 commit comments