88 "sort"
99 "unicode"
1010
11- "github.com/issue9/sliceutil"
12-
1311 "github.com/caixw/apidoc/v7/core"
1412 "github.com/caixw/apidoc/v7/internal/ast"
1513 "github.com/caixw/apidoc/v7/internal/lsp/protocol"
@@ -18,7 +16,8 @@ import (
1816)
1917
2018type tokenBuilder struct {
21- tag , attr , value int // 可用的 token
19+ uri core.URI // 当前 builder 对应的文件地址
20+ tag , attr , value int // 可用的 token
2221
2322 // 每个二级数组长度为 5,表示一组 semanticToken 数据。
2423 // 数据分别为 绝对行号,当前行的绝对起始位置,长度,以及 token 和 modifier。
@@ -44,25 +43,14 @@ func (s *server) textDocumentSemanticTokens(notify bool, in *protocol.SemanticTo
4443// value 表示属性的颜色值;
4544func semanticTokens (doc * ast.APIDoc , uri core.URI , tag , attr , value int ) []int {
4645 b := & tokenBuilder {
46+ uri : uri ,
4747 tag : tag ,
4848 attr : attr ,
4949 value : value ,
5050 tokens : make ([][]int , 0 , 100 ),
5151 }
5252
53- if doc .URI == uri {
54- b .parse (reflect .ValueOf (doc ), "APIs" )
55- }
56-
57- for _ , api := range doc .APIs {
58- matched := api .URI == uri || (api .URI == "" && doc .URI == uri )
59- if ! matched {
60- continue
61- }
62-
63- b .parse (reflect .ValueOf (api ))
64- }
65-
53+ b .parse (reflect .ValueOf (doc ))
6654 b .sort ()
6755 return b .build ()
6856}
@@ -108,20 +96,18 @@ func (b *tokenBuilder) sort() {
10896 })
10997}
11098
111- func (b * tokenBuilder ) parse (v reflect.Value , exclude ... string ) {
99+ func (b * tokenBuilder ) parse (v reflect.Value ) {
112100 v = node .RealValue (v )
113- t := v .Type ()
114- if t .Kind () != reflect .Struct {
101+ if ! b .matched (v ) {
115102 return
116103 }
117104
118105 b .parseAnonymous (v )
119106
107+ t := v .Type ()
120108 for i := 0 ; i < t .NumField (); i ++ {
121109 tf := t .Field (i )
122- if tf .Anonymous ||
123- unicode .IsLower (rune (tf .Name [0 ])) ||
124- sliceutil .Count (exclude , func (i int ) bool { return exclude [i ] == tf .Name }) > 0 { // 需要过滤的字段
110+ if tf .Anonymous || unicode .IsLower (rune (tf .Name [0 ])) {
125111 continue
126112 }
127113
@@ -136,6 +122,23 @@ func (b *tokenBuilder) parse(v reflect.Value, exclude ...string) {
136122 }
137123}
138124
125+ func (b * tokenBuilder ) matched (v reflect.Value ) bool {
126+ if v .Kind () != reflect .Struct {
127+ return false
128+ }
129+
130+ if s , ok := v .Interface ().(core.Searcher ); ok && s .Loc ().URI == b .uri {
131+ return true
132+ }
133+
134+ if ! v .CanAddr () {
135+ return false
136+ }
137+
138+ s , ok := v .Addr ().Interface ().(core.Searcher )
139+ return ok && s .Loc ().URI == b .uri
140+ }
141+
139142func (b * tokenBuilder ) parseAnonymous (v reflect.Value ) {
140143 t := v .Type ()
141144 switch elem := v .Interface ().(type ) {
0 commit comments