1
1
import * as path from 'path' ;
2
- import { workspace as Workspace , ExtensionContext , workspace , commands , window as Window ,
3
- Location ,
4
- } from 'vscode' ;
5
-
6
- import {
7
- LanguageClient , LanguageClientOptions ,
8
- ServerOptions , TransportKind , RevealOutputChannelOn ,
9
- ExecuteCommandSignature , WorkspaceFolder
10
- } from 'vscode-languageclient/node' ;
11
-
12
-
2
+ import * as vscode from 'vscode' ;
3
+ import * as lc from 'vscode-languageclient/node' ;
13
4
import * as ls from 'vscode-languageserver-protocol' ;
14
5
15
6
import registerCommands from './common/commands' ;
16
7
17
8
import CompositeDisposable from './common/compositeDisposable' ;
18
9
19
- let client : LanguageClient ;
10
+ let client : lc . LanguageClient ;
20
11
let disposables = new CompositeDisposable ( ) ;
21
12
22
13
interface ConfigurationSettings {
@@ -33,7 +24,7 @@ interface ConfigurationSettings {
33
24
}
34
25
lint ?: Object ;
35
26
keywords : Object [ ] ,
36
- workspaceFolder ?: WorkspaceFolder | undefined ;
27
+ workspaceFolder ?: lc . WorkspaceFolder | undefined ;
37
28
callFunctions ?: string [ ] ;
38
29
}
39
30
@@ -42,79 +33,90 @@ interface CodeLensReferenceArgument {
42
33
locations : ls . Location [ ]
43
34
}
44
35
45
- export function activate ( context : ExtensionContext ) {
36
+ export function activate ( context : vscode . ExtensionContext ) {
46
37
47
38
// The server is implemented in node
48
39
let serverModule = context . asAbsolutePath (
49
40
path . join ( 'server' , 'out' , 'server.js' )
50
41
) ;
51
42
52
43
let debugOptions = { execArgv : [ '--nolazy' , '--inspect=6011' ] , cwd : process . cwd ( ) } ;
53
- let serverOptions : ServerOptions = {
54
- run : { module : serverModule , transport : TransportKind . ipc , options : { cwd : process . cwd ( ) } } ,
44
+ let serverOptions : lc . ServerOptions = {
45
+ run : { module : serverModule , transport : lc . TransportKind . ipc , options : { cwd : process . cwd ( ) } } ,
55
46
debug : {
56
47
module : serverModule ,
57
- transport : TransportKind . ipc ,
48
+ transport : lc . TransportKind . ipc ,
58
49
options : debugOptions
59
50
}
60
51
} ;
61
52
62
- let clientOptions : LanguageClientOptions = {
53
+ let clientOptions : lc . LanguageClientOptions = {
63
54
documentSelector : [ { language : 'macro' , scheme : 'file' } ] ,
64
- initializationOptions : workspace . getConfiguration ( 'macro' ) ,
55
+ initializationOptions : vscode . workspace . getConfiguration ( 'macro' ) ,
65
56
synchronize : {
66
- fileEvents : workspace . createFileSystemWatcher ( '**/*.{[sS][rR][cC],[dD][eE][fF],[lL][nN][kK]}' )
57
+ fileEvents : vscode . workspace . createFileSystemWatcher ( '**/*.{[sS][rR][cC],[dD][eE][fF],[lL][nN][kK]}' )
67
58
} ,
68
59
diagnosticCollectionName : 'macro' ,
69
60
progressOnInitialization : true ,
70
- revealOutputChannelOn : RevealOutputChannelOn . Never ,
61
+ revealOutputChannelOn : lc . RevealOutputChannelOn . Never ,
71
62
middleware : {
72
-
73
- executeCommand : async ( command :string , args :any [ ] , next :ExecuteCommandSignature ) => {
63
+
64
+ executeCommand : async ( command :string , args :any [ ] , next :lc . ExecuteCommandSignature ) => {
74
65
if ( command === 'macro.codelens.references' ) {
75
66
const arg :CodeLensReferenceArgument = args [ 0 ] ;
76
67
77
68
78
69
const position = client . protocol2CodeConverter . asPosition ( arg . position ) ;
79
- const locations :Location [ ] = [ ] ;
70
+ const locations :vscode . Location [ ] = [ ] ;
80
71
for ( const location of arg . locations ) {
81
72
locations . push ( client . protocol2CodeConverter . asLocation ( location ) ) ;
82
73
}
83
74
84
- if ( Window . activeTextEditor ) {
85
- commands . executeCommand ( 'editor.action.showReferences' , Window . activeTextEditor . document . uri , position , locations ) ;
75
+ if ( vscode . window . activeTextEditor ) {
76
+ vscode . commands . executeCommand ( 'editor.action.showReferences' , vscode . window . activeTextEditor . document . uri , position , locations ) ;
86
77
}
87
78
}
88
79
else if ( command === 'macro.action.refactorsequeces' || command === 'macro.action.addsequeces' ) {
89
80
function validate ( input :string ) : string | null {
90
81
return Number . isInteger ( Number ( input ) ) ? null : 'Integer expected' ;
91
82
}
92
83
93
- const config = workspace . getConfiguration ( 'macro' ) ;
84
+ const config = vscode . workspace . getConfiguration ( 'macro' ) ;
94
85
let start = undefined ;
95
86
if ( command === 'macro.action.refactorsequeces' ) {
96
- start = await Window . showInputBox ( {
87
+ start = await vscode . window . showInputBox ( {
97
88
prompt : 'Start sequence number' ,
98
89
value : config . sequence . base ,
99
90
validateInput : validate
100
91
} ) ;
101
92
}
102
93
103
- const increment = await Window . showInputBox ( {
94
+ const increment = await vscode . window . showInputBox ( {
104
95
prompt : 'Sequence number increment' ,
105
96
value : config . sequence . increment ,
106
97
validateInput : validate
107
98
} ) ;
108
99
109
- if ( Window . activeTextEditor ) {
100
+ if ( vscode . window . activeTextEditor ) {
110
101
if ( command === 'macro.action.addsequeces' && increment ) {
111
- return next ( command , [ Window . activeTextEditor . document . uri . toString ( ) , Window . activeTextEditor . selection . start , increment ] ) ;
102
+ return next ( command , [ vscode . window . activeTextEditor . document . uri . toString ( ) , vscode . window . activeTextEditor . selection . start , increment ] ) ;
112
103
}
113
104
else if ( command === 'macro.action.refactorsequeces' && start && increment ) {
114
- return next ( command , [ Window . activeTextEditor . document . uri . toString ( ) , Window . activeTextEditor . selection . start , start , increment ] ) ;
105
+ return next ( command , [ vscode . window . activeTextEditor . document . uri . toString ( ) , vscode . window . activeTextEditor . selection . start , start , increment ] ) ;
115
106
}
116
107
}
117
108
}
109
+ else if ( command === 'macro.action.validate' ) {
110
+ const workspaceUri = args [ 0 ] ;
111
+ if ( workspaceUri ) {
112
+ return next ( command , [ workspaceUri ] ) ;
113
+ }
114
+ else {
115
+ pickFolder ( workspace => {
116
+ return next ( command , [ workspace . uri . toString ( ) ] ) ;
117
+ } ) ;
118
+ }
119
+ }
118
120
} ,
119
121
workspace : {
120
122
configuration : async ( params , _token , _next ) : Promise < any [ ] > => {
@@ -128,8 +130,8 @@ export function activate(context: ExtensionContext) {
128
130
continue ;
129
131
}
130
132
const resource = client . protocol2CodeConverter . asUri ( item . scopeUri ) ;
131
- const workspaceFolder = Workspace . getWorkspaceFolder ( resource ) ;
132
- const config = workspace . getConfiguration ( 'macro' , workspaceFolder ) ;
133
+ const workspaceFolder = vscode . workspace . getWorkspaceFolder ( resource ) ;
134
+ const config = vscode . workspace . getConfiguration ( 'macro' , workspaceFolder ) ;
133
135
const settings : ConfigurationSettings = {
134
136
codelens : config . get ( 'codelens' ) ,
135
137
lint :config . get ( 'lint' , { } ) ,
@@ -153,9 +155,8 @@ export function activate(context: ExtensionContext) {
153
155
} as any
154
156
} ;
155
157
156
-
157
158
// Create the language client and start the client.
158
- client = new LanguageClient (
159
+ client = new lc . LanguageClient (
159
160
'macroLanguageServer' ,
160
161
'Macro Language Server' ,
161
162
serverOptions ,
@@ -173,3 +174,24 @@ export function deactivate(): Thenable<void> | undefined {
173
174
}
174
175
return client . stop ( ) ;
175
176
}
177
+
178
+ export function pickFolder ( cb :( workspace : vscode . WorkspaceFolder ) => void ) {
179
+ const folders = vscode . workspace . workspaceFolders ;
180
+ if ( ! folders ) {
181
+ return ;
182
+ }
183
+
184
+ if ( folders . length === 1 ) {
185
+ cb ( folders [ 0 ] ) ;
186
+ return ;
187
+ }
188
+ vscode . window . showWorkspaceFolderPick ( { placeHolder :'' , ignoreFocusOut :true } ) . then ( selected => {
189
+ if ( selected ) {
190
+ cb ( selected ) ;
191
+ }
192
+ } ) ;
193
+ if ( folders . length === 1 ) {
194
+ cb ( folders [ 0 ] ) ;
195
+ return ;
196
+ }
197
+ }
0 commit comments