| 1 | import * as vscode from "vscode"; |
| 2 | |
| 3 | const os = require("node:os"); |
| 4 | |
| 5 | function charIsEscapedAtIndex(index: number, str: string): boolean { |
| 6 | if (index === 0) { |
| 7 | return false; |
| 8 | } |
| 9 | if (str[index - 1] !== "\\") { |
| 10 | return false; |
| 11 | } |
| 12 | return !charIsEscapedAtIndex(index - 1, str); |
| 13 | } |
| 14 | |
| 15 | export function convertSingleToDoubleQuoteJSON(json: string): string { |
| 16 | const singleQuote = "'"; |
| 17 | const doubleQuote = '"'; |
| 18 | const isQuote = (char: string) => |
| 19 | char === doubleQuote || char === singleQuote; |
| 20 | |
| 21 | let newJson = ""; |
| 22 | let insideString = false; |
| 23 | let enclosingQuoteType = doubleQuote; |
| 24 | for (let i = 0; i < json.length; i++) { |
| 25 | if (insideString) { |
| 26 | if (json[i] === enclosingQuoteType && !charIsEscapedAtIndex(i, json)) { |
| 27 | // Close string with a double quote |
| 28 | insideString = false; |
| 29 | newJson += doubleQuote; |
| 30 | } else if (json[i] === singleQuote) { |
| 31 | if (charIsEscapedAtIndex(i, json)) { |
| 32 | // Unescape single quote |
| 33 | newJson = newJson.slice(0, -1); |
| 34 | } |
| 35 | newJson += singleQuote; |
| 36 | } else if (json[i] === doubleQuote) { |
| 37 | if (!charIsEscapedAtIndex(i, json)) { |
| 38 | // Escape double quote |
| 39 | newJson += "\\"; |
| 40 | } |
| 41 | newJson += doubleQuote; |
| 42 | } else { |
| 43 | newJson += json[i]; |
| 44 | } |
| 45 | } else { |
| 46 | if (isQuote(json[i])) { |
| 47 | insideString = true; |
| 48 | enclosingQuoteType = json[i]; |
| 49 | newJson += doubleQuote; |
| 50 | } else { |
| 51 | newJson += json[i]; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return newJson; |
| 57 | } |
| 58 | |
| 59 | export function debounced(delay: number, fn: (...args: any[]) => void) { |
| 60 | let timerId: NodeJS.Timeout | null; |
| 61 | return (...args: any[]) => { |
| 62 | if (timerId) { |
| 63 | clearTimeout(timerId); |
| 64 | } |
| 65 | timerId = setTimeout(() => { |
| 66 | fn(...args); |
| 67 | timerId = null; |
| 68 | }, delay); |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | type Platform = "mac" | "linux" | "windows" | "unknown"; |
| 73 | |
| 74 | export function getPlatform(): Platform { |
| 75 | const platform = os.platform(); |
| 76 | if (platform === "darwin") { |
| 77 | return "mac"; |
| 78 | } else if (platform === "linux") { |
| 79 | return "linux"; |
| 80 | } else if (platform === "win32") { |
| 81 | return "windows"; |
| 82 | } else { |
| 83 | return "unknown"; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | export function getAltOrOption() { |
| 88 | if (getPlatform() === "mac") { |
| 89 | return "⌥"; |
| 90 | } else { |
| 91 | return "Alt"; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | export function getMetaKeyLabel() { |
| 96 | const platform = getPlatform(); |
| 97 | switch (platform) { |
| 98 | case "mac": |
| 99 | return "⌘"; |
| 100 | case "linux": |
| 101 | case "windows": |
| 102 | return "Ctrl"; |
| 103 | default: |
| 104 | return "Ctrl"; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | export function getMetaKeyName() { |
| 109 | const platform = getPlatform(); |
| 110 | switch (platform) { |
| 111 | case "mac": |
| 112 | return "Cmd"; |
| 113 | case "linux": |
| 114 | case "windows": |
| 115 | return "Ctrl"; |
| 116 | default: |
| 117 | return "Ctrl"; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | export function getExtensionVersion(): string { |
| 122 | const extension = vscode.extensions.getExtension("continue.continue"); |
| 123 | return extension?.packageJSON.version || "0.1.0"; |
| 124 | } |
| 125 | |
| 126 | export function getvsCodeUriScheme(): string { |
| 127 | return vscode.env.uriScheme; |
| 128 | } |
| 129 | |
| 130 | export function isExtensionPrerelease(): boolean { |
| 131 | const extensionVersion = getExtensionVersion(); |
| 132 | const versionParts = extensionVersion.split("."); |
| 133 | if (versionParts.length >= 2) { |
| 134 | const minorVersion = parseInt(versionParts[1], 10); |
| 135 | if (!isNaN(minorVersion)) { |
| 136 | return minorVersion % 2 !== 0; |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | } |