1
1
// Global array to store Monaco Editor instances
2
2
globalThis . qwebrEditorInstances = [ ] ;
3
3
4
+ function isValidCodeLineNumbers ( stringCodeLineNumbers ) {
5
+ // Regular expression to match valid input strings
6
+ const regex = / ^ ( \d + ( - \d + ) ? ) ( , \d + ( - \d + ) ? ) * $ / ;
7
+ return regex . test ( stringCodeLineNumbers ) ;
8
+ }
9
+
4
10
// Function that builds and registers a Monaco Editor instance
5
11
globalThis . qwebrCreateMonacoEditorInstance = function ( cellData ) {
6
12
@@ -54,7 +60,7 @@ globalThis.qwebrCreateMonacoEditorInstance = function (cellData) {
54
60
const model = editor . getModel ( ) ;
55
61
// Set EOL for the model
56
62
model . setEOL ( monaco . editor . EndOfLineSequence . LF ) ;
57
-
63
+
58
64
// Dynamically modify the height of the editor window if new lines are added.
59
65
let ignoreEvent = false ;
60
66
const updateHeight = ( ) => {
@@ -80,6 +86,56 @@ globalThis.qwebrCreateMonacoEditorInstance = function (cellData) {
80
86
}
81
87
} ;
82
88
89
+ // Function to generate decorations to highlight lines
90
+ // in the editor based on input string.
91
+ function decoratorHighlightLines ( codeLineNumbers ) {
92
+ // Store the lines to be lighlight
93
+ let linesToHighlight = [ ] ;
94
+
95
+ // Parse the codeLineNumbers string to get the line numbers to highlight
96
+ // First, split the string by commas
97
+ codeLineNumbers . split ( ',' ) . forEach ( part => {
98
+ // Check if we have a range of lines
99
+ if ( part . includes ( '-' ) ) {
100
+ // Handle range of lines (e.g., "6-8")
101
+ const [ start , end ] = part . split ( '-' ) . map ( Number ) ;
102
+ for ( let i = start ; i <= end ; i ++ ) {
103
+ linesToHighlight . push ( i ) ;
104
+ }
105
+ } else {
106
+ // Handle single line (e.g., "7")
107
+ linesToHighlight . push ( Number ( part ) ) ;
108
+ }
109
+ } ) ;
110
+
111
+ // Create monaco decorations for the lines to highlight
112
+ const decorations = linesToHighlight . map ( lineNumber => ( {
113
+ range : new monaco . Range ( lineNumber , 1 , lineNumber , 1 ) ,
114
+ options : {
115
+ isWholeLine : true ,
116
+ className : 'qwebr-editor-highlight-line'
117
+ }
118
+ } ) ) ;
119
+
120
+ // Return decorations to be applied to the editor
121
+ return decorations ;
122
+ }
123
+
124
+ // Ensure that the editor-code-line-numbers option is set and valid
125
+ // then apply styling
126
+ if ( qwebrOptions [ 'editor-code-line-numbers' ] ) {
127
+ // Remove all whitespace from the string
128
+ const codeLineNumbers = qwebrOptions [ 'editor-code-line-numbers' ] . replace ( / \s / g, '' ) ;
129
+ // Check if the string is valid for line numbers, e.g., "1,3-5,7"
130
+ if ( isValidCodeLineNumbers ( codeLineNumbers ) ) {
131
+ // Apply the decorations to the editor
132
+ editor . createDecorationsCollection ( decoratorHighlightLines ( codeLineNumbers ) ) ;
133
+ } else {
134
+ // Warn the user that the input is invalid
135
+ console . warn ( `Invalid "editor-code-line-numbers" value in code cell ${ qwebrOptions [ 'label' ] } : ${ codeLineNumbers } ` ) ;
136
+ }
137
+ }
138
+
83
139
// Helper function to check if selected text is empty
84
140
function isEmptyCodeText ( selectedCodeText ) {
85
141
return ( selectedCodeText === null || selectedCodeText === undefined || selectedCodeText === "" ) ;
0 commit comments