@theia/variable-resolver 1.53.0-next.5 → 1.53.0-next.55

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.
package/README.md CHANGED
@@ -1,114 +1,114 @@
1
- <div align='center'>
2
-
3
- <br />
4
-
5
- <img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
6
-
7
- <h2>ECLIPSE THEIA - VARIABLE-RESOLVER EXTENSION</h2>
8
-
9
- <hr />
10
-
11
- </div>
12
-
13
- ## Description
14
-
15
- The `@theia/variable-resolved` extension provides variable substitution mechanism inside of strings using `${variableName}` syntax.
16
-
17
- ### Variable Contribution Point
18
- Extension provides a hook that allows any extensions to contribute its own variables.
19
- Here's the example of contributing two variables:
20
- - `${file}` - returns the name of the file opened in the current editor
21
- - `${lineNumber}` - returns the current line number in the current file
22
-
23
- ```typescript
24
- @injectable()
25
- export class EditorVariableContribution implements VariableContribution {
26
-
27
- constructor(
28
- @inject(EditorManager) protected readonly editorManager: EditorManager
29
- ) { }
30
-
31
- registerVariables(variables: VariableRegistry): void {
32
- variables.registerVariable({
33
- name: 'file',
34
- description: 'The name of the file opened in the current editor',
35
- resolve: () => {
36
- const currentEditor = this.getCurrentEditor();
37
- if (currentEditor) {
38
- return currentEditor.uri.displayName;
39
- }
40
- return undefined;
41
- }
42
- });
43
- variables.registerVariable({
44
- name: 'lineNumber',
45
- description: 'The current line number in the current file',
46
- resolve: () => {
47
- const currentEditor = this.getCurrentEditor();
48
- if (currentEditor) {
49
- return `${currentEditor.cursor.line + 1}`;
50
- }
51
- return undefined;
52
- }
53
- });
54
- }
55
-
56
- protected getCurrentEditor(): TextEditor | undefined {
57
- const currentEditor = this.editorManager.currentEditor;
58
- if (currentEditor) {
59
- return currentEditor.editor;
60
- }
61
- return undefined;
62
- }
63
- }
64
- ```
65
-
66
- Note that a Variable is resolved to `MaybePromise<string | undefined>` which means that it can be resolved synchronously or within a Promise.
67
-
68
- ### Using the Variable Resolver Service
69
-
70
- There's the example of how one can use Variable Resolver Service in its own plugin:
71
- ```typescript
72
- @injectable()
73
- export class MyService {
74
-
75
- constructor(
76
- @inject(VariableResolverService) protected readonly variableResolver: VariableResolverService
77
- ) { }
78
-
79
- async resolve(): Promise<void> {
80
- const text = 'cursor is in file ${file} on line ${lineNumber}';
81
- const resolved = await this.variableResolver.resolve(text);
82
- console.log(resolved);
83
- }
84
- }
85
- ```
86
-
87
- If `package.json` file is currently opened and cursor is on line 5 then the following output will be logged to the console:
88
- ```
89
- cursor is in file package.json on line 5
90
- ```
91
-
92
- ## Additional Information
93
-
94
- - [API documentation for `@theia/variable-resolver`](https://eclipse-theia.github.io/theia/docs/next/modules/variable_resolver.html)
95
- - [Theia - GitHub](https://github.com/eclipse-theia/theia)
96
- - [Theia - Website](https://theia-ide.org/)
97
-
98
- ## License
99
-
100
- - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
101
- - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
102
-
103
- ## Trademark
104
- "Theia" is a trademark of the Eclipse Foundation
105
- https://www.eclipse.org/theia
106
-
107
-
108
- # Theia - Variable Resolver Extension
109
-
110
- The extension
111
-
112
- ## License
113
- - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
114
- - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
1
+ <div align='center'>
2
+
3
+ <br />
4
+
5
+ <img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
6
+
7
+ <h2>ECLIPSE THEIA - VARIABLE-RESOLVER EXTENSION</h2>
8
+
9
+ <hr />
10
+
11
+ </div>
12
+
13
+ ## Description
14
+
15
+ The `@theia/variable-resolved` extension provides variable substitution mechanism inside of strings using `${variableName}` syntax.
16
+
17
+ ### Variable Contribution Point
18
+ Extension provides a hook that allows any extensions to contribute its own variables.
19
+ Here's the example of contributing two variables:
20
+ - `${file}` - returns the name of the file opened in the current editor
21
+ - `${lineNumber}` - returns the current line number in the current file
22
+
23
+ ```typescript
24
+ @injectable()
25
+ export class EditorVariableContribution implements VariableContribution {
26
+
27
+ constructor(
28
+ @inject(EditorManager) protected readonly editorManager: EditorManager
29
+ ) { }
30
+
31
+ registerVariables(variables: VariableRegistry): void {
32
+ variables.registerVariable({
33
+ name: 'file',
34
+ description: 'The name of the file opened in the current editor',
35
+ resolve: () => {
36
+ const currentEditor = this.getCurrentEditor();
37
+ if (currentEditor) {
38
+ return currentEditor.uri.displayName;
39
+ }
40
+ return undefined;
41
+ }
42
+ });
43
+ variables.registerVariable({
44
+ name: 'lineNumber',
45
+ description: 'The current line number in the current file',
46
+ resolve: () => {
47
+ const currentEditor = this.getCurrentEditor();
48
+ if (currentEditor) {
49
+ return `${currentEditor.cursor.line + 1}`;
50
+ }
51
+ return undefined;
52
+ }
53
+ });
54
+ }
55
+
56
+ protected getCurrentEditor(): TextEditor | undefined {
57
+ const currentEditor = this.editorManager.currentEditor;
58
+ if (currentEditor) {
59
+ return currentEditor.editor;
60
+ }
61
+ return undefined;
62
+ }
63
+ }
64
+ ```
65
+
66
+ Note that a Variable is resolved to `MaybePromise<string | undefined>` which means that it can be resolved synchronously or within a Promise.
67
+
68
+ ### Using the Variable Resolver Service
69
+
70
+ There's the example of how one can use Variable Resolver Service in its own plugin:
71
+ ```typescript
72
+ @injectable()
73
+ export class MyService {
74
+
75
+ constructor(
76
+ @inject(VariableResolverService) protected readonly variableResolver: VariableResolverService
77
+ ) { }
78
+
79
+ async resolve(): Promise<void> {
80
+ const text = 'cursor is in file ${file} on line ${lineNumber}';
81
+ const resolved = await this.variableResolver.resolve(text);
82
+ console.log(resolved);
83
+ }
84
+ }
85
+ ```
86
+
87
+ If `package.json` file is currently opened and cursor is on line 5 then the following output will be logged to the console:
88
+ ```
89
+ cursor is in file package.json on line 5
90
+ ```
91
+
92
+ ## Additional Information
93
+
94
+ - [API documentation for `@theia/variable-resolver`](https://eclipse-theia.github.io/theia/docs/next/modules/variable_resolver.html)
95
+ - [Theia - GitHub](https://github.com/eclipse-theia/theia)
96
+ - [Theia - Website](https://theia-ide.org/)
97
+
98
+ ## License
99
+
100
+ - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
101
+ - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
102
+
103
+ ## Trademark
104
+ "Theia" is a trademark of the Eclipse Foundation
105
+ https://www.eclipse.org/theia
106
+
107
+
108
+ # Theia - Variable Resolver Extension
109
+
110
+ The extension
111
+
112
+ ## License
113
+ - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
114
+ - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@theia/variable-resolver",
3
- "version": "1.53.0-next.5+1e5fb536d",
3
+ "version": "1.53.0-next.55+d1a989a68c",
4
4
  "description": "Theia - Variable Resolver Extension",
5
5
  "dependencies": {
6
- "@theia/core": "1.53.0-next.5+1e5fb536d",
6
+ "@theia/core": "1.53.0-next.55+d1a989a68c",
7
7
  "tslib": "^2.6.2"
8
8
  },
9
9
  "publishConfig": {
@@ -45,10 +45,10 @@
45
45
  "watch": "theiaext watch"
46
46
  },
47
47
  "devDependencies": {
48
- "@theia/ext-scripts": "1.52.0"
48
+ "@theia/ext-scripts": "1.53.0"
49
49
  },
50
50
  "nyc": {
51
51
  "extends": "../../configs/nyc.json"
52
52
  },
53
- "gitHead": "1e5fb536d65550ad8fa0fefcec731645b2afc74a"
53
+ "gitHead": "d1a989a68c1b5ec1f9098e9126653c6346844769"
54
54
  }
@@ -1,151 +1,151 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2019 TypeFox 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 { injectable, inject, optional } from '@theia/core/shared/inversify';
18
- import { VariableContribution, VariableRegistry } from './variable';
19
- import { ApplicationServer } from '@theia/core/lib/common/application-protocol';
20
- import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
21
- import { CommandService } from '@theia/core/lib/common/command';
22
- import { OS } from '@theia/core/lib/common/os';
23
- import { PreferenceService } from '@theia/core/lib/browser/preferences/preference-service';
24
- import { ResourceContextKey } from '@theia/core/lib/browser/resource-context-key';
25
- import { VariableInput } from './variable-input';
26
- import { QuickInputService, QuickPickValue } from '@theia/core/lib/browser';
27
- import { MaybeArray, RecursivePartial } from '@theia/core/lib/common/types';
28
- import { cancelled } from '@theia/core/lib/common/cancellation';
29
- import URI from '@theia/core/lib/common/uri';
30
-
31
- @injectable()
32
- export class CommonVariableContribution implements VariableContribution {
33
-
34
- @inject(EnvVariablesServer)
35
- protected readonly env: EnvVariablesServer;
36
-
37
- @inject(CommandService)
38
- protected readonly commands: CommandService;
39
-
40
- @inject(PreferenceService)
41
- protected readonly preferences: PreferenceService;
42
-
43
- @inject(ResourceContextKey)
44
- protected readonly resourceContextKey: ResourceContextKey;
45
-
46
- @inject(QuickInputService) @optional()
47
- protected readonly quickInputService: QuickInputService;
48
-
49
- @inject(ApplicationServer)
50
- protected readonly appServer: ApplicationServer;
51
-
52
- async registerVariables(variables: VariableRegistry): Promise<void> {
53
- const execPath = await this.env.getExecPath();
54
- variables.registerVariable({
55
- name: 'execPath',
56
- resolve: () => execPath
57
- });
58
- variables.registerVariable({
59
- name: 'pathSeparator',
60
- resolve: () => OS.backend.isWindows ? '\\' : '/'
61
- });
62
- variables.registerVariable({
63
- name: 'env',
64
- resolve: async (_, envVariableName) => {
65
- const envVariable = envVariableName && await this.env.getValue(envVariableName);
66
- const envValue = envVariable && envVariable.value;
67
- return envValue || '';
68
- }
69
- });
70
- variables.registerVariable({
71
- name: 'config',
72
- resolve: (resourceUri = new URI(this.resourceContextKey.get()), preferenceName) => {
73
- if (!preferenceName) {
74
- return undefined;
75
- }
76
- return this.preferences.get(preferenceName, undefined, resourceUri && resourceUri.toString());
77
- }
78
- });
79
- variables.registerVariable({
80
- name: 'command',
81
- resolve: async (contextUri, commandId, configurationSection, commandIdVariables, configuration) => {
82
- if (commandId) {
83
- if (commandIdVariables?.[commandId]) {
84
- commandId = commandIdVariables[commandId];
85
- }
86
- const result = await this.commands.executeCommand(commandId, configuration);
87
- // eslint-disable-next-line no-null/no-null
88
- if (result === null) {
89
- throw cancelled();
90
- }
91
- return result;
92
- }
93
- }
94
- });
95
- variables.registerVariable({
96
- name: 'input',
97
- resolve: async (resourceUri = new URI(this.resourceContextKey.get()), variable, section) => {
98
- if (!variable || !section) {
99
- return undefined;
100
- }
101
- const configuration = this.preferences.get<RecursivePartial<{ inputs: MaybeArray<VariableInput> }>>(section, undefined, resourceUri && resourceUri.toString());
102
- const inputs = !!configuration && 'inputs' in configuration ? configuration.inputs : undefined;
103
- const input = Array.isArray(inputs) && inputs.find(item => !!item && item.id === variable);
104
- if (!input) {
105
- return undefined;
106
- }
107
- if (input.type === 'promptString') {
108
- if (typeof input.description !== 'string') {
109
- return undefined;
110
- }
111
- return this.quickInputService?.input({
112
- prompt: input.description,
113
- value: input.default
114
- });
115
- }
116
- if (input.type === 'pickString') {
117
- if (typeof input.description !== 'string' || !Array.isArray(input.options)) {
118
- return undefined;
119
- }
120
- const elements: Array<QuickPickValue<string>> = [];
121
- for (const option of input.options) {
122
- if (typeof option !== 'string') {
123
- return undefined;
124
- }
125
- if (option === input.default) {
126
- elements.unshift({
127
- description: 'Default',
128
- label: option,
129
- value: option
130
- });
131
- } else {
132
- elements.push({
133
- label: option,
134
- value: option
135
- });
136
- }
137
- }
138
- const selectedPick = await this.quickInputService?.showQuickPick(elements, { placeholder: input.description });
139
- return selectedPick?.value;
140
- }
141
- if (input.type === 'command') {
142
- if (typeof input.command !== 'string') {
143
- return undefined;
144
- }
145
- return this.commands.executeCommand(input.command, input.args);
146
- }
147
- return undefined;
148
- }
149
- });
150
- }
151
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2019 TypeFox 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 { injectable, inject, optional } from '@theia/core/shared/inversify';
18
+ import { VariableContribution, VariableRegistry } from './variable';
19
+ import { ApplicationServer } from '@theia/core/lib/common/application-protocol';
20
+ import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
21
+ import { CommandService } from '@theia/core/lib/common/command';
22
+ import { OS } from '@theia/core/lib/common/os';
23
+ import { PreferenceService } from '@theia/core/lib/browser/preferences/preference-service';
24
+ import { ResourceContextKey } from '@theia/core/lib/browser/resource-context-key';
25
+ import { VariableInput } from './variable-input';
26
+ import { QuickInputService, QuickPickValue } from '@theia/core/lib/browser';
27
+ import { MaybeArray, RecursivePartial } from '@theia/core/lib/common/types';
28
+ import { cancelled } from '@theia/core/lib/common/cancellation';
29
+ import URI from '@theia/core/lib/common/uri';
30
+
31
+ @injectable()
32
+ export class CommonVariableContribution implements VariableContribution {
33
+
34
+ @inject(EnvVariablesServer)
35
+ protected readonly env: EnvVariablesServer;
36
+
37
+ @inject(CommandService)
38
+ protected readonly commands: CommandService;
39
+
40
+ @inject(PreferenceService)
41
+ protected readonly preferences: PreferenceService;
42
+
43
+ @inject(ResourceContextKey)
44
+ protected readonly resourceContextKey: ResourceContextKey;
45
+
46
+ @inject(QuickInputService) @optional()
47
+ protected readonly quickInputService: QuickInputService;
48
+
49
+ @inject(ApplicationServer)
50
+ protected readonly appServer: ApplicationServer;
51
+
52
+ async registerVariables(variables: VariableRegistry): Promise<void> {
53
+ const execPath = await this.env.getExecPath();
54
+ variables.registerVariable({
55
+ name: 'execPath',
56
+ resolve: () => execPath
57
+ });
58
+ variables.registerVariable({
59
+ name: 'pathSeparator',
60
+ resolve: () => OS.backend.isWindows ? '\\' : '/'
61
+ });
62
+ variables.registerVariable({
63
+ name: 'env',
64
+ resolve: async (_, envVariableName) => {
65
+ const envVariable = envVariableName && await this.env.getValue(envVariableName);
66
+ const envValue = envVariable && envVariable.value;
67
+ return envValue || '';
68
+ }
69
+ });
70
+ variables.registerVariable({
71
+ name: 'config',
72
+ resolve: (resourceUri = new URI(this.resourceContextKey.get()), preferenceName) => {
73
+ if (!preferenceName) {
74
+ return undefined;
75
+ }
76
+ return this.preferences.get(preferenceName, undefined, resourceUri && resourceUri.toString());
77
+ }
78
+ });
79
+ variables.registerVariable({
80
+ name: 'command',
81
+ resolve: async (contextUri, commandId, configurationSection, commandIdVariables, configuration) => {
82
+ if (commandId) {
83
+ if (commandIdVariables?.[commandId]) {
84
+ commandId = commandIdVariables[commandId];
85
+ }
86
+ const result = await this.commands.executeCommand(commandId, configuration);
87
+ // eslint-disable-next-line no-null/no-null
88
+ if (result === null) {
89
+ throw cancelled();
90
+ }
91
+ return result;
92
+ }
93
+ }
94
+ });
95
+ variables.registerVariable({
96
+ name: 'input',
97
+ resolve: async (resourceUri = new URI(this.resourceContextKey.get()), variable, section) => {
98
+ if (!variable || !section) {
99
+ return undefined;
100
+ }
101
+ const configuration = this.preferences.get<RecursivePartial<{ inputs: MaybeArray<VariableInput> }>>(section, undefined, resourceUri && resourceUri.toString());
102
+ const inputs = !!configuration && 'inputs' in configuration ? configuration.inputs : undefined;
103
+ const input = Array.isArray(inputs) && inputs.find(item => !!item && item.id === variable);
104
+ if (!input) {
105
+ return undefined;
106
+ }
107
+ if (input.type === 'promptString') {
108
+ if (typeof input.description !== 'string') {
109
+ return undefined;
110
+ }
111
+ return this.quickInputService?.input({
112
+ prompt: input.description,
113
+ value: input.default
114
+ });
115
+ }
116
+ if (input.type === 'pickString') {
117
+ if (typeof input.description !== 'string' || !Array.isArray(input.options)) {
118
+ return undefined;
119
+ }
120
+ const elements: Array<QuickPickValue<string>> = [];
121
+ for (const option of input.options) {
122
+ if (typeof option !== 'string') {
123
+ return undefined;
124
+ }
125
+ if (option === input.default) {
126
+ elements.unshift({
127
+ description: 'Default',
128
+ label: option,
129
+ value: option
130
+ });
131
+ } else {
132
+ elements.push({
133
+ label: option,
134
+ value: option
135
+ });
136
+ }
137
+ }
138
+ const selectedPick = await this.quickInputService?.showQuickPick(elements, { placeholder: input.description });
139
+ return selectedPick?.value;
140
+ }
141
+ if (input.type === 'command') {
142
+ if (typeof input.command !== 'string') {
143
+ return undefined;
144
+ }
145
+ return this.commands.executeCommand(input.command, input.args);
146
+ }
147
+ return undefined;
148
+ }
149
+ });
150
+ }
151
+ }
@@ -1,19 +1,19 @@
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
- export * from './variable';
18
- export * from './variable-quick-open-service';
19
- export * from './variable-resolver-service';
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
+ export * from './variable';
18
+ export * from './variable-quick-open-service';
19
+ export * from './variable-resolver-service';