feat: add tsserver.sortImports (#322)

* docs: update yarn configuration (#311)

* feat: add tsserver.sortImports

add source.sortImports codeAction that used in editor.action.sourceAction

coc.nvim can add editor.action.sourceAction

Co-authored-by: KY64 <31939494+KY64@users.noreply.github.com>
This commit is contained in:
Heyward Fann 2021-12-21 13:56:01 +08:00 committed by GitHub
parent 2d52a842cb
commit 95321f24c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions

View file

@ -1,7 +1,7 @@
import { commands, ExtensionContext, services, workspace } from 'coc.nvim'
import TsserverService from './server'
import { AutoFixCommand, Command, ConfigurePluginCommand, FileReferencesCommand, OpenTsServerLogCommand, ReloadProjectsCommand, TypeScriptGoToProjectConfigCommand } from './server/commands'
import { OrganizeImportsCommand } from './server/organizeImports'
import { OrganizeImportsCommand, SourceImportsCommand } from './server/organizeImports'
import { PluginManager } from './utils/plugins'
interface API {
@ -25,6 +25,7 @@ export async function activate(context: ExtensionContext): Promise<API> {
registCommand(new OpenTsServerLogCommand(service))
registCommand(new TypeScriptGoToProjectConfigCommand(service))
registCommand(new OrganizeImportsCommand(service))
registCommand(new SourceImportsCommand(service))
registCommand({
id: 'tsserver.restart',
execute: (): void => {

View file

@ -19,9 +19,10 @@ export class OrganizeImportsCommand implements Command {
) {
}
private async _execute(client: TypeScriptServiceClient, document: TextDocument): Promise<WorkspaceEdit | TextEdit[] | null> {
private async _execute(client: TypeScriptServiceClient, document: TextDocument, sortOnly = false): Promise<WorkspaceEdit | TextEdit[] | null> {
let file = client.toPath(document.uri)
const args: Proto.OrganizeImportsRequestArgs = {
skipDestructiveCodeActions: sortOnly,
scope: {
type: 'file',
args: {
@ -49,7 +50,7 @@ export class OrganizeImportsCommand implements Command {
if (edit) await workspace.applyEdit(edit)
}
public async execute(document?: TextDocument): Promise<void> {
public async execute(document?: TextDocument, sortOnly = false): Promise<void> {
let client = await this.service.getClientHost()
if (!document) {
let doc = await workspace.document
@ -61,10 +62,14 @@ export class OrganizeImportsCommand implements Command {
}
document = doc.textDocument
}
await this._execute(client.serviceClient, document)
await this._execute(client.serviceClient, document, sortOnly)
}
}
export class SourceImportsCommand extends OrganizeImportsCommand {
public readonly id = 'tsserver.sortImports'
}
export class OrganizeImportsCodeActionProvider implements CodeActionProvider {
// public static readonly minVersion = API.v280
@ -91,11 +96,16 @@ export class OrganizeImportsCodeActionProvider implements CodeActionProvider {
}
await this.fileConfigManager.ensureConfigurationForDocument(document, token)
const action = CodeAction.create('Organize Imports', {
const organizeImportsAction = CodeAction.create('Organize Imports', {
title: '',
command: 'tsserver.organizeImports',
arguments: [document]
}, CodeActionKind.SourceOrganizeImports)
return [action]
const sortImportsAction = CodeAction.create('Sort Imports', {
title: '',
command: 'tsserver.sortImports',
arguments: [document, true]
}, 'source.sortImports')
return [organizeImportsAction, sortImportsAction]
}
}