@@ -10,6 +10,7 @@ import (
1010
1111 "github.com/bcmi-labs/arduino-language-server/handler/textutils"
1212 "github.com/bcmi-labs/arduino-language-server/lsp"
13+ "github.com/pkg/errors"
1314)
1415
1516// 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) {
7879 return res .File , res .Line
7980}
8081
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 )
90102 }
91- return startFile , res
103+ return inoFile , inoRange , nil
92104}
93105
94106// CppToInoLineOk converts a target (.cpp) line into a source (.ino) line and
0 commit comments