-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
160 lines (135 loc) · 4.29 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
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const parser = require('scss-parser');
let decorationsActive = true
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('scss-scope-helper activated');
const decorationType = vscode.window.createTextEditorDecorationType({after: {margin: '0 0 0 1rem'}});
let activeEditor = vscode.window.activeTextEditor;
let timeout;
function updateDecorations() {
if (!activeEditor || !decorationsActive) {
return;
}
if(!isBalancedParenthesis(activeEditor.document.getText())) {
activeEditor.setDecorations(decorationType, [])
console.log('scss-scope-helper: unbalanced brackets!')
return
}
const regEx = /(.*{$)/gm;
const text = activeEditor.document.getText();
const found = [];
let match;
while (match = regEx.exec(text)) {
let text = match;
const start = findClosingBracket(activeEditor.document.getText(), match.index + match[0].length - 1)
const end = start + 1
const contentText = '// ' + match[0].substring(0, match[0].length - 1)
const decoration = {
range: new vscode.Range(activeEditor.document.positionAt(start), activeEditor.document.positionAt(start + 1)),
// hoverMessage: 'Number **' + match[0] + '**',
renderOptions: {
light: {
after: {
contentText,
color: 'rgba(0,0,0,.3)'
}
},
dark: {
after: {
contentText,
color: 'rgba(255,255,255,.3)'
}
}
},
};
found.push(decoration);
}
activeEditor.setDecorations(decorationType, found);
}
function triggerUpdateDecorations() {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
timeout = setTimeout(updateDecorations, 500);
}
// On startup
if (activeEditor) {
triggerUpdateDecorations();
}
vscode.window.onDidChangeActiveTextEditor(editor => {
activeEditor = editor;
if (editor) {
triggerUpdateDecorations();
}
}, null, context.subscriptions);
vscode.workspace.onDidChangeTextDocument(event => {
console.log('CHANGE')
if (activeEditor && event.document === activeEditor.document) {
triggerUpdateDecorations();
}
}, null, context.subscriptions);
context.subscriptions.push(vscode.commands.registerCommand('scss-scope-helper.show', function() {
decorationsActive = true
triggerUpdateDecorations()
}));
context.subscriptions.push(vscode.commands.registerCommand('scss-scope-helper.hide', function() {
decorationsActive = false
activeEditor.setDecorations(decorationType, [])
}));
}
// this method is called when your extension is deactivated
function deactivate() {}
// https://www.geeksforgeeks.org/find-index-closing-bracket-given-opening-bracket-expression/
function findClosingBracket(expression, index) {
let i;
// If index given is invalid and is
// not an opening bracket.
if (expression[index] !== '{') {
// console.log(expression + ", " + index + ": -1\n");
return -1;
}
// Stack to store opening brackets.
let st = [];
// Traverse through string starting from
// given index.
for (i = index; i < expression.length; i++) {
// If current character is an
// opening bracket push it in stack.
if (expression[i] === '{') {
st.push(expression[i]);
} // If current character is a closing
// bracket, pop from stack. If stack
// is empty, then this closing
// bracket is required bracket.
else if (expression[i] === '}') {
st.pop();
if (st.length === 0) {
// console.log(expression + ", " + index + ": " + i + "\n");
return i;
}
}
}
}
// https://rohan-paul.github.io/javascript/2018/05/25/Parenthesis-Matching-Problem-in-JavaScript/
function isBalancedParenthesis(str) {
return !str.split('').reduce((uptoPrevChar, thisChar) => {
if(thisChar === '(' || thisChar === '{' || thisChar === '[' ) {
return ++uptoPrevChar;
} else if (thisChar === ')' || thisChar === '}' || thisChar === ']') {
return --uptoPrevChar;
}
return uptoPrevChar
}, 0);
}
module.exports = {
activate,
deactivate
}