-
Notifications
You must be signed in to change notification settings - Fork 1
/
vscode.js
99 lines (91 loc) · 2.7 KB
/
vscode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const selection = require("./selection");
const message = require("./message");
function _handleOpenVSCodeLocation() {
var element = selection.getSelectedElement(true);
if (!element) {
return;
}
_openVSCodeLocation(element);
}
function _openVSCodeLocation(element) {
// read the first line of the doc and parse into 2 parts
var doc = element.documentation;
var first = doc.split("\n")[0];
if (!first.startsWith("->")) {
// look at the parent if we don't have an instruction
if (element._parent) {
_openVSCodeLocation(element._parent);
return;
}
message.warning("No VSCode navigation instruction! Form is ->file:pattern");
return;
}
var startPort = app.preferences.get("codestar.vscodeStartPort");
var endPort = app.preferences.get("codestar.vscodeEndPort");
var parts = first.substring(2).split(":");
var file = parts[0];
var pattern = parts[1];
if (!file) {
message.warning("No file specified in navigation instruction");
return;
}
for (var port = startPort; port <= endPort; port++) {
$.ajax({
type: "POST",
url: "http://127.0.0.1:" + port + "/setLocation",
data: JSON.stringify({
file: file,
pattern: pattern
}),
success: function(data) {},
error: function(err) {},
dataType: "json"
});
}
}
function _handleSaveVSCodeLocation() {
var element = selection.getSelectedElement(true);
_saveVSCodeLocation(element);
}
function _saveVSCodeLocation(element) {
if (!element) {
return;
}
var startPort = app.preferences.get("codestar.vscodeStartPort");
var endPort = app.preferences.get("codestar.vscodeEndPort");
var latest = null;
var savedPort = null;
for (var port = startPort; port <= endPort; port++) {
function ajaxCall(saved) {
$.ajax({
type: "GET",
url: "http://127.0.0.1:" + port + "/getLocation",
success: function(data) {
var gotFocus = new Date(data.lastGotFocus);
if (!latest || gotFocus > latest) {
element.documentation = "->" + data.file + ":" + data.pattern;
savedPort = saved;
latest = gotFocus;
}
if (saved == endPort) {
reportSuccess();
}
},
error: function(err) {
if (saved == endPort) {
reportSuccess();
}
},
dataType: "json"
});
}
ajaxCall(port);
}
}
function reportSuccess() {
message.info("Saved linked VSCode location");
}
exports._handleSaveVSCodeLocation = _handleSaveVSCodeLocation;
exports._saveVSCodeLocation = _saveVSCodeLocation;
exports._handleOpenVSCodeLocation = _handleOpenVSCodeLocation;
exports._openVSCodeLocation = _openVSCodeLocation;