# @devpodio/variable-resolver

> Theia - Variable Resolver Extension

Latest version **0.6.3** (published 2020-02-14) · EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities; high maintenance score.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.6.3 |
| Published | 2020-02-14 |
| First published | 2018-11-17 |
| Weekly downloads | 0 |
| License | EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 109.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | devpodnpm, unibtc |
| Keywords | theia-extension |

## Links

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

## Dependencies (1)

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

## Recent versions

- 0.6.3 (latest) — 2020-02-14
- 0.6.2 — 2020-02-14
- 0.6.1 — 2020-02-13
- 0.6.0 — 2020-02-13
- 0.5.3 — 2019-11-19
- 0.5.2 — 2019-04-22
- 0.5.1 — 2019-04-20
- 0.5.0 — 2019-04-02
- 0.4.4 — 2019-03-26
- 0.4.3 — 2019-03-26
- 0.4.2 — 2019-03-22
- 0.4.1 — 2019-03-22
- 0.4.0-latest.4 — 2019-03-21
- 0.4.0-latest.3 — 2019-03-19
- 0.4.0-latest.2 — 2019-03-19
- … 6 more at https://npm.io/package/@devpodio/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
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)

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