Skip to content

Commit

Permalink
[vscode] fixed bad type conversion with code actions
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Efftinge <sven.efftinge@typefox.io>
  • Loading branch information
svenefftinge committed Nov 15, 2019
1 parent f3d83e2 commit ebe0580
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 55 deletions.
6 changes: 0 additions & 6 deletions packages/plugin-ext/src/common/plugin-api-rpc-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,8 @@ export interface CodeAction {
kind?: string;
}

export enum CodeActionTrigger {
Automatic = 1,
Manual = 2,
}

export interface CodeActionContext {
only?: string;
trigger: CodeActionTrigger;
}

export interface CodeActionProvider {
Expand Down
14 changes: 9 additions & 5 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ import {
RenameLocation,
FileMoveEvent,
FileWillMoveEvent,
SignatureHelpContext
SignatureHelpContext,
CodeAction,
CodeActionContext,
FoldingContext,
FoldingRange
} from './plugin-api-rpc-model';
import { ExtPluginApi } from './plugin-ext-api-contribution';
import { KeysToAnyValues, KeysToKeysToAnyValue } from './types';
Expand Down Expand Up @@ -1155,18 +1159,18 @@ export interface LanguagesExt {
handle: number,
resource: UriComponents,
rangeOrSelection: Range | Selection,
context: monaco.languages.CodeActionContext,
context: CodeActionContext,
token: CancellationToken
): Promise<monaco.languages.CodeAction[] | undefined>;
): Promise<CodeAction[] | undefined>;
$provideDocumentSymbols(handle: number, resource: UriComponents, token: CancellationToken): Promise<DocumentSymbol[] | undefined>;
$provideWorkspaceSymbols(handle: number, query: string, token: CancellationToken): PromiseLike<SymbolInformation[]>;
$resolveWorkspaceSymbol(handle: number, symbol: SymbolInformation, token: CancellationToken): PromiseLike<SymbolInformation>;
$provideFoldingRange(
handle: number,
resource: UriComponents,
context: monaco.languages.FoldingContext,
context: FoldingContext,
token: CancellationToken
): PromiseLike<monaco.languages.FoldingRange[] | undefined>;
): PromiseLike<FoldingRange[] | undefined>;
$provideDocumentColors(handle: number, resource: UriComponents, token: CancellationToken): PromiseLike<RawColorInfo[]>;
$provideColorPresentations(handle: number, resource: UriComponents, colorInfo: RawColorInfo, token: CancellationToken): PromiseLike<ColorPresentation[]>;
$provideRenameEdits(handle: number, resource: UriComponents, position: Position, newName: string, token: CancellationToken): PromiseLike<WorkspaceEditDto | undefined>;
Expand Down
32 changes: 29 additions & 3 deletions packages/plugin-ext/src/main/browser/languages-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
import { injectable, inject } from 'inversify';
import {
SerializedDocumentFilter, MarkerData, Range, WorkspaceSymbolProvider, RelatedInformation,
MarkerSeverity, DocumentLink, WorkspaceSymbolParams
MarkerSeverity, DocumentLink, WorkspaceSymbolParams, CodeAction
} from '../../common/plugin-api-rpc-model';
import { RPCProtocol } from '../../common/rpc-protocol';
import { fromLanguageSelector } from '../../plugin/type-converters';
Expand Down Expand Up @@ -672,12 +672,14 @@ export class LanguagesMainImpl implements LanguagesMain, Disposable {
protected async provideCodeActions(handle: number, model: monaco.editor.ITextModel,
rangeOrSelection: Range, context: monaco.languages.CodeActionContext,
token: monaco.CancellationToken): Promise<monaco.languages.CodeActionList | monaco.languages.CodeActionList> {
const actions = await this.proxy.$provideCodeActions(handle, model.uri, rangeOrSelection, context, token);
const actions = await this.proxy.$provideCodeActions(handle, model.uri, rangeOrSelection, {
...context
}, token);
if (!actions) {
return undefined!;
}
return {
actions,
actions: actions.map(a => toMonacoAction(a)),
dispose: () => {
// TODO this.proxy.$releaseCodeActions(handle, cacheId);
}
Expand Down Expand Up @@ -797,6 +799,30 @@ function reviveOnEnterRules(onEnterRules?: SerializedOnEnterRule[]): monaco.lang
return onEnterRules.map(reviveOnEnterRule);
}

function toMonacoAction(action: CodeAction): monaco.languages.CodeAction {
return {
...action,
diagnostics: action.diagnostics ? action.diagnostics.map(m => toMonacoMarkerData(m)) : undefined,
edit: action.edit ? toMonacoWorkspaceEdit(action.edit) : undefined
};
}

function toMonacoMarkerData(marker: MarkerData): monaco.editor.IMarkerData {
return {
...marker,
relatedInformation: marker.relatedInformation
? marker.relatedInformation.map(i => toMonacoRelatedInformation(i))
: undefined
};
}

function toMonacoRelatedInformation(relatedInfo: RelatedInformation): monaco.editor.IRelatedInformation {
return {
...relatedInfo,
resource: monaco.Uri.parse(relatedInfo.resource)
};
}

export function toMonacoWorkspaceEdit(data: WorkspaceEditDto | undefined): monaco.languages.WorkspaceEdit {
return {
edits: (data && data.edits || []).map(edit => {
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-ext/src/plugin/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ import {
ColorPresentation,
RenameLocation,
SignatureHelpContext,
CodeActionContext,
CodeAction,
FoldingRange,
} from '../common/plugin-api-rpc-model';
import { CompletionAdapter } from './languages/completion';
import { Diagnostics } from './languages/diagnostics';
Expand Down Expand Up @@ -439,9 +442,9 @@ export class LanguagesExtImpl implements LanguagesExt {
$provideCodeActions(handle: number,
resource: UriComponents,
rangeOrSelection: Range | Selection,
context: monaco.languages.CodeActionContext,
context: CodeActionContext,
token: theia.CancellationToken
): Promise<monaco.languages.CodeAction[] | undefined> {
): Promise<CodeAction[] | undefined> {
return this.withAdapter(handle, CodeActionAdapter, adapter => adapter.provideCodeAction(URI.revive(resource), rangeOrSelection, context, token));
}
// ### Code Actions Provider end
Expand Down Expand Up @@ -522,7 +525,7 @@ export class LanguagesExtImpl implements LanguagesExt {
resource: UriComponents,
context: theia.FoldingContext,
token: theia.CancellationToken
): Promise<monaco.languages.FoldingRange[] | undefined> {
): Promise<FoldingRange[] | undefined> {
return this.withAdapter(callId, FoldingProviderAdapter, adapter => adapter.provideFoldingRanges(URI.revive(resource), context, token));
}
// ### Folging Range Provider end
Expand Down
75 changes: 37 additions & 38 deletions packages/plugin-ext/src/plugin/languages/code-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as theia from '@theia/plugin';
import URI from 'vscode-uri/lib/umd';
import { Selection } from '../../common/plugin-api-rpc';
import { Range } from '../../common/plugin-api-rpc-model';
import { Range, CodeActionContext, CodeAction } from '../../common/plugin-api-rpc-model';
import * as Converter from '../type-converters';
import { DocumentsExtImpl } from '../documents';
import { Diagnostics } from './diagnostics';
Expand All @@ -35,8 +35,8 @@ export class CodeActionAdapter {
private readonly commands: CommandRegistryImpl
) { }

provideCodeAction(resource: URI, rangeOrSelection: Range | Selection,
context: monaco.languages.CodeActionContext, token: theia.CancellationToken): Promise<monaco.languages.CodeAction[] | undefined> {
async provideCodeAction(resource: URI, rangeOrSelection: Range | Selection,
context: CodeActionContext, token: theia.CancellationToken): Promise<CodeAction[] | undefined> {
const document = this.document.getDocumentData(resource);
if (!document) {
return Promise.reject(new Error(`There are no document for ${resource}`));
Expand All @@ -59,46 +59,45 @@ export class CodeActionAdapter {
only: context.only ? new CodeActionKind(context.only) : undefined
};

return Promise.resolve(this.provider.provideCodeActions(doc, ran, codeActionContext, token)).then(commandsOrActions => {
if (!Array.isArray(commandsOrActions) || commandsOrActions.length === 0) {
return undefined;
const commandsOrActions = await this.provider.provideCodeActions(doc, ran, codeActionContext, token);

if (!Array.isArray(commandsOrActions) || commandsOrActions.length === 0) {
return undefined;
}
// TODO cache toDispose and dispose it
const toDispose = new DisposableCollection();
const result: CodeAction[] = [];
for (const candidate of commandsOrActions) {
if (!candidate) {
continue;
}
// TODO cache toDispose and dispose it
const toDispose = new DisposableCollection();
const result: monaco.languages.CodeAction[] = [];
for (const candidate of commandsOrActions) {
if (!candidate) {
continue;
}
if (CodeActionAdapter._isCommand(candidate)) {
result.push({
title: candidate.title || '',
command: this.commands.converter.toSafeCommand(candidate, toDispose)
});
} else {
if (codeActionContext.only) {
if (!candidate.kind) {
/* tslint:disable-next-line:max-line-length */
console.warn(`${this.pluginId} - Code actions of kind '${codeActionContext.only.value}' requested but returned code action does not have a 'kind'. Code action will be dropped. Please set 'CodeAction.kind'.`);
} else if (!codeActionContext.only.contains(candidate.kind)) {
/* tslint:disable-next-line:max-line-length */
console.warn(`${this.pluginId} - Code actions of kind '${codeActionContext.only.value}' requested but returned code action is of kind '${candidate.kind.value}'. Code action will be dropped. Please check 'CodeActionContext.only' to only return requested code action.`);
}
if (CodeActionAdapter._isCommand(candidate)) {
result.push({
title: candidate.title || '',
command: this.commands.converter.toSafeCommand(candidate, toDispose)
});
} else {
if (codeActionContext.only) {
if (!candidate.kind) {
/* tslint:disable-next-line:max-line-length */
console.warn(`${this.pluginId} - Code actions of kind '${codeActionContext.only.value}' requested but returned code action does not have a 'kind'. Code action will be dropped. Please set 'CodeAction.kind'.`);
} else if (!codeActionContext.only.contains(candidate.kind)) {
/* tslint:disable-next-line:max-line-length */
console.warn(`${this.pluginId} - Code actions of kind '${codeActionContext.only.value}' requested but returned code action is of kind '${candidate.kind.value}'. Code action will be dropped. Please check 'CodeActionContext.only' to only return requested code action.`);
}

result.push({
title: candidate.title,
command: this.commands.converter.toSafeCommand(candidate.command, toDispose),
diagnostics: candidate.diagnostics && candidate.diagnostics.map(Converter.convertDiagnosticToMarkerData) as monaco.editor.IMarker[],
edit: candidate.edit && Converter.fromWorkspaceEdit(candidate.edit) as monaco.languages.WorkspaceEdit,
kind: candidate.kind && candidate.kind.value
});
}
}

return result;
});
result.push({
title: candidate.title,
command: this.commands.converter.toSafeCommand(candidate.command, toDispose),
diagnostics: candidate.diagnostics && candidate.diagnostics.map(Converter.convertDiagnosticToMarkerData),
edit: candidate.edit && Converter.fromWorkspaceEdit(candidate.edit),
kind: candidate.kind && candidate.kind.value
});
}
}

return result;
}

// tslint:disable-next-line:no-any
Expand Down

0 comments on commit ebe0580

Please sign in to comment.