Skip to content

Commit 53006fd

Browse files
committed
refactor: introduce Dependency to replace Identity on depencies
1 parent 344e634 commit 53006fd

File tree

8 files changed

+91
-65
lines changed

8 files changed

+91
-65
lines changed

src/compress/golang/plugin/parse/ctx.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,18 @@ func (ctx *fileContext) GetTypeId(typ ast.Expr) (x Identity, isPointer bool, isS
209209
}
210210
}
211211

212-
func (ctx *fileContext) collectFields(fields []*ast.Field, m *[]Identity) {
212+
func (ctx *fileContext) collectFields(fields []*ast.Field, m *[]Dependency) {
213213
for _, fieldDecl := range fields {
214214
id, _, isStdOrBuiltin := ctx.GetTypeId(fieldDecl.Type)
215215
if isStdOrBuiltin || id.PkgPath == "" {
216216
continue
217217
}
218-
*m = append(*m, id)
218+
*m = append(*m, Dependency{
219+
Identity: id,
220+
FileLine: ctx.FileLine(fieldDecl),
221+
})
219222
}
223+
return
220224
}
221225

222226
type importInfo struct {
@@ -329,16 +333,17 @@ func getTypeName(fset *token.FileSet, file []byte, typ ast.Expr) (ret []Identity
329333

330334
func (p *GoParser) collectTypes(ctx *fileContext, typ ast.Expr, st *Type, inlined bool) {
331335
id, _, isGoBuiltins := ctx.GetTypeId(typ)
336+
dep := NewDependency(id, ctx.FileLine(typ))
332337
if isGoBuiltins || id.PkgPath == "" {
333338
return
334339
}
335340
if err := p.referCodes(ctx, &id, p.opts.ReferCodeDepth); err != nil {
336341
fmt.Fprintf(os.Stderr, "failed to get refer code for %s: %v\n", id.Name, err)
337342
}
338343
if inlined {
339-
st.InlineStruct = append(st.InlineStruct, id)
344+
st.InlineStruct = append(st.InlineStruct, dep)
340345
} else {
341-
st.SubStruct = append(st.SubStruct, id)
346+
st.SubStruct = append(st.SubStruct, dep)
342347
}
343348
}
344349

src/compress/golang/plugin/parse/file.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ func (p *GoParser) parseSelector(ctx *fileContext, expr *ast.SelectorExpr, infos
219219
return false
220220
}
221221
id := NewIdentity(mod, path, expr.Sel.Name)
222+
dep := NewDependency(id, ctx.FileLine(expr.Sel))
222223

223224
// NOTICE: refer external codes for convinience
224225
if err := p.referCodes(ctx, &id, p.opts.ReferCodeDepth); err != nil {
@@ -233,16 +234,16 @@ func (p *GoParser) parseSelector(ctx *fileContext, expr *ast.SelectorExpr, infos
233234
// // fmt.Fprintf(os.Stderr, "failed to get type id for %s\n", expr.Name)
234235
// return false
235236
// }
236-
*infos.tys = Dedup(*infos.tys, id)
237+
*infos.tys = Dedup(*infos.tys, dep)
237238
// global var
238239
} else if _, ok := v.(*types.Const); ok {
239-
*infos.globalVars = Dedup(*infos.globalVars, id)
240+
*infos.globalVars = Dedup(*infos.globalVars, dep)
240241
// external const
241242
} else if _, ok := v.(*types.Var); ok {
242-
*infos.globalVars = Dedup(*infos.globalVars, id)
243+
*infos.globalVars = Dedup(*infos.globalVars, dep)
243244
// external function
244245
} else if _, ok := v.(*types.Func); ok {
245-
*infos.functionCalls = Dedup(*infos.functionCalls, id)
246+
*infos.functionCalls = Dedup(*infos.functionCalls, dep)
246247
}
247248
return false
248249
}
@@ -281,19 +282,20 @@ func (p *GoParser) parseSelector(ctx *fileContext, expr *ast.SelectorExpr, infos
281282
rname = rev.Name()
282283
}
283284
id := NewIdentity(mod, pkg, rname+"."+expr.Sel.Name)
285+
dep := NewDependency(id, ctx.FileLine(expr.Sel))
284286
if err := p.referCodes(ctx, &id, p.opts.ReferCodeDepth); err != nil {
285287
fmt.Fprintf(os.Stderr, "failed to get refer code for %s: %v\n", id.Name, err)
286288
}
287-
*infos.methodCalls = Dedup(*infos.methodCalls, id)
289+
*infos.methodCalls = Dedup(*infos.methodCalls, dep)
288290
return false
289291
}
290292

291293
return cont
292294
}
293295

294296
type collectInfos struct {
295-
functionCalls, methodCalls *[]Identity
296-
tys, globalVars *[]Identity
297+
functionCalls, methodCalls *[]Dependency
298+
tys, globalVars *[]Dependency
297299
}
298300

299301
// parseFunc parses all function declaration in one file
@@ -321,23 +323,20 @@ func (p *GoParser) parseFunc(ctx *fileContext, funcDecl *ast.FuncDecl) (*Functio
321323
}
322324

323325
// collect parameters
324-
params := []Identity{}
326+
var params []Dependency
325327
if funcDecl.Type.Params != nil {
326328
ctx.collectFields(funcDecl.Type.Params.List, &params)
327329
}
328330
// collect results
329-
results := []Identity{}
331+
var results []Dependency
330332
if funcDecl.Type.Results != nil {
331333
ctx.collectFields(funcDecl.Type.Results.List, &results)
332334
}
333-
// collect types
334-
tys := []Identity{}
335-
// collect global vars
336-
globalVars := []Identity{}
335+
337336
// collect content
338337
content := string(ctx.GetRawContent(funcDecl))
339338

340-
var functionCalls, methodCalls = []Identity{}, []Identity{}
339+
var functionCalls, globalVars, tys, methodCalls []Dependency
341340

342341
if funcDecl.Body == nil {
343342
goto set_func
@@ -370,31 +369,32 @@ func (p *GoParser) parseFunc(ctx *fileContext, funcDecl *ast.FuncDecl) (*Functio
370369
// }
371370
if use, ok := ctx.pkgTypeInfo.Uses[expr]; ok {
372371
id := NewIdentity(ctx.module.Name, ctx.pkgPath, callName)
372+
dep := NewDependency(id, ctx.FileLine(expr))
373373
// type name
374374
if _, isNamed := use.(*types.TypeName); isNamed {
375375
// id, ok := ctx.getTypeId(tn.Type())
376376
// if !ok {
377377
// // fmt.Fprintf(os.Stderr, "failed to get type id for %s\n", expr.Name)
378378
// return false
379379
// }
380-
tys = Dedup(tys, id)
380+
tys = Dedup(tys, dep)
381381
// global var
382382
} else if v, ok := use.(*types.Var); ok {
383383
// NOTICE: the Parent of global scope is nil?
384384
if isPkgScope(v.Parent()) {
385-
globalVars = Dedup(globalVars, id)
385+
globalVars = Dedup(globalVars, dep)
386386
}
387387
// global const
388388
} else if c, ok := use.(*types.Const); ok {
389389
if isPkgScope(c.Parent()) {
390-
globalVars = Dedup(globalVars, id)
390+
globalVars = Dedup(globalVars, dep)
391391
}
392392
return false
393393
// function
394394
} else if f, ok := use.(*types.Func); ok {
395395
// exclude method
396396
if f.Type().(*types.Signature).Recv() == nil {
397-
functionCalls = Dedup(functionCalls, id)
397+
functionCalls = Dedup(functionCalls, dep)
398398
}
399399
}
400400
}
@@ -472,7 +472,8 @@ func (p *GoParser) parseStruct(ctx *fileContext, struName string, name *ast.Iden
472472
if stru, ok := fieldDecl.Type.(*ast.StructType); ok {
473473
// anonymous struct. parse and collect it
474474
as, _ := p.parseStruct(ctx, "_"+fieldname, nil, stru)
475-
st.SubStruct = append(st.SubStruct, as.Identity)
475+
dep := NewDependency(as.Identity, ctx.FileLine(fieldDecl.Type))
476+
st.SubStruct = append(st.SubStruct, dep)
476477
} else {
477478
p.collectTypes(ctx, fieldDecl.Type, st, inlined)
478479
}

src/compress/golang/plugin/parse/parser.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,8 @@ func (p *GoParser) searchOnFile(file *ast.File, fset *token.FileSet, fcontent []
378378
var m = map[string]Identity{}
379379
// NOTICE: collect all types
380380
tname, _ := p.mockTypes(spec.Type, m, fcontent, fset, v.File, mod, pkg, impt)
381-
v.Type = &Identity{
382-
ModPath: mod,
383-
PkgPath: pkg,
384-
Name: tname,
385-
}
381+
id := NewIdentity(mod, pkg, tname)
382+
v.Type = &id
386383
} else {
387384
v.Type = lastType
388385
}

src/lang/collect/collect.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2025 CloudWeGo Authors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// https://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -56,11 +56,6 @@ type Collector struct {
5656
CollectOption
5757
}
5858

59-
type dependency struct {
60-
Location Location `json:"location"`
61-
Symbol *DocumentSymbol `json:"symbol"`
62-
}
63-
6459
type methodInfo struct {
6560
Receiver dependency `json:"receiver"`
6661
Interface *dependency `json:"implement,omitempty"` // which interface it implements

src/lang/collect/export.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ import (
2727
parse "github.com/cloudwego/abcoder/src/uniast"
2828
)
2929

30+
type dependency struct {
31+
Location Location `json:"location"`
32+
Symbol *DocumentSymbol `json:"symbol"`
33+
}
34+
35+
func (d dependency) FileLine() parse.FileLine {
36+
return parse.FileLine{
37+
File: d.Location.URI.File(),
38+
Line: d.Location.Range.Start.Line,
39+
}
40+
}
41+
3042
func newModule(name string, dir string) *parse.Module {
3143
ret := parse.NewModule(name, dir)
3244
ret.Language = parse.Rust
@@ -159,7 +171,8 @@ func (c *Collector) exportSymbol(repo *parse.Repository, symbol *DocumentSymbol,
159171
log.Error("export input symbol %s failed: %v\n", input.Symbol, err)
160172
continue
161173
}
162-
obj.Types = parse.Dedup(obj.Types, *tyid)
174+
dep := parse.NewDependency(*tyid, input.FileLine())
175+
obj.Types = parse.Dedup(obj.Types, dep)
163176
}
164177
}
165178
if info.Inputs != nil {
@@ -170,7 +183,8 @@ func (c *Collector) exportSymbol(repo *parse.Repository, symbol *DocumentSymbol,
170183
log.Error("export input symbol %s failed: %v\n", input.Symbol, err)
171184
continue
172185
}
173-
obj.Params = parse.Dedup(obj.Params, *tyid)
186+
dep := parse.NewDependency(*tyid, input.FileLine())
187+
obj.Params = parse.Dedup(obj.Params, dep)
174188
}
175189
}
176190
if info.Outputs != nil {
@@ -181,7 +195,8 @@ func (c *Collector) exportSymbol(repo *parse.Repository, symbol *DocumentSymbol,
181195
log.Error("export output symbol %s failed: %v\n", output.Symbol, err)
182196
continue
183197
}
184-
obj.Results = parse.Dedup(obj.Results, *tyid)
198+
dep := parse.NewDependency(*tyid, output.FileLine())
199+
obj.Results = parse.Dedup(obj.Results, dep)
185200
}
186201
}
187202
if info.Method != nil && info.Method.Receiver.Symbol != nil {
@@ -229,25 +244,26 @@ func (c *Collector) exportSymbol(repo *parse.Repository, symbol *DocumentSymbol,
229244
log.Error("export dep symbol %s failed: %v\n", dep.Symbol, err)
230245
continue
231246
}
247+
pdep := parse.NewDependency(*depid, dep.FileLine())
232248
switch dep.Symbol.Kind {
233249
case lsp.SKFunction:
234-
obj.FunctionCalls = parse.Dedup(obj.FunctionCalls, *depid)
250+
obj.FunctionCalls = parse.Dedup(obj.FunctionCalls, pdep)
235251
case lsp.SKMethod:
236252
if obj.MethodCalls == nil {
237-
obj.MethodCalls = make([]parse.Identity, 0, len(deps))
253+
obj.MethodCalls = make([]parse.Dependency, 0, len(deps))
238254
}
239255
// NOTICE: use loc token as key here, to make it more readable
240-
obj.MethodCalls = parse.Dedup(obj.MethodCalls, *depid)
256+
obj.MethodCalls = parse.Dedup(obj.MethodCalls, pdep)
241257
case lsp.SKVariable, lsp.SKConstant:
242258
if obj.GolobalVars == nil {
243-
obj.GolobalVars = make([]parse.Identity, 0, len(deps))
259+
obj.GolobalVars = make([]parse.Dependency, 0, len(deps))
244260
}
245-
obj.GolobalVars = parse.Dedup(obj.GolobalVars, *depid)
261+
obj.GolobalVars = parse.Dedup(obj.GolobalVars, pdep)
246262
case lsp.SKStruct, lsp.SKTypeParameter, lsp.SKInterface, lsp.SKEnum:
247263
if obj.Types == nil {
248-
obj.Types = make([]parse.Identity, 0, len(deps))
264+
obj.Types = make([]parse.Dependency, 0, len(deps))
249265
}
250-
obj.Types = parse.Dedup(obj.Types, *depid)
266+
obj.Types = parse.Dedup(obj.Types, pdep)
251267
default:
252268
log.Error("dep symbol %s not collected for %v\n", dep.Symbol, id)
253269
}
@@ -275,7 +291,7 @@ func (c *Collector) exportSymbol(repo *parse.Repository, symbol *DocumentSymbol,
275291
}
276292
switch dep.Symbol.Kind {
277293
case lsp.SKStruct, lsp.SKTypeParameter, lsp.SKInterface, lsp.SKEnum:
278-
obj.SubStruct = append(obj.SubStruct, *depid)
294+
obj.SubStruct = append(obj.SubStruct, parse.NewDependency(*depid, dep.FileLine()))
279295
default:
280296
log.Error("dep symbol %s not collected for \n", dep.Symbol, id)
281297
}

src/uniast/ast.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,36 @@ type Function struct {
257257
FileLine
258258
Content string // Content of the function, including functiion signature and body
259259

260-
Receiver *Receiver `json:",omitempty"` // Method receiver
261-
Params []Identity `json:",omitempty"` // function parameters, key is the parameter name
262-
Results []Identity `json:",omitempty"` // function results, key is the result name or type name
260+
Receiver *Receiver `json:",omitempty"` // Method receiver
261+
Params []Dependency `json:",omitempty"` // function parameters, key is the parameter name
262+
Results []Dependency `json:",omitempty"` // function results, key is the result name or type name
263263

264264
// call to in-the-project functions, key is {{pkgAlias.funcName}} or {{funcName}}
265-
FunctionCalls []Identity `json:",omitempty"`
265+
FunctionCalls []Dependency `json:",omitempty"`
266266

267267
// call to internal methods,
268268
// NOTICE: method name may be duplicated, so we collect according to the SEQUENCE of APPEARANCE
269-
MethodCalls []Identity `json:",omitempty"`
269+
MethodCalls []Dependency `json:",omitempty"`
270270

271-
Types []Identity `json:",omitempty"` // types used in the function
272-
GolobalVars []Identity `json:",omitempty"` // global vars used in the function
271+
Types []Dependency `json:",omitempty"` // types used in the function
272+
GolobalVars []Dependency `json:",omitempty"` // global vars used in the function
273273

274274
// func llm compress result
275275
CompressData *string `json:"compress_data,omitempty"`
276276
}
277277

278+
type Dependency struct {
279+
Identity
280+
FileLine `json:",omitempty"`
281+
}
282+
283+
func NewDependency(id Identity, fl FileLine) Dependency {
284+
return Dependency{
285+
Identity: id,
286+
FileLine: fl,
287+
}
288+
}
289+
278290
type Receiver struct {
279291
IsPointer bool
280292
Type Identity
@@ -308,10 +320,10 @@ type Type struct {
308320
Content string // struct declaration content
309321

310322
// field type (not include basic types), type name => type id
311-
SubStruct []Identity `json:",omitempty"`
323+
SubStruct []Dependency `json:",omitempty"`
312324

313325
// inline field type (not include basic types)
314-
InlineStruct []Identity `json:",omitempty"`
326+
InlineStruct []Dependency `json:",omitempty"`
315327

316328
// methods defined on the Struct, not including inlined type's method
317329
Methods map[string]Identity `json:",omitempty"`

0 commit comments

Comments
 (0)