diff --git a/packages/plugin-ext/src/common/plugin-api-rpc.ts b/packages/plugin-ext/src/common/plugin-api-rpc.ts index d063df23d43e8..4e26732589478 100644 --- a/packages/plugin-ext/src/common/plugin-api-rpc.ts +++ b/packages/plugin-ext/src/common/plugin-api-rpc.ts @@ -1057,6 +1057,11 @@ export interface ResourceTextEditDto { modelVersionId?: number; edits: TextEdit[]; } +export namespace ResourceTextEditDto { + export function is(arg: Object): arg is ResourceTextEditDto { + return !!arg && typeof arg === 'object' && 'resource' in arg && 'edits' in arg; + } +} export interface WorkspaceEditDto { edits: (ResourceFileEditDto | ResourceTextEditDto)[]; diff --git a/packages/plugin-ext/src/main/browser/languages-main.ts b/packages/plugin-ext/src/main/browser/languages-main.ts index c6e8ae911eb28..bbdd614f04f2e 100644 --- a/packages/plugin-ext/src/main/browser/languages-main.ts +++ b/packages/plugin-ext/src/main/browser/languages-main.ts @@ -32,7 +32,6 @@ import { LanguagesExt, WorkspaceEditDto, ResourceTextEditDto, - ResourceFileEditDto, PluginInfo } from '../../common/plugin-api-rpc'; import { injectable, inject } from 'inversify'; @@ -43,13 +42,13 @@ import { import { RPCProtocol } from '../../common/rpc-protocol'; import { fromLanguageSelector } from '../../plugin/type-converters'; import { MonacoLanguages } from '@theia/monaco/lib/browser/monaco-languages'; -import URI from 'vscode-uri/lib/umd'; import CoreURI from '@theia/core/lib/common/uri'; import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable'; import { Emitter } from '@theia/core/lib/common/event'; import { ProblemManager } from '@theia/markers/lib/browser'; import * as vst from 'vscode-languageserver-types'; import * as theia from '@theia/plugin'; +import { UriComponents } from '../../common/uri-components'; @injectable() export class LanguagesMainImpl implements LanguagesMain, Disposable { @@ -76,8 +75,8 @@ export class LanguagesMainImpl implements LanguagesMain, Disposable { return Promise.resolve(monaco.languages.getLanguages().map(l => l.id)); } - $changeLanguage(resource: URI, languageId: string): Promise { - const uri = URI.revive(resource); + $changeLanguage(resource: UriComponents, languageId: string): Promise { + const uri = monaco.Uri.revive(resource); const model = monaco.editor.getModel(uri); if (!model) { return Promise.reject(new Error('Invalid uri')); @@ -703,7 +702,7 @@ export class LanguagesMainImpl implements LanguagesMain, Disposable { protected provideRenameEdits(handle: number, model: monaco.editor.ITextModel, position: monaco.Position, newName: string, token: monaco.CancellationToken): monaco.languages.ProviderResult { - return this.proxy.$provideRenameEdits(handle, model.uri, position, newName, token).then(reviveWorkspaceEditDto); + return this.proxy.$provideRenameEdits(handle, model.uri, position, newName, token).then(toMonacoWorkspaceEdit); } protected resolveRenameLocation(handle: number, model: monaco.editor.ITextModel, @@ -797,16 +796,14 @@ function reviveOnEnterRules(onEnterRules?: SerializedOnEnterRule[]): monaco.lang return onEnterRules.map(reviveOnEnterRule); } -export function reviveWorkspaceEditDto(data: WorkspaceEditDto): monaco.languages.WorkspaceEdit { - if (data && data.edits) { - for (const edit of data.edits) { - if (typeof (edit).resource === 'object') { - (edit).resource = URI.revive((edit).resource); +export function toMonacoWorkspaceEdit(data: WorkspaceEditDto | undefined): monaco.languages.WorkspaceEdit { + return { + edits: (data && data.edits || []).map(edit => { + if (ResourceTextEditDto.is(edit)) { + return { resource: monaco.Uri.revive(edit.resource), edits: edit.edits }; } else { - (edit).newUri = URI.revive((edit).newUri); - (edit).oldUri = URI.revive((edit).oldUri); + return { newUri: monaco.Uri.revive(edit.newUri), oldUri: monaco.Uri.revive(edit.oldUri), options: edit.options }; } - } - } - return data; + }) + }; } diff --git a/packages/plugin-ext/src/main/browser/text-editors-main.ts b/packages/plugin-ext/src/main/browser/text-editors-main.ts index cf480c763de5c..11d49b15156cb 100644 --- a/packages/plugin-ext/src/main/browser/text-editors-main.ts +++ b/packages/plugin-ext/src/main/browser/text-editors-main.ts @@ -34,7 +34,7 @@ import { RPCProtocol } from '../../common/rpc-protocol'; import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable'; import { TextEditorMain } from './text-editor-main'; import { disposed } from '../../common/errors'; -import { reviveWorkspaceEditDto } from './languages-main'; +import { toMonacoWorkspaceEdit } from './languages-main'; import { MonacoBulkEditService } from '@theia/monaco/lib/browser/monaco-bulk-edit-service'; import { MonacoEditorService } from '@theia/monaco/lib/browser/monaco-editor-service'; @@ -113,7 +113,7 @@ export class TextEditorsMainImpl implements TextEditorsMain, Disposable { } $tryApplyWorkspaceEdit(dto: WorkspaceEditDto): Promise { - const edits = reviveWorkspaceEditDto(dto); + const edits = toMonacoWorkspaceEdit(dto); return new Promise(resolve => { this.bulkEditService.apply(edits).then(() => resolve(true), err => resolve(false)); }); diff --git a/packages/plugin-ext/src/plugin/languages/rename.ts b/packages/plugin-ext/src/plugin/languages/rename.ts index e2fac4144700c..8beee6ad22bbb 100644 --- a/packages/plugin-ext/src/plugin/languages/rename.ts +++ b/packages/plugin-ext/src/plugin/languages/rename.ts @@ -57,7 +57,7 @@ export class RenameAdapter { if (rejectReason) { return { rejectReason, - edits: undefined! + edits: [] }; } else { return Promise.reject(error);