11import type ShikiPlugin from 'src/main' ;
22import { SHIKI_INLINE_REGEX } from 'src/main' ;
33import { Decoration , type DecorationSet , type EditorView , ViewPlugin , type ViewUpdate } from '@codemirror/view' ;
4- import { type Range } from '@codemirror/state' ;
4+ import { type EditorState , type Range } from '@codemirror/state' ;
55import { type SyntaxNode } from '@lezer/common' ;
66import { syntaxTree } from '@codemirror/language' ;
77import { Cm6_Util } from 'src/codemirror/Cm6_Util' ;
88import { type ThemedToken } from 'shiki' ;
9+ import { editorLivePreviewField } from 'obsidian' ;
910
1011// eslint-disable-next-line @typescript-eslint/no-explicit-any
1112export function createCm6Plugin ( plugin : ShikiPlugin ) : ViewPlugin < any > {
@@ -33,10 +34,16 @@ export function createCm6Plugin(plugin: ShikiPlugin): ViewPlugin<any> {
3334 }
3435 }
3536
37+ isLivePreview ( state : EditorState ) : boolean {
38+ // @ts -ignore some strange private field not being assignable
39+ return state . field ( editorLivePreviewField ) ;
40+ }
41+
3642 /**
3743 * Updates all the widgets by traversing the syntax tree.
3844 *
3945 * @param view
46+ * @param docChanged
4047 */
4148 updateWidgets ( view : EditorView , docChanged : boolean = true ) : void {
4249 let lang = '' ;
@@ -60,10 +67,12 @@ export function createCm6Plugin(plugin: ShikiPlugin): ViewPlugin<any> {
6067 if ( content . startsWith ( '{' ) && plugin . settings . inlineHighlighting ) {
6168 const match = content . match ( SHIKI_INLINE_REGEX ) ; // format: `{lang} code`
6269 if ( match ) {
63- // if there is selection overlap, the user has the inline code block selected, so we don't want to highlight it
64- if ( Cm6_Util . checkSelectionAndRangeOverlap ( view . state . selection , node . from - 1 , node . to + 1 ) ) {
70+ const hasSelectionOverlap = Cm6_Util . checkSelectionAndRangeOverlap ( view . state . selection , node . from - 1 , node . to + 1 ) ;
71+
72+ // if there is selection overlap, the user has the inline code block selected, so we don't want to hide the language tag.
73+ // For this we just remove the decorations and rebuild them with the language tag visible.
74+ if ( hasSelectionOverlap ) {
6575 this . removeDecoration ( node . from , node . to ) ;
66- return ;
6776 }
6877 const hideTo = node . from + match [ 1 ] . length + 3 ; // hide `{lang} `
6978
@@ -72,7 +81,9 @@ export function createCm6Plugin(plugin: ShikiPlugin): ViewPlugin<any> {
7281
7382 this . removeDecoration ( node . from , node . to ) ;
7483 // add the decoration that hides the language tag
75- decorations . unshift ( Decoration . replace ( { } ) . range ( node . from , hideTo ) ) ;
84+ if ( this . isLivePreview ( view . state ) && ! hasSelectionOverlap ) {
85+ decorations . unshift ( Decoration . replace ( { } ) . range ( node . from , hideTo ) ) ;
86+ }
7687 // add the highlight decorations
7788 this . addDecoration ( node . from , node . to , decorations ) ;
7889 } catch ( e ) {
@@ -157,14 +168,10 @@ export function createCm6Plugin(plugin: ShikiPlugin): ViewPlugin<any> {
157168 addDecoration ( from : number , to : number , newDecorations : Range < Decoration > [ ] ) : void {
158169 // check if the decoration already exists and only add it if it does not exist
159170 if ( Cm6_Util . existsDecorationBetween ( this . decorations , from , to ) ) {
160- console . log ( 'exists' ) ;
161-
162171 return ;
163172 }
164173
165174 if ( newDecorations . length === 0 ) {
166- console . log ( 'empty' ) ;
167-
168175 return ;
169176 }
170177
0 commit comments