@@ -3,6 +3,7 @@ import type {
33 ExtensionContext ,
44 TextDocument ,
55 TextDocumentChangeEvent ,
6+ Uri ,
67} from "vscode" ;
78import { window , workspace } from "vscode" ;
89import type { Tree } from "web-tree-sitter" ;
@@ -102,19 +103,42 @@ export function activate(context: ExtensionContext) {
102103 return tree ;
103104 }
104105
106+ /**
107+ * Create a tree-sitter query for a given language and query source
108+ * @param languageId the vscode language id of the language to create the query for
109+ * @param source the source of the query
110+ * @returns the created query, or undefined if the language couldn't be loaded
111+ */
112+ function createQuery ( languageId : string , source : string ) : Query | undefined {
113+ const language = languages [ languageId ] ?. parser ?. language ;
114+ if ( language == null ) {
115+ throwIfLanguageIsDisabled ( languageId ) ;
116+ return undefined ;
117+ }
118+ return new Query ( language , source ) ;
119+ }
120+
105121 /**
106122 * Get the parse tree for a given document, parsing it if necessary
107123 * @param document the document to get the tree for
108124 * @returns the parse tree for the document
109125 */
110- async function getTree ( document : TextDocument ) : Promise < Tree > {
111- const uriString = document . uri . toString ( ) ;
126+ async function getTreeForUri ( uri : Uri ) : Promise < Tree > {
127+ const uriString = uri . toString ( ) ;
112128 let tree = trees . get ( uriString ) ;
113129
114130 if ( tree != null ) {
115131 return tree ;
116132 }
117133
134+ const document = workspace . textDocuments . find (
135+ ( textDocument ) => textDocument . uri . toString ( ) === uri . toString ( ) ,
136+ ) ;
137+
138+ if ( document == null ) {
139+ throw new Error ( `Document ${ uriString } is not open` ) ;
140+ }
141+
118142 tree = await openDocument ( document ) ;
119143
120144 if ( tree != null ) {
@@ -129,21 +153,6 @@ export function activate(context: ExtensionContext) {
129153 throw new UnsupportedLanguageError ( document . languageId ) ;
130154 }
131155
132- /**
133- * Create a tree-sitter query for a given language and query source
134- * @param languageId the vscode language id of the language to create the query for
135- * @param source the source of the query
136- * @returns the created query, or undefined if the language couldn't be loaded
137- */
138- function createQuery ( languageId : string , source : string ) : Query | undefined {
139- const language = languages [ languageId ] ?. parser ?. language ;
140- if ( language == null ) {
141- throwIfLanguageIsDisabled ( languageId ) ;
142- return undefined ;
143- }
144- return new Query ( language , source ) ;
145- }
146-
147156 // NOTE: if you make this an async function, it seems to cause edit anomalies
148157 function onChange ( edit : TextDocumentChangeEvent ) {
149158 const language = languages [ edit . document . languageId ] ;
@@ -180,15 +189,16 @@ export function activate(context: ExtensionContext) {
180189
181190 return {
182191 loadLanguage,
183- getTree,
184192 createQuery,
193+ getTreeForUri,
194+
195+ getTree ( document : TextDocument ) {
196+ return getTreeForUri ( document . uri ) ;
197+ } ,
185198
186199 getLanguage ( ) {
187200 throw new DeprecatedError ( "getLanguage" ) ;
188201 } ,
189- getTreeForUri ( ) {
190- throw new DeprecatedError ( "getTreeForUri" ) ;
191- } ,
192202 getNodeAtLocation ( ) {
193203 throw new DeprecatedError ( "getNodeAtLocation" ) ;
194204 } ,
0 commit comments