MCPcopy Index your code
hub / github.com/CodeGraphContext/CodeGraphContext / getLanguageForFile

Function getLanguageForFile

website/src/lib/parser.worker.ts:27–78  ·  view source on GitHub ↗
(path: string)

Source from the content-addressed store, hash-verified

25}
26
27async function getLanguageForFile(path: string) {
28 if (!parser) await initParser();
29 const extMatch = path.match(/\.([a-zA-Z0-9]+)$/);
30 if (!extMatch) return null;
31 const ext = extMatch[1].toLowerCase();
32
33 let wasmName = '';
34 switch(ext) {
35 case 'py': wasmName = 'tree-sitter-python.wasm'; break;
36 case 'js':
37 case 'jsx': wasmName = 'tree-sitter-javascript.wasm'; break;
38 case 'ts': wasmName = 'tree-sitter-typescript.wasm'; break;
39 case 'tsx': wasmName = 'tree-sitter-tsx.wasm'; break;
40 case 'java': wasmName = 'tree-sitter-java.wasm'; break;
41 case 'c':
42 case 'h': wasmName = 'tree-sitter-c.wasm'; break;
43 case 'cpp':
44 case 'hpp':
45 case 'cc': wasmName = 'tree-sitter-cpp.wasm'; break;
46 case 'cs': wasmName = 'tree-sitter-c_sharp.wasm'; break;
47 case 'go': wasmName = 'tree-sitter-go.wasm'; break;
48 case 'rs': wasmName = 'tree-sitter-rust.wasm'; break;
49 case 'rb': wasmName = 'tree-sitter-ruby.wasm'; break;
50 case 'php': wasmName = 'tree-sitter-php.wasm'; break;
51 case 'swift': wasmName = 'tree-sitter-swift.wasm'; break;
52 case 'kt':
53 case 'kts': wasmName = 'tree-sitter-kotlin.wasm'; break;
54 case 'dart': wasmName = 'tree-sitter-dart.wasm'; break;
55 case 'pl':
56 case 'pm': wasmName = 'tree-sitter-perl.wasm'; break;
57 default: return null;
58 }
59
60 if (wasmLanguageCache.has(wasmName)) {
61 return wasmLanguageCache.get(wasmName);
62 }
63
64 try {
65 const response = await fetch(`${location.origin}/wasm/${wasmName}`);
66 if (!response.ok) {
67 console.warn(`Could not load tree-sitter language: ${wasmName}. Proceeding without it.`);
68 return null;
69 }
70 const buffer = await response.arrayBuffer();
71 const lang = await Language.load(new Uint8Array(buffer));
72 wasmLanguageCache.set(wasmName, lang);
73 return lang;
74 } catch (err) {
75 console.warn(`Could not load tree-sitter language: ${wasmName}. Proceeding without it.`, err);
76 return null;
77 }
78}
79
80// ---------------------------------------------------------------------------
81// Language-specific S-expression query strings (Query API)

Callers 1

processNextBatchFunction · 0.85

Calls 4

initParserFunction · 0.85
hasMethod · 0.65
getMethod · 0.65
loadMethod · 0.45

Tested by

no test coverage detected