3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { Connection , InitializeParams , InitializeResult , NotebookDocuments , TextDocuments } from 'vscode-languageserver' ;
6
+ import { CancellationToken , Connection , InitializeParams , InitializeResult , NotebookDocuments , TextDocuments } from 'vscode-languageserver' ;
7
7
import { TextDocument } from 'vscode-languageserver-textdocument' ;
8
8
import * as lsp from 'vscode-languageserver-types' ;
9
9
import * as md from 'vscode-markdown-languageservice' ;
10
10
import { URI } from 'vscode-uri' ;
11
+ import { getLsConfiguration } from './config' ;
11
12
import { LogFunctionLogger } from './logging' ;
12
- import { parseRequestType } from './protocol' ;
13
+ import * as protocol from './protocol' ;
13
14
import { VsCodeClientWorkspace } from './workspace' ;
14
15
15
16
export async function startServer ( connection : Connection ) {
16
17
const documents = new TextDocuments ( TextDocument ) ;
17
18
const notebooks = new NotebookDocuments ( documents ) ;
18
19
19
20
connection . onInitialize ( ( params : InitializeParams ) : InitializeResult => {
21
+ const parser = new class implements md . IMdParser {
22
+ slugifier = md . githubSlugifier ;
23
+
24
+ async tokenize ( document : md . ITextDocument ) : Promise < md . Token [ ] > {
25
+ return await connection . sendRequest ( protocol . parseRequestType , { uri : document . uri . toString ( ) } ) ;
26
+ }
27
+ } ;
28
+
29
+ const config = getLsConfiguration ( {
30
+ markdownFileExtensions : params . initializationOptions . markdownFileExtensions ,
31
+ } ) ;
32
+
33
+ const workspace = new VsCodeClientWorkspace ( connection , config , documents , notebooks ) ;
34
+ const logger = new LogFunctionLogger ( connection . console . log . bind ( connection . console ) ) ;
35
+ provider = md . createLanguageService ( {
36
+ workspace,
37
+ parser,
38
+ logger,
39
+ markdownFileExtensions : config . markdownFileExtensions ,
40
+ } ) ;
41
+
20
42
workspace . workspaceFolders = ( params . workspaceFolders ?? [ ] ) . map ( x => URI . parse ( x . uri ) ) ;
21
43
return {
22
44
capabilities : {
45
+ completionProvider : { triggerCharacters : [ '.' , '/' , '#' ] } ,
46
+ definitionProvider : true ,
23
47
documentLinkProvider : { resolveProvider : true } ,
24
48
documentSymbolProvider : true ,
25
- completionProvider : { triggerCharacters : [ '.' , '/' , '#' ] } ,
26
49
foldingRangeProvider : true ,
50
+ renameProvider : { prepareProvider : true , } ,
27
51
selectionRangeProvider : true ,
28
52
workspaceSymbolProvider : true ,
29
53
workspace : {
@@ -36,23 +60,14 @@ export async function startServer(connection: Connection) {
36
60
} ;
37
61
} ) ;
38
62
39
- const parser = new class implements md . IMdParser {
40
- slugifier = md . githubSlugifier ;
41
-
42
- async tokenize ( document : md . ITextDocument ) : Promise < md . Token [ ] > {
43
- return await connection . sendRequest ( parseRequestType , { uri : document . uri . toString ( ) } ) ;
44
- }
45
- } ;
46
63
47
- const workspace = new VsCodeClientWorkspace ( connection , documents , notebooks ) ;
48
- const logger = new LogFunctionLogger ( connection . console . log . bind ( connection . console ) ) ;
49
- const provider = md . createLanguageService ( { workspace, parser, logger } ) ;
64
+ let provider : md . IMdLanguageService | undefined ;
50
65
51
66
connection . onDocumentLinks ( async ( params , token ) : Promise < lsp . DocumentLink [ ] > => {
52
67
try {
53
68
const document = documents . get ( params . textDocument . uri ) ;
54
69
if ( document ) {
55
- return await provider . getDocumentLinks ( document , token ) ;
70
+ return await provider ! . getDocumentLinks ( document , token ) ;
56
71
}
57
72
} catch ( e ) {
58
73
console . error ( e . stack ) ;
@@ -62,7 +77,7 @@ export async function startServer(connection: Connection) {
62
77
63
78
connection . onDocumentLinkResolve ( async ( link , token ) : Promise < lsp . DocumentLink | undefined > => {
64
79
try {
65
- return await provider . resolveDocumentLink ( link , token ) ;
80
+ return await provider ! . resolveDocumentLink ( link , token ) ;
66
81
} catch ( e ) {
67
82
console . error ( e . stack ) ;
68
83
}
@@ -73,7 +88,7 @@ export async function startServer(connection: Connection) {
73
88
try {
74
89
const document = documents . get ( params . textDocument . uri ) ;
75
90
if ( document ) {
76
- return await provider . getDocumentSymbols ( document , token ) ;
91
+ return await provider ! . getDocumentSymbols ( document , token ) ;
77
92
}
78
93
} catch ( e ) {
79
94
console . error ( e . stack ) ;
@@ -85,7 +100,7 @@ export async function startServer(connection: Connection) {
85
100
try {
86
101
const document = documents . get ( params . textDocument . uri ) ;
87
102
if ( document ) {
88
- return await provider . getFoldingRanges ( document , token ) ;
103
+ return await provider ! . getFoldingRanges ( document , token ) ;
89
104
}
90
105
} catch ( e ) {
91
106
console . error ( e . stack ) ;
@@ -97,7 +112,7 @@ export async function startServer(connection: Connection) {
97
112
try {
98
113
const document = documents . get ( params . textDocument . uri ) ;
99
114
if ( document ) {
100
- return await provider . getSelectionRanges ( document , params . positions , token ) ;
115
+ return await provider ! . getSelectionRanges ( document , params . positions , token ) ;
101
116
}
102
117
} catch ( e ) {
103
118
console . error ( e . stack ) ;
@@ -107,7 +122,7 @@ export async function startServer(connection: Connection) {
107
122
108
123
connection . onWorkspaceSymbol ( async ( params , token ) : Promise < lsp . WorkspaceSymbol [ ] > => {
109
124
try {
110
- return await provider . getWorkspaceSymbols ( params . query , token ) ;
125
+ return await provider ! . getWorkspaceSymbols ( params . query , token ) ;
111
126
} catch ( e ) {
112
127
console . error ( e . stack ) ;
113
128
}
@@ -118,14 +133,73 @@ export async function startServer(connection: Connection) {
118
133
try {
119
134
const document = documents . get ( params . textDocument . uri ) ;
120
135
if ( document ) {
121
- return await provider . getCompletionItems ( document , params . position , params . context ! , token ) ;
136
+ return await provider ! . getCompletionItems ( document , params . position , params . context ! , token ) ;
137
+ }
138
+ } catch ( e ) {
139
+ console . error ( e . stack ) ;
140
+ }
141
+ return [ ] ;
142
+ } ) ;
143
+
144
+ connection . onReferences ( async ( params , token ) : Promise < lsp . Location [ ] > => {
145
+ try {
146
+ const document = documents . get ( params . textDocument . uri ) ;
147
+ if ( document ) {
148
+ return await provider ! . getReferences ( document , params . position , params . context , token ) ;
122
149
}
123
150
} catch ( e ) {
124
151
console . error ( e . stack ) ;
125
152
}
126
153
return [ ] ;
127
154
} ) ;
128
155
156
+ connection . onDefinition ( async ( params , token ) : Promise < lsp . Definition | undefined > => {
157
+ try {
158
+ const document = documents . get ( params . textDocument . uri ) ;
159
+ if ( document ) {
160
+ return await provider ! . getDefinition ( document , params . position , token ) ;
161
+ }
162
+ } catch ( e ) {
163
+ console . error ( e . stack ) ;
164
+ }
165
+ return undefined ;
166
+ } ) ;
167
+
168
+ connection . onPrepareRename ( async ( params , token ) => {
169
+ try {
170
+ const document = documents . get ( params . textDocument . uri ) ;
171
+ if ( document ) {
172
+ return await provider ! . prepareRename ( document , params . position , token ) ;
173
+ }
174
+ } catch ( e ) {
175
+ console . error ( e . stack ) ;
176
+ }
177
+ return undefined ;
178
+ } ) ;
179
+
180
+ connection . onRenameRequest ( async ( params , token ) => {
181
+ try {
182
+ const document = documents . get ( params . textDocument . uri ) ;
183
+ if ( document ) {
184
+ const edit = await provider ! . getRenameEdit ( document , params . position , params . newName , token ) ;
185
+ console . log ( JSON . stringify ( edit ) ) ;
186
+ return edit ;
187
+ }
188
+ } catch ( e ) {
189
+ console . error ( e . stack ) ;
190
+ }
191
+ return undefined ;
192
+ } ) ;
193
+
194
+ connection . onRequest ( protocol . getReferencesToFileInWorkspace , ( async ( params : { uri : string } , token : CancellationToken ) => {
195
+ try {
196
+ return await provider ! . getFileReferences ( URI . parse ( params . uri ) , token ) ;
197
+ } catch ( e ) {
198
+ console . error ( e . stack ) ;
199
+ }
200
+ return undefined ;
201
+ } ) ) ;
202
+
129
203
documents . listen ( connection ) ;
130
204
notebooks . listen ( connection ) ;
131
205
connection . listen ( ) ;
0 commit comments