Skip to content
Merged
Show file tree
Hide file tree
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 @@ -8,7 +8,7 @@ import {
VersionedTextDocumentIdentifier,
WorkspaceEdit
} from 'vscode-languageserver';
import { Document, mapRangeToOriginal, isRangeInTag } from '../../../lib/documents';
import { Document, mapRangeToOriginal, isRangeInTag, isInTag } from '../../../lib/documents';
import { pathToUrl, flatten } from '../../../utils';
import { CodeActionsProvider } from '../../interfaces';
import { SnapshotFragment, SvelteSnapshotFragment } from '../DocumentSnapshot';
Expand Down Expand Up @@ -144,7 +144,9 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
document,
doc,
edit,
true
true,
isInTag(range.start, document.scriptInfo) ||
isInTag(range.start, document.moduleScriptInfo)
);
}
return TextEdit.replace(
Expand Down Expand Up @@ -214,10 +216,10 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
...refactor,
title: refactor.title
.replace(
'Extract to inner function in function \'render\'',
"Extract to inner function in function 'render'",
'Extract to function'
)
.replace('Extract to constant in function \'render\'', 'Extract to constant')
.replace("Extract to constant in function 'render'", 'Extract to constant')
}))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,14 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
for (const action of actions) {
for (const change of action.changes) {
edit.push(
...this.codeActionChangesToTextEdit(document, fragment, change, isImport)
...this.codeActionChangesToTextEdit(
document,
fragment,
change,
isImport,
isInTag(comp.position, document.scriptInfo) ||
isInTag(comp.position, document.moduleScriptInfo)
)
);
}
}
Expand Down Expand Up @@ -342,20 +349,28 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
doc: Document,
fragment: SvelteSnapshotFragment,
changes: ts.FileTextChanges,
isImport: boolean
isImport: boolean,
actionTriggeredInScript: boolean
): TextEdit[] {
return changes.textChanges.map((change) =>
this.codeActionChangeToTextEdit(doc, fragment, change, isImport)
this.codeActionChangeToTextEdit(
doc,
fragment,
change,
isImport,
actionTriggeredInScript
)
);
}

codeActionChangeToTextEdit(
doc: Document,
fragment: SvelteSnapshotFragment,
change: ts.TextChange,
isImport: boolean
isImport: boolean,
actionTriggeredInScript: boolean
): TextEdit {
change.newText = this.changeSvelteComponentImport(change.newText);
change.newText = this.changeComponentImport(change.newText, actionTriggeredInScript);

const scriptTagInfo = fragment.scriptInfo;
if (!scriptTagInfo) {
Expand Down Expand Up @@ -427,10 +442,11 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
return name.replace(/(\w+)__SvelteComponent_/, '$1');
}

private changeSvelteComponentImport(importText: string) {
private changeComponentImport(importText: string, actionTriggeredInScript: boolean) {
const changedName = this.changeSvelteComponentName(importText);
if (importText !== changedName) {
// For some reason, TS sometimes adds the `type` modifier. Remove it.
if (importText !== changedName || !actionTriggeredInScript) {
// For some reason, TS sometimes adds the `type` modifier. Remove it
// in case of Svelte component imports or if import triggered from markup.
return changedName.replace(' type ', ' ');
}

Expand Down