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 @@ -27,6 +27,11 @@ import { changeSvelteComponentName, convertRange } from '../utils';
import { CompletionsProviderImpl } from './CompletionProvider';
import { findContainingNode, isNoTextSpanInGeneratedCode, SnapshotFragmentMap } from './utils';

/**
* TODO change this to protocol constant if it's part of the protocol
*/
export const SORT_IMPORT_CODE_ACTION_KIND = 'source.sortImports';

interface RefactorArgs {
type: 'refactor';
refactorName: string;
Expand All @@ -51,6 +56,27 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
return await this.organizeImports(document, cancellationToken);
}

if (context.only?.[0] === SORT_IMPORT_CODE_ACTION_KIND) {
return await this.organizeImports(
document,
cancellationToken,
/**skipDestructiveCodeActions */ true
);
}

// for source action command (all source.xxx)
// vscode would show different source code action kinds to choose from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I trigger this code path? And what would happen then - would VS Code show a prompt of which action I prefer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type 'Source Action' in the command palette. This is the only command I find that is easy to debug this code action in the UI.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, thanks!

if (context.only?.[0] === CodeActionKind.Source) {
return [
...(await this.organizeImports(document, cancellationToken)),
...(await this.organizeImports(
document,
cancellationToken,
/**skipDestructiveCodeActions */ true
))
];
}

if (
context.diagnostics.length &&
(!context.only || context.only.includes(CodeActionKind.QuickFix))
Expand All @@ -67,7 +93,8 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {

private async organizeImports(
document: Document,
cancellationToken: CancellationToken | undefined
cancellationToken: CancellationToken | undefined,
skipDestructiveCodeActions = false
): Promise<CodeAction[]> {
if (!document.scriptInfo && !document.moduleScriptInfo) {
return [];
Expand All @@ -92,7 +119,8 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
const changes = lang.organizeImports(
{
fileName: tsDoc.filePath,
type: 'file'
type: 'file',
skipDestructiveCodeActions
},
{
semicolons: useSemicolons
Expand Down Expand Up @@ -125,9 +153,11 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {

return [
CodeAction.create(
'Organize Imports',
skipDestructiveCodeActions ? 'Sort Imports' : 'Organize Imports',
{ documentChanges },
CodeActionKind.SourceOrganizeImports
skipDestructiveCodeActions
? SORT_IMPORT_CODE_ACTION_KIND
: CodeActionKind.SourceOrganizeImports
)
];
}
Expand Down
2 changes: 2 additions & 0 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { debounceThrottle, isNotNullOrUndefined, normalizeUri, urlToPath } from
import { FallbackWatcher } from './lib/FallbackWatcher';
import { configLoader } from './lib/documents/configLoader';
import { setIsTrusted } from './importPackage';
import { SORT_IMPORT_CODE_ACTION_KIND } from './plugins/typescript/features/CodeActionsProvider';

namespace TagCloseRequest {
export const type: RequestType<TextDocumentPositionParams, string | null, any> =
Expand Down Expand Up @@ -211,6 +212,7 @@ export function startServer(options?: LSOptions) {
codeActionKinds: [
CodeActionKind.QuickFix,
CodeActionKind.SourceOrganizeImports,
SORT_IMPORT_CODE_ACTION_KIND,
...(clientSupportApplyEditCommand ? [CodeActionKind.Refactor] : [])
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
} from 'vscode-languageserver';
import { Document, DocumentManager } from '../../../../src/lib/documents';
import { LSConfigManager } from '../../../../src/ls-config';
import { CodeActionsProviderImpl } from '../../../../src/plugins/typescript/features/CodeActionsProvider';
import {
CodeActionsProviderImpl,
SORT_IMPORT_CODE_ACTION_KIND
} from '../../../../src/plugins/typescript/features/CodeActionsProvider';
import { CompletionsProviderImpl } from '../../../../src/plugins/typescript/features/CompletionProvider';
import { LSAndTSDocResolver } from '../../../../src/plugins/typescript/LSAndTSDocResolver';
import { __resetCache } from '../../../../src/plugins/typescript/service';
Expand Down Expand Up @@ -426,6 +429,98 @@ function test(useNewTransformation: boolean) {
]);
});

it('sort imports', async () => {
const { provider, document } = setup('codeactions.svelte');

const codeActions = await provider.getCodeActions(
document,
Range.create(Position.create(1, 4), Position.create(1, 5)),
{
diagnostics: [],
only: [SORT_IMPORT_CODE_ACTION_KIND]
}
);
(<TextDocumentEdit>codeActions[0]?.edit?.documentChanges?.[0])?.edits.forEach(
(edit) => (edit.newText = harmonizeNewLines(edit.newText))
);

assert.deepStrictEqual(codeActions, [
{
edit: {
documentChanges: [
{
edits: [
{
// eslint-disable-next-line max-len
newText:
"import { A,B } from 'bla';\n" +
"import { C } from 'blubb';\n" +
"import { D } from 'd';\n",

range: {
start: {
character: 0,
line: 1
},
end: {
character: 0,
line: 2
}
}
},
{
newText: '',
range: {
start: {
character: 0,
line: 2
},
end: {
character: 0,
line: 3
}
}
},
{
newText: '',
range: {
start: {
character: 0,
line: 3
},
end: {
character: 0,
line: 4
}
}
},
{
newText: '',
range: {
start: {
character: 0,
line: 4
},
end: {
character: 0,
line: 5
}
}
}
],
textDocument: {
uri: getUri('codeactions.svelte'),
version: null
}
}
]
},
kind: SORT_IMPORT_CODE_ACTION_KIND,
title: 'Sort Imports'
}
]);
});

it('organizes imports with module script', async () => {
const { provider, document } = setup('organize-imports-with-module.svelte');

Expand Down