Skip to content

Commit b61e6a2

Browse files
committed
get rename info query
refs #59
1 parent 10edf6f commit b61e6a2

12 files changed

+126
-15
lines changed

keymaps/atom-typescript.cson

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
'cmd-b': 'typescript:go-to-declaration'
88
'ctrl-;': 'typescript:context-actions'
99
'cmd-;': 'typescript:context-actions'
10+
'f2': 'typescript:rename-variable'

lib/main/atom/atomUtils.js

+7
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ function onDiskAndTs(editor) {
2323
return false;
2424
}
2525
exports.onDiskAndTs = onDiskAndTs;
26+
function getFilePathPosition() {
27+
var editor = atom.workspace.getActiveTextEditor();
28+
var filePath = editor.getPath();
29+
var position = getEditorPosition(editor);
30+
return { filePath: filePath, position: position };
31+
}
32+
exports.getFilePathPosition = getFilePathPosition;

lib/main/atom/atomUtils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ export function onDiskAndTs(editor: AtomCore.IEditor) {
3030
}
3131
return false;
3232
}
33+
34+
export function getFilePathPosition(): { filePath: string; position: number } {
35+
var editor = atom.workspace.getActiveTextEditor();
36+
var filePath = editor.getPath();
37+
var position = getEditorPosition(editor);
38+
return { filePath, position };
39+
}

lib/main/atom/commands.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,20 @@ function registerCommands() {
8383
});
8484
atom.commands.add('atom-text-editor', 'typescript:rename-variable', function (e) {
8585
atom.notifications.addInfo('coming soon. UI test only');
86-
renameView.panelView.renameThis({
87-
text: 'someText',
88-
onCancel: function () { return console.log('cancel'); },
89-
onCommit: function (newText) { return console.log(newText); }
86+
parent.getRenameInfo(atomUtils.getFilePathPosition()).then(function (res) {
87+
if (!res.canRename) {
88+
atom.notifications.addInfo('AtomTS: Rename not available at cursor location');
89+
return;
90+
}
91+
renameView.panelView.renameThis({
92+
text: res.displayName,
93+
onCancel: function () {
94+
console.log('cancel');
95+
},
96+
onCommit: function (newText) {
97+
console.log(newText);
98+
}
99+
});
90100
});
91101
});
92102
}

lib/main/atom/commands.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,30 @@ export function registerCommands() {
112112
atom.commands.add('atom-text-editor', 'typescript:rename-variable',(e) => {
113113
// TODO: get text
114114
atom.notifications.addInfo('coming soon. UI test only');
115-
renameView.panelView.renameThis({
116-
text: 'someText',
117-
onCancel: () => console.log('cancel'),
118-
onCommit: (newText) => console.log(newText)
115+
116+
/*function restoreEditor() {
117+
// TODO: if (activePane.isDestroyed()) return;
118+
119+
var v = atom.views.getView(activeEditor);
120+
v.focus();
121+
}*/
122+
parent.getRenameInfo(atomUtils.getFilePathPosition()).then((res) => {
123+
if (!res.canRename) {
124+
atom.notifications.addInfo('AtomTS: Rename not available at cursor location');
125+
return;
126+
}
127+
renameView.panelView.renameThis({
128+
text: res.displayName,
129+
onCancel: () => {
130+
console.log('cancel');
131+
},
132+
onCommit: (newText) => {
133+
console.log(newText);
134+
}
135+
});
119136
});
137+
138+
120139
});
121140

122141
/// Register autocomplete commands to show documentations

lib/main/atom/views/renameView.js

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var RenameView = (function (_super) {
1111
__extends(RenameView, _super);
1212
function RenameView() {
1313
_super.apply(this, arguments);
14+
this.editorAtRenameStart = null;
1415
}
1516
RenameView.prototype.init = function () {
1617
var _this = this;
@@ -38,11 +39,17 @@ var RenameView = (function (_super) {
3839
});
3940
};
4041
RenameView.prototype.clearView = function () {
42+
if (this.editorAtRenameStart && !this.editorAtRenameStart.isDestroyed()) {
43+
var view = atom.views.getView(this.editorAtRenameStart);
44+
view.focus();
45+
}
4146
panel.hide();
4247
this.options = {};
48+
this.editorAtRenameStart = null;
4349
};
4450
RenameView.prototype.renameThis = function (options) {
4551
this.options = options;
52+
this.editorAtRenameStart = atom.workspace.getActiveEditor();
4653
panel.show();
4754
this.newNameEditor.model.setText(options.text);
4855
this.newNameEditor.model.selectAll();

lib/main/atom/views/renameView.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ export class RenameView
4444
});
4545
}
4646

47+
public editorAtRenameStart:AtomCore.IEditor = null;
4748
public clearView() {
49+
if(this.editorAtRenameStart && !this.editorAtRenameStart.isDestroyed()){
50+
var view = atom.views.getView(this.editorAtRenameStart);
51+
view.focus();
52+
}
4853
panel.hide();
4954
this.options = <any>{};
55+
this.editorAtRenameStart = null;
5056
}
5157

5258
public renameThis(options: RenameViewOptions) {
53-
5459
this.options = options;
60+
this.editorAtRenameStart = atom.workspace.getActiveEditor();
5561
panel.show();
5662
this.newNameEditor.model.setText(options.text);
5763
this.newNameEditor.model.selectAll();

lib/main/lang/projectService.js

+20
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,23 @@ function errorsForFile(query) {
183183
return { errors: diagnostics.map(project.diagnosticToTSError) };
184184
}
185185
exports.errorsForFile = errorsForFile;
186+
function getRenameInfo(query) {
187+
var project = getOrCreateProject(query.filePath);
188+
var info = project.languageService.getRenameInfo(query.filePath, query.position);
189+
if (info && info.canRename) {
190+
return {
191+
canRename: true,
192+
localizedErrorMessage: info.localizedErrorMessage,
193+
displayName: info.displayName,
194+
fullDisplayName: info.fullDisplayName,
195+
kind: info.kind,
196+
kindModifiers: info.kindModifiers,
197+
};
198+
}
199+
else {
200+
return {
201+
canRename: false
202+
};
203+
}
204+
}
205+
exports.getRenameInfo = getRenameInfo;

lib/main/lang/projectService.ts

+31
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,34 @@ export function errorsForFile(query: FilePathQuery): {
309309

310310
return { errors: diagnostics.map(project.diagnosticToTSError) };
311311
}
312+
313+
export interface GetRenameInfoQuery extends FilePathPositionQuery { }
314+
export interface GetRenameInfoResponse {
315+
canRename: boolean;
316+
localizedErrorMessage?: string;
317+
displayName?: string;
318+
fullDisplayName?: string; // this includes the namespace name
319+
kind?: string;
320+
kindModifiers?: string;
321+
// TODO: add text span information
322+
}
323+
export function getRenameInfo(query: GetRenameInfoQuery): GetRenameInfoResponse {
324+
var project = getOrCreateProject(query.filePath);
325+
var info = project.languageService.getRenameInfo(query.filePath,query.position);
326+
if(info && info.canRename){
327+
return {
328+
canRename: true,
329+
localizedErrorMessage: info.localizedErrorMessage,
330+
displayName: info.displayName,
331+
fullDisplayName: info.fullDisplayName,
332+
kind: info.kind,
333+
kindModifiers: info.kindModifiers,
334+
// TODO: use info.triggerSpan
335+
}
336+
}
337+
else{
338+
return {
339+
canRename: false
340+
}
341+
}
342+
}

lib/typings/atom/atom.d.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ declare module AtomCore {
540540
subscribeToBuffer():void;
541541
subscribeToDisplayBuffer():void;
542542
getViewClass():any; // return type are EditorView
543-
destroyed():void;
543+
isDestroyed():boolean;
544544
copy():IEditor;
545545
getTitle():string;
546546
getLongTitle():string;
@@ -777,6 +777,7 @@ declare module AtomCore {
777777
deserializeParams(params:any):any;
778778
getViewClass():any; // return type are PaneView
779779
isActive():boolean;
780+
isDestroyed():boolean;
780781
focus():void;
781782
blur():void;
782783
activate():void;
@@ -890,14 +891,14 @@ declare module AtomCore {
890891
interface IWorkspaceStatic {
891892
new():IWorkspace;
892893
}
893-
894+
894895
interface IWorkspacePanelOptions{
895896
item:any;
896897
visible?:boolean;
897898
priority?:number;
898899
}
899-
900-
interface Panel{
900+
901+
interface Panel{
901902
getItem():any;
902903
getPriority():any;
903904
isVisible():boolean;
@@ -911,7 +912,7 @@ declare module AtomCore {
911912
addRightPanel(options:IWorkspacePanelOptions):Panel;
912913
addTopPanel(options:IWorkspacePanelOptions):Panel;
913914
addModalPanel(options:IWorkspacePanelOptions):Panel;
914-
915+
915916
deserializeParams(params:any):any;
916917
serializeParams():{paneContainer:any;fullScreen:boolean;};
917918
eachEditor(callback: Function): void;
@@ -944,7 +945,7 @@ declare module AtomCore {
944945
itemOpened(item:any):void;
945946
onPaneItemDestroyed(item:any):void;
946947
destroyed():void;
947-
948+
948949
onDidChangeActivePaneItem(item:any):Disposable;
949950
}
950951

lib/worker/parent.js

+1
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,4 @@ exports.getDefinitionsAtPosition = childQuery(projectService.getDefinitionsAtPos
125125
exports.updateText = childQuery(projectService.updateText);
126126
exports.errorsForFile = childQuery(projectService.errorsForFile);
127127
exports.getSignatureHelps = childQuery(projectService.getSignatureHelps);
128+
exports.getRenameInfo = childQuery(projectService.getRenameInfo);

lib/worker/parent.ts

+1
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,4 @@ export var getDefinitionsAtPosition = childQuery(projectService.getDefinitionsAt
165165
export var updateText = childQuery(projectService.updateText);
166166
export var errorsForFile = childQuery(projectService.errorsForFile);
167167
export var getSignatureHelps = childQuery(projectService.getSignatureHelps);
168+
export var getRenameInfo = childQuery(projectService.getRenameInfo);

0 commit comments

Comments
 (0)