Skip to content

Commit

Permalink
feat: support auto closing tags for JSX
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Oct 22, 2022
1 parent b0aa1db commit 6d5264f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions plugins/typescript/src/index.ts
Expand Up @@ -4,6 +4,7 @@ import * as semver from 'semver';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as shared from '@volar/shared';

export { getSemanticTokenLegend } from './createLanguageService';

Expand Down Expand Up @@ -48,6 +49,21 @@ export default function (): LanguageServicePlugin {
);
},

doAutoInsert(document, position, ctx) {
if (
(document.languageId === 'javascriptreact' || document.languageId === 'typescriptreact')
&& ctx.lastChange.text.endsWith('>')
) {
const configName = document.languageId === 'javascriptreact' ? 'javascript.autoClosingTags' : 'typescript.autoClosingTags';
const config = context.env.configurationHost?.getConfiguration<boolean>(configName) ?? true;
if (config) {
const tsLs = context.typescript.languageService;
const close = tsLs.getJsxClosingTagAtPosition(shared.getPathOfUri(document.uri), document.offsetAt(position));
return close?.newText;
}
}
},

complete: {

triggerCharacters: [
Expand Down

0 comments on commit 6d5264f

Please sign in to comment.