1
1
/*----------------------------------------------------------------------------
2
2
-- Language Server Protocol --
3
3
-- --
4
- -- Copyright (C) 2018-2021 , AdaCore --
4
+ -- Copyright (C) 2018-2023 , AdaCore --
5
5
-- --
6
6
-- This is free software; you can redistribute it and/or modify it under --
7
7
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -22,7 +22,6 @@ import {
22
22
LanguageClientOptions ,
23
23
Middleware ,
24
24
ServerOptions ,
25
- ExecutableOptions
26
25
} from 'vscode-languageclient/node' ;
27
26
import { platform } from 'os' ;
28
27
import * as process from 'process' ;
@@ -32,7 +31,6 @@ import gnatproveTaskProvider from './gnatproveTaskProvider';
32
31
import { getSubprogramSymbol } from './gnatproveTaskProvider' ;
33
32
import { alsCommandExecutor } from './alsExecuteCommand' ;
34
33
import { ALSClientFeatures } from './alsClientFeatures' ;
35
- import { string } from 'fp-ts' ;
36
34
37
35
let alsTaskProvider : vscode . Disposable [ ] = [
38
36
vscode . tasks . registerTaskProvider ( GPRTaskProvider . gprBuildType , new GPRTaskProvider ( ) ) ,
@@ -47,45 +45,55 @@ let alsTaskProvider: vscode.Disposable[] = [
47
45
/**
48
46
* Add a subprogram box above the subprogram enclosing the cursor's position, if any.
49
47
*
50
- * @example :
48
+ * @example
51
49
*
52
50
* -------
53
51
* - Foo -
54
52
* -------
55
53
*
56
54
* procedure Foo is
57
55
*/
58
- function addSupbrogramBox ( ) {
56
+ async function addSupbrogramBox ( ) {
59
57
const activeEditor = vscode . window . activeTextEditor ;
60
58
61
- getSubprogramSymbol ( activeEditor )
62
- . then ( symbol => {
63
- if ( symbol !== null ) {
64
- const name : string = symbol . name ?? ""
65
- const insertPos = new vscode . Position ( symbol . range . start . line , 0 ) ;
66
- const indentationRange = new vscode . Range ( insertPos , symbol . range . start )
67
- const indentation : string = activeEditor ?. document . getText ( indentationRange ) ?? ""
68
- const eol : string = activeEditor ?. document . eol == vscode . EndOfLine . CRLF ? "\r\n" : "\n" ;
59
+ await getSubprogramSymbol ( activeEditor ) . then ( async ( symbol ) => {
60
+ if ( symbol !== null ) {
61
+ const name : string = symbol . name ?? '' ;
62
+ const insertPos = new vscode . Position ( symbol . range . start . line , 0 ) ;
63
+ const indentationRange = new vscode . Range ( insertPos , symbol . range . start ) ;
64
+ const indentation : string = activeEditor ?. document . getText ( indentationRange ) ?? '' ;
65
+ const eol : string = activeEditor ?. document . eol == vscode . EndOfLine . CRLF ? '\r\n' : '\n' ;
69
66
70
- // Generate the subprogram box after retrieving the indentation of the line of
71
- // the subprogram's body declaration.
72
- let text : string = indentation + "---" + '-' . repeat ( name . length ) + "---" + eol
73
- + indentation + "-- " + name + " --" + eol
74
- + indentation + "---" + '-' . repeat ( name . length ) + "---" + eol
75
- + eol ;
67
+ // Generate the subprogram box after retrieving the indentation of the line of
68
+ // the subprogram's body declaration.
69
+ const text : string =
70
+ indentation +
71
+ '---' +
72
+ '-' . repeat ( name . length ) +
73
+ '---' +
74
+ eol +
75
+ indentation +
76
+ '-- ' +
77
+ name +
78
+ ' --' +
79
+ eol +
80
+ indentation +
81
+ '---' +
82
+ '-' . repeat ( name . length ) +
83
+ '---' +
84
+ eol +
85
+ eol ;
76
86
77
- activeEditor ?. document . eol . toString
78
- if ( activeEditor ) {
79
-
80
- activeEditor . edit ( editBuilder => {
81
- editBuilder . insert ( insertPos , text ) ;
82
- } ) ;
83
- }
87
+ if ( activeEditor ) {
88
+ await activeEditor . edit ( ( editBuilder ) => {
89
+ editBuilder . insert ( insertPos , text ) ;
90
+ } ) ;
84
91
}
85
- } )
92
+ }
93
+ } ) ;
86
94
}
87
95
88
- export function activate ( context : vscode . ExtensionContext ) : void {
96
+ export async function activate ( context : vscode . ExtensionContext ) : Promise < void > {
89
97
function createClient ( id : string , name : string , extra : string [ ] , pattern : string ) {
90
98
let serverModule = context . asAbsolutePath ( process . platform + '/ada_language_server' ) ;
91
99
if ( process . env . ALS ) serverModule = process . env . ALS ;
@@ -97,28 +105,32 @@ export function activate(context: vscode.ExtensionContext): void {
97
105
// Retrieve the user's custom environment variables if specified in their
98
106
// settings/workspace: we'll then launch any child process with this custom
99
107
// environment
100
- var user_platform = platform ( ) ;
101
- var env_config_name : string = " terminal.integrated.env.linux" ;
108
+ const user_platform = platform ( ) ;
109
+ let env_config_name = ' terminal.integrated.env.linux' ;
102
110
103
111
switch ( user_platform ) {
104
- case 'darwin' : env_config_name = "terminal.integrated.env.osx"
112
+ case 'darwin' :
113
+ env_config_name = 'terminal.integrated.env.osx' ;
105
114
break ;
106
- case 'win32' : env_config_name = "terminal.integrated.env.windows" ;
115
+ case 'win32' :
116
+ env_config_name = 'terminal.integrated.env.windows' ;
107
117
break ;
108
- default : env_config_name = "terminal.integrated.env.linux" ;
118
+ default :
119
+ env_config_name = 'terminal.integrated.env.linux' ;
109
120
}
110
121
111
- const custom_env = vscode . workspace . getConfiguration ( ) . get (
112
- env_config_name ) ?? Object . create ( null )
122
+ const custom_env = vscode . workspace . getConfiguration ( ) . get < [ string ] > ( env_config_name ) ;
113
123
114
- for ( let var_name in custom_env ) {
115
- process . env [ var_name ] = custom_env [ var_name ] ;
124
+ if ( custom_env ) {
125
+ for ( const var_name in custom_env ) {
126
+ process . env [ var_name ] = custom_env [ var_name ] ;
127
+ }
116
128
}
117
129
118
130
// Options to control the server
119
131
const serverOptions : ServerOptions = {
120
132
run : { command : serverModule , args : extra } ,
121
- debug : { command : serverModule , args : extra }
133
+ debug : { command : serverModule , args : extra } ,
122
134
} ;
123
135
124
136
// Options to control the language client
@@ -193,63 +205,82 @@ export function activate(context: vscode.ExtensionContext): void {
193
205
194
206
context . subscriptions . push ( vscode . workspace . onDidChangeConfiguration ( configChanged ) ) ;
195
207
context . subscriptions . push ( vscode . commands . registerCommand ( 'ada.otherFile' , otherFileHandler ) ) ;
196
- context . subscriptions . push ( vscode . commands . registerCommand ( 'ada.subprogramBox' , addSupbrogramBox ) ) ;
208
+ context . subscriptions . push (
209
+ vscode . commands . registerCommand ( 'ada.subprogramBox' , addSupbrogramBox )
210
+ ) ;
211
+
212
+ type ALSSourceDirDescription = {
213
+ name : string ;
214
+ uri : string ;
215
+ } ;
197
216
198
- // Check if we need to add some source directories to the workspace (e.g: when imported projects'
199
- // source directories are not placed under the root project's directory).
217
+ // Check if we need to add some source directories to the workspace (e.g: when imported
218
+ // projects' source directories are not placed under the root project's directory).
200
219
// Do nothing is the user did not setup any workspace file.
201
220
if ( vscode . workspace . workspaceFile !== undefined ) {
202
- client . onReady ( ) . then ( ( ) => {
203
- client . sendRequest ( "workspace/alsSourceDirs" ) . then (
204
- data => {
205
- const source_dirs = JSON . parse ( JSON . stringify ( data ) ) ;
206
- const workspace_folders = vscode . workspace . workspaceFolders ?? [ ]
207
- let workspace_dirs_to_add : { uri : vscode . Uri , name ?: string | undefined } [ ] = [ ]
221
+ await client . onReady ( ) . then ( async ( ) => {
222
+ await client
223
+ . sendRequest < [ ALSSourceDirDescription ] > ( 'workspace/alsSourceDirs' )
224
+ . then ( async ( source_dirs ) => {
225
+ const workspace_folders = vscode . workspace . workspaceFolders ?? [ ] ;
226
+ const workspace_dirs_to_add : { uri : vscode . Uri ; name ?: string | undefined } [ ] =
227
+ [ ] ;
208
228
209
- for ( var source_dir of source_dirs ) {
210
- const source_dir_uri = vscode . Uri . parse ( source_dir . uri )
211
- let source_dir_path = source_dir_uri . path
212
- let workspace_folder_path = workspace_folders [ 0 ] . uri . path
229
+ for ( const source_dir of source_dirs ) {
230
+ const source_dir_uri = vscode . Uri . parse ( source_dir . uri ) ;
231
+ const source_dir_path = source_dir_uri . path ;
213
232
214
- function is_subdirectory ( dir : string , parent : string ) {
215
- // Use lower-case on Windows since drives can be specified in VS Code either
216
- // with lower or upper case characters.
217
- if ( process . platform == " win32" ) {
233
+ const is_subdirectory = ( dir : string , parent : string ) => {
234
+ // Use lower-case on Windows since drives can be specified in VS Code
235
+ // either with lower or upper case characters.
236
+ if ( process . platform == ' win32' ) {
218
237
dir = dir . toLowerCase ( ) ;
219
238
parent = parent . toLowerCase ( ) ;
220
239
}
221
240
222
241
return dir . startsWith ( parent + '/' ) ;
242
+ } ;
223
243
224
- }
225
-
226
- // If the source directory is not under one of the workspace folders, push this
227
- // source directory to the workspace folders to add later.
228
- if ( ! workspace_folders . some ( workspace_folder =>
229
- is_subdirectory ( source_dir_path , workspace_folder . uri . path ) ) ) {
230
- workspace_dirs_to_add . push ( { name : source_dir . name , uri : source_dir_uri } ) ;
244
+ // If the source directory is not under one of the workspace folders, push
245
+ // this source directory to the workspace folders to add later.
246
+ if (
247
+ ! workspace_folders . some ( ( workspace_folder ) =>
248
+ is_subdirectory ( source_dir_path , workspace_folder . uri . path )
249
+ )
250
+ ) {
251
+ workspace_dirs_to_add . push ( {
252
+ name : source_dir . name ,
253
+ uri : source_dir_uri ,
254
+ } ) ;
231
255
}
232
256
}
233
257
234
258
// If there are some source directories missing in the workspace, ask the user
235
259
// to add them in his workspace.
236
260
if ( workspace_dirs_to_add . length > 0 ) {
237
- vscode . window
238
- . showInformationMessage ( "Some project source directories are not "
239
- + "listed in your workspace: do you want to add them?" , "Yes" , "No" )
240
- . then ( answer => {
241
- if ( answer === "Yes" ) {
242
- for ( var workspace_dir of workspace_dirs_to_add ) {
261
+ await vscode . window
262
+ . showInformationMessage (
263
+ 'Some project source directories are not ' +
264
+ 'listed in your workspace: do you want to add them?' ,
265
+ 'Yes' ,
266
+ 'No'
267
+ )
268
+ . then ( ( answer ) => {
269
+ if ( answer === 'Yes' ) {
270
+ for ( const workspace_dir of workspace_dirs_to_add ) {
243
271
vscode . workspace . updateWorkspaceFolders (
244
- vscode . workspace . workspaceFolders ? vscode . workspace . workspaceFolders . length : 0 , null ,
245
- workspace_dir ) ;
272
+ vscode . workspace . workspaceFolders
273
+ ? vscode . workspace . workspaceFolders . length
274
+ : 0 ,
275
+ null ,
276
+ workspace_dir
277
+ ) ;
246
278
}
247
279
}
248
- } )
280
+ } ) ;
249
281
}
250
- }
251
- )
252
- } )
282
+ } ) ;
283
+ } ) ;
253
284
}
254
285
}
255
286
0 commit comments