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

Move TS/JS to use organize imports codeAction instead of command #47850

Merged
merged 4 commits into from
Apr 18, 2018
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
34 changes: 0 additions & 34 deletions extensions/typescript-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"onCommand:javascript.goToProjectConfig",
"onCommand:typescript.goToProjectConfig",
"onCommand:typescript.openTsServerLog",
"onCommand:typescript.organizeImports",
"onCommand:workbench.action.tasks.runTask"
],
"main": "./out/extension",
Expand Down Expand Up @@ -464,23 +463,6 @@
"command": "typescript.restartTsServer",
"title": "%typescript.restartTsServer%",
"category": "TypeScript"
},
{
"command": "typescript.organizeImports",
"title": "%typescript.organizeImports%",
"category": "TypeScript"
},
{
"command": "javascript.organizeImports",
"title": "%typescript.organizeImports%",
"category": "JavaScript"
}
],
"keybindings": [
{
"command": "typescript.organizeImports",
"key": "shift+alt+o",
"when": "typescript.isManagedFile && typescript.canOrganizeImports"
}
],
"menus": {
Expand Down Expand Up @@ -528,22 +510,6 @@
{
"command": "typescript.restartTsServer",
"when": "typescript.isManagedFile"
},
{
"command": "typescript.organizeImports",
"when": "editorLangId == typescriptreact && typescript.isManagedFile && typescript.canOrganizeImports"
},
{
"command": "typescript.organizeImports",
"when": "editorLangId == typescript && typescript.isManagedFile && typescript.canOrganizeImports"
},
{
"command": "javascript.organizeImports",
"when": "editorLangId == javascriptreact && typescript.isManagedFile && typescript.canOrganizeImports"
},
{
"command": "javascript.organizeImports",
"when": "editorLangId == javascript && typescript.isManagedFile && typescript.canOrganizeImports"
}
]
},
Expand Down
1 change: 0 additions & 1 deletion extensions/typescript-language-features/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"typescript.autoImportSuggestions.enabled": "Enable/disable auto import suggestions. Requires TypeScript >=2.6.1",
"typescript.experimental.syntaxFolding": "Enables/disables syntax aware folding markers.",
"taskDefinition.tsconfig.description": "The tsconfig file that defines the TS build.",
"typescript.organizeImports": "Organize Imports",
"javascript.suggestionActions.enabled": "Enable/disable suggestion diagnostics for JavaScript files in the editor. Requires TypeScript >= 2.8",
"typescript.suggestionActions.enabled": "Enable/disable suggestion diagnostics for TypeScript files in the editor. Requires TypeScript >= 2.8."
}
7 changes: 0 additions & 7 deletions extensions/typescript-language-features/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import ManagedFileContextManager from './utils/managedFileContext';
import { lazy, Lazy } from './utils/lazy';
import * as fileSchemes from './utils/fileSchemes';
import LogDirectoryProvider from './utils/logDirectoryProvider';
import { OrganizeImportsCommand, OrganizeImportsContextManager } from './features/organizeImports';

export function activate(
context: vscode.ExtensionContext
Expand Down Expand Up @@ -74,11 +73,6 @@ function createLazyClientHost(

context.subscriptions.push(clientHost);

const organizeImportsContext = new OrganizeImportsContextManager();
clientHost.serviceClient.onTsServerStarted(api => {
organizeImportsContext.onDidChangeApiVersion(api);
}, null, context.subscriptions);

clientHost.serviceClient.onReady(() => {
context.subscriptions.push(
ProjectStatus.create(
Expand All @@ -103,7 +97,6 @@ function registerCommands(
commandManager.register(new commands.RestartTsServerCommand(lazyClientHost));
commandManager.register(new commands.TypeScriptGoToProjectConfigCommand(lazyClientHost));
commandManager.register(new commands.JavaScriptGoToProjectConfigCommand(lazyClientHost));
commandManager.register(new OrganizeImportsCommand(lazyClientHost));
}

function isSupportedDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,27 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';

import * as nls from 'vscode-nls';
import * as Proto from '../protocol';
import { Command } from '../utils/commandManager';
import { ITypeScriptServiceClient } from '../typescriptService';
import { Command, CommandManager } from '../utils/commandManager';
import { isSupportedLanguageMode } from '../utils/languageModeIds';
import * as typeconverts from '../utils/typeConverters';

import { isSupportedLanguageMode } from '../utils/languageModeIds';
import API from '../utils/api';
import { Lazy } from '../utils/lazy';
import TypeScriptServiceClientHost from '../typeScriptServiceClientHost';
import { ITypeScriptServiceClient } from '../typescriptService';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();


export class OrganizeImportsCommand implements Command {
public static readonly Ids = ['javascript.organizeImports', 'typescript.organizeImports'];
class OrganizeImportsCommand implements Command {
public static readonly Id = '_typescript.organizeImports';

public readonly id = OrganizeImportsCommand.Ids;
public readonly id = OrganizeImportsCommand.Id;

constructor(
private readonly lazyClientHost: Lazy<TypeScriptServiceClientHost>
private readonly client: ITypeScriptServiceClient
) { }

public async execute(): Promise<boolean> {
// Don't force activation
if (!this.lazyClientHost.hasValue) {
return false;
}

const client = this.lazyClientHost.value.serviceClient;
if (!client.apiVersion.has280Features()) {
if (!this.client.apiVersion.has280Features()) {
return false;
}

Expand All @@ -43,7 +33,7 @@ export class OrganizeImportsCommand implements Command {
return false;
}

const file = client.normalizePath(editor.document.uri);
const file = this.client.normalizePath(editor.document.uri);
if (!file) {
return false;
}
Expand All @@ -56,67 +46,42 @@ export class OrganizeImportsCommand implements Command {
}
}
};
const response = await client.execute('organizeImports', args);
const response = await this.client.execute('organizeImports', args);
if (!response || !response.success) {
return false;
}

const edits = typeconverts.WorkspaceEdit.fromFromFileCodeEdits(client, response.body);
const edits = typeconverts.WorkspaceEdit.fromFromFileCodeEdits(this.client, response.body);
return await vscode.workspace.applyEdit(edits);
}
}

/**
* When clause context set when the ts version supports organize imports.
*/
const contextName = 'typescript.canOrganizeImports';

export class OrganizeImportsContextManager {

private currentValue: boolean = false;

public onDidChangeApiVersion(apiVersion: API): any {
this.updateContext(apiVersion.has280Features());
}

private updateContext(newValue: boolean) {
if (newValue === this.currentValue) {
return;
}

vscode.commands.executeCommand('setContext', contextName, newValue);
this.currentValue = newValue;
}
}


export class OrganizeImportsCodeActionProvider implements vscode.CodeActionProvider {
private static readonly organizeImportsKind = vscode.CodeActionKind.Source.append('organizeImports');

public constructor(
private readonly client: ITypeScriptServiceClient
) { }
private readonly client: ITypeScriptServiceClient,
commandManager: CommandManager
) {
commandManager.register(new OrganizeImportsCommand(client));
}

public readonly metadata: vscode.CodeActionProviderMetadata = {
providedCodeActionKinds: [OrganizeImportsCodeActionProvider.organizeImportsKind]
providedCodeActionKinds: [vscode.CodeActionKind.SourceOrganizeImports]
};

public provideCodeActions(
document: vscode.TextDocument,
_document: vscode.TextDocument,
_range: vscode.Range,
_context: vscode.CodeActionContext,
_token: vscode.CancellationToken
): vscode.CodeAction[] {
if (!isSupportedLanguageMode(document)) {
return [];
}

if (!this.client.apiVersion.has280Features()) {
return [];
}

const action = new vscode.CodeAction(localize('oraganizeImportsAction.title', "Organize Imports"), OrganizeImportsCodeActionProvider.organizeImportsKind);
action.command = { title: '', command: OrganizeImportsCommand.Ids[0] };
const action = new vscode.CodeAction(
localize('oraganizeImportsAction.title', "Organize Imports"),
vscode.CodeActionKind.SourceOrganizeImports);
action.command = { title: '', command: OrganizeImportsCommand.Id };
return [action];
}
}
10 changes: 6 additions & 4 deletions extensions/typescript-language-features/src/languageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class LanguageProvider {
constructor(
private readonly client: TypeScriptServiceClient,
private readonly description: LanguageDescription,
commandManager: CommandManager,
private readonly commandManager: CommandManager,
typingsStatus: TypingsStatus
) {
this.formattingOptionsManager = new FormattingConfigurationManager(client);
Expand Down Expand Up @@ -123,9 +123,6 @@ export default class LanguageProvider {
const refactorProvider = new (await import('./features/refactorProvider')).default(client, this.formattingOptionsManager, commandManager);
this.disposables.push(languages.registerCodeActionsProvider(selector, refactorProvider, refactorProvider.metadata));

const organizeImportsProvider = new (await import('./features/organizeImports')).OrganizeImportsCodeActionProvider(client);
this.disposables.push(languages.registerCodeActionsProvider(selector, organizeImportsProvider, organizeImportsProvider.metadata));

await this.initFoldingProvider();
this.disposables.push(workspace.onDidChangeConfiguration(c => {
if (c.affectsConfiguration(foldingSetting)) {
Expand Down Expand Up @@ -247,6 +244,11 @@ export default class LanguageProvider {
if (this.client.apiVersion.has213Features()) {
this.versionDependentDisposables.push(languages.registerTypeDefinitionProvider(selector, new (await import('./features/typeDefinitionProvider')).default(this.client)));
}

if (this.client.apiVersion.has280Features()) {
const organizeImportsProvider = new (await import('./features/organizeImports')).OrganizeImportsCodeActionProvider(this.client, this.commandManager);
this.versionDependentDisposables.push(languages.registerCodeActionsProvider(selector, organizeImportsProvider, organizeImportsProvider.metadata));
}
}

public triggerAllDiagnostics(): void {
Expand Down
45 changes: 41 additions & 4 deletions src/vs/editor/contrib/codeAction/codeActionCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ import { IFileService } from 'vs/platform/files/common/files';
import { optional } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { CodeActionModel, CodeActionsComputeEvent, HAS_REFACTOR_PROVIDER, HAS_SOURCE_ACTION_PROVIDER } from './codeActionModel';
import { CodeActionModel, CodeActionsComputeEvent, SUPPORTED_CODE_ACTIONS } from './codeActionModel';
import { CodeActionAutoApply, CodeActionFilter, CodeActionKind } from './codeActionTrigger';
import { CodeActionContextMenu } from './codeActionWidget';
import { LightBulbWidget } from './lightBulbWidget';
import { escapeRegExpCharacters } from 'vs/base/common/strings';

function contextKeyForSupportedActions(kind: CodeActionKind) {
return ContextKeyExpr.regex(
SUPPORTED_CODE_ACTIONS.keys()[0],
new RegExp('(\\s|^)' + escapeRegExpCharacters(kind.value) + '\\b'));
}

export class QuickFixController implements IEditorContribution {

Expand Down Expand Up @@ -247,7 +254,9 @@ export class RefactorAction extends EditorAction {
menuOpts: {
group: '1_modification',
order: 2,
when: ContextKeyExpr.and(EditorContextKeys.writable, HAS_REFACTOR_PROVIDER),
when: ContextKeyExpr.and(
EditorContextKeys.writable,
contextKeyForSupportedActions(CodeActionKind.Refactor)),
}
});
}
Expand All @@ -274,8 +283,9 @@ export class SourceAction extends EditorAction {
menuOpts: {
group: '1_modification',
order: 2.1,
when: ContextKeyExpr.and(EditorContextKeys.writable, HAS_SOURCE_ACTION_PROVIDER),

when: ContextKeyExpr.and(
EditorContextKeys.writable,
contextKeyForSupportedActions(CodeActionKind.Source)),
}
});
}
Expand All @@ -286,4 +296,31 @@ export class SourceAction extends EditorAction {
{ kind: CodeActionKind.Source, includeSourceActions: true },
CodeActionAutoApply.Never);
}
}

export class OrganizeImportsAction extends EditorAction {

static readonly Id = 'editor.action.organizeImports';

constructor() {
super({
id: OrganizeImportsAction.Id,
label: nls.localize('organizeImports.label', "Organize Imports"),
alias: 'Organize Imports',
precondition: ContextKeyExpr.and(
EditorContextKeys.writable,
contextKeyForSupportedActions(CodeActionKind.SourceOrganizeImports)),
kbOpts: {
kbExpr: EditorContextKeys.editorTextFocus,
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_O
}
});
}

public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
return showCodeActionsForEditorSelection(editor,
nls.localize('editor.action.organize.noneMessage', "No organize imports action available"),
{ kind: CodeActionKind.SourceOrganizeImports, includeSourceActions: true },
CodeActionAutoApply.IfSingle);
}
}
3 changes: 2 additions & 1 deletion src/vs/editor/contrib/codeAction/codeActionContributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
*--------------------------------------------------------------------------------------------*/

import { registerEditorAction, registerEditorCommand, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { SourceAction, QuickFixController, QuickFixAction, CodeActionCommand, RefactorAction } from 'vs/editor/contrib/codeAction/codeActionCommands';
import { SourceAction, QuickFixController, QuickFixAction, CodeActionCommand, RefactorAction, OrganizeImportsAction } from 'vs/editor/contrib/codeAction/codeActionCommands';


registerEditorContribution(QuickFixController);
registerEditorAction(QuickFixAction);
registerEditorAction(RefactorAction);
registerEditorAction(SourceAction);
registerEditorAction(OrganizeImportsAction);
registerEditorCommand(new CodeActionCommand());
Loading