1
- import { Disposable , TextEditorSelectionChangeEvent } from "@cursorless/common" ;
2
- import type { SyntaxNode , TreeCursor } from "web-tree-sitter" ;
3
- import { ide } from "../singletons/ide.singleton" ;
4
- import { TreeSitter } from "../typings/TreeSitter" ;
1
+ import type { Disposable , IDE } from "@cursorless/common" ;
5
2
6
3
/**
7
4
* Debug logger
8
5
*/
9
6
export class Debug {
10
7
private disposableConfiguration ?: Disposable ;
11
- private disposableSelection ?: Disposable ;
12
8
active : boolean ;
13
9
14
- constructor ( private treeSitter : TreeSitter ) {
15
- ide ( ) . disposeOnExit ( this ) ;
10
+ constructor ( private ide : IDE ) {
11
+ ide . disposeOnExit ( this ) ;
16
12
17
13
this . evaluateSetting = this . evaluateSetting . bind ( this ) ;
18
- this . logBranchTypes = this . logBranchTypes . bind ( this ) ;
19
14
this . active = true ;
20
15
21
- switch ( ide ( ) . runMode ) {
16
+ switch ( ide . runMode ) {
22
17
// Development mode. Always enable.
23
18
case "development" :
24
19
this . enableDebugLog ( ) ;
@@ -31,7 +26,7 @@ export class Debug {
31
26
case "production" :
32
27
this . evaluateSetting ( ) ;
33
28
this . disposableConfiguration =
34
- ide ( ) . configuration . onDidChangeConfiguration ( this . evaluateSetting ) ;
29
+ ide . configuration . onDidChangeConfiguration ( this . evaluateSetting ) ;
35
30
break ;
36
31
}
37
32
}
@@ -46,98 +41,22 @@ export class Debug {
46
41
if ( this . disposableConfiguration ) {
47
42
this . disposableConfiguration . dispose ( ) ;
48
43
}
49
- if ( this . disposableSelection ) {
50
- this . disposableSelection . dispose ( ) ;
51
- }
52
44
}
53
45
54
46
private enableDebugLog ( ) {
55
47
this . active = true ;
56
- this . disposableSelection = ide ( ) . onDidChangeTextEditorSelection (
57
- this . logBranchTypes ,
58
- ) ;
59
48
}
60
49
61
50
private disableDebugLog ( ) {
62
51
this . active = false ;
63
- if ( this . disposableSelection ) {
64
- this . disposableSelection . dispose ( ) ;
65
- this . disposableSelection = undefined ;
66
- }
67
52
}
68
53
69
54
private evaluateSetting ( ) {
70
- const debugEnabled = ide ( ) . configuration . getOwnConfiguration ( "debug" ) ;
55
+ const debugEnabled = this . ide . configuration . getOwnConfiguration ( "debug" ) ;
71
56
if ( debugEnabled ) {
72
57
this . enableDebugLog ( ) ;
73
58
} else {
74
59
this . disableDebugLog ( ) ;
75
60
}
76
61
}
77
-
78
- private logBranchTypes ( event : TextEditorSelectionChangeEvent ) {
79
- let node : SyntaxNode ;
80
- try {
81
- node = this . treeSitter . getNodeAtLocation (
82
- ide ( ) . activeTextEditor ! . document ,
83
- event . selections [ 0 ] ,
84
- ) ;
85
- } catch ( error ) {
86
- return ;
87
- }
88
-
89
- const ancestors : SyntaxNode [ ] = [ node ] ;
90
- while ( node . parent != null ) {
91
- ancestors . unshift ( node . parent ) ;
92
- node = node . parent ;
93
- }
94
-
95
- const cursor = node . tree . walk ( ) ;
96
- this . printCursorLocationInfo ( ancestors , cursor , 0 ) ;
97
- }
98
-
99
- private printCursorLocationInfo (
100
- nodes : SyntaxNode [ ] ,
101
- cursor : TreeCursor ,
102
- index : number ,
103
- ) {
104
- const field = cursor . currentFieldName ;
105
- const fieldText = field != null ? `${ field } : ` : "" ;
106
- const indent = " " . repeat ( index ) ;
107
- const nodeIsLast = index === nodes . length - 1 ;
108
- const { nodeIsNamed } = cursor ;
109
- let text = `${ indent } ${ fieldText } ` ;
110
-
111
- if ( nodeIsNamed ) {
112
- text += `(${ cursor . nodeType } ` ;
113
- if ( nodeIsLast ) {
114
- text += ")" ;
115
- }
116
- } else {
117
- text += `"${ cursor . nodeType } "` ;
118
- }
119
-
120
- console . log ( text ) ;
121
-
122
- if (
123
- ! nodeIsLast &&
124
- this . cursorGoToChildWithId ( cursor , nodes [ index + 1 ] . id )
125
- ) {
126
- this . printCursorLocationInfo ( nodes , cursor , index + 1 ) ;
127
- }
128
-
129
- if ( nodeIsNamed && ! nodeIsLast ) {
130
- console . log ( `${ indent } )` ) ;
131
- }
132
- }
133
-
134
- private cursorGoToChildWithId ( cursor : TreeCursor , id : number ) : boolean {
135
- cursor . gotoFirstChild ( ) ;
136
- while ( cursor . currentNode . id !== id ) {
137
- if ( ! cursor . gotoNextSibling ( ) ) {
138
- return false ;
139
- }
140
- }
141
- return true ;
142
- }
143
62
}
0 commit comments