1- import * as fs from "fs" ;
2- import * as path from "path" ;
3- import * as semver from "semver" ;
4- import * as vscode from "vscode" ;
5- import * as treeSitter from "web-tree-sitter" ;
1+ import * as path from "node:path" ;
2+ import type {
3+ ExtensionContext ,
4+ TextDocument ,
5+ TextDocumentChangeEvent ,
6+ } from "vscode" ;
7+ import { window , workspace } from "vscode" ;
8+ import type { Tree } from "web-tree-sitter" ;
9+ import { Parser , Query , Language as TreeSitterLanguage } from "web-tree-sitter" ;
10+ import {
11+ isLanguageDisabled ,
12+ throwIfLanguageIsDisabled ,
13+ } from "./disabledLanguages" ;
614import {
715 DeprecatedError ,
816 LanguageFailedToLoad ,
917 UnsupportedLanguageError ,
1018} from "./errors" ;
19+ import { languages } from "./languages" ;
1120import { Trees } from "./Trees" ;
12- import { isDocumentVisible } from "./utils" ;
13- import type { Language } from "./types" ;
14-
15- /* eslint-disable @typescript-eslint/naming-convention */
16-
17- // Be sure to declare the language in package.json
18- const languages : Record < string , Language | undefined > = {
19- "java-properties" : { module : "tree-sitter-properties" } ,
20- "talon-list" : { module : "tree-sitter-talon" } ,
21- agda : { module : "tree-sitter-agda" } ,
22- c : { module : "tree-sitter-c" } ,
23- clojure : { module : "tree-sitter-clojure" } ,
24- cpp : { module : "tree-sitter-cpp" } ,
25- csharp : { module : "tree-sitter-c_sharp" } ,
26- css : { module : "tree-sitter-css" } ,
27- dart : { module : "tree-sitter-dart" } ,
28- elixir : { module : "tree-sitter-elixir" } ,
29- elm : { module : "tree-sitter-elm" } ,
30- gdscript : { module : "tree-sitter-gdscript" } ,
31- gleam : { module : "tree-sitter-gleam" } ,
32- go : { module : "tree-sitter-go" } ,
33- haskell : { module : "tree-sitter-haskell" } ,
34- html : { module : "tree-sitter-html" } ,
35- java : { module : "tree-sitter-java" } ,
36- javascript : { module : "tree-sitter-javascript" } ,
37- javascriptreact : { module : "tree-sitter-javascript" } ,
38- json : { module : "tree-sitter-json" } ,
39- jsonc : { module : "tree-sitter-json" } ,
40- jsonl : { module : "tree-sitter-json" } ,
41- julia : { module : "tree-sitter-julia" } ,
42- kotlin : { module : "tree-sitter-kotlin" } ,
43- latex : { module : "tree-sitter-latex" } ,
44- lua : { module : "tree-sitter-lua" } ,
45- markdown : { module : "tree-sitter-markdown" } ,
46- nix : { module : "tree-sitter-nix" } ,
47- perl : { module : "tree-sitter-perl" } ,
48- php : { module : "tree-sitter-php" } ,
49- properties : { module : "tree-sitter-properties" } ,
50- python : { module : "tree-sitter-python" } ,
51- r : { module : "tree-sitter-r" } ,
52- ruby : { module : "tree-sitter-ruby" } ,
53- rust : { module : "tree-sitter-rust" } ,
54- scala : { module : "tree-sitter-scala" } ,
55- scm : { module : "tree-sitter-query" } ,
56- scss : { module : "tree-sitter-scss" } ,
57- shellscript : { module : "tree-sitter-bash" } ,
58- sparql : { module : "tree-sitter-sparql" } ,
59- starlark : { module : "tree-sitter-python" } ,
60- swift : { module : "tree-sitter-swift" } ,
61- talon : { module : "tree-sitter-talon" } ,
62- terraform : { module : "tree-sitter-hcl" } ,
63- typescript : { module : "tree-sitter-typescript" } ,
64- typescriptreact : { module : "tree-sitter-tsx" } ,
65- xml : { module : "tree-sitter-xml" } ,
66- yaml : { module : "tree-sitter-yaml" } ,
67- zig : { module : "tree-sitter-zig" } ,
68- } ;
21+ import { getWasmPath , isDocumentVisible } from "./utils" ;
6922
7023// For some reason this crashes if we put it inside activate
7124// Fix: this isn't a field, suppress package member coloring like Go
72- const initParser = treeSitter . Parser . init ( ) ;
25+ const initParser = Parser . init ( ) ;
7326
74- export function activate ( context : vscode . ExtensionContext ) {
27+ export function activate ( context : ExtensionContext ) {
7528 // Parse of all visible documents
7629 const trees = new Trees ( ) ;
7730
78- /**
79- * FIXME: On newer vscode versions some Tree sitter parser throws memory errors
80- * https://github.com/cursorless-dev/cursorless/issues/2879
81- * https://github.com/cursorless-dev/vscode-parse-tree/issues/110
82- */
83- const disabledLanguages =
84- semver . lt ( vscode . version , "1.107.0" ) && semver . gte ( vscode . version , "1.98.0" )
85- ? new Set ( [ "latex" , "swift" ] )
86- : null ;
87-
88- const validateGetLanguage = ( languageId : string ) => {
89- if ( disabledLanguages ?. has ( languageId ) ) {
90- throw new Error (
91- `${ languageId } is disabled on vscode versions 1.98.0 through 1.06.3. See https://github.com/cursorless-dev/cursorless/issues/2879` ,
92- ) ;
93- }
94- } ;
95-
9631 /**
9732 * Load the parser model for a given language
9833 * @param languageId The vscode language id of the language to load
@@ -112,45 +47,31 @@ export function activate(context: vscode.ExtensionContext) {
11247 }
11348
11449 // Disabled on certain vscode versions due to memory errors in tree-sitter parsers
115- if ( disabledLanguages ?. has ( languageId ) ) {
50+ if ( isLanguageDisabled ( languageId ) ) {
11651 return false ;
11752 }
11853
119- const absolute = getWasmPath ( language . module ) ;
54+ const absolute = getWasmPath ( context . extensionPath , language . module ) ;
12055 const wasm = path . relative ( process . cwd ( ) , absolute ) ;
12156
12257 await initParser ;
12358
124- const lang = await treeSitter . Language . load ( wasm ) ;
125- const parser = new treeSitter . Parser ( ) ;
59+ const lang = await TreeSitterLanguage . load ( wasm ) ;
60+ const parser = new Parser ( ) ;
12661 parser . setLanguage ( lang ) ;
12762 language . parser = parser ;
12863
12964 return true ;
13065 }
13166
132- function getWasmPath ( moduleName : string ) : string {
133- const absolute = path . join (
134- context . extensionPath ,
135- "parsers" ,
136- moduleName + ".wasm" ,
137- ) ;
138-
139- if ( ! fs . existsSync ( absolute ) ) {
140- throw Error ( `Parser ${ moduleName } not found at ${ absolute } ` ) ;
141- }
142-
143- return absolute ;
144- }
145-
14667 /**
14768 * Open a document and parse it, returning the resulting tree
14869 * @param document the document to open
14970 * @returns the resulting tree, or undefined if the language couldn't be loaded
15071 */
15172 async function openDocument (
152- document : vscode . TextDocument ,
153- ) : Promise < treeSitter . Tree | undefined > {
73+ document : TextDocument ,
74+ ) : Promise < Tree | undefined > {
15475 const uriString = document . uri . toString ( ) ;
15576 let tree = trees . get ( uriString ) ;
15677
@@ -186,9 +107,7 @@ export function activate(context: vscode.ExtensionContext) {
186107 * @param document the document to get the tree for
187108 * @returns the parse tree for the document
188109 */
189- async function getTree (
190- document : vscode . TextDocument ,
191- ) : Promise < treeSitter . Tree > {
110+ async function getTree ( document : TextDocument ) : Promise < Tree > {
192111 const uriString = document . uri . toString ( ) ;
193112 let tree = trees . get ( uriString ) ;
194113
@@ -203,7 +122,7 @@ export function activate(context: vscode.ExtensionContext) {
203122 }
204123
205124 if ( document . languageId in languages ) {
206- validateGetLanguage ( document . languageId ) ;
125+ throwIfLanguageIsDisabled ( document . languageId ) ;
207126 throw new LanguageFailedToLoad ( document . languageId ) ;
208127 }
209128
@@ -216,47 +135,44 @@ export function activate(context: vscode.ExtensionContext) {
216135 * @param source the source of the query
217136 * @returns the created query, or undefined if the language couldn't be loaded
218137 */
219- function createQuery (
220- languageId : string ,
221- source : string ,
222- ) : treeSitter . Query | undefined {
138+ function createQuery ( languageId : string , source : string ) : Query | undefined {
223139 const language = languages [ languageId ] ?. parser ?. language ;
224140 if ( language == null ) {
225- validateGetLanguage ( languageId ) ;
141+ throwIfLanguageIsDisabled ( languageId ) ;
226142 return undefined ;
227143 }
228- return new treeSitter . Query ( language , source ) ;
144+ return new Query ( language , source ) ;
229145 }
230146
231147 // NOTE: if you make this an async function, it seems to cause edit anomalies
232- function onChange ( edit : vscode . TextDocumentChangeEvent ) {
148+ function onChange ( edit : TextDocumentChangeEvent ) {
233149 const language = languages [ edit . document . languageId ] ;
234150 if ( language ?. parser != null ) {
235151 trees . updateTree ( language . parser , edit ) ;
236152 }
237153 }
238154
239155 async function openAllVisibleDocuments ( ) {
240- for ( const editor of vscode . window . visibleTextEditors ) {
156+ for ( const editor of window . visibleTextEditors ) {
241157 await openDocument ( editor . document ) ;
242158 }
243159 }
244160
245- async function openDocumentIfVisible ( document : vscode . TextDocument ) {
161+ async function openDocumentIfVisible ( document : TextDocument ) {
246162 if ( isDocumentVisible ( document ) ) {
247163 await openDocument ( document ) ;
248164 }
249165 }
250166
251- function closeDocument ( document : vscode . TextDocument ) {
167+ function closeDocument ( document : TextDocument ) {
252168 trees . delete ( document . uri . toString ( ) ) ;
253169 }
254170
255171 context . subscriptions . push (
256- vscode . window . onDidChangeVisibleTextEditors ( openAllVisibleDocuments ) ,
257- vscode . workspace . onDidChangeTextDocument ( onChange ) ,
258- vscode . workspace . onDidCloseTextDocument ( closeDocument ) ,
259- vscode . workspace . onDidOpenTextDocument ( openDocumentIfVisible ) ,
172+ window . onDidChangeVisibleTextEditors ( openAllVisibleDocuments ) ,
173+ workspace . onDidChangeTextDocument ( onChange ) ,
174+ workspace . onDidCloseTextDocument ( closeDocument ) ,
175+ workspace . onDidOpenTextDocument ( openDocumentIfVisible ) ,
260176 ) ;
261177
262178 // Don't wait for the initial load, it takes too long to inspect the themes and causes VSCode extension host to hang
0 commit comments