1- import { Code , Change , Position , Operation , type FoldRange } from "./code" ;
1+ import { Code , Change , Position , Operation , type FoldRange , WordHighlight , areWordHighlightsEqual } from "./code" ;
22import { Renderer } from './renderer/Renderer' ;
33import { getPosFromMouse } from './mouse' ;
44import { Selection , hasDiagnosticSelection } from "./selection" ;
@@ -39,6 +39,7 @@ export interface EditorOptions {
3939 focusedDiffEnabled ?: boolean ;
4040 focusedDiffContextLines ?: number ;
4141 codeFoldingEnabled ?: boolean ;
42+ wordHighlightEnabled ?: boolean ;
4243}
4344
4445export interface EditorState {
@@ -54,6 +55,8 @@ export interface EditorState {
5455 foldRanges : FoldRange [ ] ;
5556 collapsedFoldStarts : Set < number > ;
5657 codeFoldingEnabled : boolean ;
58+ wordHighlightEnabled : boolean ;
59+ wordHighlight : WordHighlight | null ;
5760}
5861
5962export class AnycodeEditor {
@@ -104,6 +107,9 @@ export class AnycodeEditor {
104107 private readonly readOnly : boolean ;
105108 private collapsedFoldStarts : Set < number > = new Set ( ) ;
106109 private codeFoldingEnabled : boolean ;
110+
111+ private wordHighlightEnabled : boolean ;
112+ private wordHighlight : WordHighlight | null = null ;
107113
108114 constructor (
109115 initialText = '' ,
@@ -116,6 +122,7 @@ export class AnycodeEditor {
116122 this . focusedDiffEnabled = options . focusedDiffEnabled ?? false ;
117123 this . focusedDiffContextLines = Math . max ( 0 , options . focusedDiffContextLines ?? 3 ) ;
118124 this . codeFoldingEnabled = options . codeFoldingEnabled ?? true ;
125+ this . wordHighlightEnabled = options . wordHighlightEnabled ?? true ;
119126 // Set initial cursor position
120127 if ( options . line !== undefined && options . column !== undefined ) {
121128 this . offset = this . code . getOffset ( options . line , options . column ) ;
@@ -318,9 +325,40 @@ export class AnycodeEditor {
318325 public setCursor ( line : number , column : number ) : void {
319326 const offset = this . code . getOffset ( line , column ) ;
320327 this . offset = offset ;
328+ this . updateWordHighlight ( ) ;
321329 this . renderer . renderCursor ( line , column ) ;
322330 }
323331
332+ private updateWordHighlight ( ) {
333+ if ( ! this . code ) return ;
334+
335+ if ( ! this . wordHighlightEnabled ) {
336+ if ( this . wordHighlight !== null ) {
337+ this . wordHighlight = null ;
338+ if ( this . renderer ) {
339+ this . renderer . renderWordHighlight ( this . getEditorState ( ) ) ;
340+ }
341+ }
342+ return ;
343+ }
344+ const highlight = this . code . getWordAtOffset ( this . offset ) ;
345+ const hasChanged = ! areWordHighlightsEqual ( highlight , this . wordHighlight ) ;
346+
347+ if ( hasChanged ) {
348+ this . wordHighlight = highlight ;
349+ if ( this . renderer ) {
350+ this . renderer . renderWordHighlight ( this . getEditorState ( ) ) ;
351+ }
352+ }
353+ }
354+
355+ public setWordHighlightEnabled ( enabled : boolean ) {
356+ if ( this . wordHighlightEnabled !== enabled ) {
357+ this . wordHighlightEnabled = enabled ;
358+ this . updateWordHighlight ( ) ;
359+ }
360+ }
361+
324362 public setSelectionRange (
325363 startLine : number ,
326364 startColumn : number ,
@@ -332,6 +370,7 @@ export class AnycodeEditor {
332370 const endOffset = this . code . getOffset ( endLine , endColumn ) ;
333371 this . selection = new Selection ( startOffset , endOffset ) ;
334372 this . offset = endOffset ;
373+ this . updateWordHighlight ( ) ;
335374
336375 if ( center ) {
337376 this . renderer . focusCenter ( this . getEditorState ( ) ) ;
@@ -529,6 +568,8 @@ export class AnycodeEditor {
529568 foldRanges : this . code . getFoldRanges ( ) ,
530569 collapsedFoldStarts : this . collapsedFoldStarts ,
531570 codeFoldingEnabled : this . codeFoldingEnabled ,
571+ wordHighlightEnabled : this . wordHighlightEnabled ,
572+ wordHighlight : this . wordHighlight ,
532573 } ;
533574 }
534575
@@ -627,6 +668,7 @@ export class AnycodeEditor {
627668 //if (o == this.offset) { return; }
628669
629670 this . offset = o ;
671+ this . updateWordHighlight ( ) ;
630672
631673 const { line, column } = this . code . getPosition ( this . offset ) ;
632674 this . renderer . renderCursor ( line , column ) ;
@@ -1194,9 +1236,15 @@ export class AnycodeEditor {
11941236 this . code = result . ctx . code ;
11951237 this . recomputeDiffs ( ) ;
11961238 }
1197- if ( offsetChanged ) this . offset = result . ctx . offset ;
1239+ if ( offsetChanged ) {
1240+ this . offset = result . ctx . offset ;
1241+ }
11981242 if ( selectionChanged ) this . selection = result . ctx . selection || null ;
11991243
1244+ if ( textChanged || offsetChanged ) {
1245+ this . updateWordHighlight ( ) ;
1246+ }
1247+
12001248 const state = this . getEditorState ( ) ;
12011249
12021250 if ( textChanged ) {
0 commit comments