Skip to content

Commit

Permalink
(feat) better commit characters handling (#1742)
Browse files Browse the repository at this point in the history
#1737
- no commit characters in invalid state in template
- no commit characters when TS says it's `isNewIdentifierLocation` (matches VS Code behavior)
  • Loading branch information
dummdidumm committed Nov 29, 2022
1 parent 8983fa3 commit 3a4c838
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { getMarkdownDocumentation } from '../previewer';
import {
changeSvelteComponentName,
convertRange,
getCommitCharactersForScriptElement,
isInScript,
scriptElementKindToCompletionItemKind
} from '../utils';
Expand Down Expand Up @@ -72,6 +71,7 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
* Therefore, only use the characters the typescript compiler treats as valid.
*/
private readonly validTriggerCharacters = ['.', '"', "'", '`', '/', '@', '<', '#'] as const;
private commitCharacters = ['.', ',', ';', '('];
/**
* For performance reasons, try to reuse the last completion if possible.
*/
Expand Down Expand Up @@ -192,16 +192,20 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
return null;
}

let completions =
lang.getCompletionsAtPosition(
filePath,
offset,
{
...userPreferences,
triggerCharacter: validTriggerCharacter
},
formatSettings
)?.entries || [];
const response = lang.getCompletionsAtPosition(
filePath,
offset,
{
...userPreferences,
triggerCharacter: validTriggerCharacter
},
formatSettings
);
const addCommitCharacters =
// replicating VS Code behavior https://github.com/microsoft/vscode/blob/main/extensions/typescript-language-features/src/languageFeatures/completions.ts
response?.isNewIdentifierLocation !== true &&
(!tsDoc.parserError || isInScript(position, tsDoc));
let completions = response?.entries || [];

if (!completions.length) {
completions =
Expand Down Expand Up @@ -256,14 +260,16 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn

const existingImports = this.getExistingImports(document);
const wordRangeStartPosition = document.positionAt(wordRange.start);
const fileUrl = pathToUrl(tsDoc.filePath); // moved here due to perf reasons
const completionItems = completions
.filter(isValidCompletion(document, position, !!tsDoc.parserError))
.map((comp) =>
this.toCompletionItem(
tsDoc,
comp,
pathToUrl(tsDoc.filePath),
fileUrl,
position,
addCommitCharacters,
existingImports
)
)
Expand Down Expand Up @@ -414,6 +420,7 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
comp: ts.CompletionEntry,
uri: string,
position: Position,
addCommitCharacters: boolean,
existingImports: Set<string>
): AppCompletionItem<CompletionEntryWithIdentifier> | null {
const completionLabelAndInsert = this.getCompletionLabelAndInsert(snapshot, comp);
Expand All @@ -436,7 +443,7 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
label,
insertText,
kind: scriptElementKindToCompletionItemKind(comp.kind),
commitCharacters: getCommitCharactersForScriptElement(comp.kind),
commitCharacters: addCommitCharacters ? this.commitCharacters : undefined,
// Make sure svelte component takes precedence
sortText: isSvelteComp ? '-1' : comp.sortText,
preselect: isSvelteComp ? true : comp.isRecommended,
Expand Down
33 changes: 0 additions & 33 deletions packages/language-server/src/plugins/typescript/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,39 +247,6 @@ export function scriptElementKindToCompletionItemKind(
return CompletionItemKind.Property;
}

export function getCommitCharactersForScriptElement(
kind: ts.ScriptElementKind
): string[] | undefined {
const commitCharacters: string[] = [];
switch (kind) {
case ts.ScriptElementKind.memberGetAccessorElement:
case ts.ScriptElementKind.memberSetAccessorElement:
case ts.ScriptElementKind.constructSignatureElement:
case ts.ScriptElementKind.callSignatureElement:
case ts.ScriptElementKind.indexSignatureElement:
case ts.ScriptElementKind.enumElement:
case ts.ScriptElementKind.interfaceElement:
commitCharacters.push('.');
break;

case ts.ScriptElementKind.moduleElement:
case ts.ScriptElementKind.alias:
case ts.ScriptElementKind.constElement:
case ts.ScriptElementKind.letElement:
case ts.ScriptElementKind.variableElement:
case ts.ScriptElementKind.localVariableElement:
case ts.ScriptElementKind.memberVariableElement:
case ts.ScriptElementKind.classElement:
case ts.ScriptElementKind.functionElement:
case ts.ScriptElementKind.memberFunctionElement:
commitCharacters.push('.', ',');
commitCharacters.push('(');
break;
}

return commitCharacters.length === 0 ? undefined : commitCharacters;
}

export function mapSeverity(category: ts.DiagnosticCategory): DiagnosticSeverity {
switch (category) {
case ts.DiagnosticCategory.Error:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function test(useNewTransformation: boolean) {
insertText: undefined,
kind: CompletionItemKind.Method,
sortText: '11',
commitCharacters: ['.', ',', '('],
commitCharacters: ['.', ',', ';', '('],
preselect: undefined,
textEdit: undefined
});
Expand All @@ -111,7 +111,7 @@ function test(useNewTransformation: boolean) {
insertText: undefined,
kind: CompletionItemKind.Field,
sortText: '11',
commitCharacters: ['.', ',', '('],
commitCharacters: ['.', ',', ';', '('],
preselect: undefined,
textEdit: undefined
});
Expand Down Expand Up @@ -512,7 +512,7 @@ function test(useNewTransformation: boolean) {
const { documentation, detail } = await completionProvider.resolveCompletion(document, {
label: 'foo',
kind: 6,
commitCharacters: ['.', ',', '('],
commitCharacters: ['.', ',', ';', '('],
data: {
name: 'foo',
kind: ts.ScriptElementKind.alias,
Expand Down Expand Up @@ -1168,7 +1168,7 @@ function test(useNewTransformation: boolean) {
insertText: 'import { blubb } from "../definitions";',
kind: CompletionItemKind.Function,
sortText: '11',
commitCharacters: ['.', ',', '('],
commitCharacters: undefined,
preselect: undefined,
textEdit: {
newText: '{ blubb } from "../definitions";',
Expand Down Expand Up @@ -1226,7 +1226,7 @@ function test(useNewTransformation: boolean) {
insertText: '?.toString',
kind: CompletionItemKind.Method,
sortText: '11',
commitCharacters: ['.', ',', '('],
commitCharacters: ['.', ',', ';', '('],
preselect: undefined,
textEdit: {
newText: '.toString',
Expand Down Expand Up @@ -1268,7 +1268,7 @@ function test(useNewTransformation: boolean) {
sortText: '11',
preselect: undefined,
insertText: undefined,
commitCharacters: undefined,
commitCharacters: ['.', ',', ';', '('],
textEdit: {
newText: '@hi',
range: {
Expand Down

0 comments on commit 3a4c838

Please sign in to comment.