| 1 | import { machineIdSync } from "node-machine-id"; |
| 2 | import * as URI from "uri-js"; |
| 3 | import * as vscode from "vscode"; |
| 4 | |
| 5 | export function translate(range: vscode.Range, lines: number): vscode.Range { |
| 6 | return new vscode.Range( |
| 7 | range.start.line + lines, |
| 8 | range.start.character, |
| 9 | range.end.line + lines, |
| 10 | range.end.character, |
| 11 | ); |
| 12 | } |
| 13 | |
| 14 | export function getNonce() { |
| 15 | let text = ""; |
| 16 | const possible = |
| 17 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 18 | for (let i = 0; i < 32; i++) { |
| 19 | text += possible.charAt(Math.floor(Math.random() * possible.length)); |
| 20 | } |
| 21 | return text; |
| 22 | } |
| 23 | |
| 24 | export function getExtensionUri(): vscode.Uri { |
| 25 | return vscode.extensions.getExtension("Continue.continue")!.extensionUri; |
| 26 | } |
| 27 | |
| 28 | export function getViewColumnOfFile( |
| 29 | uri: vscode.Uri, |
| 30 | ): vscode.ViewColumn | undefined { |
| 31 | for (const tabGroup of vscode.window.tabGroups.all) { |
| 32 | for (const tab of tabGroup.tabs) { |
| 33 | if ( |
| 34 | (tab?.input as any)?.uri && |
| 35 | URI.equal((tab.input as any).uri, uri.toString()) |
| 36 | ) { |
| 37 | return tabGroup.viewColumn; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | return undefined; |
| 42 | } |
| 43 | |
| 44 | export function getRightViewColumn(): vscode.ViewColumn { |
| 45 | // When you want to place in the rightmost panel if there is already more than one, otherwise use Beside |
| 46 | let column = vscode.ViewColumn.Beside; |
| 47 | const columnOrdering = [ |
| 48 | vscode.ViewColumn.One, |
| 49 | vscode.ViewColumn.Beside, |
| 50 | vscode.ViewColumn.Two, |
| 51 | vscode.ViewColumn.Three, |
| 52 | vscode.ViewColumn.Four, |
| 53 | vscode.ViewColumn.Five, |
| 54 | vscode.ViewColumn.Six, |
| 55 | vscode.ViewColumn.Seven, |
| 56 | vscode.ViewColumn.Eight, |
| 57 | vscode.ViewColumn.Nine, |
| 58 | ]; |
| 59 | for (const tabGroup of vscode.window.tabGroups.all) { |
| 60 | if ( |
| 61 | columnOrdering.indexOf(tabGroup.viewColumn) > |
| 62 | columnOrdering.indexOf(column) |
| 63 | ) { |
| 64 | column = tabGroup.viewColumn; |
| 65 | } |
| 66 | } |
| 67 | return column; |
| 68 | } |
| 69 | |
| 70 | let showTextDocumentInProcess = false; |
| 71 | |
| 72 | export function openEditorAndRevealRange( |
| 73 | uri: vscode.Uri, |
| 74 | range?: vscode.Range, |
| 75 | viewColumn?: vscode.ViewColumn, |
| 76 | preview?: boolean, |
| 77 | ): Promise<vscode.TextEditor> { |
| 78 | return new Promise((resolve, _) => { |
| 79 | vscode.workspace.openTextDocument(uri).then(async (doc) => { |
| 80 | try { |
| 81 | // An error is thrown mysteriously if you open two documents in parallel, hence this |
| 82 | while (showTextDocumentInProcess) { |
| 83 | await new Promise((resolve) => { |
| 84 | setInterval(() => { |
| 85 | resolve(null); |
| 86 | }, 200); |
| 87 | }); |
| 88 | } |
| 89 | showTextDocumentInProcess = true; |
| 90 | vscode.window |
| 91 | .showTextDocument(doc, { |
| 92 | viewColumn: getViewColumnOfFile(uri) || viewColumn, |
| 93 | preview, |
| 94 | }) |
| 95 | .then((editor) => { |
| 96 | if (range) { |
| 97 | editor.revealRange(range); |
| 98 | } |
| 99 | resolve(editor); |
| 100 | showTextDocumentInProcess = false; |
| 101 | }); |
| 102 | } catch (err) { |
| 103 | console.log(err); |
| 104 | } |
| 105 | }); |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | export function getUniqueId() { |
| 110 | const id = vscode.env.machineId; |
| 111 | if (id === "someValue.machineId") { |
| 112 | return machineIdSync(); |
| 113 | } |
| 114 | return vscode.env.machineId; |
| 115 | } |