Elffuss BraidHub

kikocis/continue public

9d8db5fc402e · 5.5 KiBRawPermalink
1export function removeQuotesAndEscapes(input: string): string {
2 let output = input.trim();
3
4 // Replace smart quotes
5 output = output.replaceAll("“", '"');
6 output = output.replaceAll("”", '"');
7 output = output.replaceAll("‘", "'");
8 output = output.replaceAll("’", "'");
9
10 // Remove escapes
11 output = output.replaceAll('\\"', '"');
12 output = output.replaceAll("\\'", "'");
13 output = output.replaceAll("\\n", "\n");
14 output = output.replaceAll("\\t", "\t");
15 output = output.replaceAll("\\\\", "\\");
16 while (
17 (output.startsWith('"') && output.endsWith('"')) ||
18 (output.startsWith("'") && output.endsWith("'"))
19 ) {
20 output = output.slice(1, -1);
21 }
22
23 while (output.startsWith("`") && output.endsWith("`")) {
24 output = output.slice(1, -1);
25 }
26
27 return output;
28}
29
30export function dedentAndGetCommonWhitespace(s: string): [string, string] {
31 const lines = s.split("\n");
32 if (lines.length === 0 || (lines[0].trim() === "" && lines.length === 1)) {
33 return ["", ""];
34 }
35
36 // Longest common whitespace prefix
37 let lcp = lines[0].split(lines[0].trim())[0];
38 // Iterate through the lines
39 for (let i = 1; i < lines.length; i++) {
40 // Empty lines are wildcards
41 if (lines[i].trim() === "") {
42 continue; // hey that's us!
43 }
44
45 if (lcp === undefined) {
46 lcp = lines[i].split(lines[i].trim())[0];
47 }
48
49 // Iterate through the leading whitespace characters of the current line
50 for (let j = 0; j < lcp.length; j++) {
51 // If it doesn't have the same whitespace as lcp, then update lcp
52 if (j >= lines[i].length || lcp[j] !== lines[i][j]) {
53 lcp = lcp.slice(0, j);
54 if (lcp === "") {
55 return [s, ""];
56 }
57 break;
58 }
59 }
60 }
61
62 if (lcp === undefined) {
63 return [s, ""];
64 }
65
66 return [lines.map((x) => x.replace(lcp, "")).join("\n"), lcp];
67}
68
69export function getMarkdownLanguageTagForFile(filepath: string): string {
70 const extToLangMap: { [key: string]: string } = {
71 py: "python",
72 js: "javascript",
73 jsx: "jsx",
74 tsx: "tsx",
75 ts: "typescript",
76 java: "java",
77 class:"java", //.class files decompile to Java
78 go: "go",
79 rb: "ruby",
80 rs: "rust",
81 c: "c",
82 cpp: "cpp",
83 cs: "csharp",
84 php: "php",
85 scala: "scala",
86 swift: "swift",
87 kt: "kotlin",
88 md: "markdown",
89 json: "json",
90 html: "html",
91 css: "css",
92 sh: "shell",
93 yaml: "yaml",
94 toml: "toml",
95 tex: "latex",
96 sql: "sql",
97 ps1: "powershell",
98 };
99
100 const ext = sanitizeExtension(filepath.split(".").pop());
101 return ext ? (extToLangMap[ext] ?? ext) : "";
102}
103
104
105function sanitizeExtension(ext?: string): string|undefined {
106 if (ext) {
107 //ignore ranges in extension eg. "java (11-23)"
108 const match = ext.match(/^(\S+)\s*(\(.*\))?$/);
109 if (match) {
110 ext = match[1];
111 }
112 }
113 return ext;
114}
115
116export function copyOf(obj: any): any {
117 if (obj === null || obj === undefined) {
118 return obj;
119 }
120 return JSON.parse(JSON.stringify(obj));
121}
122
123export function deduplicateArray<T>(
124 array: T[],
125 equal: (a: T, b: T) => boolean,
126): T[] {
127 const result: T[] = [];
128
129 for (const item of array) {
130 if (!result.some((existingItem) => equal(existingItem, item))) {
131 result.push(item);
132 }
133 }
134
135 return result;
136}
137
138export type TODO = any;
139
140export function dedent(strings: TemplateStringsArray, ...values: any[]) {
141 let raw = "";
142 for (let i = 0; i < strings.length; i++) {
143 raw += strings[i];
144
145 // Handle the value if it exists
146 if (i < values.length) {
147 let value = String(values[i]);
148 // If the value contains newlines, we need to adjust the indentation
149 if (value.includes("\n")) {
150 // Find the indentation level of the last line in strings[i]
151 let lines = strings[i].split("\n");
152 let lastLine = lines[lines.length - 1];
153 let match = lastLine.match(/(^|\n)([^\S\n]*)$/);
154 let indent = match ? match[2] : "";
155 // Add indentation to all lines except the first line of value
156 let valueLines = value.split("\n");
157 valueLines = valueLines.map((line, index) =>
158 index === 0 ? line : indent + line,
159 );
160 value = valueLines.join("\n");
161 }
162 raw += value;
163 }
164 }
165
166 // Now dedent the full string
167 let result = raw.replace(/^\n/, "").replace(/\n\s*$/, "");
168 let lines = result.split("\n");
169
170 // Remove leading/trailing blank lines
171 while (lines.length > 0 && lines[0].trim() === "") {
172 lines.shift();
173 }
174 while (lines.length > 0 && lines[lines.length - 1].trim() === "") {
175 lines.pop();
176 }
177
178 // Calculate minimum indentation (excluding empty lines)
179 let minIndent = lines.reduce((min: any, line: any) => {
180 if (line.trim() === "") return min;
181 let match = line.match(/^(\s*)/);
182 let indent = match ? match[1].length : 0;
183 return min === null ? indent : Math.min(min, indent);
184 }, null);
185
186 if (minIndent !== null && minIndent > 0) {
187 // Remove the minimum indentation from each line
188 lines = lines.map((line) => line.slice(minIndent));
189 }
190
191 return lines.join("\n");
192}
193
194/**
195 * Removes code blocks from a message.
196 *
197 * Return modified message text.
198 */
199export function removeCodeBlocksAndTrim(text: string): string {
200 const codeBlockRegex = /```[\s\S]*?```/g;
201
202 // Remove code blocks from the message text
203 const textWithoutCodeBlocks = text.replace(codeBlockRegex, "");
204
205 return textWithoutCodeBlocks.trim();
206}
207
208export function splitCamelCaseAndNonAlphaNumeric(value: string) {
209 return value
210 .split(/(?<=[a-z0-9])(?=[A-Z])|[^a-zA-Z0-9]/)
211 .filter(t => t.length > 0)
212 .map(t => t.toLowerCase());
213}