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

Commit

Permalink
Add support for rename refactors (#270)
Browse files Browse the repository at this point in the history
closes #13
  • Loading branch information
lexi-lambda authored and Aerijo committed Aug 4, 2019
1 parent 3f44ceb commit 693b338
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
75 changes: 75 additions & 0 deletions lib/adapters/rename-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as atomIde from 'atom-ide';
import Convert from '../convert';
import {
Point,
TextEditor,
} from 'atom';
import {
LanguageClientConnection,
RenameParams,
ServerCapabilities,
TextDocumentEdit,
TextEdit,
} from '../languageclient';

export default class RenameAdapter {
public static canAdapt(serverCapabilities: ServerCapabilities): boolean {
return serverCapabilities.renameProvider === true;
}

public static async getRename(
connection: LanguageClientConnection,
editor: TextEditor,
point: Point,
newName: string,
): Promise<Map<atomIde.IdeUri, atomIde.TextEdit[]> | null> {
const edit = await connection.rename(
RenameAdapter.createRenameParams(editor, point, newName),
);
if (edit === null) {
return null;
}

if (edit.documentChanges) {
return RenameAdapter.convertDocumentChanges(edit.documentChanges);
} else if (edit.changes) {
return RenameAdapter.convertChanges(edit.changes);
} else {
return null;
}
}

public static createRenameParams(editor: TextEditor, point: Point, newName: string): RenameParams {
return {
textDocument: Convert.editorToTextDocumentIdentifier(editor),
position: Convert.pointToPosition(point),
newName,
};
}

public static convertChanges(
changes: { [uri: string]: TextEdit[] },
): Map<atomIde.IdeUri, atomIde.TextEdit[]> {
const result = new Map();
Object.keys(changes).forEach((uri) => {
result.set(
Convert.uriToPath(uri),
Convert.convertLsTextEdits(changes[uri]),
);
});
return result;
}

public static convertDocumentChanges(
documentChanges: TextDocumentEdit[],
): Map<atomIde.IdeUri, atomIde.TextEdit[]> {
const result = new Map();
documentChanges.forEach((documentEdit) => {
result.set(
Convert.uriToPath(documentEdit.textDocument.uri),
Convert.convertLsTextEdits(documentEdit.edits),
);
});
return result;
}
}
23 changes: 23 additions & 0 deletions lib/auto-languageclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import LinterPushV2Adapter from './adapters/linter-push-v2-adapter';
import LoggingConsoleAdapter from './adapters/logging-console-adapter';
import NotificationsAdapter from './adapters/notifications-adapter';
import OutlineViewAdapter from './adapters/outline-view-adapter';
import RenameAdapter from './adapters/rename-adapter';
import SignatureHelpAdapter from './adapters/signature-help-adapter';
import * as Utils from './utils';
import { Socket } from 'net';
Expand Down Expand Up @@ -756,6 +757,28 @@ export default class AutoLanguageClient {
);
}

public provideRefactor(): atomIde.RefactorProvider {
return {
grammarScopes: this.getGrammarScopes(),
priority: 1,
rename: this.getRename.bind(this),
};
}

protected async getRename(editor: TextEditor, position: Point, newName: string) {
const server = await this._serverManager.getServer(editor);
if (server == null || !RenameAdapter.canAdapt(server.capabilities)) {
return null;
}

return RenameAdapter.getRename(
server.connection,
editor,
position,
newName,
);
}

public consumeSignatureHelp(registry: atomIde.SignatureHelpRegistry): Disposable {
this._signatureHelpRegistry = registry;
for (const server of this._serverManager.getActiveServers()) {
Expand Down
6 changes: 6 additions & 0 deletions typings/atom-ide/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ declare module 'atom-ide' {
): Promise<CodeAction[] | null>;
}

export interface RefactorProvider {
grammarScopes: string[];
priority: number;
rename?(editor: TextEditor, position: Point, newName: string): Promise<Map<IdeUri, TextEdit[]> | null>;
}

export interface BusySignalOptions {
/**
* Can say that a busy signal will only appear when a given file is open.
Expand Down

0 comments on commit 693b338

Please sign in to comment.