forked from frizLabz-FFriZz/PineScript-v6-vscode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPineScriptList.ts
More file actions
374 lines (342 loc) · 15.5 KB
/
Copy pathPineScriptList.ts
File metadata and controls
374 lines (342 loc) · 15.5 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { path } from './index'
import * as vscode from 'vscode'
import { Class } from './PineClass'
import * as fs from 'fs'
import * as os from 'os'
/** PineScriptList class is responsible for managing a list of Pine Scripts. */
export class PineScriptList {
/** Holds the list of scripts */
private scriptList: any[] = []
/** Holds the URI of an existing file */
private existingFileUri: vscode.Uri | undefined
/** Holds the URI of a new existing file */
private newExistingFileUri: vscode.Uri | undefined
/** Holds the URI of the library icon */
private readonly libraryIcon: vscode.Uri = vscode.Uri.file(path.join(__dirname, '..', 'media', 'library.png'))
/** Holds the URI of the strategy icon */
private readonly strategyIcon: vscode.Uri = vscode.Uri.file(path.join(__dirname, '..', 'media', 'strategy.png'))
/** Holds the URI of the study icon */
private readonly studyIcon: vscode.Uri = vscode.Uri.file(path.join(__dirname, '..', 'media', 'study.png'))
/** Holds the URI of the default icon */
private readonly defaultIcon: vscode.Uri = vscode.Uri.file(path.join(__dirname, '..', 'media', 'default.png'))
/**
* Returns the list of scripts.
* @returns {Promise<any[]>} A promise that resolves to the list of scripts.
*/
async getScriptList(): Promise<any[]> {
return this.scriptList
}
/**
* Shows a menu to select a script to open.
* @param {string} toFetch - The type of scripts to fetch ('saved' or 'built-in').
*/
async showMenu(toFetch: string) {
try {
const quickPick = await this.loadScriptList(toFetch) // Load the script list
/** Handles the event when an item is selected in the Quick Pick menu. */
quickPick.onDidAccept(async () => {
// Get the selected item
const selected = quickPick.selectedItems[0]
// If the selected item exists and its description includes 'Version:'
if (selected && selected.label) {
// Get the detail and label of the selected item
const selectedLabel = selected.label
const selectedDescription = selected.description
// Find the script in the script list that matches the detail of the selected item
let selectedDict = this.scriptList.find((match: any) => match.scriptName === selectedLabel)
// If no matching script is found, find the script that matches the label of the selected item
if (!selectedDict && selectedDescription) {
selectedDict = this.scriptList.find(
(match: any) => match.scriptTitle === selectedDescription.replace(/(.*)\sv[0-9]+$/, '$1'),
)
}
if (selectedDict) {
if (toFetch === 'built-in') {
await this.openScript(selectedDict.scriptIdPart, 'last', toFetch)
quickPick.dispose()
return
}
// If a matching script is found
const { version } = selectedDict
const versionItems: vscode.QuickPickItem[] = [] // Initialize an empty array to hold the version items
for (let i = 1; i <= version; i++) {
// Populate the version items array with the versions of the script
versionItems.unshift({
label: `${i}`,
})
}
// Set the items, placeholder, and title of the Quick Pick menu
quickPick.items = versionItems
quickPick.placeholder = `Select a Version for ${selectedDict.scriptName}`
quickPick.title = `${selectedDict.scriptName} Version Selection`
quickPick.show()
quickPick.onDidAccept(() => {
// Handle the event when a version is selected in the Quick Pick menu
const versionSelected = quickPick.selectedItems[0] // Get the selected version
if (versionSelected.label) {
// If the selected version has a description
const selectedVersion = versionSelected.label
// Get the version number from the description
this.openScript(selectedDict.scriptIdPart, selectedVersion, toFetch) // Open the script with the selected version
quickPick.dispose() // Dispose of the Quick Pick menu
}
})
}
}
})
} catch (error) {
console.error('Error in showMenu:', error)
}
}
async icon(dict: Record<string, any>): Promise<vscode.Uri> {
// Define a function to get the icon for a script based on its kind
switch (dict.extra.kind) {
case 'study':
return this.studyIcon
case 'strategy':
return this.strategyIcon
case 'library':
return this.libraryIcon
default:
return this.defaultIcon
}
}
/**
* Populates the script list with the user's saved scripts.
* @param {vscode.QuickPickItem[]} scriptList - The list of scripts.
*/
async builtInScriptList(scriptList: vscode.QuickPickItem[]) {
this.scriptList = await Class.PineRequest.getStandardList()
this.scriptList.sort((a, b) => b.modified - a.modified)
for (const dict of this.scriptList) {
const options: vscode.QuickPickItem = {
label: dict.scriptName,
iconPath: await this.icon(dict),
}
scriptList.push(options)
}
}
/**
* Loads the script list.
* @param {string} toFetch - The type of scripts to fetch ('saved' or 'built-in').
* @returns {Promise<vscode.QuickPick>} A promise that resolves to the Quick Pick menu.
*/
async loadScriptList(toFetch: string): Promise<vscode.QuickPick<vscode.QuickPickItem>> {
const quickPick = vscode.window.createQuickPick() // Create a Quick Pick menu
let scripts: vscode.QuickPickItem[] = [] // Initialize an empty array to hold the scripts
// Populate the scripts array with the scripts from the script list
if (toFetch === 'saved') {
// await this.userScriptList(scripts)
quickPick.placeholder = 'User Pine Scripts'
} else if (toFetch === 'built-in') {
await this.builtInScriptList(scripts)
quickPick.placeholder = 'Built-In Pine Scripts'
}
// Set the items, placeholder, and title of the Quick Pick menu
quickPick.items = scripts
quickPick.title = 'Pine Scripts'
quickPick.show()
return quickPick
}
/**
* Opens a script with the given name, script ID part, and version.
* @param {string} scriptIdPart - The script ID part of the script to open.
* @param {string} version - The version of the script to open.
* @param {string} toFetch - The type of scripts to fetch ('saved' or 'built-in').
*/
async openScript(scriptIdPart: string, version: string, toFetch: string = 'saved') {
// send the request to get the script text
if (toFetch === 'saved') {
const response = await Class.PineRequest.getScript(scriptIdPart, version)
await this.handlePineScript(response)
return response
} else if (toFetch === 'built-in') {
const response = await Class.PineRequest.getBuiltInScript(scriptIdPart)
await this.handlePineScript(response)
return response
}
}
/**
* Handles the Pine script response.
* @param {any} response - The response object containing the script details.
*/
async handlePineScript(response: any): Promise<void> {
// Check if there is at least one workspace folder
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
const workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath
const scriptDir = path.join(workspaceFolder, 'PineScripts')
const scriptPrevDir = path.join(scriptDir, 'PreviousPineScripts')
await this.createDirectoryIfNotExists(scriptDir)
await this.createDirectoryIfNotExists(scriptPrevDir)
const existingFilePath = path.join(scriptDir, `${response.scriptName}.pine`)
const existingFileUri = vscode.Uri.file(existingFilePath)
try {
const existingFileStat = await vscode.workspace.fs.stat(existingFileUri)
if (existingFileStat) {
await this.processExistingFile(existingFileUri, scriptDir, response.scriptName, response.source)
await this.openDocument(existingFileUri)
}
} catch (err) {
await vscode.workspace.fs.writeFile(existingFileUri, Buffer.from(response.source))
await this.openDocument(existingFileUri)
}
} else {
// No workspace folder is open, so we'll save the file in a temporary location
const tempFolderPath = os.tmpdir()
const tempFilePath = path.join(tempFolderPath, `${response.scriptName}.pine`)
const tempFileUri = vscode.Uri.file(tempFilePath)
await vscode.workspace.fs.writeFile(tempFileUri, Buffer.from(response.source))
await this.openDocument(tempFileUri)
}
}
/**
* Creates a directory if it does not exist.
* @param {string} dirPath - The path of the directory to create.
*/
async createDirectoryIfNotExists(dirPath: string): Promise<void> {
try {
await vscode.workspace.fs.stat(vscode.Uri.file(dirPath))
} catch (err) {
await vscode.workspace.fs.createDirectory(vscode.Uri.file(dirPath))
}
}
/**
* Processes an existing file by renaming it with a unique number suffix if a file with the same name already exists.
* @param {vscode.Uri} existingFileUri - The URI of the existing file.
* @param {string} scriptDir - The directory of the script.
* @param {string} name - The name of the script.
* @param {string} newContent - The new content of the script.
*/
async processExistingFile(
existingFileUri: vscode.Uri,
scriptDir: string,
name: string,
newContent: string,
): Promise<void> {
const previousScriptsDir = path.join(scriptDir, 'PreviousPineScripts')
await this.ensureDirectoryExists(previousScriptsDir)
let counter = 1
let newFileName = `${name}_${counter}.pine`
let newFilePath = path.join(previousScriptsDir, newFileName)
// Loop to find a unique file name
while (fs.existsSync(newFilePath)) {
counter++
newFileName = `${name}_${counter}.pine`
newFilePath = path.join(previousScriptsDir, newFileName)
}
const newFileUri = vscode.Uri.file(newFilePath)
await vscode.workspace.fs.rename(existingFileUri, newFileUri, { overwrite: true })
const newScriptFilePath = path.join(scriptDir, `${name}.pine`)
const newScriptFileUri = vscode.Uri.file(newScriptFilePath)
await vscode.workspace.fs.writeFile(newScriptFileUri, Buffer.from(newContent))
}
/**
* Ensures that the given directory exists, creating it if necessary.
* @param {string} dirPath - The path to the directory.
*/
private async ensureDirectoryExists(dirPath: string): Promise<void> {
if (!fs.existsSync(dirPath)) {
await vscode.workspace.fs.createDirectory(vscode.Uri.file(dirPath))
}
}
/**
* Opens a document with the given file URI.
* @param {vscode.Uri} fileUri - The URI of the file to open.
*/
async openDocument(fileUri: vscode.Uri): Promise<void> {
const doc = await vscode.workspace.openTextDocument(fileUri)
await vscode.window.showTextDocument(doc)
}
}
// async userScriptList(scriptList: vscode.QuickPickItem[]) {
// this.scriptList = await Class.PineRequest.getSavedList()
// this.scriptList.sort((a, b) => b.modified - a.modified)
// for (const dict of this.scriptList) {
// const options: vscode.QuickPickItem = {
// label: dict.scriptName,
// iconPath: await this.icon(dict),
// description: dict.scriptTitle + ` v${dict.version}`,
// }
// scriptList.push(options)
// }
// }
// /**
// * Processes an existing file by moving it to the previous scripts directory, saving the new content, and opening the diff view.
// * @param {vscode.Uri} existingFileUri - The URI of the existing file.
// * @param {string} scriptDir - The directory of the script.
// * @param {string} name - The name of the script.
// * @param {string} newContent - The new content of the script.
// */
// async processExistingFile(
// existingFileUri: vscode.Uri,
// scriptDir: string,
// name: string,
// newContent: string,
// ): Promise<void> {
// const previousScriptsDir = path.join(scriptDir, 'PreviousPineScripts')
// const newExistingFilePath = path.join(previousScriptsDir, `${name}.last.pine`)
// const newExistingFileUri = vscode.Uri.file(newExistingFilePath)
// await vscode.workspace.fs.rename(existingFileUri, newExistingFileUri, { overwrite: true })
// const newFilePath = path.join(scriptDir, `${name}.pine`)
// const newFileUri = vscode.Uri.file(newFilePath)
// await vscode.workspace.fs.writeFile(newFileUri, Buffer.from(newContent))
// await this.openDiffView(newExistingFileUri, newFileUri, name)
// }
// /**
// * Opens the diff view between two files.
// * @param {vscode.Uri} oldFileUri - The URI of the old file.
// * @param {vscode.Uri} newFileUri - The URI of the new file.
// * @param {string} name - The name of the script.
// */
// async openDiffView(oldFileUri: vscode.Uri, newFileUri: vscode.Uri, name: string): Promise<void> {
// const oldDoc = await vscode.workspace.openTextDocument(oldFileUri)
// const newDoc = await vscode.workspace.openTextDocument(newFileUri)
// await vscode.commands.executeCommand('vscode.diff', oldDoc.uri, newDoc.uri, `${name} (Old vs New)`)
// }
/** Compares the current version of a script with an older version. */
// async compareWithOldVersion() {
// const editor = vscode.window.activeTextEditor // Get the active text editor
// if (!editor) {
// // If no active editor is found, show a warning message and return
// vscode.window.showWarningMessage('No active editor found')
// return
// }
// // Get the current file name
// const currentFileName = decodeURIComponent(path.basename(editor.document.fileName, '.pine'))
// // Fetch the list of saved scripts from the API
// const scriptList = await Class.PineRequest.getSavedList()
// // Filter the list of scripts to find the ones that match the current file name
// const matchingScripts = scriptList.filter((script: any) => script.scriptName === currentFileName)
// if (matchingScripts.length === 0) {
// // If no matching scripts are found, show a warning message and return
// vscode.window.showWarningMessage('No matching script found for the current file')
// return
// }
// const script = matchingScripts[0] // Get the first matching script
// const versions = Array.from({ length: script.version }, (_, i) => String(script.version - i)) // Create an array of version numbers for the script
// const selectedVersion = await vscode.window.showQuickPick(versions, {
// // Show a Quick Pick menu to select a version
// placeHolder: 'Select a Version',
// title: `Select a Version for ${script.scriptName}`,
// })
// if (!selectedVersion) {
// // If no version is selected, show a warning message and return
// vscode.window.showWarningMessage('No version selected')
// return
// }
// const docData = await Class.PineRequest.getScript(script.scriptIdPart, selectedVersion.toString()) // Fetch the selected version of the script
// const tempFilePath = path.join(
// // Write the script to a temporary file
// os.tmpdir(),
// `${encodeURIComponent(currentFileName)}_${selectedVersion}.pine`,
// )
// const tempFileUri = vscode.Uri.file(tempFilePath)
// await vscode.workspace.fs.writeFile(tempFileUri, Buffer.from(docData.source))
// const tempDoc = await vscode.workspace.openTextDocument(tempFileUri) // Open a diff view comparing the temporary file with the current file
// await vscode.commands.executeCommand(
// 'vscode.diff',
// tempDoc.uri,
// editor.document.uri,
// `${currentFileName} (v${selectedVersion} vs Opened)`,
// )
// }