-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
137 lines (111 loc) · 6.38 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
// 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 LOREMIPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?";
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "FirstVSCExtension" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('FirstVSCExtension.replacePlaceholderText', function () {
// The code you place here will be executed every time your command is executed
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage("Editor does not exist");
return;
}
const text = editor.document.getText();
//Find the index of <body> element in HTML file
//Then find the position (line) that tag is on and save it to a new Position variable
// NOTE: bodyStart is INCLUSIVE and bodyEnd is EXCLUSIVE
let bodyStart = editor.document.positionAt(text.indexOf("<body>"));
let bodyStartText = editor.document.lineAt(bodyStart);
//In case the pre-formatted files have some unsurprisingly stupid formatting
bodyStart = editor.document.positionAt(text.indexOf("<body>") + bodyStartText.text.indexOf(">") + 1);
let bodyEnd = editor.document.positionAt(text.indexOf("</body>"));
let bodyRange = new vscode.Range(bodyStart, bodyEnd);
console.log();
const body = editor.document.getText(bodyRange);
let bodyElements = body.split("\n");
let rangesToEdit = [];
let wordCounts = [];
let inNav = false;
//console.log(bodyElements);
for (let i = 0; i < bodyElements.length; i++) {
bodyElements[i] = bodyElements[i].trim();
let tagOpenIndex = bodyElements[i].indexOf(">") + 1;
let tagCloseIndex = bodyElements[i].lastIndexOf("<");
//console.log(`Tag open index = ${tagOpenIndex} Tag close index = ${tagCloseIndex}`);
if (bodyElements[i].includes("nav")) {
inNav = true;
}
if (!inNav) {
//Does the line contain <> : If yes
if (tagOpenIndex != -1 && tagCloseIndex != -1) {
//Is there text following the ">" : If yes
if (tagOpenIndex < tagCloseIndex) {
//Are there tags nested inside the text?
if (bodyElements[i].indexOf(">", tagOpenIndex) + 1 != tagOpenIndex) {
console.log(`${bodyElements[i]} has detected further tags in line`);
tagOpenIndex = bodyElements[i].indexOf(">", tagOpenIndex) + 1;
tagCloseIndex = bodyElements[i].indexOf("<", tagOpenIndex);
}
let workingRange = new vscode.Range(editor.document.positionAt(text.indexOf(bodyElements[i]) + tagOpenIndex), editor.document.positionAt(text.indexOf(bodyElements[i]) + tagCloseIndex));
rangesToEdit.push(workingRange);
wordCounts.push(getWordCount(bodyElements[i]));
}
//Is there an open tag but no close tag on the same line? : If yes
else if (tagOpenIndex < bodyElements[i].length - 1) {
let workingRange = new vscode.Range(editor.document.positionAt(text.indexOf(bodyElements[i]) + tagOpenIndex), editor.document.positionAt(text.indexOf(bodyElements[i]) + bodyElements[i].length));
rangesToEdit.push(workingRange);
wordCounts.push(getWordCount(bodyElements[i]));
}
//Is there a close tag but no open tag on the same line? : If yes
else if (tagCloseIndex > 0) {
let workingRange = new vscode.Range(editor.document.positionAt(text.indexOf(bodyElements[i])), editor.document.positionAt(text.indexOf(bodyElements[i]) + tagCloseIndex));
rangesToEdit.push(workingRange);
wordCounts.push(getWordCount(bodyElements[i]));
}
}
//Is this a blank garbage value from the split? : If no
else if (bodyElements[i].length > 1) {
let workingRange = new vscode.Range(editor.document.positionAt(text.indexOf(bodyElements[i])), editor.document.positionAt(text.indexOf(bodyElements[i]) + bodyElements[i].length));
rangesToEdit.push(workingRange);
wordCounts.push(getWordCount(bodyElements[i]));
}
}
if (bodyElements[i].includes("/nav")) {
inNav = false;
}
}
//console.log(rangesToEdit);
//console.log(bodyElements);
editor.edit((edit) => {
for (let i = 0; i < rangesToEdit.length; i++) {
edit.replace(rangesToEdit[i], lorem(wordCounts[i]));
}
})
vscode.window.showInformationMessage("All text replaced");
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
function getWordCount(string) {
return string.split(" ").length;
}
function lorem(n) {
return LOREMIPSUM.split(" ").slice(0, n).join(" ");
}
// this method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}