0.6.3 • Published 4 years ago

@devpodio/variable-resolver v0.6.3

Weekly downloads
-
License
EPL-2.0 OR GPL-2....
Repository
github
Last release
4 years ago

Theia - Variable Resolver Extension

The extension provides variable substitution mechanism inside of strings using ${variableName} syntax.

Variable Contribution Point

Extension provides a hook that allows any extensions to contribute its own variables. Here's the example of contributing two variables:

  • ${file} - returns the name of the file opened in the current editor
  • ${lineNumber} - returns the current line number in the current file
@injectable()
export class EditorVariableContribution implements VariableContribution {

    constructor(
        @inject(EditorManager) protected readonly editorManager: EditorManager
    ) { }

    registerVariables(variables: VariableRegistry): void {
        variables.registerVariable({
            name: 'file',
            description: 'The name of the file opened in the current editor',
            resolve: () => {
                const currentEditor = this.getCurrentEditor();
                if (currentEditor) {
                    return currentEditor.uri.displayName;
                }
                return undefined;
            }
        });
        variables.registerVariable({
            name: 'lineNumber',
            description: 'The current line number in the current file',
            resolve: () => {
                const currentEditor = this.getCurrentEditor();
                if (currentEditor) {
                    return `${currentEditor.cursor.line + 1}`;
                }
                return undefined;
            }
        });
    }

    protected getCurrentEditor(): TextEditor | undefined {
        const currentEditor = this.editorManager.currentEditor;
        if (currentEditor) {
            return currentEditor.editor;
        }
        return undefined;
    }
}

Note that a Variable is resolved to MaybePromise<string | undefined> which means that it can be resolved synchronously or within a Promise.

Using the Variable Resolver Service

There's the example of how one can use Variable Resolver Service in its own plugin:

@injectable()
export class MyService {

    constructor(
        @inject(VariableResolverService) protected readonly variableResolver: VariableResolverService
    ) { }

    async resolve(): Promise<void> {
        const text = 'cursor is in file ${file} on line ${lineNumber}';
        const resolved = await this.variableResolver.resolve(text);
        console.log(resolved);
    }
}

If package.json file is currently opened and cursor is on line 5 then the following output will be logged to the console:

cursor is in file package.json on line 5

License

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.3

4 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0-latest.4

5 years ago

0.4.0-latest.3

5 years ago

0.4.0-latest.2

5 years ago

0.4.0-latest.1

5 years ago

0.3.20

5 years ago

0.3.19

5 years ago

0.3.18

5 years ago

0.3.17

5 years ago

0.3.16

5 years ago