|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -import { commands, window, ExtensionContext } from 'vscode'; |
| 3 | +import { commands, window, ExtensionContext, Range, ViewColumn, Uri, Disposable} from 'vscode'; |
4 | 4 | import { CodeActionParams, LanguageClient } from 'vscode-languageclient';
|
5 | 5 | import { Commands } from './commands';
|
6 | 6 | import { applyWorkspaceEdit } from './extension';
|
7 |
| -import { ListOverridableMethodsRequest, AddOverridableMethodsRequest, CheckHashCodeEqualsStatusRequest, GenerateHashCodeEqualsRequest } from './protocol'; |
| 7 | +import { ListOverridableMethodsRequest, AddOverridableMethodsRequest, CheckHashCodeEqualsStatusRequest, GenerateHashCodeEqualsRequest, |
| 8 | +OrganizeImportsRequest, ImportCandidate, ImportSelection } from './protocol'; |
8 | 9 |
|
9 | 10 | export function registerCommands(languageClient: LanguageClient, context: ExtensionContext) {
|
10 | 11 | registerOverrideMethodsCommand(languageClient, context);
|
11 | 12 | registerHashCodeEqualsCommand(languageClient, context);
|
| 13 | + registerOrganizeImportsCommand(languageClient, context); |
| 14 | + registerChooseImportCommand(context); |
12 | 15 | }
|
13 | 16 |
|
14 | 17 | function registerOverrideMethodsCommand(languageClient: LanguageClient, context: ExtensionContext): void {
|
@@ -100,3 +103,58 @@ function registerHashCodeEqualsCommand(languageClient: LanguageClient, context:
|
100 | 103 | applyWorkspaceEdit(workspaceEdit, languageClient);
|
101 | 104 | }));
|
102 | 105 | }
|
| 106 | + |
| 107 | +function registerOrganizeImportsCommand(languageClient: LanguageClient, context: ExtensionContext): void { |
| 108 | + context.subscriptions.push(commands.registerCommand(Commands.ORGANIZE_IMPORTS, async (params: CodeActionParams) => { |
| 109 | + const workspaceEdit = await languageClient.sendRequest(OrganizeImportsRequest.type, params); |
| 110 | + applyWorkspaceEdit(workspaceEdit, languageClient); |
| 111 | + })); |
| 112 | +} |
| 113 | + |
| 114 | +function registerChooseImportCommand(context: ExtensionContext): void { |
| 115 | + context.subscriptions.push(commands.registerCommand(Commands.CHOOSE_IMPORTS, async (uri: string, selections: ImportSelection[]) => { |
| 116 | + const chosen: ImportCandidate[] = []; |
| 117 | + const fileUri: Uri = Uri.parse(uri); |
| 118 | + for (let i = 0; i < selections.length; i++) { |
| 119 | + const selection : ImportSelection = selections[i]; |
| 120 | + // Move the cursor to the code line with ambiguous import choices. |
| 121 | + await window.showTextDocument(fileUri, { preserveFocus: true, selection: selection.range, viewColumn: ViewColumn.One }); |
| 122 | + const candidates: ImportCandidate[] = selection.candidates; |
| 123 | + const items = candidates.map((item) => { |
| 124 | + return { |
| 125 | + label: item.fullyQualifiedName, |
| 126 | + origin: item |
| 127 | + }; |
| 128 | + }); |
| 129 | + |
| 130 | + const fullyQualifiedName = candidates[0].fullyQualifiedName; |
| 131 | + const typeName = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(".") + 1); |
| 132 | + const disposables: Disposable[] = []; |
| 133 | + try { |
| 134 | + const pick = await new Promise<any>((resolve, reject) => { |
| 135 | + const input = window.createQuickPick(); |
| 136 | + input.title = "Organize Imports"; |
| 137 | + input.step = i + 1; |
| 138 | + input.totalSteps = selections.length; |
| 139 | + input.placeholder = `Choose type '${typeName}' to import`; |
| 140 | + input.items = items; |
| 141 | + disposables.push( |
| 142 | + input.onDidChangeSelection(items => resolve(items[0])), |
| 143 | + input.onDidHide(() => { |
| 144 | + reject(undefined); |
| 145 | + }), |
| 146 | + input |
| 147 | + ); |
| 148 | + input.show(); |
| 149 | + }); |
| 150 | + chosen.push(pick ? pick.origin : null); |
| 151 | + } catch (err) { |
| 152 | + break; |
| 153 | + } finally { |
| 154 | + disposables.forEach(d => d.dispose()); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return chosen; |
| 159 | + })); |
| 160 | +} |
0 commit comments