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
Prev Previous commit
Next Next commit
CppToIno range conversion now considers endline overlaps
  • Loading branch information
cmaglie committed Dec 18, 2020
commit 303635ec6a5a2d1c3872f6bf8e64d4a760342ab3
28 changes: 25 additions & 3 deletions handler/sourcemapper/ino.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,40 @@ func (s *InoMapper) CppToInoRange(cppRange lsp.Range) (string, lsp.Range) {
return inoFile, inoRange
}

// AdjustedRangeErr is returned if the range overlaps with a non-ino section by just the
// last newline character.
type AdjustedRangeErr struct{}

func (e AdjustedRangeErr) Error() string {
return "the range has been adjusted to allow final newline"
}

// 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.
// If the range ends on the beginning of a new line in another .ino file, the range
// is adjusted and AdjustedRangeErr is reported as err: the range may be still valid.
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)
if inoFile == endInoFile {
// All done
return inoFile, inoRange, nil
}
return inoFile, inoRange, nil

// Special case: the last line ends up in the "not-ino" area
if inoRange.End.Character == 0 {
if checkFile, checkLine := s.CppToInoLine(cppRange.End.Line - 1); checkFile == inoFile {
// Adjust the range and return it with an AdjustedRange notification
inoRange.End.Line = checkLine + 1
return inoFile, inoRange, AdjustedRangeErr{}
}
}

// otherwise the range is not recoverable, just report error
return inoFile, inoRange, errors.Errorf("invalid range conversion %s -> %s:%d-%s:%d", cppRange, inoFile, startLine, endInoFile, endLine)
}

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