Skip to content

Commit 6b50753

Browse files
author
dean
committed
add return-to-declaration functionality
1 parent 32ee7be commit 6b50753

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

dist/main/atom/commands/goToDeclaration.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ const tslib_1 = require("tslib");
33
const registry_1 = require("./registry");
44
const utils_1 = require("../utils");
55
const simpleSelectionView_1 = require("../views/simpleSelectionView");
6+
const prevCursorPositions = [];
7+
function open(item) {
8+
atom.workspace.open(item.file, {
9+
initialLine: item.start.line - 1,
10+
initialColumn: item.start.offset - 1
11+
});
12+
}
613
registry_1.commands.set("typescript:go-to-declaration", deps => {
714
return (e) => tslib_1.__awaiter(this, void 0, void 0, function* () {
815
if (!utils_1.commandForTypeScript(e)) {
@@ -11,6 +18,7 @@ registry_1.commands.set("typescript:go-to-declaration", deps => {
1118
const location = utils_1.getFilePathPosition();
1219
const client = yield deps.getClient(location.file);
1320
const result = yield client.executeDefinition(location);
21+
prevCursorPositions.push(location);
1422
if (result.body.length > 1) {
1523
simpleSelectionView_1.simpleSelectionView({
1624
items: result.body,
@@ -27,11 +35,18 @@ registry_1.commands.set("typescript:go-to-declaration", deps => {
2735
else {
2836
open(result.body[0]);
2937
}
30-
function open(item) {
31-
atom.workspace.open(item.file, {
32-
initialLine: item.start.line - 1,
33-
initialColumn: item.start.offset - 1
34-
});
38+
});
39+
});
40+
registry_1.commands.set("typescript:return-from-declaration", deps => {
41+
return (e) => tslib_1.__awaiter(this, void 0, void 0, function* () {
42+
const position = prevCursorPositions.pop();
43+
if (!position) {
44+
atom.notifications.addInfo('AtomTS: Previous position not found.');
45+
return;
3546
}
47+
open({
48+
file: position.file,
49+
start: { line: position.line, offset: position.offset }
50+
});
3651
});
3752
});

keymaps/atom-typescript.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# Stronger bindings
1313
'atom-workspace':
1414
'f6': 'typescript:build'
15+
'f10': 'typescript:return-from-declaration'
1516
'f12': 'typescript:go-to-declaration'
1617
'ctrl-\'': 'typescript:sync'
1718
'cmd-\'': 'typescript:sync'

lib/main/atom/commands/goToDeclaration.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import {commands} from "./registry"
22
import {commandForTypeScript, getFilePathPosition} from "../utils"
33
import {simpleSelectionView} from "../views/simpleSelectionView"
44

5+
const prevCursorPositions:any[] = [];
6+
7+
function open(item: {file: string, start: {line: number, offset: number}}) {
8+
atom.workspace.open(item.file, {
9+
initialLine: item.start.line - 1,
10+
initialColumn: item.start.offset - 1
11+
})
12+
}
13+
514
commands.set("typescript:go-to-declaration", deps => {
615
return async e => {
716
if (!commandForTypeScript(e)) {
@@ -12,6 +21,8 @@ commands.set("typescript:go-to-declaration", deps => {
1221
const client = await deps.getClient(location.file)
1322
const result = await client.executeDefinition(location)
1423

24+
prevCursorPositions.push(location);
25+
1526
if (result.body!.length > 1) {
1627
simpleSelectionView({
1728
items: result.body!,
@@ -27,12 +38,19 @@ commands.set("typescript:go-to-declaration", deps => {
2738
} else {
2839
open(result.body![0])
2940
}
30-
31-
function open(item: {file: string, start: {line: number, offset: number}}) {
32-
atom.workspace.open(item.file, {
33-
initialLine: item.start.line - 1,
34-
initialColumn: item.start.offset - 1
35-
})
36-
}
3741
}
38-
})
42+
});
43+
44+
commands.set("typescript:return-from-declaration", deps => {
45+
return async e => {
46+
const position = prevCursorPositions.pop();
47+
if (!position) {
48+
atom.notifications.addInfo('AtomTS: Previous position not found.');
49+
return;
50+
}
51+
open({
52+
file: position.file,
53+
start: { line: position.line, offset: position.offset }
54+
});
55+
}
56+
});

menus/atomts-menus.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'submenu': [
88
{ 'label': 'Build', 'command': 'typescript:build' }
99
{ 'label': 'Go To Declaration', 'command': 'typescript:go-to-declaration' }
10+
{ 'label': 'Return From Declaration', 'command': 'typescript:return-from-declaration' }
1011
]
1112
]
1213
}

0 commit comments

Comments
 (0)