Skip to content

Commit d03920d

Browse files
committed
Leverage clangd to detect function prototypes changes
1 parent a6563f3 commit d03920d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

Diff for: handler/handler.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -624,23 +624,33 @@ func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeText
624624

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

632-
log.Println("--! DIRTY CHANGE, force sketch rebuild!")
632+
// Detect changes in critical lines (for example function definitions)
633+
// and trigger arduino-preprocessing + clangd restart.
634+
dirty := false
635+
for _, sym := range handler.buildSketchSymbols {
636+
if sym.Range.Overlaps(cppRange) {
637+
dirty = true
638+
log.Println("--! DIRTY CHANGE detected using symbol tables, force sketch rebuild!")
639+
break
640+
}
641+
}
642+
if handler.sketchMapper.ApplyTextChange(doc.URI, inoChange) {
643+
dirty = true
644+
log.Println("--! DIRTY CHANGE detected with sketch mapper, force sketch rebuild!")
645+
}
646+
if dirty {
633647
handler.scheduleRebuildEnvironment()
634648
}
635649

636650
// log.Println("New version:----------")
637651
// log.Println(handler.sketchMapper.CppText.Text)
638652
// log.Println("----------------------")
639653

640-
cppRange, ok := handler.sketchMapper.InoToCppLSPRangeOk(doc.URI, *inoChange.Range)
641-
if !ok {
642-
return nil, errors.Errorf("invalid change range %s:%s", doc.URI, *inoChange.Range)
643-
}
644654
cppChange := lsp.TextDocumentContentChangeEvent{
645655
Range: &cppRange,
646656
RangeLength: inoChange.RangeLength,

Diff for: lsp/structures.go

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ func (p Position) String() string {
2121
return fmt.Sprintf("%d:%d", p.Line, p.Character)
2222
}
2323

24+
// In returns true if the Position is within the Range r
25+
func (p Position) In(r Range) bool {
26+
return p.AfterOrEq(r.Start) && p.BeforeOrEq(r.End)
27+
}
28+
29+
// BeforeOrEq returns true if the Position is before or equal the give Position
30+
func (p Position) BeforeOrEq(q Position) bool {
31+
return p.Line < q.Line || (p.Line == q.Line && p.Character <= q.Character)
32+
}
33+
34+
// AfterOrEq returns true if the Position is after or equal the give Position
35+
func (p Position) AfterOrEq(q Position) bool {
36+
return p.Line > q.Line || (p.Line == q.Line && p.Character >= q.Character)
37+
}
38+
2439
type Range struct {
2540
/**
2641
* The range's start position.
@@ -37,6 +52,11 @@ func (r Range) String() string {
3752
return fmt.Sprintf("%s-%s", r.Start, r.End)
3853
}
3954

55+
// Overlaps returns true if the Range overlaps with the given Range p
56+
func (r Range) Overlaps(p Range) bool {
57+
return r.Start.In(p) || r.End.In(p) || p.Start.In(r) || p.End.In(r)
58+
}
59+
4060
type Location struct {
4161
URI DocumentURI `json:"uri"`
4262
Range Range `json:"range"`

0 commit comments

Comments
 (0)