-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organize imports for js and ts (#45237)
* Organize imports for js and ts Adds a new 'Organize Imports' command for js and ts. This command is only availible on TS 2.8+. We'll hold off on merging this PR until we pick up a TS 2.8 insiders build Fixes #45108 * Add keybinding for organize imports
- Loading branch information
Showing
10 changed files
with
139 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
|
||
import * as Proto from '../protocol'; | ||
import { Command } from '../utils/commandManager'; | ||
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'; | ||
|
||
export class OrganizeImportsCommand implements Command { | ||
public static readonly ID = 'typescript.organizeImports'; | ||
public readonly id = OrganizeImportsCommand.ID; | ||
|
||
constructor( | ||
private readonly lazyClientHost: Lazy<TypeScriptServiceClientHost> | ||
) { } | ||
|
||
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()) { | ||
return false; | ||
} | ||
|
||
const editor = vscode.window.activeTextEditor; | ||
if (!editor || !isSupportedLanguageMode(editor.document)) { | ||
return false; | ||
} | ||
|
||
const file = client.normalizePath(editor.document.uri); | ||
if (!file) { | ||
return false; | ||
} | ||
|
||
const args: Proto.OrganizeImportsRequestArgs = { | ||
scope: { | ||
type: 'file', | ||
args: { | ||
file | ||
} | ||
} | ||
}; | ||
const response = await client.execute('organizeImports', args); | ||
if (!response || !response.success) { | ||
return false; | ||
} | ||
|
||
const edits = typeconverts.WorkspaceEdit.fromFromFileCodeEdits(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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters