# @wiptheia/variable-resolver

> Theia - Variable Resolver Extension

Latest version **0.3.101529329103** (published 2018-06-18) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @wiptheia/variable-resolver
pnpm add @wiptheia/variable-resolver
yarn add @wiptheia/variable-resolver
bun add @wiptheia/variable-resolver
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.101529329103 |
| Published | 2018-06-18 |
| First published | 2018-05-07 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 76.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | florentbenoit |
| Keywords | theia-extension |

## Links

- npm: https://www.npmjs.com/package/@wiptheia/variable-resolver
- Repository: https://github.com/theia-ide/theia
- Issues: https://github.com/theia-ide/theia/issues
- npm.io page: https://npm.io/package/@wiptheia/variable-resolver

## Dependencies (1)

- [@wiptheia/core](https://npm.io/package/@wiptheia/core.md) ^0.3.11

## Recent versions

- 0.3.101529329103 (latest) — 2018-06-18
- 0.3.101529316316 — 2018-06-18
- 0.3.101528895082 — 2018-06-13
- 0.3.101528878795 — 2018-06-13
- 0.3.101528708905 — 2018-06-11
- 0.3.101528377761 — 2018-06-07
- 0.3.101528290741 — 2018-06-06
- 0.3.101528281264 — 2018-06-06
- 0.3.101528129305 — 2018-06-04
- 0.3.101527835504 — 2018-06-01
- 0.3.101527832848 — 2018-06-01
- 0.3.101527753241 — 2018-05-31
- 0.3.101527588686 — 2018-05-29
- 0.3.101527586002 — 2018-05-29
- 0.3.101527002539 — 2018-05-22
- … 12 more at https://npm.io/package/@wiptheia/variable-resolver/versions

## README

# 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

```typescript
@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:
```typescript
@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
[Apache-2.0](https://github.com/theia-ide/theia/blob/master/LICENSE)

---
_Source: https://npm.io/package/@wiptheia/variable-resolver · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
