/
codelens.go
80 lines (70 loc) · 1.84 KB
/
codelens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package lsp
import (
"encoding/json"
"github.com/kralicky/protocompile/linker"
"github.com/kralicky/tools-lite/gopls/pkg/protocol"
"google.golang.org/protobuf/reflect/protoreflect"
)
func (c *Cache) ComputeCodeLens(uri protocol.DocumentURI) ([]protocol.CodeLens, error) {
c.resultsMu.RLock()
defer c.resultsMu.RUnlock()
if !c.resolver.IsRealWorkspaceLocalFile(uri) {
return nil, nil
}
parseRes, err := c.FindParseResultByURI(uri)
if err != nil {
return nil, err
}
if _, ok := parseRes.AST().Pragma(PragmaNoGenerate); ok {
return nil, nil
}
var codeLenses []protocol.CodeLens
req, _ := json.Marshal(GenerateCodeRequest{
URIs: []protocol.DocumentURI{uri},
})
codeLenses = append(codeLenses,
protocol.CodeLens{
Command: &protocol.Command{
Title: "Generate File",
Command: "protols.generate",
Arguments: []json.RawMessage{json.RawMessage(req)},
},
},
)
if pkg := protoreflect.FullName(parseRes.FileDescriptorProto().GetPackage()); pkg != "" {
var uris []protocol.DocumentURI
c.results.RangeFilesByPackage(pkg, func(f linker.File) bool {
uri, err := c.resolver.PathToURI(f.Path())
if err != nil {
return true
}
uris = append(uris, uri)
return true
})
req, _ := json.Marshal(GenerateCodeRequest{
URIs: uris,
})
codeLenses = append(codeLenses,
protocol.CodeLens{
Command: &protocol.Command{
Title: "Generate Package",
Command: "protols.generate",
Arguments: []json.RawMessage{json.RawMessage(req)},
},
},
)
}
req, _ = json.Marshal(GenerateWorkspaceRequest{
Workspace: c.workspace,
})
codeLenses = append(codeLenses,
protocol.CodeLens{
Command: &protocol.Command{
Title: "Generate Workspace",
Command: "protols.generateWorkspace",
Arguments: []json.RawMessage{json.RawMessage(req)},
},
},
)
return codeLenses, nil
}