Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
connection.onNotification(PublishDiagnosticsNotification.type, async params => {
const uri = new URL(params.uri)
const sourcegraphTextDocumentUri = toSourcegraphTextDocumentUri(uri)
diagnosticsByUri.set(sourcegraphTextDocumentUri.href, params.diagnostics)
// Mark quick fixes with a light bulb
const codeActionDecorations: sourcegraph.TextDocumentDecoration[] = []
for (const diagnostic of params.diagnostics) {
const codeActionParams: CodeActionParams = {
textDocument: {
uri: uri.href,
},
range: diagnostic.range,
context: {
diagnostics: [diagnostic],
only: [CodeActionKind.QuickFix],
},
}
try {
const codeActions = await connection.sendRequest(CodeActionRequest.type, codeActionParams)
codeActionDecorations.push(
...asArray(codeActions).map(
(codeAction): sourcegraph.TextDocumentDecoration => ({
range: convertRange(diagnostic.range),
after: {
contentText: '💡',
hoverMessage: codeAction.title,
},
})
)
)
} catch (err) {
tsAction: Proto.CodeFixAction
): Promise {
if (!tsAction.fixId || !this.client.apiVersion.gte(API.v270)) {
return undefined
}
// Make sure there are multiple diagnostics of the same type in the file
if (!this.client.diagnosticsManager
.getDiagnostics(document.uri)
.some(x => x.code === diagnostic.code && x !== diagnostic)) {
return
}
const action: CodeAction = {
title: tsAction.fixAllDescription || 'Fix all in file',
kind: CodeActionKind.QuickFix
}
action.diagnostics = [diagnostic]
action.command = {
command: ApplyFixAllCodeAction.ID,
arguments: [file, tsAction],
title: ''
}
return action
}
}
tsAction: Proto.CodeFixAction
): Promise {
if (!tsAction.fixId || !this.client.apiVersion.gte(API.v270)) {
return undefined
}
// Make sure there are multiple diagnostics of the same type in the file
if (!this.diagnosticsManager
.getDiagnostics(document.uri)
.some(x => x.code === diagnostic.code && x !== diagnostic)) {
return
}
const action: CodeAction = {
title: tsAction.fixAllDescription || 'Fix all in file',
kind: CodeActionKind.QuickFix
}
action.diagnostics = [diagnostic]
action.command = {
command: ApplyFixAllCodeAction.ID,
arguments: [file, tsAction],
title: ''
}
return action
}
}
private getSingleFixForTsCodeAction(
diagnostic: Diagnostic,
tsAction: Proto.CodeAction
): CodeAction {
const codeAction: CodeAction = {
title: tsAction.description,
kind: CodeActionKind.QuickFix
}
codeAction.edit = getEditForCodeAction(this.client, tsAction)
codeAction.diagnostics = [diagnostic]
if (tsAction.commands) {
codeAction.command = {
command: ApplyCodeActionCommand.ID,
arguments: [tsAction],
title: tsAction.description
}
}
return codeAction
}
'tsserver')
)
this.disposables.push(
languages.registerCodeActionProvider(
languageIds,
new QuickfixProvider(client),
'tsserver',
[CodeActionKind.QuickFix]))
this.disposables.push(
languages.registerCodeActionProvider(
languageIds,
new ImportfixProvider(this.client.bufferSyncSupport),
'tsserver',
[CodeActionKind.QuickFix]))
let cachedResponse = new CachedNavTreeResponse()
if (this.client.apiVersion.gte(API.v206)
&& conf.get('referencesCodeLens.enable')) {
this.disposables.push(
languages.registerCodeLensProvider(
languageIds,
new ReferencesCodeLensProvider(client, cachedResponse)))
}
if (this.client.apiVersion.gte(API.v220)
&& conf.get('implementationsCodeLens.enable')) {
this.disposables.push(
languages.registerCodeLensProvider(
languageIds,
new ImplementationsCodeLensProvider(client, cachedResponse)))
}
public async getQuickfixActions(range?: Range): Promise {
let document = await workspace.document
if (!document) return []
range = range || Range.create(0, 0, document.lineCount, 0)
let diagnostics = diagnosticManager.getDiagnosticsInRange(document.textDocument, range)
let context: CodeActionContext = { diagnostics, only: [CodeActionKind.QuickFix] }
let codeActionsMap = await languages.getCodeActions(document.textDocument, range, context, true)
if (!codeActionsMap) return []
let codeActions: CodeAction[] = []
for (let clientId of codeActionsMap.keys()) {
let actions = codeActionsMap.get(clientId)
for (let action of actions) {
if (action.kind !== CodeActionKind.QuickFix) continue
(action as any).clientId = clientId
codeActions.push(action)
}
}
return codeActions
}
public async getQuickfixActions(range?: Range): Promise {
let document = await workspace.document
if (!document) return []
range = range || Range.create(0, 0, document.lineCount, 0)
let diagnostics = diagnosticManager.getDiagnosticsInRange(document.textDocument, range)
let context: CodeActionContext = { diagnostics, only: [CodeActionKind.QuickFix] }
let codeActionsMap = await languages.getCodeActions(document.textDocument, range, context, true)
if (!codeActionsMap) return []
let codeActions: CodeAction[] = []
for (let clientId of codeActionsMap.keys()) {
let actions = codeActionsMap.get(clientId)
for (let action of actions) {
if (action.kind !== CodeActionKind.QuickFix) continue
(action as any).clientId = clientId
codeActions.push(action)
}
}
return codeActions
}
public async doQuickfix(): Promise {
let actions = await this.getCurrentCodeActions(null, [CodeActionKind.QuickFix])
if (!actions || actions.length == 0) {
workspace.showMessage('No quickfix action available', 'warning')
return false
}
await this.applyCodeAction(actions[0])
await this.nvim.command(`silent! call repeat#set("\\(coc-fix-current)", -1)`)
return true
}