Skip to content

Commit

Permalink
[plugin-ext] fix WorkspaceEdit conversion
Browse files Browse the repository at this point in the history
Fixes #6295

Signed-off-by: Alex Tugarev <alex.tugarev@typefox.io>
  • Loading branch information
AlexTugarev committed Oct 2, 2019
1 parent 74d3790 commit 8f03fc0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
5 changes: 5 additions & 0 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)[];
Expand Down
27 changes: 12 additions & 15 deletions packages/plugin-ext/src/main/browser/languages-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
LanguagesExt,
WorkspaceEditDto,
ResourceTextEditDto,
ResourceFileEditDto,
PluginInfo
} from '../../common/plugin-api-rpc';
import { injectable, inject } from 'inversify';
Expand All @@ -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 {
Expand All @@ -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<void> {
const uri = URI.revive(resource);
$changeLanguage(resource: UriComponents, languageId: string): Promise<void> {
const uri = monaco.Uri.revive(resource);
const model = monaco.editor.getModel(uri);
if (!model) {
return Promise.reject(new Error('Invalid uri'));
Expand Down Expand Up @@ -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<monaco.languages.WorkspaceEdit & monaco.languages.Rejection> {
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,
Expand Down Expand Up @@ -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 (<ResourceTextEditDto>edit).resource === 'object') {
(<ResourceTextEditDto>edit).resource = URI.revive((<ResourceTextEditDto>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 {
(<ResourceFileEditDto>edit).newUri = URI.revive((<ResourceFileEditDto>edit).newUri);
(<ResourceFileEditDto>edit).oldUri = URI.revive((<ResourceFileEditDto>edit).oldUri);
return { newUri: monaco.Uri.revive(edit.newUri), oldUri: monaco.Uri.revive(edit.oldUri), options: edit.options };
}
}
}
return <monaco.languages.WorkspaceEdit>data;
})
};
}
4 changes: 2 additions & 2 deletions packages/plugin-ext/src/main/browser/text-editors-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -113,7 +113,7 @@ export class TextEditorsMainImpl implements TextEditorsMain, Disposable {
}

$tryApplyWorkspaceEdit(dto: WorkspaceEditDto): Promise<boolean> {
const edits = reviveWorkspaceEditDto(dto);
const edits = toMonacoWorkspaceEdit(dto);
return new Promise(resolve => {
this.bulkEditService.apply(edits).then(() => resolve(true), err => resolve(false));
});
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/languages/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class RenameAdapter {
if (rejectReason) {
return <WorkspaceEditDto>{
rejectReason,
edits: undefined!
edits: []
};
} else {
return Promise.reject<WorkspaceEditDto>(error);
Expand Down

0 comments on commit 8f03fc0

Please sign in to comment.