Skip to content

Commit

Permalink
fix(command): arguments of executeCommand, fix #51
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Aug 16, 2018
1 parent 3a3beab commit 9954eba
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
11 changes: 5 additions & 6 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Neovim} from '@chemzqm/neovim'
import { Neovim } from '@chemzqm/neovim'
import * as language from 'vscode-languageserver-protocol'
import {Disposable, Location, Position} from 'vscode-languageserver-protocol'
import {echoErr, wait, showQuickpick} from './util'
import { Disposable, Location, Position } from 'vscode-languageserver-protocol'
import { echoErr, showQuickpick, wait } from './util'
import workspace from './workspace'
const logger = require('./util/logger')('commands')

Expand All @@ -21,7 +21,7 @@ class CommandItem implements Disposable, Command {
}

public execute(...args: any[]): void | Promise<any> {
let {impl, thisArg} = this
let { impl, thisArg } = this
return impl.apply(thisArg, args || [])
}

Expand Down Expand Up @@ -56,7 +56,6 @@ export class CommandManager implements Disposable {
id: 'editor.action.rename',
execute: async (uri: string, position: Position) => {
await workspace.jumpTo(uri, position)
await wait(50)
await plugin.cocAction(['rename'])
}
}, true)
Expand All @@ -79,7 +78,7 @@ export class CommandManager implements Disposable {
})
this.register({
id: 'workspace.showOutput',
execute: async (name?:string) => {
execute: async (name?: string) => {
if (name) {
workspace.showOutputChannel(name)
} else {
Expand Down
41 changes: 21 additions & 20 deletions src/extensions/tsserver/features/quickfix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import {CancellationToken, CodeAction, CodeActionContext, CodeActionKind, Diagnostic, Range, TextDocument} from 'vscode-languageserver-protocol'
import commandManager, {Command} from '../../../commands'
import {CodeActionProvider} from '../../../provider'
import { CancellationToken, CodeAction, CodeActionContext, CodeActionKind, Diagnostic, Range, TextDocument } from 'vscode-languageserver-protocol'
import commandManager, { Command } from '../../../commands'
import { CodeActionProvider } from '../../../provider'
import workspace from '../../../workspace'
import * as Proto from '../protocol'
import {ITypeScriptServiceClient} from '../typescriptService'
import { ITypeScriptServiceClient } from '../typescriptService'
import API from '../utils/api'
import {applyCodeActionCommands, getEditForCodeAction} from '../utils/codeAction'
import { applyCodeActionCommands, getEditForCodeAction } from '../utils/codeAction'
import * as typeConverters from '../utils/typeConverters'
import BufferSyncSupport from './bufferSyncSupport'
import {DiagnosticsManager} from './diagnostics'
import { DiagnosticsManager } from './diagnostics'

class ApplyCodeActionCommand implements Command {
public static readonly ID = '_typescript.applyCodeActionCommand'
public readonly id = ApplyCodeActionCommand.ID

constructor(
private readonly client: ITypeScriptServiceClient,
) {}
) { }

public async execute(action: Proto.CodeFixAction): Promise<boolean> {
return applyCodeActionCommands(this.client, action)
Expand All @@ -33,7 +33,7 @@ class ApplyFixAllCodeAction implements Command {

constructor(
private readonly client: ITypeScriptServiceClient,
) {}
) { }

public async execute(
file: string,
Expand All @@ -46,28 +46,29 @@ class ApplyFixAllCodeAction implements Command {
const args: Proto.GetCombinedCodeFixRequestArgs = {
scope: {
type: 'file',
args: {file}
args: { file }
},
fixId: tsAction.fixId
}

try {
const combinedCodeFixesResponse = await this.client.execute('getCombinedCodeFix', args)
if (!combinedCodeFixesResponse.body) {
const { body } = await this.client.execute('getCombinedCodeFix', args)
if (!body) {
return
}

const edit = typeConverters.WorkspaceEdit.fromFileCodeEdits(
this.client,
combinedCodeFixesResponse.body.changes
body.changes
)
await workspace.applyEdit(edit)
const token = CancellationToken.None

if (combinedCodeFixesResponse.command) {
commandManager.executeCommand(
ApplyCodeActionCommand.ID,
combinedCodeFixesResponse.command
)
const { commands } = body
if (commands && commands.length) {
for (const command of commands) {
await this.client.execute('applyCodeActionCommand', { command }, token)
}
}
} catch {
// noop
Expand All @@ -88,13 +89,13 @@ class DiagnosticsSet {
}

private static key(diagnostic: Diagnostic): string {
const {start, end} = diagnostic.range
const { start, end } = diagnostic.range
return `${diagnostic.code}-${start.line},${start.character}-${end.line},${end.character}`
}

private constructor(
private readonly _values: Map<string, Diagnostic>
) {}
) { }

public get values(): Iterable<Diagnostic> {
return this._values.values()
Expand All @@ -104,7 +105,7 @@ class DiagnosticsSet {
class SupportedCodeActionProvider {
private _supportedCodeActions?: Thenable<Set<number>>

public constructor(private readonly client: ITypeScriptServiceClient) {}
public constructor(private readonly client: ITypeScriptServiceClient) { }

public async getFixableDiagnosticsForContext(
context: CodeActionContext
Expand Down
30 changes: 14 additions & 16 deletions src/extensions/tsserver/features/refactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import {CancellationToken, CodeAction, CodeActionContext, CodeActionKind, Range, TextDocument, WorkspaceEdit} from 'vscode-languageserver-protocol'
import commandManager, {Command} from '../../../commands'
import {CodeActionProvider, CodeActionProviderMetadata} from '../../../provider'
import {showQuickpick} from '../../../util'
import { CancellationToken, CodeAction, CodeActionContext, CodeActionKind, Range, TextDocument, WorkspaceEdit } from 'vscode-languageserver-protocol'
import commandManager, { Command } from '../../../commands'
import { CodeActionProvider, CodeActionProviderMetadata } from '../../../provider'
import { showQuickpick } from '../../../util'
import workspace from '../../../workspace'
import * as Proto from '../protocol'
import {ITypeScriptServiceClient} from '../typescriptService'
import { ITypeScriptServiceClient } from '../typescriptService'
import * as typeConverters from '../utils/typeConverters'
import FormattingOptionsManager from './fileConfigurationManager'
import touch = require('touch')
const logger = require('../../../util/logger')('tsserver-refactor')

class ApplyRefactoringCommand implements Command {
public static readonly ID = '_typescript.applyRefactoring'
public readonly id = ApplyRefactoringCommand.ID

constructor(private readonly client: ITypeScriptServiceClient) {}
constructor(private readonly client: ITypeScriptServiceClient) { }

public async execute(
document: TextDocument,
Expand All @@ -38,24 +37,23 @@ class ApplyRefactoringCommand implements Command {
return false
}

const workspaceEdit = this.toWorkspaceEdit(body)
const workspaceEdit = await this.toWorkspaceEdit(body)
if (!(await workspace.applyEdit(workspaceEdit))) {
return false
}

const renameLocation = body.renameLocation
if (renameLocation) {
commandManager.executeCommand('editor.action.rename', [
commandManager.executeCommand('editor.action.rename',
document.uri,
typeConverters.Position.fromLocation(renameLocation)
])
)
}
return true
}

private toWorkspaceEdit(body: Proto.RefactorEditInfo): WorkspaceEdit {
private async toWorkspaceEdit(body: Proto.RefactorEditInfo): Promise<WorkspaceEdit> {
for (const edit of body.edits) {
touch.sync(edit.fileName)
await workspace.createFile(edit.fileName, { ignoreIfExists: true })
}
let workspaceEdit = typeConverters.WorkspaceEdit.fromFileCodeEdits(
this.client,
Expand All @@ -69,17 +67,17 @@ class SelectRefactorCommand implements Command {
public static readonly ID = '_typescript.selectRefactoring'
public readonly id = SelectRefactorCommand.ID

constructor(private readonly doRefactoring: ApplyRefactoringCommand) {}
constructor(private readonly doRefactoring: ApplyRefactoringCommand) { }

public async execute(
document: TextDocument,
file: string,
info: Proto.ApplicableRefactorInfo,
range: Range
): Promise<boolean> {
let {actions} = info
let { actions } = info
const idx = actions.length == 1 ? 0 : await showQuickpick(workspace.nvim,
actions.map(action => action.name)
actions.map(action => action.description || action.name)
)
if (idx == -1) return false
let label = info.actions[idx].name
Expand Down
8 changes: 4 additions & 4 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import debounce from 'debounce'
import { Neovim } from '@chemzqm/neovim'
import { Definition, FormattingOptions, Hover, Location, MarkedString, MarkupContent, Range, SymbolInformation, SymbolKind, TextDocument, DocumentSymbol, Disposable } from 'vscode-languageserver-protocol'
import debounce from 'debounce'
import { Definition, Disposable, DocumentSymbol, FormattingOptions, Hover, Location, MarkedString, MarkupContent, Range, SymbolInformation, SymbolKind, TextDocument } from 'vscode-languageserver-protocol'
import Uri from 'vscode-uri'
import CodeLensBuffer from './codelens'
import commandManager from './commands'
import diagnosticManager from './diagnostic/manager'
import languages from './languages'
import { ServiceStat } from './types'
import { echoErr, echoMessage, echoWarning, showQuickpick, disposeAll } from './util'
import { disposeAll, echoErr, echoMessage, echoWarning, showQuickpick } from './util'
import workspace from './workspace'
const logger = require('./util/logger')('Handler')

Expand Down Expand Up @@ -263,7 +263,7 @@ export default class Handler {
return
}
let newName = await nvim.call('input', ['new name:', curname])
await nvim.command('normal! :<C-u>')
nvim.command('normal! :<C-u>', true)
if (!newName) {
echoWarning(nvim, 'Empty word, canceled')
return
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ export interface IWorkspace {
echoLines(lines: string[]): Promise<void>
getCurrentState(): Promise<EditerState>
jumpTo(uri: string, position: Position): Promise<void>
createFile(filepath: string, opts: { ignoreIfExists?: boolean }): Promise<void>
openResource(uri: string): Promise<void>
createOutputChannel(name: string): OutputChannel
showOutputChannel(name: string): void
Expand Down
10 changes: 10 additions & 0 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,16 @@ export class Workspace implements IWorkspace {
}
}

public async createFile(filepath: string, opts: { ignoreIfExists?: boolean } = {}): Promise<void> {
if (fs.existsSync(filepath) && opts.ignoreIfExists) return
let uri = Uri.file(filepath).toString()
let doc = this.getDocument(uri)
if (doc) return
let encoding = await this.nvim.getOption('fileencoding') as string
fs.writeFileSync(filepath, '', encoding || '')
if (!doc) await this.openResource(uri)
}

public async openResource(uri: string, cmd = 'drop'): Promise<void> {
let u = Uri.parse(uri)
// not supported
Expand Down

0 comments on commit 9954eba

Please sign in to comment.