-
Notifications
You must be signed in to change notification settings - Fork 485
Expand file tree
/
Copy pathlanguages.ts
More file actions
155 lines (143 loc) · 5.46 KB
/
languages.ts
File metadata and controls
155 lines (143 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import * as path from 'path'
// Import some types for wasm & .scm files
import './types'
/* ------------------------------------------------------------------ */
/* 1 . WASM files
/* ------------------------------------------------------------------ */
// Import WASM files from @vscode/tree-sitter-wasm
import csharpWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-c-sharp.wasm' with { type: 'file' }
import cppWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-cpp.wasm' with { type: 'file' }
import goWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-go.wasm' with { type: 'file' }
import javaWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-java.wasm' with { type: 'file' }
import javascriptWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-javascript.wasm' with { type: 'file' }
import pythonWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-python.wasm' with { type: 'file' }
import rubyWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-ruby.wasm' with { type: 'file' }
import rustWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-rust.wasm' with { type: 'file' }
import tsxWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-tsx.wasm' with { type: 'file' }
import typescriptWasmPath from '@vscode/tree-sitter-wasm/wasm/tree-sitter-typescript.wasm' with { type: 'file' }
import { Language, Parser, Query } from 'web-tree-sitter'
import { DEBUG_PARSING } from './parse'
/* ------------------------------------------------------------------ */
/* 2 . Queries
/* ------------------------------------------------------------------ */
import csharpQuery from './tree-sitter-queries/tree-sitter-c_sharp-tags.scm'
import cppQuery from './tree-sitter-queries/tree-sitter-cpp-tags.scm'
import goQuery from './tree-sitter-queries/tree-sitter-go-tags.scm'
import javaQuery from './tree-sitter-queries/tree-sitter-java-tags.scm'
import javascriptQuery from './tree-sitter-queries/tree-sitter-javascript-tags.scm'
import pythonQuery from './tree-sitter-queries/tree-sitter-python-tags.scm'
import rubyQuery from './tree-sitter-queries/tree-sitter-ruby-tags.scm'
import rustQuery from './tree-sitter-queries/tree-sitter-rust-tags.scm'
import typescriptQuery from './tree-sitter-queries/tree-sitter-typescript-tags.scm'
/* ------------------------------------------------------------------ */
/* 2 . Data structures */
/* ------------------------------------------------------------------ */
export interface LanguageConfig {
extensions: string[]
wasmFile: string
queryText: string
/* Loaded lazily ↓ */
parser?: Parser
query?: Query
language?: Language
}
const languageTable: LanguageConfig[] = [
{
extensions: ['.ts'],
wasmFile: typescriptWasmPath,
queryText: typescriptQuery,
},
{
extensions: ['.tsx'],
wasmFile: tsxWasmPath,
queryText: typescriptQuery,
},
{
extensions: ['.js', '.jsx'],
wasmFile: javascriptWasmPath,
queryText: javascriptQuery,
},
{
extensions: ['.py'],
wasmFile: pythonWasmPath,
queryText: pythonQuery,
},
{
extensions: ['.java'],
wasmFile: javaWasmPath,
queryText: javaQuery,
},
{
extensions: ['.cs'],
wasmFile: csharpWasmPath,
queryText: csharpQuery,
},
// Note: C WASM not available in @vscode/tree-sitter-wasm, keeping disabled for now
// {
// extensions: ['.c', '.h'],
// wasmFile: cWasm,
// queryText: cQuery,
// },
{
extensions: ['.cpp', '.hpp'],
wasmFile: cppWasmPath,
queryText: cppQuery,
},
{
extensions: ['.rs'],
wasmFile: rustWasmPath,
queryText: rustQuery,
},
{
extensions: ['.rb'],
wasmFile: rubyWasmPath,
queryText: rubyQuery,
},
{ extensions: ['.go'], wasmFile: goWasmPath, queryText: goQuery },
// Note: PHP WASM not available in @vscode/tree-sitter-wasm, keeping disabled for now
// {
// extensions: ['.php'],
// wasmFile: phpWasm,
// queryText: phpQuery,
// },
]
/* ------------------------------------------------------------------ */
/* 4 . One-time library init */
/* ------------------------------------------------------------------ */
// Initialize tree-sitter - in binary builds, WASM files are bundled as assets
const parserReady = Parser.init()
/* ------------------------------------------------------------------ */
/* 5 . Public helper */
/* ------------------------------------------------------------------ */
export async function getLanguageConfig(
filePath: string,
): Promise<LanguageConfig | undefined> {
const ext = path.extname(filePath)
const cfg = languageTable.find((c) => c.extensions.includes(ext))
if (!cfg) return undefined
if (!cfg.parser) {
try {
await parserReady
// Use the imported WASM file directly
const parser = new Parser()
// NOTE (James): For some reason, Bun gives the wrong path to the imported WASM file,
// so we need to delete one level of ../.
let lang
try {
const actualPath = cfg.wasmFile.replace('../', '')
lang = await Language.load(actualPath)
} catch (err) {
lang = await Language.load(cfg.wasmFile)
}
parser.setLanguage(lang)
cfg.language = lang
cfg.parser = parser
cfg.query = new Query(lang, cfg.queryText)
} catch (err) {
if (DEBUG_PARSING)
console.error('[tree-sitter] load error for', filePath, err)
return undefined
}
}
return cfg
}