-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
203 lines (164 loc) · 5.22 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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");
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
const configuration = vscode.workspace.getConfiguration("remToPxComment");
// this is only initiated when we open the app for the first time.
let decorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: `${configuration.lineBgColor}20`,
after: { margin: "0 0 0 1rem" },
isWholeLine: true,
});
let decorationTypeNoBgColor = vscode.window.createTextEditorDecorationType({
backgroundColor: "unset",
after: { margin: "0 0 0 1rem" },
isWholeLine: true,
});
/**
* @param {vscode.ExtensionContext} context
*/
function activate() {
// context
vscode.workspace.onDidChangeTextDocument((event) => {
// visibleTextEditors -> array of all open editors
const openEditor = vscode.window.visibleTextEditors.filter(
// returns the editor that we currently save
(editor) => editor.document.uri === event.document.uri
)[0];
// we deocorate that one editor
decorate(openEditor);
});
}
function convertToRemOrPx(value, convertTo = "rem") {
var regxToGetNumberFromString = /[\d\.]+/;
var numValue = value.match(regxToGetNumberFromString)[0];
const configuration = vscode.workspace.getConfiguration("remToPxComment");
let finalValue;
if (convertTo === "rem") {
finalValue = numValue * configuration.remConversionValue;
} else if (convertTo === "px") {
finalValue = numValue / configuration.remConversionValue;
} else {
return NaN;
}
if (isNaN(finalValue)) {
return null;
} else {
return finalValue;
}
}
function getLengthOfAllStringsInArray(arr) {
if (arr == null) {
return 0;
}
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum = sum + arr[i].length;
}
return sum;
}
function decorate(editor) {
const configuration = vscode.workspace.getConfiguration("remToPxComment");
// gets all the text in editor
let sourceCode = editor.document.getText();
// regx to get rem
let regex = /[\d\.]+rem/g;
let pxRegex = /[\d\.]+px/g;
// an array that contains the decorated text
let decorationsArray = [];
const sourceCodeArr = sourceCode.split("\n");
const convertToPx = configuration.convertToPx;
const convertToRem = configuration.convertToRem;
function containsNumbers(str) {
return /\d/.test(str);
}
for (let line = 0; line < sourceCodeArr.length; line++) {
let match = null;
if (convertToPx) {
if (sourceCodeArr[line].match(regex) !== null) {
match = sourceCodeArr[line].match(regex);
}
}
if (convertToRem) {
if (sourceCodeArr[line].match(pxRegex) !== null) {
match = sourceCodeArr[line].match(pxRegex);
}
}
if (match !== null && match.length !== 0 && containsNumbers(match[0])) {
let matchIndex;
if (match[0].includes("rem")) {
matchIndex = /[\d\.]+rem/.exec(sourceCodeArr[line]).index;
}
if (match[0].includes("px")) {
matchIndex = /[\d\.]+px/.exec(sourceCodeArr[line]).index;
}
// if line has a rem text
// what is Range ? https://code.visualstudio.com/api/references/vscode-api#Range
let range = new vscode.Range(
// what is Position https://code.visualstudio.com/api/references/vscode-api#Position
new vscode.Position(line, matchIndex),
new vscode.Position(
line,
matchIndex + getLengthOfAllStringsInArray(match) + 100000
)
);
let finalRenderStr = "";
if (match[0].includes("rem")) {
match.forEach((item) => {
finalRenderStr = `${finalRenderStr} ${
convertToRemOrPx(item, "px")
? `${convertToRemOrPx(item, "px")}px`
: ""
}`;
});
}
if (match[0].includes("px")) {
match.forEach((item) => {
finalRenderStr = `${finalRenderStr} ${
convertToRemOrPx(item, "rem")
? `${convertToRemOrPx(item, "rem")}rem`
: ""
}`;
});
}
let decoration;
if (match[0].includes("px")) {
decoration = {
range: range,
renderOptions: {
after: {
contentText: `${finalRenderStr}`,
color: configuration.pxCommentColor,
},
},
};
}
if (match[0].includes("rem")) {
decoration = {
range: range,
renderOptions: {
after: {
contentText: `${finalRenderStr}`,
color: configuration.remCommentColor,
},
},
};
}
// range tells what position to highlight
// pushing this is array
decorationsArray.push(decoration);
}
}
// finally sets the decorationType (color etc) , decorationsArray (what to exactly deocrate)
editor.setDecorations(
configuration.hasLineBgColor ? decorationType : decorationTypeNoBgColor,
decorationsArray
);
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate,
};