@@ -5,7 +5,7 @@ import CodeBlockCustomizerPlugin from "../main";
55import { CodeblockCustomizerSettings , FoldingScope } from "../Settings" ;
66import { DEFAULT_COLLAPSE_TEXT , fadeOutLineCount } from "../Const" ;
77import { FoldCommand , FoldingState } from "../EditorView/EditorEffects" ;
8- import { createButtons , toggleFold , extractLinesFromHTML , attachEventListeners , renderCodeBlockLines , CodeBlockData , extractCodeBlocksFromSection , extractCodeBlocksFromAdmonition , reassignFadeOutClasses , getHtmlCodeBlockData , prepareHtmlCodeElement } from "./ReadingViewUtils" ;
8+ import { createButtons , toggleFold , extractLinesFromHTML , attachEventListeners , renderCodeBlockLines , CodeBlockData , extractCodeBlocksFromSection , extractCodeBlocksFromAdmonition , reassignFadeOutClasses , getHtmlCodeBlockData , prepareHtmlCodeElement , getBlockLine } from "./ReadingViewUtils" ;
99import { CBCParameters , getAllParameters } from "../Parsing" ;
1010import { countHtmlCodeElements , findHtmlCodeBlocks , isHtmlCodeBlockStart } from "../HtmlCodeBlocks" ;
1111
@@ -64,12 +64,12 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
6464
6565 this . allPreElements = [ preElement ] ;
6666
67- const charPos = this . getCharPos ( allFileLines ) ;
67+ const charPos = this . getCharPos ( allFileLines , sectionInfo . lineStart ) ;
6868 const blockData : CodeBlockData = {
6969 firstLine : firstLine ,
7070 contentLines : contentLines ,
71- startLine : sectionInfo . lineStart ,
72- endLine : sectionInfo . lineEnd
71+ startLine : 0 ,
72+ endLine : sectionInfo . lineEnd - sectionInfo . lineStart
7373 } ;
7474 await this . processSingleCodeBlock ( preElement , blockData , { charPos, isPrinting : false } ) ;
7575 } // renderExternal
@@ -196,9 +196,7 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
196196 return ;
197197 }
198198
199- const charPos = this . getCharPos ( fileContentLines ) ;
200-
201- await this . processCodeBlockFirstLines ( allBlocks , charPos ) ;
199+ await this . processCodeBlockFirstLines ( allBlocks , fileContentLines ) ;
202200 } // processCodeBlockSection
203201
204202 private isHtmlCodeBlockSection ( firstLine : string | undefined ) {
@@ -232,13 +230,9 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
232230 }
233231 } // getFileContentLines
234232
235- private getCharPos ( fileContentLines : string [ ] ) {
236- if ( ! this . codeBlockSectionInfo ) {
237- return - 1 ;
238- }
239-
233+ private getCharPos ( fileContentLines : string [ ] , line : number ) {
240234 let charPos = 0 ;
241- for ( let i = 0 ; i < this . codeBlockSectionInfo . lineStart ; i ++ ) {
235+ for ( let i = 0 ; i < line ; i ++ ) {
242236 if ( typeof fileContentLines [ i ] !== 'string' ) {
243237 console . error ( `Inconsistent data for file ${ this . context . sourcePath } . Could not calculate character position.` ) ;
244238 charPos = - 1 ;
@@ -250,8 +244,8 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
250244 return charPos !== - 1 ? charPos : undefined ;
251245 } // getCharPos
252246
253- private async processCodeBlockFirstLines ( allBlocks : CodeBlockData [ ] , charPos ?: number ) {
254- if ( ! this . allPreElements ) {
247+ private async processCodeBlockFirstLines ( allBlocks : CodeBlockData [ ] , fileContentLines : string [ ] ) {
248+ if ( ! this . allPreElements || ! this . codeBlockSectionInfo ) {
255249 return ;
256250 }
257251
@@ -261,17 +255,19 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
261255 }
262256
263257 for ( const [ key , preElement ] of this . allPreElements . entries ( ) ) {
264- const isRerenderQueued = this . codeBlockSectionInfo && this . plugin . rerenderQueue . has ( this . codeBlockSectionInfo . lineStart + ( allBlocks [ key ] ?. firstLineOffset ?? 0 ) ) ;
265-
266- if ( preElement . classList . contains ( 'codeblock-customizer-pre' ) && ! isRerenderQueued ) {
258+ const currentBlock = allBlocks [ key ] ;
259+ if ( ! currentBlock ) {
267260 continue ;
268261 }
269262
270- const currentBlock = allBlocks [ key ] ;
271- if ( ! currentBlock ) {
263+ const blockLine = getBlockLine ( this . codeBlockSectionInfo . lineStart , currentBlock ) ;
264+ const isRerenderQueued = this . plugin . rerenderQueue . has ( this . plugin . getRerenderKey ( this . context . sourcePath , blockLine ) ) ;
265+
266+ if ( preElement . classList . contains ( 'codeblock-customizer-pre' ) && ! isRerenderQueued ) {
272267 continue ;
273268 }
274269
270+ const charPos = this . getCharPos ( fileContentLines , blockLine ) ;
275271 await this . processSingleCodeBlock ( preElement , currentBlock , { charPos, isPrinting : false } ) ;
276272 }
277273 } // processCodeBlockFirstLines
@@ -325,7 +321,7 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
325321 let firstLine = codeBlockFirstLine ;
326322 let isRerender = isParameterRerender ;
327323 // for HTML code blocks the editor uses the line of the opening <code> tag as the first line (it can be after the <pre> line)
328- const lineStart = this . codeBlockSectionInfo ? this . codeBlockSectionInfo . lineStart + ( blockData . firstLineOffset ?? 0 ) : undefined ;
324+ const lineStart = this . codeBlockSectionInfo ? getBlockLine ( this . codeBlockSectionInfo . lineStart , blockData ) : undefined ;
329325
330326 if ( ! isPrinting && lineStart !== undefined ) {
331327 const result = this . handleRerenderOverride ( lineStart , codeBlockFirstLine , isParameterRerender ) ;
@@ -384,16 +380,17 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
384380 let firstLine = initialFirstLine ;
385381 let isRerender = initialIsRerender ;
386382
387- const override = this . plugin . rerenderQueue . get ( lineStart ) ;
383+ const queueKey = this . plugin . getRerenderKey ( this . context . sourcePath , lineStart ) ;
384+ const override = this . plugin . rerenderQueue . get ( queueKey ) ;
388385 if ( override ) {
389386 isRerender = true ;
390387 firstLine = override . content ;
391388 const newCount = override . count - 1 ;
392389
393390 if ( newCount > 0 ) {
394- this . plugin . rerenderQueue . set ( lineStart , { content : override . content , count : newCount } ) ;
391+ this . plugin . rerenderQueue . set ( queueKey , { content : override . content , count : newCount } ) ;
395392 } else {
396- this . plugin . rerenderQueue . delete ( lineStart ) ;
393+ this . plugin . rerenderQueue . delete ( queueKey ) ;
397394 }
398395 }
399396
0 commit comments