-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
223 lines (175 loc) · 6.57 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
const vscode = require('vscode');
const fileParser = require('./Controllers/fileParser');
const fileToMD = require('./Controllers/filetoMD');
const fs = require("fs");
const gptController = require('./Controllers/gptController');
const folderParser = require('./Controllers/folderParser');
const folderMD = require('./Controllers/convertToMdFolder');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Extension is now active!');
let disposable = vscode.commands.registerCommand("extension.showTextField", function () {
let editor = vscode.window.activeTextEditor;
vscode.window.showInputBox({
prompt: "Enter your text"
}).then(function (inputText) {
editor.edit(editBuilder => {
if (inputText != undefined) {
let id = Math.floor(Math.random() * (100 - 0 + 1)) + 0
while (id in everyIDGenerated) {
id = Math.floor(Math.random() * (100 - 0 + 1)) + 0
}
everyIDGenerated.push(id)
if (editor.selection.start.line === editor.selection.end.line) {
inputText = " " + checkLanguage() + " @/w" + `${id}` + " " + inputText
} else {
inputText = checkLanguage() + " @<r" + `${id}` + " " + inputText
}
closingText = checkLanguage() + " @r>" + `${id}`
let newText;
if (editor.selection.start.line === editor.selection.end.line) {
newText = `${editor.document.getText(editor.selection)}` + `${inputText}`
} else {
newText = `${inputText}` + '\n' + `${editor.document.getText(editor.selection)}` + '\n' + `${closingText}`
}
const selections = editor.selections; // to handle mutliple selection
for (const selection of selections) {
editBuilder.replace(selection, newText);
}
}
});
});
});
let callParserNConverter = vscode.commands.registerCommand("extension.callParserNConverter", async function () {
console.log("calling parser n converter")
function getFileName(path) {
const pathArray = path.split('/');
return pathArray[pathArray.length - 1];
}
const writePath = vscode.workspace.workspaceFolders[0].uri.fsPath
let files = await vscode.workspace.findFiles('**/*.*', '**/node_modules/**');
files = files.sort()
fs.writeFileSync(`${writePath}/markdownfile.md`, "")
// folder info if available
async function processFolder() {
try {
const folderData = await folderParser.folderParser(`${writePath}/info.pii`)
if (folderData != undefined) {
await folderMD.convertToMdFolder(folderData, `${vscode.workspace.workspaceFolders[0].uri.fsPath}/markdownfile.md`)
}
} catch (e) {
console.log(e)
}
}
processFolder()
async function processFiles() {
for (let i = 0; i < files.length; i++) {
let file = files[i].fsPath;
try {
const arrayOfItemCode = await fileParser.fileParser(file, writePath, getFileName(file));
if (arrayOfItemCode != undefined) {
await fileToMD.convertToMDFile(arrayOfItemCode, writePath, getFileName(file));
}
} catch (e) {
console.log(e);
}
}
}
processFiles();
})
let gptAPI = vscode.commands.registerCommand("extension.gptAPI", async function () {
console.log("gpt API")
let editor = vscode.window.activeTextEditor;
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "Smart Comments",
cancellable: false
}, async (progress) => {
// simulate a long-running task
progress.report({ message: "Running GPT..." });
await new Promise(resolve => setTimeout(resolve, 100));
if (editor.document.getText(editor.selection).trim() == "") {
progress.report({ increment: 100, message: "Error. Please select something." });
await new Promise(resolve => setTimeout(resolve, 100));
return
}
let codeExplication = await gptController.runCompletion(editor.document.getText(editor.selection))
codeExplication = codeExplication.trim()
// update the progress indicator
progress.report({ increment: 50, message: "Processing results..." });
await new Promise(resolve => setTimeout(resolve, 100));
editor.edit(editBuilder => {
let id = Math.floor(Math.random() * (100 - 0 + 1)) + 0
while (id in everyIDGenerated) {
id = Math.floor(Math.random() * (100 - 0 + 1)) + 0
}
everyIDGenerated.push(id)
if (editor.selection.start.line === editor.selection.end.line) {
codeExplication = " " + checkLanguage() + " @/w" + `${id}` + " " + codeExplication
} else {
codeExplication = checkLanguage() + " @<r" + `${id}` + " " + codeExplication
}
closingText = checkLanguage() + " @r>" + `${id}`
let newText;
if (editor.selection.start.line === editor.selection.end.line) {
newText = `${editor.document.getText(editor.selection)}` + `${codeExplication}`
} else {
newText = `${codeExplication}` + '\n' + `${editor.document.getText(editor.selection)}` + '\n' + `${closingText}`
}
const selections = editor.selections; // to handle mutliple selection
for (const selection of selections) {
editBuilder.replace(selection, newText);
}
})
// update the progress indicator
progress.report({ increment: 50, message: "Done!" });
await new Promise(resolve => setTimeout(resolve, 1000));
})
})
let createProjectInfoFile = vscode.commands.registerCommand("extension.createProjectInfoFile", async function () {
console.log("creating project info file")
fs.writeFileSync(`${vscode.workspace.workspaceFolders[0].uri.fsPath}/info.pii`, infoPiiText)
})
// Register the command to the keyboard shortcut
context.subscriptions.push(disposable);
context.subscriptions.push(callParserNConverter);
context.subscriptions.push(gptAPI);
context.subscriptions.push(createProjectInfoFile);
}
function deactivate() { }
module.exports = {
activate,
deactivate
}
function checkLanguage() {
// si preferable law zabatit built in mais ca marchait pas pr aucune raison
// a revoir
const editor = vscode.window.activeTextEditor;
const document = editor.document;
const language = document.languageId
switch (language) {
case "python":
return "#"
case "ruby":
return "#"
case "haskell":
return "- -"
case "r":
return "#"
case "erlang":
return "%"
case "perl":
return "#"
case "html":
return null // waj3it ras for now
case "css":
return null // waj3it ras for now
default:
return "//"
}
}
let everyIDGenerated = []
let infoPiiText =
"@title=\"title\"\n\n@version=\"v 1.1.1\"\n@date=\"12/12/12\"\n\n@authors=\"x, y, ghada\"\n@mail=\"x@mail.com, y@mail.com, ghada@mail.com\"\n\n@description=\"this is a description\"\n\n@requirements=\"ios16, windows11\"\n\n@paragraph=\"this is a paragraph\""