This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for rename refactors (#270)
closes #13
- Loading branch information
1 parent
3f44ceb
commit 693b338
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters