Skip to content

Leverage clangd to detect function prototypes changes #44

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 1 commit into from
Jan 5, 2021
Merged
Changes from all commits
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
28 changes: 19 additions & 9 deletions handler/handler.go
Original file line number Diff line number Diff line change
@@ -624,23 +624,33 @@ func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeText

cppChanges := []lsp.TextDocumentContentChangeEvent{}
for _, inoChange := range req.ContentChanges {
dirty := handler.sketchMapper.ApplyTextChange(doc.URI, inoChange)
if dirty {
// TODO: Detect changes in critical lines (for example function definitions)
// and trigger arduino-preprocessing + clangd restart.
cppRange, ok := handler.sketchMapper.InoToCppLSPRangeOk(doc.URI, *inoChange.Range)
if !ok {
return nil, errors.Errorf("invalid change range %s:%s", doc.URI, *inoChange.Range)
}

log.Println("--! DIRTY CHANGE, force sketch rebuild!")
// Detect changes in critical lines (for example function definitions)
// and trigger arduino-preprocessing + clangd restart.
dirty := false
for _, sym := range handler.buildSketchSymbols {
if sym.Range.Overlaps(cppRange) {
dirty = true
log.Println("--! DIRTY CHANGE detected using symbol tables, force sketch rebuild!")
break
}
}
if handler.sketchMapper.ApplyTextChange(doc.URI, inoChange) {
dirty = true
log.Println("--! DIRTY CHANGE detected with sketch mapper, force sketch rebuild!")
}
if dirty {
handler.scheduleRebuildEnvironment()
}

// log.Println("New version:----------")
// log.Println(handler.sketchMapper.CppText.Text)
// log.Println("----------------------")

cppRange, ok := handler.sketchMapper.InoToCppLSPRangeOk(doc.URI, *inoChange.Range)
if !ok {
return nil, errors.Errorf("invalid change range %s:%s", doc.URI, *inoChange.Range)
}
cppChange := lsp.TextDocumentContentChangeEvent{
Range: &cppRange,
RangeLength: inoChange.RangeLength,
20 changes: 20 additions & 0 deletions lsp/structures.go
Original file line number Diff line number Diff line change
@@ -21,6 +21,21 @@ func (p Position) String() string {
return fmt.Sprintf("%d:%d", p.Line, p.Character)
}

// In returns true if the Position is within the Range r
func (p Position) In(r Range) bool {
return p.AfterOrEq(r.Start) && p.BeforeOrEq(r.End)
}

// BeforeOrEq returns true if the Position is before or equal the give Position
func (p Position) BeforeOrEq(q Position) bool {
return p.Line < q.Line || (p.Line == q.Line && p.Character <= q.Character)
}

// AfterOrEq returns true if the Position is after or equal the give Position
func (p Position) AfterOrEq(q Position) bool {
return p.Line > q.Line || (p.Line == q.Line && p.Character >= q.Character)
}

type Range struct {
/**
* The range's start position.
@@ -37,6 +52,11 @@ func (r Range) String() string {
return fmt.Sprintf("%s-%s", r.Start, r.End)
}

// Overlaps returns true if the Range overlaps with the given Range p
func (r Range) Overlaps(p Range) bool {
return r.Start.In(p) || r.End.In(p) || p.Start.In(r) || p.End.In(r)
}

type Location struct {
URI DocumentURI `json:"uri"`
Range Range `json:"range"`