-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
122 lines (98 loc) · 3.61 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var fs = require('fs');
const path = require('path');
const vscode = require('vscode');
function get_file_and_folder_name(file) {
let parsed = path.parse(file);
return { dir: path.dirname(file), name: parsed.name, name_w_extension: parsed.name + parsed.ext }
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Header / Source Side-By-Side is installed and active!');
let hs_switch = vscode.commands.registerCommand('header-source-side-by-side.switch_file_in_same_window', function () {
let active_doc = vscode.window.activeTextEditor;
let ff = get_file_and_folder_name(active_doc.document.fileName);
fs.readdir(ff.dir, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
let split = file.split(".");
if (split.length < 1)
return;
let name = split[0];
let extension = split[split.length - 1];
if (name == ff.name && file != ff.name_w_extension) {
console.log("true " + file + " ff " + ff.name_w_extension);
// Open Editor
vscode.workspace.openTextDocument(ff.dir + "/" + file).then(file => {
vscode.window.showTextDocument(file);
})
}
});
});
});
// workbench.action.moveEditorToPreviousGroup
let side_by_side = vscode.commands.registerCommand('header-source-side-by-side.side-by-side', function () {
let current_open_docs = vscode.window.visibleTextEditors;
let active_doc = vscode.window.activeTextEditor;
let ff = get_file_and_folder_name(active_doc.document.fileName);
let active_column = active_doc.viewColumn;
let total_columns = vscode.window.visibleTextEditors.length;
if (active_column > 2)
active_column = 1;
fs.readdir(ff.dir, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
let split = file.split(".");
if (split.length < 1)
return;
let name = split[0];
let extension = split[split.length - 1];
if (name == ff.name && file != ff.name_w_extension) {
console.log("true " + file + " ff " + ff.name_w_extension);
let open_columns = vscode.window.visibleTextEditors;
let found_visible_column_with_winning_file = false;
for (let i = 0; i < open_columns.length; i++) {
const column = open_columns[i];
if (column.document.fileName == ff.dir + "/" + file) {
// skip this one because it's our current one
if (i == active_column)
continue;
// we found a file in editor id (i) switch to (i) editor.
console.log(active_column);
console.log(i);
if (i < active_column)
vscode.commands.executeCommand('workbench.action.focusFirstEditorGroup')
else if (i > active_column)
vscode.commands.executeCommand('workbench.action.focusSecondEditorGroup')
found_visible_column_with_winning_file = true;
}
}
// skip opening a new editor if we found one
if (found_visible_column_with_winning_file)
return;
// Open Editor
if (active_column == 2)
vscode.commands.executeCommand('workbench.action.focusFirstEditorGroup')
else if (active_column == 1)
vscode.commands.executeCommand('workbench.action.focusSecondEditorGroup')
vscode.workspace.openTextDocument(ff.dir + "/" + file).then(file => {
vscode.window.showTextDocument(file);
})
}
});
});
});
context.subscriptions.push(side_by_side);
context.subscriptions.push(hs_switch);
}
// this method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}