Skip to content

add return-to-declaration functionality on the tsserver branch #1192

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

Merged
merged 2 commits into from
Mar 19, 2017
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
30 changes: 24 additions & 6 deletions dist/main/atom/commands/goToDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const tslib_1 = require("tslib");
const registry_1 = require("./registry");
const utils_1 = require("../utils");
const simpleSelectionView_1 = require("../views/simpleSelectionView");
const prevCursorPositions = [];
function open(item) {
atom.workspace.open(item.file, {
initialLine: item.start.line - 1,
initialColumn: item.start.offset - 1
});
}
registry_1.commands.set("typescript:go-to-declaration", deps => {
return (e) => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!utils_1.commandForTypeScript(e)) {
Expand All @@ -21,17 +28,28 @@ registry_1.commands.set("typescript:go-to-declaration", deps => {
`;
},
filterKey: 'filePath',
confirmed: item => open(item)
confirmed: item => {
prevCursorPositions.push(location);
open(item);
}
});
}
else {
prevCursorPositions.push(location);
open(result.body[0]);
}
function open(item) {
atom.workspace.open(item.file, {
initialLine: item.start.line - 1,
initialColumn: item.start.offset - 1
});
});
});
registry_1.commands.set("typescript:return-from-declaration", deps => {
return (e) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const position = prevCursorPositions.pop();
if (!position) {
atom.notifications.addInfo('AtomTS: Previous position not found.');
return;
}
open({
file: position.file,
start: { line: position.line, offset: position.offset }
});
});
});
1 change: 1 addition & 0 deletions keymaps/atom-typescript.cson
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# Stronger bindings
'atom-workspace':
'f6': 'typescript:build'
'f10': 'typescript:return-from-declaration'
'f12': 'typescript:go-to-declaration'
'ctrl-\'': 'typescript:sync'
'cmd-\'': 'typescript:sync'
Expand Down
40 changes: 30 additions & 10 deletions lib/main/atom/commands/goToDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import {commands} from "./registry"
import {commandForTypeScript, getFilePathPosition} from "../utils"
import {commandForTypeScript, getFilePathPosition, FileLocationQuery} from "../utils"
import {simpleSelectionView} from "../views/simpleSelectionView"

const prevCursorPositions:FileLocationQuery[] = [];

function open(item: {file: string, start: {line: number, offset: number}}) {
atom.workspace.open(item.file, {
initialLine: item.start.line - 1,
initialColumn: item.start.offset - 1
})
}

commands.set("typescript:go-to-declaration", deps => {
return async e => {
if (!commandForTypeScript(e)) {
Expand All @@ -22,17 +31,28 @@ commands.set("typescript:go-to-declaration", deps => {
`
},
filterKey: 'filePath',
confirmed: item => open(item)
confirmed: item => {
prevCursorPositions.push(location);
open(item)
}
})
} else {
prevCursorPositions.push(location);
open(result.body![0])
}

function open(item: {file: string, start: {line: number, offset: number}}) {
atom.workspace.open(item.file, {
initialLine: item.start.line - 1,
initialColumn: item.start.offset - 1
})
}
}
})
});

commands.set("typescript:return-from-declaration", deps => {
return async e => {
const position = prevCursorPositions.pop();
if (!position) {
atom.notifications.addInfo('AtomTS: Previous position not found.');
return;
}
open({
file: position.file,
start: { line: position.line, offset: position.offset }
});
Copy link
Contributor

Choose a reason for hiding this comment

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

The new object you're creating here has the exact same shape as position local, so there's no need for that. You could pass it directly, though you'll see that it's not what open expects.

Copy link
Author

Choose a reason for hiding this comment

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

Not completely sure what you mean here. position doesn't have a start attribute, it looks like:

{
    file: string,
    line: number,
    offset: number
}

Open expect the line and offset to be children of start, which is why I wrote it out like that. Are you saying to just change the definition of open to correspond with the position argument?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, gotcha, I didn't see the start bit. Carry on 😆

}
});
1 change: 1 addition & 0 deletions menus/atomts-menus.cson
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'submenu': [
{ 'label': 'Build', 'command': 'typescript:build' }
{ 'label': 'Go To Declaration', 'command': 'typescript:go-to-declaration' }
{ 'label': 'Return From Declaration', 'command': 'typescript:return-from-declaration' }
]
]
}
Expand Down