-
Notifications
You must be signed in to change notification settings - Fork 108
/
textdocument.go
142 lines (114 loc) · 3.55 KB
/
textdocument.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: MIT
package lsp
import (
"path/filepath"
"github.com/issue9/sliceutil"
"github.com/caixw/apidoc/v7/build"
"github.com/caixw/apidoc/v7/core"
"github.com/caixw/apidoc/v7/internal/ast"
"github.com/caixw/apidoc/v7/internal/lang"
"github.com/caixw/apidoc/v7/internal/lsp/protocol"
)
// textDocument/didChange
//
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didChange
func (s *server) textDocumentDidChange(notify bool, in *protocol.DidChangeTextDocumentParams, out *interface{}) error {
f := s.findFolder(in.TextDocument.URI)
if f == nil {
return nil
}
f.parsedMux.Lock()
defer f.parsedMux.Unlock()
if !deleteURI(f.doc, in.TextDocument.URI) {
return nil
}
f.clearDiagnostics()
for _, blk := range in.Blocks() {
f.parseBlock(blk)
}
f.srv.textDocumentPublishDiagnostics(f)
return nil
}
func (f *folder) parseBlock(block core.Block) {
var input *build.Input
ext := filepath.Ext(block.Location.URI.String())
for _, i := range f.cfg.Inputs {
if sliceutil.Count(i.Exts, func(index string) bool { return index == ext }) > 0 {
input = i
break
}
}
if input == nil { // 无需解析
return
}
f.doc.ParseBlocks(f.h, func(blocks chan core.Block) {
lang.Parse(f.h, input.Lang, block, blocks)
})
if err := f.srv.apidocOutline(f); err != nil {
f.srv.printErr(err)
}
}
func deleteURI(doc *ast.APIDoc, uri core.URI) (deleted bool) {
l := len(doc.APIs)
doc.APIs = sliceutil.Delete(doc.APIs, func(i *ast.API) bool {
return i.URI == uri
})
deleted = l > len(doc.APIs)
if doc.URI == uri {
*doc = ast.APIDoc{APIs: doc.APIs}
deleted = true
}
return deleted
}
// textDocument/publishDiagnostics
//
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_publishDiagnostics
func (s *server) textDocumentPublishDiagnostics(f *folder) {
for _, p := range f.diagnostics {
if err := s.Notify("textDocument/publishDiagnostics", p); err != nil {
s.erro.Println(err)
}
}
}
// 清空所有的诊断信息
func (f *folder) clearDiagnostics() {
for _, p := range f.diagnostics {
p.Diagnostics = p.Diagnostics[:0]
if err := f.srv.Notify("textDocument/publishDiagnostics", p); err != nil {
f.srv.erro.Println(err)
}
}
f.diagnostics = make(map[core.URI]*protocol.PublishDiagnosticsParams, 0)
}
// textDocument/foldingRange
//
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_foldingRange
func (s *server) textDocumentFoldingRange(notify bool, in *protocol.FoldingRangeParams, out *[]protocol.FoldingRange) error {
f := s.findFolder(in.TextDocument.URI)
if f == nil {
return nil
}
lineFoldingOnly := s.clientParams != nil &&
s.clientParams.Capabilities.TextDocument.FoldingRange != nil &&
s.clientParams.Capabilities.TextDocument.FoldingRange.LineFoldingOnly
folds := make([]protocol.FoldingRange, 0, 10)
f.parsedMux.RLock()
defer f.parsedMux.RUnlock()
if f.doc.URI == in.TextDocument.URI {
folds = append(folds, protocol.BuildFoldingRange(f.doc.Base, lineFoldingOnly))
}
for _, api := range f.doc.APIs {
if api.URI == in.TextDocument.URI {
folds = append(folds, protocol.BuildFoldingRange(api.Base, lineFoldingOnly))
}
}
*out = folds
return nil
}
// textDocument/completion
//
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
func (s *server) textDocumentCompletion(notify bool, in *protocol.CompletionParams, out *protocol.CompletionList) error {
// TODO
return nil
}