Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refactor) simplify completion provider #293

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
mapCompletionItemToOriginal,
mapRangeToOriginal,
} from '../../../lib/documents';
import { isNotNullOrUndefined, pathToUrl } from '../../../utils';
import { pathToUrl } from '../../../utils';
import { AppCompletionItem, AppCompletionList, CompletionsProvider } from '../../interfaces';
import { SvelteSnapshotFragment } from '../DocumentSnapshot';
import { LSAndTSDocResolver } from '../LSAndTSDocResolver';
Expand Down Expand Up @@ -92,27 +92,18 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
}

const completionItems = completions.entries
.map((comp) =>
this.toCompletionItem(fragment, comp, pathToUrl(tsDoc.filePath), position),
)
.filter(isNotNullOrUndefined)
.map((comp) => this.toCompletionItem(comp, pathToUrl(tsDoc.filePath), position))
.map((comp) => mapCompletionItemToOriginal(fragment, comp));

return CompletionList.create(completionItems, !!tsDoc.parserError);
}

private toCompletionItem(
fragment: SvelteSnapshotFragment,
comp: ts.CompletionEntry,
uri: string,
position: Position,
): AppCompletionItem<CompletionEntryWithIdentifer> | null {
const result = this.getCompletionLabelAndInsert(fragment, comp);
if (!result) {
return null;
}

const { label, insertText, isSvelteComp } = result;
): AppCompletionItem<CompletionEntryWithIdentifer> {
const { label, insertText, isSvelteComp } = this.getCompletionLabelAndInsert(comp);

return {
label,
Expand All @@ -131,22 +122,11 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
};
}

private getCompletionLabelAndInsert(
fragment: SvelteSnapshotFragment,
comp: ts.CompletionEntry,
) {
let { kind, kindModifiers, name, source } = comp;
private getCompletionLabelAndInsert(comp: ts.CompletionEntry) {
const { kind, kindModifiers, name, source } = comp;
const isScriptElement = kind === ts.ScriptElementKind.scriptElement;
const hasModifier = Boolean(comp.kindModifiers);

const isSvelteComp = this.isSvelteComponentImport(`import ${name} from ${source}`);
if (isSvelteComp) {
name = this.changeSvelteComponentName(name);

if (this.isExistingSvelteComponentImport(fragment, name, source)) {
return null;
}
}

if (isScriptElement && hasModifier) {
return {
Expand All @@ -155,28 +135,13 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
isSvelteComp,
};
}
if (isSvelteComp && kind === ts.ScriptElementKind.classElement) {
return {
insertText: name,
label: name,
isSvelteComp,
};
}

return {
label: name,
isSvelteComp,
};
}

private isExistingSvelteComponentImport(
fragment: SvelteSnapshotFragment,
name: string,
source?: string,
): boolean {
const importStatement = new RegExp(`import ${name} from ["'\`][\\s\\S]+\\.svelte["'\`]`);
return !!source && !!fragment.text.match(importStatement);
}

async resolveCompletion(
document: Document,
completionItem: AppCompletionItem<CompletionEntryWithIdentifer>,
Expand Down Expand Up @@ -260,10 +225,6 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
fragment: SvelteSnapshotFragment,
change: ts.TextChange,
): TextEdit {
if (this.isSvelteComponentImport(change.newText)) {
change.newText = this.changeSvelteComponentImportName(change.newText);
}

const scriptTagInfo = fragment.scriptInfo;
if (!scriptTagInfo) {
// no script tag defined yet, add it.
Expand Down Expand Up @@ -299,19 +260,6 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
private isSvelteComponentImport(text: string) {
return /import \w+ from [\s\S]*.svelte($|"|'| )/.test(text);
}

private changeSvelteComponentImportName(text: string) {
return text.replace(
/import (\w+) from /,
(_, componentMatch) => `import ${this.changeSvelteComponentName(componentMatch)} from `,
);
}

private changeSvelteComponentName(name: string) {
const newName = name.replace(/(\w+)Svelte$/, '$1');
// make sure first letter is uppercase
return newName[0].toUpperCase() + newName.substr(1);
}
}

const beginOfDocumentRange = Range.create(Position.create(0, 0), Position.create(0, 0));