Skip to content

Commit 30f6cbc

Browse files
committed
Factorize handling of environment variables
1 parent 811fed8 commit 30f6cbc

File tree

2 files changed

+89
-66
lines changed

2 files changed

+89
-66
lines changed

integration/vscode/ada/src/extension.ts

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
-- of the license. --
1616
----------------------------------------------------------------------------*/
1717

18+
import * as process from 'process';
1819
import * as vscode from 'vscode';
1920
import {
2021
Disposable,
@@ -25,14 +26,10 @@ import {
2526
ServerOptions,
2627
SymbolKind,
2728
} from 'vscode-languageclient/node';
28-
import { platform } from 'os';
29-
import * as process from 'process';
30-
import GnatTaskProvider from './gnatTaskProvider';
31-
import GprTaskProvider from './gprTaskProvider';
32-
import { getEnclosingSymbol } from './gnatTaskProvider';
3329
import { alsCommandExecutor } from './alsExecuteCommand';
34-
import { ALSClientFeatures } from './alsClientFeatures';
35-
import { substituteVariables } from './helpers';
30+
import GnatTaskProvider, { getEnclosingSymbol } from './gnatTaskProvider';
31+
import GprTaskProvider from './gprTaskProvider';
32+
import { getEvaluatedCustomEnv } from './helpers';
3633

3734
export let contextClients: ContextClients;
3835
export let mainLogChannel: vscode.OutputChannel;
@@ -181,29 +178,11 @@ function createClient(
181178
// Retrieve the user's custom environment variables if specified in their
182179
// settings/workspace: we'll then launch any child process with this custom
183180
// environment
184-
const user_platform = platform();
185-
let env_config_name = 'terminal.integrated.env.linux';
186-
187-
switch (user_platform) {
188-
case 'darwin':
189-
env_config_name = 'terminal.integrated.env.osx';
190-
break;
191-
case 'win32':
192-
env_config_name = 'terminal.integrated.env.windows';
193-
break;
194-
default:
195-
env_config_name = 'terminal.integrated.env.linux';
196-
}
197-
198-
const custom_env = vscode.workspace.getConfiguration().get<[string]>(env_config_name);
181+
const custom_env = getEvaluatedCustomEnv();
199182

200183
if (custom_env) {
201184
for (const var_name in custom_env) {
202-
let var_value: string = custom_env[var_name];
203-
204-
// Substitute VS Code variable references that might be present
205-
// in the JSON settings configuration (e.g: "PATH": "${workspaceFolder}/obj")
206-
var_value = var_value.replace(/(\$\{.*\})/, substituteVariables);
185+
const var_value: string = custom_env[var_name];
207186
process.env[var_name] = var_value;
208187
}
209188
}
@@ -243,42 +222,44 @@ function createClient(
243222
async function addSupbrogramBox() {
244223
const activeEditor = vscode.window.activeTextEditor;
245224

246-
await getEnclosingSymbol(
247-
activeEditor, [SymbolKind.Function, SymbolKind.Module]).then(async (symbol) => {
248-
if (symbol !== null) {
249-
const name: string = symbol.name ?? '';
250-
const insertPos = new vscode.Position(symbol.range.start.line, 0);
251-
const indentationRange = new vscode.Range(insertPos, symbol.range.start);
252-
const indentation: string = activeEditor?.document.getText(indentationRange) ?? '';
253-
const eol: string = activeEditor?.document.eol == vscode.EndOfLine.CRLF ? '\r\n' : '\n';
254-
255-
// Generate the subprogram box after retrieving the indentation of the line of
256-
// the subprogram's body declaration.
257-
const text: string =
258-
indentation +
259-
'---' +
260-
'-'.repeat(name.length) +
261-
'---' +
262-
eol +
263-
indentation +
264-
'-- ' +
265-
name +
266-
' --' +
267-
eol +
268-
indentation +
269-
'---' +
270-
'-'.repeat(name.length) +
271-
'---' +
272-
eol +
273-
eol;
274-
275-
if (activeEditor) {
276-
await activeEditor.edit((editBuilder) => {
277-
editBuilder.insert(insertPos, text);
278-
});
225+
await getEnclosingSymbol(activeEditor, [SymbolKind.Function, SymbolKind.Module]).then(
226+
async (symbol) => {
227+
if (symbol !== null) {
228+
const name: string = symbol.name ?? '';
229+
const insertPos = new vscode.Position(symbol.range.start.line, 0);
230+
const indentationRange = new vscode.Range(insertPos, symbol.range.start);
231+
const indentation: string = activeEditor?.document.getText(indentationRange) ?? '';
232+
const eol: string =
233+
activeEditor?.document.eol == vscode.EndOfLine.CRLF ? '\r\n' : '\n';
234+
235+
// Generate the subprogram box after retrieving the indentation of the line of
236+
// the subprogram's body declaration.
237+
const text: string =
238+
indentation +
239+
'---' +
240+
'-'.repeat(name.length) +
241+
'---' +
242+
eol +
243+
indentation +
244+
'-- ' +
245+
name +
246+
' --' +
247+
eol +
248+
indentation +
249+
'---' +
250+
'-'.repeat(name.length) +
251+
'---' +
252+
eol +
253+
eol;
254+
255+
if (activeEditor) {
256+
await activeEditor.edit((editBuilder) => {
257+
editBuilder.insert(insertPos, text);
258+
});
259+
}
279260
}
280261
}
281-
});
262+
);
282263
}
283264

284265
type ALSSourceDirDescription = {

integration/vscode/ada/src/helpers.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
1515
-- of the license. --
1616
----------------------------------------------------------------------------*/
17-
import * as vscode from 'vscode';
17+
import { platform } from 'os';
1818
import * as path from 'path';
19+
import * as vscode from 'vscode';
1920

2021
/**
2122
* Substitue any variable reference present in the given string. VS Code
2223
* variable references are listed here:
2324
* https://code.visualstudio.com/docs/editor/variables-reference
24-
* @param str
25-
* @param recursive
26-
* @returns
25+
* @param str - string to perform substitution on
26+
* @param recursive - whether to perform substitution recursively on the result
27+
* of each substitution until there are no variables to substitute.
28+
*
29+
* @returns string after applying substitutions
2730
*/
28-
export function substituteVariables(str: string, recursive = false) {
31+
export function substituteVariables(str: string, recursive = false): string {
2932
const workspaces = vscode.workspace.workspaceFolders ?? [];
3033
const workspace = workspaces.length ? workspaces[0] : null;
3134
const activeEditor = vscode.window.activeTextEditor;
@@ -81,20 +84,59 @@ export function substituteVariables(str: string, recursive = false) {
8184
}
8285

8386
str = str.replace(/\${env:(.*?)}/g, function (variable) {
87+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
8488
return process.env[variable.match(/\${env:(.*?)}/)![1]] || '';
8589
});
8690

8791
str = str.replace(/\${config:(.*?)}/g, function (variable) {
92+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
8893
return vscode.workspace.getConfiguration().get(variable.match(/\${config:(.*?)}/)![1], '');
8994
});
9095

9196
if (
9297
recursive &&
9398
str.match(
99+
// eslint-disable-next-line max-len
94100
/\${(workspaceFolder|workspaceFolderBasename|fileWorkspaceFolder|relativeFile|fileBasename|fileBasenameNoExtension|fileExtname|fileDirname|cwd|pathSeparator|lineNumber|selectedText|env:(.*?)|config:(.*?))}/
95101
)
96102
) {
97103
str = substituteVariables(str, recursive);
98104
}
99105
return str;
100106
}
107+
108+
export function getCustomEnv() {
109+
const user_platform = platform();
110+
let env_config_name = 'terminal.integrated.env';
111+
112+
switch (user_platform) {
113+
case 'darwin':
114+
env_config_name += '.osx';
115+
break;
116+
case 'win32':
117+
env_config_name += '.windows';
118+
break;
119+
default:
120+
env_config_name += '.linux';
121+
}
122+
123+
const custom_env = vscode.workspace.getConfiguration().get<[string]>(env_config_name);
124+
125+
return custom_env;
126+
}
127+
128+
export function getEvaluatedCustomEnv() {
129+
const custom_env = getCustomEnv();
130+
131+
if (custom_env) {
132+
for (const var_name in custom_env) {
133+
// Substitute VS Code variable references that might be present
134+
// in the JSON settings configuration (e.g: "PATH": "${workspaceFolder}/obj")
135+
custom_env[var_name] = custom_env[var_name].replace(/(\$\{.*\})/, (substring) =>
136+
substituteVariables(substring, false)
137+
);
138+
}
139+
}
140+
141+
return custom_env;
142+
}

0 commit comments

Comments
 (0)