@theia/variable-resolver 1.48.1 → 1.48.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +114 -114
  2. package/lib/browser/common-variable-contribution.d.ts +16 -16
  3. package/lib/browser/common-variable-contribution.js +161 -161
  4. package/lib/browser/index.d.ts +3 -3
  5. package/lib/browser/index.js +21 -21
  6. package/lib/browser/variable-input-schema.d.ts +2 -2
  7. package/lib/browser/variable-input-schema.js +130 -130
  8. package/lib/browser/variable-input.d.ts +20 -20
  9. package/lib/browser/variable-input.js +2 -2
  10. package/lib/browser/variable-quick-open-service.d.ts +14 -14
  11. package/lib/browser/variable-quick-open-service.js +69 -69
  12. package/lib/browser/variable-resolver-frontend-contribution.d.ts +13 -13
  13. package/lib/browser/variable-resolver-frontend-contribution.js +53 -53
  14. package/lib/browser/variable-resolver-frontend-contribution.spec.d.ts +4 -4
  15. package/lib/browser/variable-resolver-frontend-contribution.spec.js +82 -82
  16. package/lib/browser/variable-resolver-frontend-module.d.ts +3 -3
  17. package/lib/browser/variable-resolver-frontend-module.js +37 -37
  18. package/lib/browser/variable-resolver-service.d.ts +50 -50
  19. package/lib/browser/variable-resolver-service.js +162 -162
  20. package/lib/browser/variable-resolver-service.spec.d.ts +1 -1
  21. package/lib/browser/variable-resolver-service.spec.js +75 -75
  22. package/lib/browser/variable.d.ts +55 -55
  23. package/lib/browser/variable.js +73 -73
  24. package/lib/browser/variable.spec.d.ts +1 -1
  25. package/lib/browser/variable.spec.js +90 -90
  26. package/lib/common/variable-types.d.ts +7 -7
  27. package/lib/common/variable-types.js +17 -17
  28. package/package.json +4 -4
  29. package/src/browser/common-variable-contribution.ts +151 -151
  30. package/src/browser/index.ts +19 -19
  31. package/src/browser/variable-input-schema.ts +131 -131
  32. package/src/browser/variable-input.ts +47 -47
  33. package/src/browser/variable-quick-open-service.ts +62 -62
  34. package/src/browser/variable-resolver-frontend-contribution.spec.ts +98 -98
  35. package/src/browser/variable-resolver-frontend-contribution.ts +50 -50
  36. package/src/browser/variable-resolver-frontend-module.ts +40 -40
  37. package/src/browser/variable-resolver-service.spec.ts +83 -83
  38. package/src/browser/variable-resolver-service.ts +185 -185
  39. package/src/browser/variable.spec.ts +106 -106
  40. package/src/browser/variable.ts +111 -111
  41. package/src/common/variable-types.ts +23 -23
@@ -1,83 +1,83 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018 Red Hat, Inc. and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import * as chai from 'chai';
18
- import { Container } from '@theia/core/shared/inversify';
19
- import { cancelled } from '@theia/core/lib/common';
20
- import { VariableRegistry } from './variable';
21
- import { VariableResolverService } from './variable-resolver-service';
22
-
23
- const expect = chai.expect;
24
-
25
- before(() => {
26
- chai.config.showDiff = true;
27
- chai.config.includeStack = true;
28
- });
29
-
30
- describe('variable-resolver-service', () => {
31
-
32
- let testContainer: Container;
33
- let variableRegistry: VariableRegistry;
34
- let variableResolverService: VariableResolverService;
35
-
36
- beforeEach(() => {
37
- testContainer = new Container();
38
- testContainer.bind(VariableRegistry).toSelf().inSingletonScope();
39
- testContainer.bind(VariableResolverService).toSelf().inSingletonScope();
40
- variableRegistry = testContainer.get(VariableRegistry);
41
- variableRegistry.registerVariable({
42
- name: 'file',
43
- description: 'current file',
44
- resolve: () => Promise.resolve('package.json')
45
- });
46
- variableRegistry.registerVariable({
47
- name: 'lineNumber',
48
- description: 'current line number',
49
- resolve: () => Promise.resolve('6')
50
- });
51
- variableResolverService = testContainer.get(VariableResolverService);
52
- });
53
-
54
- it('should resolve known variables in a text', async () => {
55
- const resolved = await variableResolverService.resolve('file: ${file}; line: ${lineNumber}');
56
- expect(resolved).is.equal('file: package.json; line: 6');
57
- });
58
-
59
- it('should resolve known variables in a string array', async () => {
60
- const resolved = await variableResolverService.resolveArray(['file: ${file}', 'line: ${lineNumber}']);
61
- expect(resolved!.length).to.be.equal(2);
62
- expect(resolved).to.contain('file: package.json');
63
- expect(resolved).to.contain('line: 6');
64
- });
65
-
66
- it('should skip unknown variables', async () => {
67
- const resolved = await variableResolverService.resolve('workspace: ${workspaceRoot}; file: ${file}; line: ${lineNumber}');
68
- expect(resolved).is.equal('workspace: ${workspaceRoot}; file: package.json; line: 6');
69
- });
70
-
71
- it('should return undefined when a variable throws with `cancelled()` while resolving', async () => {
72
- variableRegistry.registerVariable({
73
- name: 'command',
74
- resolve: (contextUri, commandId) => {
75
- if (commandId === 'testCommand') {
76
- throw cancelled();
77
- }
78
- }
79
- });
80
- const resolved = await variableResolverService.resolve('workspace: ${command:testCommand}; file: ${file}; line: ${lineNumber}');
81
- expect(resolved).equal(undefined);
82
- });
83
- });
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018 Red Hat, Inc. and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import * as chai from 'chai';
18
+ import { Container } from '@theia/core/shared/inversify';
19
+ import { cancelled } from '@theia/core/lib/common';
20
+ import { VariableRegistry } from './variable';
21
+ import { VariableResolverService } from './variable-resolver-service';
22
+
23
+ const expect = chai.expect;
24
+
25
+ before(() => {
26
+ chai.config.showDiff = true;
27
+ chai.config.includeStack = true;
28
+ });
29
+
30
+ describe('variable-resolver-service', () => {
31
+
32
+ let testContainer: Container;
33
+ let variableRegistry: VariableRegistry;
34
+ let variableResolverService: VariableResolverService;
35
+
36
+ beforeEach(() => {
37
+ testContainer = new Container();
38
+ testContainer.bind(VariableRegistry).toSelf().inSingletonScope();
39
+ testContainer.bind(VariableResolverService).toSelf().inSingletonScope();
40
+ variableRegistry = testContainer.get(VariableRegistry);
41
+ variableRegistry.registerVariable({
42
+ name: 'file',
43
+ description: 'current file',
44
+ resolve: () => Promise.resolve('package.json')
45
+ });
46
+ variableRegistry.registerVariable({
47
+ name: 'lineNumber',
48
+ description: 'current line number',
49
+ resolve: () => Promise.resolve('6')
50
+ });
51
+ variableResolverService = testContainer.get(VariableResolverService);
52
+ });
53
+
54
+ it('should resolve known variables in a text', async () => {
55
+ const resolved = await variableResolverService.resolve('file: ${file}; line: ${lineNumber}');
56
+ expect(resolved).is.equal('file: package.json; line: 6');
57
+ });
58
+
59
+ it('should resolve known variables in a string array', async () => {
60
+ const resolved = await variableResolverService.resolveArray(['file: ${file}', 'line: ${lineNumber}']);
61
+ expect(resolved!.length).to.be.equal(2);
62
+ expect(resolved).to.contain('file: package.json');
63
+ expect(resolved).to.contain('line: 6');
64
+ });
65
+
66
+ it('should skip unknown variables', async () => {
67
+ const resolved = await variableResolverService.resolve('workspace: ${workspaceRoot}; file: ${file}; line: ${lineNumber}');
68
+ expect(resolved).is.equal('workspace: ${workspaceRoot}; file: package.json; line: 6');
69
+ });
70
+
71
+ it('should return undefined when a variable throws with `cancelled()` while resolving', async () => {
72
+ variableRegistry.registerVariable({
73
+ name: 'command',
74
+ resolve: (contextUri, commandId) => {
75
+ if (commandId === 'testCommand') {
76
+ throw cancelled();
77
+ }
78
+ }
79
+ });
80
+ const resolved = await variableResolverService.resolve('workspace: ${command:testCommand}; file: ${file}; line: ${lineNumber}');
81
+ expect(resolved).equal(undefined);
82
+ });
83
+ });
@@ -1,185 +1,185 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018 Red Hat, Inc. and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- /* eslint-disable @typescript-eslint/no-explicit-any */
18
-
19
- import { injectable, inject } from '@theia/core/shared/inversify';
20
- import { VariableRegistry } from './variable';
21
- import URI from '@theia/core/lib/common/uri';
22
- import { CommandIdVariables } from '../common/variable-types';
23
- import { isCancelled } from '@theia/core';
24
-
25
- export interface VariableResolveOptions {
26
- context?: URI;
27
- /**
28
- * Used for resolving inputs, see https://code.visualstudio.com/docs/editor/variables-reference#_input-variables
29
- */
30
- configurationSection?: string;
31
- commandIdVariables?: CommandIdVariables;
32
- configuration?: unknown;
33
- }
34
-
35
- /**
36
- * The variable resolver service should be used to resolve variables in strings.
37
- */
38
- @injectable()
39
- export class VariableResolverService {
40
-
41
- protected static VAR_REGEXP = /\$\{(.*?)\}/g;
42
-
43
- @inject(VariableRegistry) protected readonly variableRegistry: VariableRegistry;
44
-
45
- /**
46
- * Resolve the variables in the given string array.
47
- * @param value The array of data to resolve variables in.
48
- * @param options Options of the variable resolution.
49
- * @returns Promise to array with variables resolved. Never rejects.
50
- *
51
- * @deprecated since 1.28.0 use {@link resolve} instead.
52
- */
53
- resolveArray(value: string[], options: VariableResolveOptions = {}): Promise<string[] | undefined> {
54
- return this.resolve(value, options);
55
- }
56
-
57
- /**
58
- * Resolve the variables for all strings found in the object and nested objects.
59
- * @param value Data to resolve variables in.
60
- * @param options Options of the variable resolution
61
- * @returns Promise to object with variables resolved. Returns `undefined` if a variable resolution was cancelled.
62
- */
63
- async resolve<T>(value: T, options: VariableResolveOptions = {}): Promise<T | undefined> {
64
- const context = new VariableResolverService.Context(this.variableRegistry, options);
65
- try {
66
- return await this.doResolve(value, context);
67
- } catch (error) {
68
- if (isCancelled(error)) {
69
- return undefined;
70
- }
71
- throw error;
72
- }
73
- }
74
-
75
- protected async doResolve(value: any, context: VariableResolverService.Context): Promise<any> {
76
- // eslint-disable-next-line no-null/no-null
77
- if (value === undefined || value === null) {
78
- return value;
79
- }
80
- if (typeof value === 'string') {
81
- return this.doResolveString(value, context);
82
- }
83
- if (Array.isArray(value)) {
84
- return this.doResolveArray(value, context);
85
- }
86
- if (typeof value === 'object') {
87
- return this.doResolveObject(value, context);
88
- }
89
- return value;
90
- }
91
-
92
- protected async doResolveObject(obj: object, context: VariableResolverService.Context): Promise<object> {
93
- const result: {
94
- [prop: string]: Object | undefined
95
- } = {};
96
- for (const name of Object.keys(obj)) {
97
- const value = (obj as any)[name];
98
- const resolved = await this.doResolve(value, context);
99
- result[name] = resolved;
100
- }
101
- return result;
102
- }
103
-
104
- protected async doResolveArray(values: Array<Object | undefined>, context: VariableResolverService.Context): Promise<Array<Object | undefined>> {
105
- const result: (Object | undefined)[] = [];
106
- for (const value of values) {
107
- const resolved = await this.doResolve(value, context);
108
- result.push(resolved);
109
- }
110
- return result;
111
- }
112
-
113
- protected async doResolveString(value: string, context: VariableResolverService.Context): Promise<string> {
114
- await this.resolveVariables(value, context);
115
- return value.replace(VariableResolverService.VAR_REGEXP, (match: string, varName: string) => {
116
- const varValue = context.get(varName);
117
- return varValue !== undefined ? varValue : match;
118
- });
119
- }
120
-
121
- protected async resolveVariables(value: string, context: VariableResolverService.Context): Promise<void> {
122
- const variableRegExp = new RegExp(VariableResolverService.VAR_REGEXP);
123
- let match;
124
- // eslint-disable-next-line no-null/no-null
125
- while ((match = variableRegExp.exec(value)) !== null) {
126
- const variableName = match[1];
127
- await context.resolve(variableName);
128
- }
129
- }
130
- }
131
- export namespace VariableResolverService {
132
-
133
- export class Context {
134
-
135
- protected readonly resolved = new Map<string, string | undefined>();
136
-
137
- constructor(
138
- protected readonly variableRegistry: VariableRegistry,
139
- protected readonly options: VariableResolveOptions
140
- ) { }
141
-
142
- get(name: string): string | undefined {
143
- return this.resolved.get(name);
144
- }
145
-
146
- async resolve(name: string): Promise<void> {
147
- if (this.resolved.has(name)) {
148
- return;
149
- }
150
- try {
151
- let variableName = name;
152
- let argument: string | undefined;
153
- const parts = name.split(':', 2);
154
- if (parts.length > 1) {
155
- variableName = parts[0];
156
- argument = parts[1];
157
- }
158
- const variable = this.variableRegistry.getVariable(variableName);
159
- const resolved = await variable?.resolve(
160
- this.options.context,
161
- argument,
162
- this.options.configurationSection,
163
- this.options.commandIdVariables,
164
- this.options.configuration
165
- );
166
- if (
167
- typeof resolved === 'bigint' ||
168
- typeof resolved === 'boolean' ||
169
- typeof resolved === 'number' ||
170
- typeof resolved === 'string'
171
- ) {
172
- this.resolved.set(name, `${resolved}`);
173
- } else {
174
- this.resolved.set(name, undefined);
175
- }
176
- } catch (e) {
177
- if (isCancelled(e)) {
178
- throw e;
179
- }
180
- this.resolved.set(name, undefined);
181
- console.error(`Failed to resolve '${name}' variable:`, e);
182
- }
183
- }
184
- }
185
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018 Red Hat, Inc. and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ /* eslint-disable @typescript-eslint/no-explicit-any */
18
+
19
+ import { injectable, inject } from '@theia/core/shared/inversify';
20
+ import { VariableRegistry } from './variable';
21
+ import URI from '@theia/core/lib/common/uri';
22
+ import { CommandIdVariables } from '../common/variable-types';
23
+ import { isCancelled } from '@theia/core';
24
+
25
+ export interface VariableResolveOptions {
26
+ context?: URI;
27
+ /**
28
+ * Used for resolving inputs, see https://code.visualstudio.com/docs/editor/variables-reference#_input-variables
29
+ */
30
+ configurationSection?: string;
31
+ commandIdVariables?: CommandIdVariables;
32
+ configuration?: unknown;
33
+ }
34
+
35
+ /**
36
+ * The variable resolver service should be used to resolve variables in strings.
37
+ */
38
+ @injectable()
39
+ export class VariableResolverService {
40
+
41
+ protected static VAR_REGEXP = /\$\{(.*?)\}/g;
42
+
43
+ @inject(VariableRegistry) protected readonly variableRegistry: VariableRegistry;
44
+
45
+ /**
46
+ * Resolve the variables in the given string array.
47
+ * @param value The array of data to resolve variables in.
48
+ * @param options Options of the variable resolution.
49
+ * @returns Promise to array with variables resolved. Never rejects.
50
+ *
51
+ * @deprecated since 1.28.0 use {@link resolve} instead.
52
+ */
53
+ resolveArray(value: string[], options: VariableResolveOptions = {}): Promise<string[] | undefined> {
54
+ return this.resolve(value, options);
55
+ }
56
+
57
+ /**
58
+ * Resolve the variables for all strings found in the object and nested objects.
59
+ * @param value Data to resolve variables in.
60
+ * @param options Options of the variable resolution
61
+ * @returns Promise to object with variables resolved. Returns `undefined` if a variable resolution was cancelled.
62
+ */
63
+ async resolve<T>(value: T, options: VariableResolveOptions = {}): Promise<T | undefined> {
64
+ const context = new VariableResolverService.Context(this.variableRegistry, options);
65
+ try {
66
+ return await this.doResolve(value, context);
67
+ } catch (error) {
68
+ if (isCancelled(error)) {
69
+ return undefined;
70
+ }
71
+ throw error;
72
+ }
73
+ }
74
+
75
+ protected async doResolve(value: any, context: VariableResolverService.Context): Promise<any> {
76
+ // eslint-disable-next-line no-null/no-null
77
+ if (value === undefined || value === null) {
78
+ return value;
79
+ }
80
+ if (typeof value === 'string') {
81
+ return this.doResolveString(value, context);
82
+ }
83
+ if (Array.isArray(value)) {
84
+ return this.doResolveArray(value, context);
85
+ }
86
+ if (typeof value === 'object') {
87
+ return this.doResolveObject(value, context);
88
+ }
89
+ return value;
90
+ }
91
+
92
+ protected async doResolveObject(obj: object, context: VariableResolverService.Context): Promise<object> {
93
+ const result: {
94
+ [prop: string]: Object | undefined
95
+ } = {};
96
+ for (const name of Object.keys(obj)) {
97
+ const value = (obj as any)[name];
98
+ const resolved = await this.doResolve(value, context);
99
+ result[name] = resolved;
100
+ }
101
+ return result;
102
+ }
103
+
104
+ protected async doResolveArray(values: Array<Object | undefined>, context: VariableResolverService.Context): Promise<Array<Object | undefined>> {
105
+ const result: (Object | undefined)[] = [];
106
+ for (const value of values) {
107
+ const resolved = await this.doResolve(value, context);
108
+ result.push(resolved);
109
+ }
110
+ return result;
111
+ }
112
+
113
+ protected async doResolveString(value: string, context: VariableResolverService.Context): Promise<string> {
114
+ await this.resolveVariables(value, context);
115
+ return value.replace(VariableResolverService.VAR_REGEXP, (match: string, varName: string) => {
116
+ const varValue = context.get(varName);
117
+ return varValue !== undefined ? varValue : match;
118
+ });
119
+ }
120
+
121
+ protected async resolveVariables(value: string, context: VariableResolverService.Context): Promise<void> {
122
+ const variableRegExp = new RegExp(VariableResolverService.VAR_REGEXP);
123
+ let match;
124
+ // eslint-disable-next-line no-null/no-null
125
+ while ((match = variableRegExp.exec(value)) !== null) {
126
+ const variableName = match[1];
127
+ await context.resolve(variableName);
128
+ }
129
+ }
130
+ }
131
+ export namespace VariableResolverService {
132
+
133
+ export class Context {
134
+
135
+ protected readonly resolved = new Map<string, string | undefined>();
136
+
137
+ constructor(
138
+ protected readonly variableRegistry: VariableRegistry,
139
+ protected readonly options: VariableResolveOptions
140
+ ) { }
141
+
142
+ get(name: string): string | undefined {
143
+ return this.resolved.get(name);
144
+ }
145
+
146
+ async resolve(name: string): Promise<void> {
147
+ if (this.resolved.has(name)) {
148
+ return;
149
+ }
150
+ try {
151
+ let variableName = name;
152
+ let argument: string | undefined;
153
+ const parts = name.split(':', 2);
154
+ if (parts.length > 1) {
155
+ variableName = parts[0];
156
+ argument = parts[1];
157
+ }
158
+ const variable = this.variableRegistry.getVariable(variableName);
159
+ const resolved = await variable?.resolve(
160
+ this.options.context,
161
+ argument,
162
+ this.options.configurationSection,
163
+ this.options.commandIdVariables,
164
+ this.options.configuration
165
+ );
166
+ if (
167
+ typeof resolved === 'bigint' ||
168
+ typeof resolved === 'boolean' ||
169
+ typeof resolved === 'number' ||
170
+ typeof resolved === 'string'
171
+ ) {
172
+ this.resolved.set(name, `${resolved}`);
173
+ } else {
174
+ this.resolved.set(name, undefined);
175
+ }
176
+ } catch (e) {
177
+ if (isCancelled(e)) {
178
+ throw e;
179
+ }
180
+ this.resolved.set(name, undefined);
181
+ console.error(`Failed to resolve '${name}' variable:`, e);
182
+ }
183
+ }
184
+ }
185
+ }