-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
60 lines (48 loc) · 1.65 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
const vscode = require('vscode');
function okToAddLog(line, nextLine) {
return (
emptyLineBeforeNonEmptyLine(line, nextLine) ||
(validLastChar(line) && !isReturn(line) && !withinObjectOrFnChain(nextLine))
);
}
function emptyLineBeforeNonEmptyLine(line, nextLine) {
return !line && !!nextLine;
}
function validLastChar(line) {
const lastChar = line[line.length - 1];
return [';', ')', '{', '}'].includes(lastChar);
}
function isReturn(line) {
return line.startsWith('return');
}
function withinObjectOrFnChain(nextLine) {
const nextLineFirstChar = nextLine[0];
const nextLineLastChar = nextLine[nextLine.length - 1];
return nextLineLastChar === ',' || nextLineFirstChar === '.';
}
function activate(context) {
const disposable = vscode.commands.registerCommand('extension.addNumberLogs', function () {
// Get the active text editor
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const document = editor.document;
const selection = editor.selection;
const text = document.getText(selection);
let logNumber = 1;
const newText = text.split('\n').reduce((newString, line, idx, linesArray) => {
const trimmedLine = line.trim();
const trimmedNextLine = (linesArray[idx + 1] || '').trim();
if (okToAddLog(trimmedLine, trimmedNextLine)) return `${newString}${line}\n${`console.log(${logNumber++})`}\n`;
return `${newString}${line}\n`
}, '');
editor.edit(editBuilder => editBuilder.replace(selection, newText));
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}