Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 10, 2024
1 parent f2ab701 commit 05f20db
Show file tree
Hide file tree
Showing 17 changed files with 883 additions and 915 deletions.
17 changes: 15 additions & 2 deletions packages/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export * from '@volar/typescript';
export * from './lib/semantic';
export * from './lib/syntactic';
export { Provide } from './lib/plugins/semantic';

import { create as createDirectiveCommentServicePlugin } from './lib/plugins/directiveComment';
import { create as createDocCommentTemplateServicePlugin } from './lib/plugins/docCommentTemplate';
import { create as createSemanticServicePlugin } from './lib/plugins/semantic';
import { create as createSyntacticServicePlugin } from './lib/plugins/syntactic';

export function create(ts: typeof import('typescript')) {
return [
createSemanticServicePlugin(ts),
createSyntacticServicePlugin(ts),
createDocCommentTemplateServicePlugin(ts),
createDirectiveCommentServicePlugin(),
];
}
2 changes: 1 addition & 1 deletion packages/typescript/lib/configs/getUserPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path-browserify';
import type * as ts from 'typescript';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { getConfigTitle } from '../shared';
import type { SharedContext } from '../types';
import type { SharedContext } from '../plugins/semanticFeatures/types';

export async function getUserPreferences(
ctx: SharedContext,
Expand Down
85 changes: 85 additions & 0 deletions packages/typescript/lib/plugins/directiveComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import type * as vscode from '@volar/language-service';
import * as nls from 'vscode-nls';
import { isTsDocument } from '../shared';

const localize = nls.loadMessageBundle(); // TODO: not working

interface Directive {
readonly value: string;
readonly description: string;
}

const directives: Directive[] = [
{
value: '@ts-check',
description: localize(
'ts-check',
"Enables semantic checking in a JavaScript file. Must be at the top of a file.")
}, {
value: '@ts-nocheck',
description: localize(
'ts-nocheck',
"Disables semantic checking in a JavaScript file. Must be at the top of a file.")
}, {
value: '@ts-ignore',
description: localize(
'ts-ignore',
"Suppresses @ts-check errors on the next line of a file.")
}, {
value: '@ts-expect-error',
description: localize(
'ts-expect-error',
"Suppresses @ts-check errors on the next line of a file, expecting at least one to exist.")
}
];

export function create(): vscode.ServicePlugin {
return {
name: 'typescript-directive-comment',
triggerCharacters: ['@'],
create(): vscode.ServicePluginInstance {

return {

provideCompletionItems(document, position) {

if (!isTsDocument(document))
return;

const prefix = document.getText({
start: { line: position.line, character: 0 },
end: position,
});
const match = prefix.match(/^\s*\/\/+\s?(@[a-zA-Z\-]*)?$/);
if (match) {

const items = directives.map(directive => {

const item: vscode.CompletionItem = { label: directive.value };
item.insertTextFormat = 2 satisfies typeof vscode.InsertTextFormat.Snippet;
item.detail = directive.description;
const range: vscode.Range = {
start: {
line: position.line,
character: Math.max(0, position.character - (match[1] ? match[1].length : 0)),
},
end: position,
};
item.textEdit = {
range,
newText: directive.value,
};

return item;
});

return {
isIncomplete: false,
items,
};
}
},
};
},
};
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
import type * as vscode from '@volar/language-service';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import * as nls from 'vscode-nls';
import type { SharedContext } from '../../../types';
import { getLineText } from './resolve';
import { isTsDocument } from '../shared';
import { getLanguageService } from './syntacticLanguageService';
import { getLineText } from '../utils/lspConverters';

const localize = nls.loadMessageBundle(); // TODO: not working

const defaultJsDoc = `/**\n * $0\n */`;

export function register(ctx: SharedContext) {
return (document: TextDocument, position: vscode.Position) => {
if (!isPotentiallyValidDocCompletionPosition(document, position))
return;
export function create(ts: typeof import('typescript')): vscode.ServicePlugin {
return {
name: 'typescript-doc-comment-template',
triggerCharacters: ['*'],
create(): vscode.ServicePluginInstance {

const fileName = ctx.uriToFileName(document.uri);
const offset = document.offsetAt(position);
return {

const docCommentTemplate = ctx.languageService.getDocCommentTemplateAtPosition(fileName, offset);
if (!docCommentTemplate)
return;
provideCompletionItems(document, position) {

let insertText: string;
if (!isTsDocument(document))
return;

// Workaround for #43619
// docCommentTemplate previously returned undefined for empty jsdoc templates.
// TS 2.7 now returns a single line doc comment, which breaks indentation.
if (docCommentTemplate.newText === '/** */') {
insertText = defaultJsDoc;
} else {
insertText = templateToSnippet(docCommentTemplate.newText);
}
if (!isPotentiallyValidDocCompletionPosition(document, position))
return;

const { languageService, fileName } = getLanguageService(ts, document);
const offset = document.offsetAt(position);
const docCommentTemplate = languageService.getDocCommentTemplateAtPosition(fileName, offset);
if (!docCommentTemplate)
return;

let insertText: string;

// Workaround for #43619
// docCommentTemplate previously returned undefined for empty jsdoc templates.
// TS 2.7 now returns a single line doc comment, which breaks indentation.
if (docCommentTemplate.newText === '/** */') {
insertText = defaultJsDoc;
} else {
insertText = templateToSnippet(docCommentTemplate.newText);
}

const item = createCompletionItem(document, position, insertText);
const item = createCompletionItem(document, position, insertText);

return item;
return {
isIncomplete: false,
items: [item],
};
},
};
},
};
}

Expand Down
Loading

0 comments on commit 05f20db

Please sign in to comment.