MCPcopy Create free account
hub / github.com/continuedev/continue / detectLanguage

Function detectLanguage

extensions/cli/src/ui/SyntaxHighlighter.ts:206–243  ·  view source on GitHub ↗
(code: string)

Source from the content-addressed store, hash-verified

204}
205
206export function detectLanguage(code: string): string {
207 // Simple language detection based on common patterns
208 const patterns = [
209 { regex: /^\s*import\s+.*from\s+['"]/, language: "javascript" },
210 { regex: /^\s*const\s+\w+\s*=\s*require\s*\(/, language: "javascript" },
211 { regex: /^\s*function\s+\w+\s*\(/, language: "javascript" },
212 { regex: /^\s*interface\s+\w+/, language: "typescript" },
213 { regex: /^\s*type\s+\w+\s*=/, language: "typescript" },
214 { regex: /^\s*def\s+\w+.*:/, language: "python" },
215 { regex: /^\s*class\s+\w+.*:/, language: "python" },
216 { regex: /^\s*public\s+class\s+\w+/, language: "java" },
217 { regex: /^\s*#include\s*</, language: "c" },
218 { regex: /^\s*using\s+namespace\s+/, language: "cpp" },
219 { regex: /^\s*using\s+System\s*;/, language: "csharp" },
220 { regex: /^\s*package\s+main/, language: "go" },
221 { regex: /^\s*fn\s+\w+\s*\(/, language: "rust" },
222 { regex: /^\s*<\?php/, language: "php" },
223 { regex: /^\s*SELECT\s+.*FROM\s+/i, language: "sql" },
224 { regex: /^\s*\{\s*$/, language: "json" },
225 { regex: /^\s*---\s*$/, language: "yaml" },
226 { regex: /^\s*#!/, language: "bash" },
227 { regex: /^\s*<(!DOCTYPE html|html)/i, language: "html" },
228 { regex: /^\s*@media\s+/, language: "css" },
229 { regex: /^\s*#\s+/, language: "markdown" },
230 ];
231
232 // Check patterns line by line for multiline code
233 const lines = code.split("\n");
234 for (const line of lines) {
235 for (const pattern of patterns) {
236 if (pattern.regex.test(line)) {
237 return pattern.language;
238 }
239 }
240 }
241
242 return "javascript"; // default fallback
243}

Callers 2

renderMarkdownFunction · 0.70

Calls 1

testMethod · 0.80

Tested by

no test coverage detected