Skip to content

Commit 842ac7a

Browse files
committed
fix: 修复cpp class method路径问题
1 parent 53f6190 commit 842ac7a

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

lang/collect/export.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -478,18 +478,21 @@ func (c *Collector) exportSymbol(repo *uniast.Repository, symbol *DocumentSymbol
478478
}
479479
}
480480

481-
// cpp get method name without class name
481+
// cpp get method name without class name and namespace
482482
if c.Language == uniast.Cpp && rid != nil {
483-
rec := strings.TrimSpace(rid.Name)
484-
if rec != "" {
485-
searchStr := rec + "::"
486-
if idx := strings.Index(name, searchStr); idx >= 0 {
487-
name = name[idx+len(searchStr):]
488-
}
483+
p := strings.IndexByte(name, '(')
484+
head, tail := name, ""
485+
if p >= 0 {
486+
head, tail = name[:p], name[p:]
489487
}
488+
489+
if idx := strings.LastIndex(head, "::"); idx >= 0 {
490+
head = head[idx+2:]
491+
}
492+
name = head + tail
490493
}
491494

492-
if k == SKFunction {
495+
if k == SKFunction || c.Language == uniast.Cpp {
493496
// NOTICE: class static method name is: type::method
494497
id.Name += "::" + name
495498
} else {
@@ -583,7 +586,17 @@ func (c *Collector) exportSymbol(repo *uniast.Repository, symbol *DocumentSymbol
583586
continue
584587
}
585588
// NOTICE: use method name as key here
586-
obj.Methods[method.Name] = *mid
589+
if c.Language == uniast.Cpp {
590+
methodName := c.cppBaseName(method.Name)
591+
_, methodExist := obj.Methods[methodName]
592+
isHeaderMethod := strings.HasSuffix(method.Location.URI.File(), ".h")
593+
if methodExist && isHeaderMethod {
594+
continue
595+
}
596+
obj.Methods[methodName] = *mid
597+
} else {
598+
obj.Methods[method.Name] = *mid
599+
}
587600
}
588601
}
589602
obj.Identity = *id

testdata/cpp/0_simple/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ int main() {
77

88
util::Greeter hi("Hello");
99
std::cout << hi.greet("Alice") << "\n";
10-
10+
1111
hi.bump();
1212
hi.bump();
1313

0 commit comments

Comments
 (0)