-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
268 lines (256 loc) · 7.13 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const vscode = require('vscode')
function randomConsolLogIcon () {
const iconArray = [
'📄',
'🗒️',
'📝',
'📃',
'📑',
'🧾',
'📜',
'📋',
'📇',
'📰',
'📓',
'📔',
'📒',
'📕',
'📗',
'📘',
'📙',
'📚',
'📖',
'🗞️',
'📑',
'🔖',
'🏷️'
]
const randomIcon = iconArray[Math.floor(Math.random() * iconArray.length)]
return randomIcon
}
async function addLazyDebugging (
editor,
selection,
text,
language,
currentLine,
currentLineText,
currentLineIdentation
) {
const newLine = currentLine + 1
const icon = randomConsolLogIcon()
// create a map object with the language and the console log text and the need for identation or not
const languageMap = {
javascript: {
defaultStatement: 'console.log',
consoleLogText: `console.log(\`${icon} LazyLogX Lazy Debugging for: ${text} => \${${text}}\`);\n`,
extraIdentation: false,
controlFlowStatements: [
'if',
'else',
'for',
'while',
'try',
'catch',
'finally',
'with',
'function',
'class'
]
},
typescript: {
defaultStatement: 'console.log',
consoleLogText: `console.log(\`${icon} LazyLogX Lazy Debugging for: ${text} => \${${text}}\`);\n`,
extraIdentation: false,
identationLength: null,
controlFlowStatements: [
'if',
'else',
'for',
'while',
'try',
'catch',
'finally',
'with',
'function',
'class'
]
},
typescriptreact: {
defaultStatement: 'console.log',
consoleLogText: `console.log(\`${icon} LazyLogX Lazy Debugging for: ${text} => \${${text}}\`);\n`,
extraIdentation: false,
identationLength: null,
controlFlowStatements: [
'if',
'else',
'for',
'while',
'try',
'catch',
'finally',
'with',
'function',
'class'
]
},
python: {
defaultStatement: 'print',
consoleLogText: `print(\"${icon} LazyLogX Lazy Debugging for: ${text} => \" + str(${text}))\n`,
extraIdentation: true,
identationLength: 4,
controlFlowStatements: [
'if',
'elif',
'else',
'for',
'while',
'try',
'except',
'finally',
'with',
'def',
'class'
]
},
go: {
defaultStatement: 'fmt.Println',
consoleLogText: `fmt.Println(\"${icon} LazyLogX Lazy Debugging for: ${text} => \" + ${text})\n`,
extraIdentation: true,
identationLength: 4,
controlFlowStatements: [
'if',
'else',
'for',
'switch',
'select',
'range',
'func',
'struct',
'interface',
'map',
'chan'
]
},
shellscript: {
defaultStatement: 'echo',
consoleLogText: `echo \"${icon} LazyLogX Lazy Debugging for: \\${text} => \" ${text}\n`,
extraIdentation: true,
identationLength: 4,
controlFlowStatements: [
'if',
'else',
'for',
'while',
'case',
'function',
'select'
]
}
}
// if text is contain a space then print a warning message
if (text.includes(' ')) {
vscode.window.showWarningMessage(
'Sorry, the selected text contains a space 😔'
)
return
}
// check if the language is on the list of the languageMap, if it is not return a warning message saying that the language is not supported
if (!languageMap[language]) {
vscode.window.showWarningMessage(
`Sorry, this language (${language}) is not supported yet 😔`
)
return
}
// if the current line text contains the default statement then delete the line and move one line up
if (currentLineText.includes(languageMap[language].defaultStatement)) {
editor.edit((editBuilder) => {
editBuilder.delete(
new vscode.Range(
new vscode.Position(currentLine, 0),
new vscode.Position(currentLine + 1, 0)
)
)
})
}
// else if if the current line text doesnt contains the console log text then check if there is a selection or not
else if (!currentLineText.includes(languageMap[language].consoleLogText)) {
// if there is not a selection return an error
if (selection.isEmpty) {
vscode.window.showErrorMessage('Cmon now, select something first! 😅')
return
}
}
// check if the language is supported
// if the language is supoprted then check if it requires extra identation
let languageSpecificConsoleLog = ''
if (languageMap[language]) {
// check if the current line text contains any of the control flow statements
// if it does add the idendationLength spaces to the console log text
// if it doesn't contain any of the control flow statements then just add the console log text
if (
languageMap[language].controlFlowStatements.some((statement) =>
currentLineText.includes(statement)
)
) {
// add the identation length to the console log text as many times as the identation length
// create a string with as many spaces as specified on the identationLength and do it smart
let identationString = ''
for (let i = 0; i < languageMap[language].identationLength; i++) {
identationString += ' '
}
languageSpecificConsoleLog =
identationString + languageMap[language].consoleLogText
} else {
languageSpecificConsoleLog = languageMap[language].consoleLogText
}
}
// create the new line text
const newLineText = currentLineIdentation + languageSpecificConsoleLog
// insert the new line text
editor.edit((editBuilder) => {
editBuilder.insert(new vscode.Position(newLine, 0), newLineText)
})
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate (context) {
console.log('Congratulations, your extension "lazylogx" is now active!')
try {
const disposable = vscode.commands.registerCommand(
'lazylogx.addLazyDebugging',
function () {
// get the sellected text from the editor
const editor = vscode.window.activeTextEditor
if (!editor) throw new Error('No editor found')
const selection = editor.selection
const text = editor.document.getText(selection)
// detect what is the language of the file
const language = editor.document.languageId
const currentLine = editor.selection.active.line
// get the current line text and the current line identation
const currentLineText = editor.document.lineAt(currentLine).text
const currentLineIdentation = currentLineText.substring(
0,
currentLineText.indexOf(currentLineText.trim())
)
addLazyDebugging(
editor,
selection,
text,
language,
currentLine,
currentLineText,
currentLineIdentation
)
}
)
context.subscriptions.push(disposable)
} catch (error) {}
}
function deactivate () {}
module.exports = {
activate,
deactivate
}