1
- import { Range , TextDocument } from "@cursorless/common" ;
1
+ import {
2
+ Disposable ,
3
+ FileSystem ,
4
+ Notifier ,
5
+ Range ,
6
+ TextDocument ,
7
+ getCursorlessRepoRoot ,
8
+ } from "@cursorless/common" ;
9
+ import { join } from "path" ;
2
10
import { SyntaxNode } from "web-tree-sitter" ;
3
11
import { TreeSitter } from ".." ;
12
+ import { ide } from "../singletons/ide.singleton" ;
4
13
import { LanguageDefinition } from "./LanguageDefinition" ;
5
14
6
15
/**
@@ -14,6 +23,8 @@ const LANGUAGE_UNDEFINED = Symbol("LANGUAGE_UNDEFINED");
14
23
* constructing them as necessary
15
24
*/
16
25
export class LanguageDefinitions {
26
+ private notifier : Notifier = new Notifier ( ) ;
27
+
17
28
/**
18
29
* Maps from language id to {@link LanguageDefinition} or
19
30
* {@link LANGUAGE_UNDEFINED} if language doesn't have new-style definitions.
@@ -28,8 +39,31 @@ export class LanguageDefinitions {
28
39
string ,
29
40
LanguageDefinition | typeof LANGUAGE_UNDEFINED
30
41
> = new Map ( ) ;
42
+ private queryDir : string ;
43
+ private disposables : Disposable [ ] = [ ] ;
44
+
45
+ constructor (
46
+ fileSystem : FileSystem ,
47
+ private treeSitter : TreeSitter ,
48
+ ) {
49
+ // Use the repo root as the root for development mode, so that we can
50
+ // we can make hot-reloading work for the queries
51
+ this . queryDir = join (
52
+ ide ( ) . runMode === "development"
53
+ ? getCursorlessRepoRoot ( )
54
+ : ide ( ) . assetsRoot ,
55
+ "queries" ,
56
+ ) ;
31
57
32
- constructor ( private treeSitter : TreeSitter ) { }
58
+ if ( ide ( ) . runMode === "development" ) {
59
+ this . disposables . push (
60
+ fileSystem . watchDir ( this . queryDir , ( ) => {
61
+ this . languageDefinitions . clear ( ) ;
62
+ this . notifier . notifyListeners ( ) ;
63
+ } ) ,
64
+ ) ;
65
+ }
66
+ }
33
67
34
68
/**
35
69
* Get a language definition for the given language id, if the language
@@ -44,7 +78,7 @@ export class LanguageDefinitions {
44
78
45
79
if ( definition == null ) {
46
80
definition =
47
- LanguageDefinition . create ( this . treeSitter , languageId ) ??
81
+ LanguageDefinition . create ( this . treeSitter , this . queryDir , languageId ) ??
48
82
LANGUAGE_UNDEFINED ;
49
83
50
84
this . languageDefinitions . set ( languageId , definition ) ;
@@ -59,4 +93,10 @@ export class LanguageDefinitions {
59
93
public getNodeAtLocation ( document : TextDocument , range : Range ) : SyntaxNode {
60
94
return this . treeSitter . getNodeAtLocation ( document , range ) ;
61
95
}
96
+
97
+ onDidChangeDefinition = this . notifier . registerListener ;
98
+
99
+ dispose ( ) {
100
+ this . disposables . forEach ( ( disposable ) => disposable . dispose ( ) ) ;
101
+ }
62
102
}
0 commit comments