Skip to content

Commit a7fe119

Browse files
committed
Fix multiline templates prototype generation
1 parent 75a2be8 commit a7fe119

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

src/arduino.cc/builder/ctags/ctags_has_issues.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,73 @@ func (p *CTagsParser) prototypeAndCodeDontMatch(tag *types.CTag) bool {
110110
code = removeEverythingAfterClosingRoundBracket(code)
111111
// Get how many characters are "missing"
112112
n := strings.Index(prototype, code)
113+
line := 0
113114
// Add these characters to "code" string
114-
code = getFunctionProtoWithNPreviousCharacters(tag, code, n)
115+
code, line = getFunctionProtoWithNPreviousCharacters(tag, code, n)
115116
// Check again for perfect matching
116117
ret = strings.Index(code, prototype)
118+
if ret != -1 {
119+
tag.Line = line
120+
}
117121
}
118122

119123
return ret == -1
120124
}
121125

126+
func findTemplateMultiline(tag *types.CTag) string {
127+
code, _ := getFunctionProtoUntilTemplateToken(tag, tag.Code)
128+
return removeEverythingAfterClosingRoundBracket(code)
129+
}
130+
122131
func removeEverythingAfterClosingRoundBracket(s string) string {
123132
n := strings.Index(s, ")")
124133
return s[0 : n+1]
125134
}
126135

127-
func getFunctionProtoWithNPreviousCharacters(tag *types.CTag, code string, n int) string {
136+
func getFunctionProtoUntilTemplateToken(tag *types.CTag, code string) (string, int) {
137+
138+
/* FIXME I'm ugly */
139+
line := 0
140+
141+
file, err := os.Open(tag.Filename)
142+
if err == nil {
143+
defer file.Close()
144+
145+
scanner := bufio.NewScanner(file)
146+
multilinecomment := false
147+
var textBuffer []string
148+
149+
// buffer lines until we get to the start of this tag
150+
for scanner.Scan() && line < (tag.Line-1) {
151+
line++
152+
text := scanner.Text()
153+
textBuffer = append(textBuffer, text)
154+
}
155+
156+
for line > 0 && !strings.Contains(code, TEMPLATE) {
157+
158+
line = line - 1
159+
text := textBuffer[line]
160+
161+
text, multilinecomment = removeComments(text, multilinecomment)
162+
163+
code = text + code
164+
}
165+
}
166+
return code, line
167+
}
168+
169+
func getFunctionProtoWithNPreviousCharacters(tag *types.CTag, code string, n int) (string, int) {
128170

129171
/* FIXME I'm ugly */
130172
expectedPrototypeLen := len(code) + n
173+
line := 0
131174

132175
file, err := os.Open(tag.Filename)
133176
if err == nil {
134177
defer file.Close()
135178

136179
scanner := bufio.NewScanner(file)
137-
line := 0
138180
multilinecomment := false
139181
var textBuffer []string
140182

@@ -156,7 +198,7 @@ func getFunctionProtoWithNPreviousCharacters(tag *types.CTag, code string, n int
156198
code = removeSpacesAndTabs(code)
157199
}
158200
}
159-
return code
201+
return code, line
160202
}
161203

162204
func removeComments(text string, multilinecomment bool) (string, bool) {

src/arduino.cc/builder/ctags/ctags_parser.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,20 @@ func (p *CTagsParser) addPrototypes() {
8181
}
8282

8383
func addPrototype(tag *types.CTag) {
84-
if strings.Index(tag.Prototype, TEMPLATE) == 0 || strings.Index(tag.Code, TEMPLATE) == 0 {
85-
code := tag.Code
86-
if strings.Contains(code, "{") {
87-
code = code[:strings.Index(code, "{")]
84+
if strings.Index(tag.Prototype, TEMPLATE) == 0 {
85+
if strings.Index(tag.Code, TEMPLATE) == 0 {
86+
code := tag.Code
87+
if strings.Contains(code, "{") {
88+
code = code[:strings.Index(code, "{")]
89+
} else {
90+
code = code[:strings.LastIndex(code, ")")+1]
91+
}
92+
tag.Prototype = code + ";"
8893
} else {
89-
code = code[:strings.LastIndex(code, ")")+1]
94+
//tag.Code is 99% multiline, recreate it
95+
code := findTemplateMultiline(tag)
96+
tag.Prototype = code + ";"
9097
}
91-
tag.Prototype = code + ";"
9298
return
9399
}
94100

0 commit comments

Comments
 (0)