Skip to content

Commit

Permalink
refactor(handler): return boolean result
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Jun 11, 2019
1 parent 4a506e3 commit 5915a9e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 73 deletions.
20 changes: 16 additions & 4 deletions doc/coc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ Available Actions ~
"jumpDefinition" [{openCommand}] *coc-action-jumpDefinition*

jump to definition position of the current symbol.
Return `v:false` when location not found.

|coc-list| is used when more than one position is available.

Expand All @@ -1229,6 +1230,7 @@ Available Actions ~
"jumpDeclaration" [{openCommand}] *coc-action-jumpDeclaration*

jump to declaration position of the current symbol.
Return `v:false` when location not found.

same behavior as "jumpDefinition".

Expand All @@ -1237,24 +1239,28 @@ Available Actions ~
"jumpImplementation" [{openCommand}] *coc-action-jumpImplementation*

Jump to implementation position of the current symbol.
Return `v:false` when location not found.

same behavior as "jumpDefinition"

"jumpTypeDefinition" [{openCommand}] *coc-action-jumpTypeDefinition*

Jump to type definition position of the current symbol.
Return `v:false` when location not found.

same behavior as "jumpDefinition"

"jumpReferences" [{openCommand}] *coc-action-jumpReferences*

Jump to references position of the current symbol.
Return `v:false` when location not found.

same behavior as "jumpDefinition"

"doHover" *coc-action-doHover*

Show documentation of the current word in a preview window.
Return `v:false` when hover not found.

Use `coc.preferences.hoverTarget` to change hover behavior.

Expand All @@ -1263,10 +1269,11 @@ Available Actions ~

"showSignatureHelp" *coc-action-showSignatureHelp*

Echo signature help of current function, you may want to set up an
autocmd like this: >
Echo signature help of current function, return `v:false` when
signature not found. You may want to set up an autocmd like this: >
autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
autocmd User CocJumpPlaceholder call
\ CocActionAsync('showSignatureHelp')
<
"getCurrentFunctionSymbol" *coc-action-getCurrentFunctionSymbol*

Expand Down Expand Up @@ -1300,6 +1307,7 @@ Available Actions ~
"format" *coc-action-format*

Format current buffer using the language server.
Return `v:false` when format failed.

"formatSelected" [{mode}] *coc-action-formatSelected*

Expand Down Expand Up @@ -1347,6 +1355,8 @@ Available Actions ~
Fold the current buffer, optionally use {kind} for filtering folds,
{kind} could be either 'comment', 'imports' or 'region'

Return `v:false` when failed.

"highlight" *coc-action-highlight*

Highlight the symbols under the cursor.
Expand All @@ -1361,7 +1371,8 @@ Available Actions ~

Open a link under the cursor with {command}.
{command} default to `edit`.
File and URL links are supported.

File and URL links are supported, return `v:false` when failed.

"extensionStats" *coc-action-extensionStats*

Expand Down Expand Up @@ -1423,6 +1434,7 @@ Available Actions ~
"doQuickfix" *coc-action-doQuickfix*

Do the first quickfix action for the current line.
Return `v:false` when no quickfix action found.

------------------------------------------------------------------------------
COMMANDS *coc-commands*
Expand Down
114 changes: 63 additions & 51 deletions src/handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,70 +261,75 @@ export default class Handler {
return currentFunctionName
}

public async onHover(): Promise<void> {
public async onHover(): Promise<boolean> {
let { document, position } = await workspace.getCurrentState()
let hovers = await languages.getHover(document, position)
if (hovers && hovers.length) {
await this.previewHover(hovers)
} else {
workspace.showMessage('No definition found.', 'warning')
let target = this.preferences.hoverTarget
if (target == 'float') {
this.hoverFactory.close()
} else if (target == 'preview') {
this.nvim.command('pclose', true)
}
return true
}
let target = this.preferences.hoverTarget
if (target == 'float') {
this.hoverFactory.close()
} else if (target == 'preview') {
this.nvim.command('pclose', true)
}
return false
}

public async gotoDefinition(openCommand?: string): Promise<void> {
public async gotoDefinition(openCommand?: string): Promise<boolean> {
let { document, position } = await workspace.getCurrentState()
let definition = await languages.getDefinition(document, position)
if (isEmpty(definition)) {
this.onEmptyLocation('Definition', definition)
} else {
await this.handleLocations(definition, openCommand)
return false
}
await this.handleLocations(definition, openCommand)
return true
}

public async gotoDeclaration(openCommand?: string): Promise<void> {
public async gotoDeclaration(openCommand?: string): Promise<boolean> {
let { document, position } = await workspace.getCurrentState()
let definition = await languages.getDeclaration(document, position)
if (isEmpty(definition)) {
this.onEmptyLocation('Declaration', definition)
} else {
await this.handleLocations(definition, openCommand)
return false
}
await this.handleLocations(definition, openCommand)
return true
}

public async gotoTypeDefinition(openCommand?: string): Promise<void> {
public async gotoTypeDefinition(openCommand?: string): Promise<boolean> {
let { document, position } = await workspace.getCurrentState()
let definition = await languages.getTypeDefinition(document, position)
if (isEmpty(definition)) {
this.onEmptyLocation('Type definition', definition)
} else {
await this.handleLocations(definition, openCommand)
return false
}
await this.handleLocations(definition, openCommand)
return true
}

public async gotoImplementation(openCommand?: string): Promise<void> {
public async gotoImplementation(openCommand?: string): Promise<boolean> {
let { document, position } = await workspace.getCurrentState()
let definition = await languages.getImplementation(document, position)
if (isEmpty(definition)) {
this.onEmptyLocation('Implementation', definition)
} else {
await this.handleLocations(definition, openCommand)
return false
}
await this.handleLocations(definition, openCommand)
return true
}

public async gotoReferences(openCommand?: string): Promise<void> {
public async gotoReferences(openCommand?: string): Promise<boolean> {
let { document, position } = await workspace.getCurrentState()
let locs = await languages.getReferences(document, { includeDeclaration: false }, position)
if (isEmpty(locs)) {
this.onEmptyLocation('References', locs)
} else {
await this.handleLocations(locs, openCommand)
return false
}
await this.handleLocations(locs, openCommand)
return true
}

public async getDocumentSymbols(): Promise<SymbolInfo[]> {
Expand Down Expand Up @@ -370,22 +375,22 @@ export default class Handler {
return res
}

public async rename(newName?: string): Promise<void> {
public async rename(newName?: string): Promise<boolean> {
let { nvim } = this
let { document, position } = await workspace.getCurrentState()
if (!document) return
let res = await languages.prepareRename(document, position)
let buf = await nvim.buffer
let doc = workspace.getDocument(buf.id)
let position = await workspace.getCursorPosition()
if (!doc) return false
let res = await languages.prepareRename(doc.textDocument, position)
if (res === false) {
workspace.showMessage('Invalid position for rename', 'error')
return
return false
}
let doc = workspace.getDocument(document.uri)
if (!doc) return
doc.forceSync()
let curname: string
if (res == null) {
let range = doc.getWordRangeAtPosition(position)
if (range) curname = document.getText(range)
if (range) curname = doc.textDocument.getText(range)
} else {
if (Range.is(res)) {
let line = doc.getline(res.start.line)
Expand All @@ -396,31 +401,33 @@ export default class Handler {
}
if (!curname) {
workspace.showMessage('Invalid position', 'warning')
return
return false
}
if (!newName) {
newName = await nvim.call('input', ['new name:', curname])
nvim.command('normal! :<C-u>', true)
if (!newName) {
workspace.showMessage('Empty word, canceled', 'warning')
return
return false
}
}
let edit = await languages.provideRenameEdits(document, position, newName)
let edit = await languages.provideRenameEdits(doc.textDocument, position, newName)
if (!edit) {
workspace.showMessage('Server return empty response for rename', 'warning')
return
return false
}
await workspace.applyEdit(edit)
return true
}

public async documentFormatting(): Promise<void> {
public async documentFormatting(): Promise<boolean> {
let document = await workspace.document
if (!document) return
if (!document) return false
let options = await workspace.getFormatOptions(document.uri)
let textEdits = await languages.provideDocumentFormattingEdits(document.textDocument, options)
if (!textEdits || textEdits.length == 0) return
if (!textEdits || textEdits.length == 0) return false
await document.applyEdits(this.nvim, textEdits)
return true
}

public async documentRangeFormatting(mode: string): Promise<number> {
Expand Down Expand Up @@ -525,13 +532,15 @@ export default class Handler {
return await this.getCodeActions(bufnr, range, only)
}

public async doQuickfix(): Promise<void> {
public async doQuickfix(): Promise<boolean> {
let actions = await this.getCurrentCodeActions(null, [CodeActionKind.QuickFix])
if (!actions || actions.length == 0) {
return workspace.showMessage('No quickfix action available', 'warning')
workspace.showMessage('No quickfix action available', 'warning')
return false
}
await this.applyCodeAction(actions[0])
await this.nvim.command(`silent! call repeat#set("\\<Plug>(coc-fix-current)", -1)`)
return true
}

public async applyCodeAction(action: CodeAction): Promise<void> {
Expand Down Expand Up @@ -563,18 +572,18 @@ export default class Handler {
await this.codeLensManager.doAction()
}

public async fold(kind?: string): Promise<void> {
public async fold(kind?: string): Promise<boolean> {
let document = await workspace.document
let win = await this.nvim.window
let foldmethod = await win.getOption('foldmethod')
if (foldmethod != 'manual') {
workspace.showMessage('foldmethod option should be manual!', 'error')
return
return false
}
let ranges = await languages.provideFoldingRanges(document.textDocument, {})
if (!ranges || ranges.length == 0) {
workspace.showMessage('no range found', 'warning')
return
return false
}
if (kind) {
ranges = ranges.filter(o => o.kind == kind)
Expand All @@ -586,7 +595,9 @@ export default class Handler {
let cmd = `${startLine + 1}, ${endLine + 1}fold`
this.nvim.command(cmd, true)
}
return true
}
return false
}

public async pickColor(): Promise<void> {
Expand Down Expand Up @@ -636,6 +647,7 @@ export default class Handler {
return false
}
}
return false
}

public async getCommands(): Promise<CommandItem[]> {
Expand Down Expand Up @@ -691,13 +703,12 @@ export default class Handler {
}
}

private async triggerSignatureHelp(document: Document, position: Position): Promise<void> {
private async triggerSignatureHelp(document: Document, position: Position): Promise<boolean> {
if (this.signatureTokenSource) {
this.signatureTokenSource.cancel()
this.signatureTokenSource = null
}
let part = document.getline(position.line).slice(0, position.character)
if (/\)\s*$/.test(part)) return
let idx = Math.max(part.lastIndexOf(','), part.lastIndexOf('('))
if (idx != -1) position.character = idx + 1
let tokenSource = this.signatureTokenSource = new CancellationTokenSource()
Expand All @@ -709,9 +720,9 @@ export default class Handler {
}, 3000)
let signatureHelp = await languages.getSignatureHelp(document.textDocument, position, token)
clearTimeout(timer)
if (token.isCancellationRequested || !signatureHelp) {
if (token.isCancellationRequested || !signatureHelp || signatureHelp.signatures.length == 0) {
this.signatureFactory.close()
return
return false
}
let { activeParameter, activeSignature, signatures } = signatureHelp
if (activeSignature) {
Expand Down Expand Up @@ -842,14 +853,15 @@ export default class Handler {
}
this.nvim.callTimer('coc#util#echo_signatures', [signatureList], true)
}
return true
}

public async showSignatureHelp(): Promise<void> {
public async showSignatureHelp(): Promise<boolean> {
let buffer = await this.nvim.buffer
let document = workspace.getDocument(buffer.id)
if (!document) return
if (!document) return false
let position = await workspace.getCursorPosition()
await this.triggerSignatureHelp(document, position)
return await this.triggerSignatureHelp(document, position)
}

public async handleLocations(definition: Definition | LocationLink[], openCommand?: string | false): Promise<void> {
Expand Down
2 changes: 0 additions & 2 deletions src/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ class Languages {
@check
public async getCodeActions(document: TextDocument, range: Range, context: CodeActionContext, silent = false): Promise<Map<string, CodeAction[]>> {
if (!silent && !this.codeActionManager.hasProvider(document)) {
workspace.showMessage('Code action provider not found for current document', 'error')
return null
}
return await this.codeActionManager.provideCodeActions(document, range, context, this.token)
Expand Down Expand Up @@ -440,7 +439,6 @@ class Languages {
@check
public async provideFoldingRanges(document: TextDocument, context: FoldingContext): Promise<FoldingRange[] | null> {
if (!this.formatRangeManager.hasProvider(document)) {
workspace.showMessage('Folding ranges provider not found for current document', 'error')
return null
}
return await this.foldingRangeManager.provideFoldingRanges(document, context, this.token)
Expand Down
Loading

0 comments on commit 5915a9e

Please sign in to comment.