-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
105 lines (96 loc) · 2.87 KB
/
extension.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
100
101
102
103
104
105
var vscode = require("vscode");
var history = [];
var historyPosition = 0;
var historyMaxSize = 50;
var lastEditLocation;
var editGroupTimeThreshold = 500;
var lastEditTime;
function _onEvent(e) {
var lastChangeTooRecent = Date.now() - lastEditTime < editGroupTimeThreshold;
var isOnSameLine =
lastEditLocation &&
getEventPosition(e).range.end.line === lastEditLocation.position.end.line;
if (!lastChangeTooRecent && !isOnSameLine && isValidEditFile(e)) {
saveChangePosition(e);
}
}
function isValidEditFile(e) {
var isGitRelatedChange = e.document.uri._scheme === "git";
var isOutpuTabChange = e.document.uri.scheme === "output";
return !isOutpuTabChange && !isGitRelatedChange;
}
function getEventPosition(event) {
return event.contentChanges[event.contentChanges.length - 1];
}
function saveChangePosition(event) {
lastEditLocation = {
position: getEventPosition(event).range,
file: event.document.uri.path
};
lastEditTime = Date.now();
if (history.length >= historyMaxSize) {
var elementsToRemove = history.length - historyMaxSize + 1;
history.splice(0, elementsToRemove);
}
history.push(lastEditLocation);
historyPosition = history.length;
}
function scrollToLocation(editor, edit) {
editor.revealRange(edit.position);
editor.selection = new vscode.Selection(
edit.position.start,
edit.position.end
);
}
function navigate(goingBack) {
if (history.length > 0) {
lastEditLocation = goingBack
? history[historyPosition - 1]
: history[historyPosition + 1];
if (lastEditLocation) {
historyPosition = goingBack ? historyPosition - 1 : historyPosition + 1;
} else {
lastEditLocation = goingBack ? history[0] : history[history.length - 1];
}
openOrShowLocation(lastEditLocation);
vscode.window.setStatusBarMessage(
`Edit ${historyPosition + 1} of ${history.length}`,
1500
);
}
}
function openOrShowLocation(location) {
var lastEditEditorFound = vscode.workspace.textDocuments.find(function(item) {
return item.fileName === location.file;
});
if (lastEditEditorFound) {
vscode.window.showTextDocument(lastEditEditorFound).then(function(editor) {
scrollToLocation(editor, location);
});
} else {
vscode.workspace
.openTextDocument(location.file)
.then(vscode.window.showTextDocument)
.then(function(editor) {
scrollToLocation(editor, location);
});
}
}
module.exports = {
activate: function(context) {
var disposable = vscode.workspace.onDidChangeTextDocument(_onEvent);
var goBack = vscode.commands.registerCommand(
"gotoLastEdit.goBack",
function() {
navigate(true);
}
);
var goForward = vscode.commands.registerCommand(
"gotoLastEdit.goForward",
function() {
navigate(false);
}
);
context.subscriptions.push(disposable, goBack, goForward);
}
};