-
Notifications
You must be signed in to change notification settings - Fork 8
/
ruby_erb.js
140 lines (119 loc) · 4.79 KB
/
ruby_erb.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
const vscode = require('vscode');
const erb_regex = /<%(=?|-?|#?)\s.*(-?)%>/;
const erb_opener_regex = /<%[\=\#\-]?/;
const erb_closer_regex = /-?%>/;
const erb_blocks = [
['<%=', '%>'],
['<%', '%>'],
['<%#', '%>']
];
function activate(context) {
const subs = context.subscriptions
subs.push(vscode.commands.registerCommand('erb.toggleTags', function () {
let editor = vscode.window.activeTextEditor;
if (editor) {
toggleTags(editor);
}
}));
}
exports.activate = activate;
function deactivate() {
}
exports.deactivate = deactivate;
function findSorroundingTags(text) {
return [text.match(erb_opener_regex)[0], text.match(erb_closer_regex)[0]];
}
function insertErbTags(text) {
return `${erb_blocks[0][0]} ${text} ${erb_blocks[0][1]}`;
}
function replaceErbTags(text) {
let tags = findSorroundingTags(text);
let next_tags = getNextErbTags(tags);
return text.replace(tags[0], next_tags[0]).replace(tags[1], next_tags[1]);
}
function getNextErbTags(tags) {
let tags_str = JSON.stringify(tags);
for (let i = 0; i < erb_blocks.length; i++) {
if (JSON.stringify(erb_blocks[i]) == tags_str) {
if (i+1 >= erb_blocks.length) {
return erb_blocks[0];
} else {
return erb_blocks[i+1]
}
}
}
return erb_blocks[0]
}
function getSelectionRange(selection, editor) {
let line = editor.document.lineAt(selection.start);
let selected_text = editor.document.getText(selection);
let new_selection = new vscode.Selection(selection.start, selection.end);
let start_position = new_selection.start;
let end_position = new_selection.end;
let opener_position = [];
let closer_position = [];
while (start_position.character > line.firstNonWhitespaceCharacterIndex) {
start_position = new vscode.Position(line.lineNumber, new_selection.start.character - 1);
new_selection = new vscode.Selection(start_position, end_position);
if (editor.document.getText(new_selection).match(erb_opener_regex)){
opener_position.push(start_position);
break;
}
}
while (end_position.character < line.range.end.character) {
end_position = new vscode.Position(line.lineNumber, new_selection.end.character + 1);
new_selection = new vscode.Selection(start_position, end_position);
if (editor.document.getText(new_selection).match(erb_closer_regex)){
closer_position.push(end_position);
break;
}
}
if (opener_position.length > 0 && closer_position.length > 0) {
return new vscode.Range(new_selection.start, new_selection.end);
} else if ( selection.isEmpty && editor.document.getText(selection).trim().length === 0 && line.isEmptyOrWhitespace) {
start_position = new vscode.Position(selection.start.line, line.firstNonWhitespaceCharacterIndex);
end_position = line.range.end;
return new vscode.Range(start_position, end_position);
}
return new vscode.Range(selection.start, selection.end);
}
function toggleTags(editor) {
let selections_map = {};
let new_selections = [];
let line_offset = 0;
let selections = editor.selections.filter(function(selection) { return selection.isSingleLine });
// Push selections to selections_map grouped by line
selections.forEach(function (selection) {
return selections_map[selection.start.line] ? selections_map[selection.start.line].push(selection) : selections_map[selection.start.line] = [selection];
});
editor.edit(function(editBuilder) {
for (let key in selections_map) {
if (selections_map.hasOwnProperty(key)) {
line_offset = 0;
// Order selections by ltr position
selections_map[key] = selections_map[key].sort(function (first, second) { return first.end.isBefore(second.start) ? -1 : 1; });
selections_map[key].forEach(function(selection) {
let selectedRange = getSelectionRange(selection, editor);
let selected_text = editor.document.getText(selectedRange);
let new_text;
let new_selection;
if (selected_text.match(erb_regex)) {
new_text = replaceErbTags(selected_text);
} else {
new_text = insertErbTags(selected_text);
}
let delta = new_text.length - selected_text.length;
if (selected_text.trim().length == 0) {
new_selection = new vscode.Selection(selection.start.line, selection.end.character + delta - 3, selection.end.line, selection.end.character + delta - 3);
} else {
new_selection = new vscode.Selection(selection.start.line, selection.start.character + line_offset, selection.end.line, selection.end.character + line_offset + delta);
}
new_selections.push(new_selection);
line_offset += delta;
editBuilder.replace(selectedRange, new_text);
});
}
}
});
editor.selections = new_selections;
}