-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
192 lines (160 loc) · 7.31 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
// @ts-nocheck
// 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
/**
* @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 "flizkconsole" 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 disposables = vscode.commands.registerCommand('flizkconsole.helloWorld', function () {
// // The code you place here will be executed every time your command is executed
// // Display a message box to the user
// vscode.window.showInformationMessage('Hello World from flizkConsole!');
// });
let disposable = vscode.commands.registerCommand('flizkConsole.displayLogMessage', function () {
// The code you place here will be executed every time your command is executed
const editor = vscode.window.activeTextEditor;
const lineText = editor.document.lineAt(editor.selection.active.line).text;
// console.log(lineText.replace("="," "))
let selectedText = editor.document.getText(editor.selection);
vscode.env.clipboard.writeText(selectedText)
// console.log(editor.document.getText())
let language = editor.document.languageId;
// console.log(`The current document's language is ${language}`);
if (language == "typescript" || language == "typescriptreact" || language == "javascript") {
console.log('lineText :31 ', editor.document.getText );
consoleLog()
// vscode.window.showInformationMessage('Added Log from flizkConsole! ', selectedText);
} else if (language == "php") {
consoleLogPhp()
// vscode.window.showInformationMessage('Added Log from flizkConsole! ', selectedText);
} else {
vscode.window.showWarningMessage('Have not added log ! ');
}
});
vscode.commands.registerCommand('flizkConsole.commentLogMessage', function () {
// The code you place here will be executed every time your command is executed
const editor = vscode.window.activeTextEditor;
let language = editor.document.languageId;
// console.log(`The current document's language is ${language}`);
if (language == "typescript" || language == "typescriptreact" || language == "javascript") {
commentConsoleLog()
} else if (language == "php") {
}else {
vscode.window.showWarningMessage('There is Nothing to Comment ! ');
}
});
vscode.commands.registerCommand('flizkConsole.removeLogMessage', function () {
const editor = vscode.window.activeTextEditor;
let language = editor.document.languageId;
if (language == "typescript" || language == "typescriptreact" || language == "javascript") {
commentConsoleLog(true)
}
})
vscode.commands.registerCommand('flizkConsole.uncommentLogMessage', function () {
// The code you place here will be executed every time your command is executed
const editor = vscode.window.activeTextEditor;
let language = editor.document.languageId;
// console.log(`The current document's language is ${language}`);
if (language == "typescript" || language == "typescriptreact" || language == "javascript") {
uncommentConsoleLog()
vscode.window.showInformationMessage('uncommented console.log from flizkConsole! ');
} else if (language == "php") {
}else {
vscode.window.showWarningMessage('There is Nothing to Comment ! ');
}
// Display a message box to the user
});
function uncommentConsoleLog() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage("Editor Does Not Exist");
return;
}
const regex = /\/\/\s*console\.log\(([^)]*)\);|\/\*\s*console\.log\(([^)]*)\);\s*\*\/|#\s*console\.log\(([^)]*)\);/g;
const visibleRange = editor.visibleRanges[0]; // Get the visible range of the editor
// Create an array to hold the edited lines
let editedLines = [];
for (let i = visibleRange.start.line; i <= visibleRange.end.line; i++) {
let line = editor.document.lineAt(i);
let newText = line.text;
// Check if the line is commented and contains a commented console.log statement
if ((newText.trim().startsWith("//") || newText.trim().startsWith("/*") || newText.trim().startsWith("#")) && regex.test(newText)) {
newText = newText.replace(regex, "console.log($1$2$3);"); // Uncomment the console.log statement
}
editedLines.push(newText);
}
// Apply the changes to the visible lines of the document
editor.edit(editBuilder => {
for (let i = visibleRange.start.line; i <= visibleRange.end.line; i++) {
editBuilder.replace(editor.document.lineAt(i).range, editedLines[i - visibleRange.start.line]);
}
}).then(() => {
vscode.window.showInformationMessage('Uncommented console.log statements in visible text.');
}).catch(err => {
console.error(err);
});
}
function commentConsoleLog(removeLogs=false) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage("Editor Does Not Exist");
return;
}
const regex = /console\.log\(([^)]*)\);/g; // Updated regex to capture everything inside the parentheses
const visibleRange = editor.visibleRanges[0]; // Get the visible range of the editor
// Create an array to hold the edited lines
let editedLines = [];
for (let i = visibleRange.start.line; i <= visibleRange.end.line; i++) {
let line = editor.document.lineAt(i);
let newText = line.text;
let conditons,setNew
// Check if the line is not already commented and contains a console.log statement
if (!removeLogs) {
conditons=regex.test(newText) && !newText.trim().startsWith("//");
setNew=newText.replace(regex, "// console.log($1);");
}else{
conditons=regex.test(newText)
setNew=newText.replace(regex, "")
}
if (conditons) {
console.log(removeLogs,"Logs")
newText = setNew; // Comment out the console.log statement
}
editedLines.push(newText);
}
// Apply the changes to the visible lines of the document
editor.edit(editBuilder => {
for (let i = visibleRange.start.line; i <= visibleRange.end.line; i++) {
editBuilder.replace(editor.document.lineAt(i).range, editedLines[i - visibleRange.start.line]);
}
}).then(() => {
vscode.window.showInformationMessage('Commented console.log statements in visible text.');
}).catch(err => {
console.error(err);
});
}
function consoleLog() {
vscode.commands.executeCommand("editor.action.insertLineAfter");
vscode.commands.executeCommand("editor.action.insertSnippet", { "snippet": "console.log('${CLIPBOARD} :${TM_LINE_INDEX} ', ${CLIPBOARD} $1)$2;" });
}
function consoleLogPhp() {
vscode.commands.executeCommand("editor.action.insertLineAfter");
vscode.commands.executeCommand("editor.action.insertSnippet", { "snippet": "\Log::info('${CLIPBOARD} :${TM_LINE_INDEX} '.${CLIPBOARD} $1)$2;" });
}
context.subscriptions.push(disposable);
}
// This method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}