Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 693b338

Browse files
lexi-lambdaAerijo
authored andcommitted
Add support for rename refactors (#270)
closes #13
1 parent 3f44ceb commit 693b338

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

lib/adapters/rename-adapter.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as atomIde from 'atom-ide';
2+
import Convert from '../convert';
3+
import {
4+
Point,
5+
TextEditor,
6+
} from 'atom';
7+
import {
8+
LanguageClientConnection,
9+
RenameParams,
10+
ServerCapabilities,
11+
TextDocumentEdit,
12+
TextEdit,
13+
} from '../languageclient';
14+
15+
export default class RenameAdapter {
16+
public static canAdapt(serverCapabilities: ServerCapabilities): boolean {
17+
return serverCapabilities.renameProvider === true;
18+
}
19+
20+
public static async getRename(
21+
connection: LanguageClientConnection,
22+
editor: TextEditor,
23+
point: Point,
24+
newName: string,
25+
): Promise<Map<atomIde.IdeUri, atomIde.TextEdit[]> | null> {
26+
const edit = await connection.rename(
27+
RenameAdapter.createRenameParams(editor, point, newName),
28+
);
29+
if (edit === null) {
30+
return null;
31+
}
32+
33+
if (edit.documentChanges) {
34+
return RenameAdapter.convertDocumentChanges(edit.documentChanges);
35+
} else if (edit.changes) {
36+
return RenameAdapter.convertChanges(edit.changes);
37+
} else {
38+
return null;
39+
}
40+
}
41+
42+
public static createRenameParams(editor: TextEditor, point: Point, newName: string): RenameParams {
43+
return {
44+
textDocument: Convert.editorToTextDocumentIdentifier(editor),
45+
position: Convert.pointToPosition(point),
46+
newName,
47+
};
48+
}
49+
50+
public static convertChanges(
51+
changes: { [uri: string]: TextEdit[] },
52+
): Map<atomIde.IdeUri, atomIde.TextEdit[]> {
53+
const result = new Map();
54+
Object.keys(changes).forEach((uri) => {
55+
result.set(
56+
Convert.uriToPath(uri),
57+
Convert.convertLsTextEdits(changes[uri]),
58+
);
59+
});
60+
return result;
61+
}
62+
63+
public static convertDocumentChanges(
64+
documentChanges: TextDocumentEdit[],
65+
): Map<atomIde.IdeUri, atomIde.TextEdit[]> {
66+
const result = new Map();
67+
documentChanges.forEach((documentEdit) => {
68+
result.set(
69+
Convert.uriToPath(documentEdit.textDocument.uri),
70+
Convert.convertLsTextEdits(documentEdit.edits),
71+
);
72+
});
73+
return result;
74+
}
75+
}

lib/auto-languageclient.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import LinterPushV2Adapter from './adapters/linter-push-v2-adapter';
1818
import LoggingConsoleAdapter from './adapters/logging-console-adapter';
1919
import NotificationsAdapter from './adapters/notifications-adapter';
2020
import OutlineViewAdapter from './adapters/outline-view-adapter';
21+
import RenameAdapter from './adapters/rename-adapter';
2122
import SignatureHelpAdapter from './adapters/signature-help-adapter';
2223
import * as Utils from './utils';
2324
import { Socket } from 'net';
@@ -756,6 +757,28 @@ export default class AutoLanguageClient {
756757
);
757758
}
758759

760+
public provideRefactor(): atomIde.RefactorProvider {
761+
return {
762+
grammarScopes: this.getGrammarScopes(),
763+
priority: 1,
764+
rename: this.getRename.bind(this),
765+
};
766+
}
767+
768+
protected async getRename(editor: TextEditor, position: Point, newName: string) {
769+
const server = await this._serverManager.getServer(editor);
770+
if (server == null || !RenameAdapter.canAdapt(server.capabilities)) {
771+
return null;
772+
}
773+
774+
return RenameAdapter.getRename(
775+
server.connection,
776+
editor,
777+
position,
778+
newName,
779+
);
780+
}
781+
759782
public consumeSignatureHelp(registry: atomIde.SignatureHelpRegistry): Disposable {
760783
this._signatureHelpRegistry = registry;
761784
for (const server of this._serverManager.getActiveServers()) {

typings/atom-ide/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ declare module 'atom-ide' {
213213
): Promise<CodeAction[] | null>;
214214
}
215215

216+
export interface RefactorProvider {
217+
grammarScopes: string[];
218+
priority: number;
219+
rename?(editor: TextEditor, position: Point, newName: string): Promise<Map<IdeUri, TextEdit[]> | null>;
220+
}
221+
216222
export interface BusySignalOptions {
217223
/**
218224
* Can say that a busy signal will only appear when a given file is open.

0 commit comments

Comments
 (0)