Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin-ext] Fix WorkspaceEdits for rename providers #6304

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't break with the expected handling of rejectReason, which BTW is commented with a todo to be a proper rejection in the origin of this.

};
} else {
return Promise.reject<WorkspaceEditDto>(error);
Expand Down