-
Notifications
You must be signed in to change notification settings - Fork 12
/
opener.js
189 lines (159 loc) · 6.36 KB
/
opener.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const vscode = require('vscode');
const shared = require('./shared.js');
function openMain() {
var editor = vscode.window.activeTextEditor;
shared.testNoTab(editor);
var currentFilePath = editor.document.uri.fsPath;
// console.log('The current file = ' + currentFilePath);
if (editor.document.isUntitled) {
vscode.window.showInformationMessage("This doesn't work on files that aren't saved in the GitHub docs repository.");
return;
}
// Use the current selection or cursor, and get the type (i.e. a reusable/feature flag) and value (i.e. the reusable path or the feature flag name)
var selection = editor.selection;
if (selection.isEmpty) {
var typeAndValue = shared.getTypeAndValue(editor);
}
else { typeAndValue = shared.getTypeAndValue(editor.document.getText(selection)); }
// console.log('typeAndValue = ' + typeAndValue);
if (!typeAndValue) {
vscode.window.showInformationMessage("You didn't select a valid reusable, variable, or feature flag.");
return;
} else {
var matchType = typeAndValue[0];
var pathOrName = typeAndValue[1];
}
// console.log('matchType = ' + matchType);
var directorySeparator = "/";
// Detect whether this is Windows
var isWin = process.platform === "win32";
if (isWin) directorySeparator = "\\";
// console.log('isWin = ' + isWin);
// console.log('directorySeparator = ' + directorySeparator);
var filepath = pathOrName;
filepath = filepath.replace(/\./g, directorySeparator);
var regex = new RegExp(".*\\" + directorySeparator + "(docs|docs-internal)\\" + directorySeparator, "g");
var regexmatchArray = currentFilePath.match(regex);
switch (matchType) {
case 'reusable':
var basepath = regexmatchArray[0] + "data" + directorySeparator;
break;
case 'feature':
var basepath = regexmatchArray[0] + "data" + directorySeparator + "features" + directorySeparator;
break;
}
// console.log('basepath = ' + basepath);
if (filepath.indexOf('variables') === 0) {
var isVariable = true;
// Get the variable name at the end of the filepath
regex = new RegExp("\\" + directorySeparator + "([^\\" + directorySeparator + "]*$)");
regexmatchArray = filepath.match(regex);
var variableName = regexmatchArray[1];
// console.log("variableName = " + variableName);
// Remove directorySeparator + variableName from the end of filepath:
filepath = filepath.replace(new RegExp("\\" + directorySeparator + variableName + '$'), '');
filepath = basepath + filepath + ".yml";
}
else {
switch (matchType) {
case 'reusable':
filepath = basepath + filepath + ".md";
break;
case 'feature':
filepath = basepath + filepath + ".yml";
break;
}
}
filepath = decodeURIComponent(filepath);
// console.log('Path of file to open = ' + filepath);
vscode.workspace.openTextDocument(filepath).then(doc => {
return vscode.window.showTextDocument(doc).then(e => {
e.edit(editObject => {
if (isVariable) findLineNumberOfVariable(variableName);
});
});
}, (err) => {
vscode.window.showErrorMessage("File not found: " + filepath);
});
}
function findLineNumberOfVariable(variableName) {
var neweditor = vscode.window.activeTextEditor; // The newly opened document
var lineNumberOfVariable = 0;
var currentCursorLineNumber = neweditor.selection.active.line + 1;
var matchResultArray;
let lineCount = neweditor.document.lineCount;
for (let parseLine = 0; parseLine < lineCount; parseLine++) {
let lineText = neweditor.document.lineAt(parseLine);
let regex = new RegExp(variableName + ":");
matchResultArray = lineText.text.match(regex);
if (matchResultArray) {
lineNumberOfVariable = parseLine + 1;
// console.log("'" + variableName + ":' matched on line: " + lineNumberOfVariable);
moveCursor(currentCursorLineNumber, lineNumberOfVariable);
centralizar(lineNumberOfVariable);
return;
}
}
if (!matchResultArray) {
// Move the cursor to line 1 of the variables file
vscode.commands.executeCommand("cursorMove", {
to: "up",
by: "line",
value: currentCursorLineNumber
})
vscode.window.showInformationMessage("'" + variableName + "' isn't defined in this file.");
}
}
function centralizar(lineToCenter) {
// Put the current line as close as possible to the centre of the visible area in the editor
vscode.commands.executeCommand("revealLine", {
lineNumber: lineToCenter,
at: "center"
});
}
function moveCursor(currentCursorLineNumber, targetLineNumber) {
// console.log("Move the cursor from its current position on line: " + currentCursorLineNumber);
var errorPreamble = "Something is wrong with the cursorMove: ";
// The executeCommand("cursorMove") command only works relative to the current cursor position
// and doesn't allow you to specify the line you want to move to.
// So we get around this by moving the cursor to the start of the document
// and then down to the line we've already determined contains the variable.
// Move the cursor to line 1 of the variables file
vscode.commands.executeCommand("cursorMove", {
to: "up",
by: "line",
value: currentCursorLineNumber
})
.then(undefined, err => {
console.error(errorPreamble + "up. " + err);
});
// Move the cursor down to the desired line
vscode.commands.executeCommand("cursorMove", {
to: "down",
by: "line",
value: targetLineNumber -1
})
.then(undefined, err => {
console.error(errorPreamble + "down. " + err);
});
// The select property allows you to select everything between the current cursor position
// and the position you've moved the cursor to.
// So to select the whole line we first move the cursor to the start of the current line.
// And then to the end of the line, including the select property
vscode.commands.executeCommand("cursorMove", {
to: "wrappedLineStart"
})
.then(undefined, err => {
console.error(errorPreamble + "wrappedLineStart. " + err);
});
vscode.commands.executeCommand("cursorMove", {
to: "wrappedLineEnd",
select: true
})
.then(undefined, err => {
console.error(errorPreamble + "wrappedLineEnd. " + err);
});
}
module.exports = {
openMain
};