Skip to content

Implemented "textDocument/formatter" #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added InoMapper.CppToInoRangeOk() method
  • Loading branch information
cmaglie committed Dec 18, 2020
commit 2c80ae772f34662b79f222efcc48a030c3dbbd06
32 changes: 22 additions & 10 deletions handler/sourcemapper/ino.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/bcmi-labs/arduino-language-server/handler/textutils"
"github.com/bcmi-labs/arduino-language-server/lsp"
"github.com/pkg/errors"
)

// InoMapper is a mapping between the .ino sketch and the preprocessed .cpp file
Expand Down Expand Up @@ -78,17 +79,28 @@ func (s *InoMapper) CppToInoLine(targetLine int) (string, int) {
return res.File, res.Line
}

// CppToInoRange converts a target (.cpp) lsp.Range into a source.ino:lsp.Range
func (s *InoMapper) CppToInoRange(r lsp.Range) (string, lsp.Range) {
startFile, startLine := s.CppToInoLine(r.Start.Line)
endFile, endLine := s.CppToInoLine(r.End.Line)
res := r
res.Start.Line = startLine
res.End.Line = endLine
if startFile != endFile {
panic("invalid range conversion")
// CppToInoRange converts a target (.cpp) lsp.Range into a source.ino:lsp.Range.
// It will panic if the range spans across multiple ino files.
func (s *InoMapper) CppToInoRange(cppRange lsp.Range) (string, lsp.Range) {
inoFile, inoRange, err := s.CppToInoRangeOk(cppRange)
if err != nil {
panic(err.Error())
}
return inoFile, inoRange
}

// CppToInoRangeOk converts a target (.cpp) lsp.Range into a source.ino:lsp.Range.
// It returns an error if the range spans across multiple ino files.
func (s *InoMapper) CppToInoRangeOk(cppRange lsp.Range) (string, lsp.Range, error) {
inoFile, startLine := s.CppToInoLine(cppRange.Start.Line)
endInoFile, endLine := s.CppToInoLine(cppRange.End.Line)
inoRange := cppRange
inoRange.Start.Line = startLine
inoRange.End.Line = endLine
if inoFile != endInoFile {
return "", lsp.Range{}, errors.Errorf("invalid range conversion %s -> %s:%d-%s:%d", cppRange, inoFile, startLine, endInoFile, endLine)
}
return startFile, res
return inoFile, inoRange, nil
}

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