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" ;
52
63/**
74 * Debug logger
85 */
96export class Debug {
107 private disposableConfiguration ?: Disposable ;
11- private disposableSelection ?: Disposable ;
128 active : boolean ;
139
14- constructor ( private treeSitter : TreeSitter ) {
15- ide ( ) . disposeOnExit ( this ) ;
10+ constructor ( private ide : IDE ) {
11+ ide . disposeOnExit ( this ) ;
1612
1713 this . evaluateSetting = this . evaluateSetting . bind ( this ) ;
18- this . logBranchTypes = this . logBranchTypes . bind ( this ) ;
1914 this . active = true ;
2015
21- switch ( ide ( ) . runMode ) {
16+ switch ( ide . runMode ) {
2217 // Development mode. Always enable.
2318 case "development" :
2419 this . enableDebugLog ( ) ;
@@ -31,7 +26,7 @@ export class Debug {
3126 case "production" :
3227 this . evaluateSetting ( ) ;
3328 this . disposableConfiguration =
34- ide ( ) . configuration . onDidChangeConfiguration ( this . evaluateSetting ) ;
29+ ide . configuration . onDidChangeConfiguration ( this . evaluateSetting ) ;
3530 break ;
3631 }
3732 }
@@ -46,98 +41,22 @@ export class Debug {
4641 if ( this . disposableConfiguration ) {
4742 this . disposableConfiguration . dispose ( ) ;
4843 }
49- if ( this . disposableSelection ) {
50- this . disposableSelection . dispose ( ) ;
51- }
5244 }
5345
5446 private enableDebugLog ( ) {
5547 this . active = true ;
56- this . disposableSelection = ide ( ) . onDidChangeTextEditorSelection (
57- this . logBranchTypes ,
58- ) ;
5948 }
6049
6150 private disableDebugLog ( ) {
6251 this . active = false ;
63- if ( this . disposableSelection ) {
64- this . disposableSelection . dispose ( ) ;
65- this . disposableSelection = undefined ;
66- }
6752 }
6853
6954 private evaluateSetting ( ) {
70- const debugEnabled = ide ( ) . configuration . getOwnConfiguration ( "debug" ) ;
55+ const debugEnabled = this . ide . configuration . getOwnConfiguration ( "debug" ) ;
7156 if ( debugEnabled ) {
7257 this . enableDebugLog ( ) ;
7358 } else {
7459 this . disableDebugLog ( ) ;
7560 }
7661 }
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- }
14362}
0 commit comments