Framework for decorating a TypeScript language service with additional support for languages embedded inside of template strings
Перейти к файлу
Matt Bierner aec707e70d
Merge pull request #26 from microsoft/dependabot/npm_and_yarn/minimatch-and-mocha-3.1.2
Bump minimatch and mocha
2022-12-16 14:01:09 -08:00
.github/workflows setup ci 2022-04-20 10:55:49 +08:00
.vscode Update `getSupportedCodeFixes` to support TS 5.0+. See https://github.com/microsoft/TypeScript/pull/51769 for details. 2022-12-16 11:56:57 -08:00
e2e Merge pull request #24 from microsoft/dev/mjbvz/ts-5 2022-12-16 12:39:52 -08:00
src Update `getSupportedCodeFixes` to support TS 5.0+. See https://github.com/microsoft/TypeScript/pull/51769 for details. 2022-12-16 11:56:57 -08:00
.editorconfig Fork from /typescript-styled-plugin 2017-10-09 20:06:51 -07:00
.eslintrc.js tslint -> eslint 2022-04-19 14:40:50 -07:00
.gitignore Make tests run on updated versions of TS 2018-06-06 11:54:13 -07:00
.travis.yml Hook up linting for travis 2018-07-02 16:16:28 -07:00
CHANGELOG.md Update `getSupportedCodeFixes` to support TS 5.0+. See https://github.com/microsoft/TypeScript/pull/51769 for details. 2022-12-16 11:56:57 -08:00
CODE_OF_CONDUCT.md Fork from /typescript-styled-plugin 2017-10-09 20:06:51 -07:00
LICENSE.txt Fork from /typescript-styled-plugin 2017-10-09 20:06:51 -07:00
README.md Other small typos. 2020-10-07 20:13:55 -07:00
SECURITY.md Microsoft mandatory file 2022-08-17 14:53:33 +00:00
package-lock.json Bump minimatch and mocha 2022-12-16 20:40:33 +00:00
package.json Bump minimatch and mocha 2022-12-16 20:40:33 +00:00
tsconfig.json Target es2016 2018-11-16 15:22:07 -08:00
tslint.json Use newer versions of mocha and tslint 2021-03-02 18:06:32 +00:00

README.md

TypeScript Template Language Service Decorator

Framework for decorating a TypeScript language service with additional support for languages embedded inside of template strings.

Build Status

Usage

This framework helps you to extend TypeScript's editor support for languages embedded inside of template strings. It hides most of the details of dealing with template strings so that you only have to worry about working with the template string contents themselves.

Support for embedded template languages is implemented using the TemplateLanguageService interface. Here's a simple TemplateLanguageService that adds completions that repeat the prior characters in a template string

import { TemplateLanguageService, TemplateContext } from 'typescript-template-language-service-decorator';

class EchoTemplateLanguageService implements TemplateLanguageService {
    getCompletionsAtPosition(
        context: TemplateContext,
        position: ts.LineAndCharacter
    ): ts.CompletionInfo {
        const line = context.text.split(/\n/g)[position.line];
        return {
            isGlobalCompletion: false,
            isMemberCompletion: false,
            isNewIdentifierLocation: false,
            entries: [
                {
                    name: line.slice(0, position.character),
                    kind: '',
                    kindModifiers: 'echo',
                    sortText: 'echo'
                }
            ]
        };
    }
}

The TemplateLanguageService operates on the contents of template nodes. context.text for example returns the text content of the template string, and the position passed to getCompletionsAtPosition is relative to the template string body.

The decorateWithTemplateLanguageService method takes a existing TypeScript language service and decorates it with a TemplateLanguageService. Here's how you would use this method to create a simple TypeScript server plugin for the EchoTemplateLanguageService

import * as ts from 'typescript/lib/tsserverlibrary';
import { decorateWithTemplateLanguageService } from 'typescript-template-language-service-decorator';

export = (mod: { typescript: typeof ts }) => {
    return {
        create(info: ts.server.PluginCreateInfo): ts.LanguageService {
            return decorateWithTemplateLanguageService(
                mod.typescript,
                info.languageService,
                info.project,
                new EchoTemplateLanguageService(),
                { tags: ['echo'] });
        }
    };
};

This plugin will now add echo completions to all template strings tagged with echo.

Examples

For more advanced examples of using this library:

Contributing

To build, you'll need Git and Node.js.

First, fork the typescript-template-language-service-decorator repo and clone your fork:

git clone https://github.com/YOUR_GITHUB_ACCOUNT_NAME/typescript-template-language-service-decorator.git
cd typescript-template-language-service-decorator

Then install dev dependencies:

npm install

The plugin is written in TypeScript. The source code is in the src/ directory with the compiled JavaScript output to the lib/ directory. Kick off a build using the compile script:

npm run compile

And then run the end to end tests with the e2e script:

(cd e2e && npm install)
npm run e2e

You can submit bug fixes and features through pull requests. To get started, first checkout a new feature branch on your local repo:

git checkout -b my-awesome-new-feature-branch

Make the desired code changes, commit them, and then push the changes up to your forked repository:

git push origin my-awesome-new-feature-branch

Then submit a pull request against the Microsoft typescript-template-language-service-decorator repository.

Please also see our Code of Conduct.

Credits

Code originally forked from: https://github.com/Quramy/ts-graphql-plugin