@@ -101,6 +101,8 @@ pub struct CodeEditor {
101101 theme : ColorTheme ,
102102 syntax : Syntax ,
103103 numlines : bool ,
104+ numlines_shift : isize ,
105+ numlines_only_natural : bool ,
104106 fontsize : f32 ,
105107 rows : usize ,
106108 vscroll : bool ,
@@ -126,6 +128,8 @@ impl Default for CodeEditor {
126128 theme : ColorTheme :: GRUVBOX ,
127129 syntax : Syntax :: rust ( ) ,
128130 numlines : true ,
131+ numlines_shift : 0 ,
132+ numlines_only_natural : false ,
129133 fontsize : 10.0 ,
130134 rows : 10 ,
131135 vscroll : true ,
@@ -181,6 +185,26 @@ impl CodeEditor {
181185 CodeEditor { numlines, ..self }
182186 }
183187
188+ /// Shift lines numbering by this value
189+ ///
190+ /// **Default: 0**
191+ pub fn with_numlines_shift ( self , numlines_shift : isize ) -> Self {
192+ CodeEditor {
193+ numlines_shift,
194+ ..self
195+ }
196+ }
197+
198+ /// Show lines numbering only above zero, useful for enabling numbering since nth row
199+ ///
200+ /// **Default: false**
201+ pub fn with_numlines_only_natural ( self , numlines_only_natural : bool ) -> Self {
202+ CodeEditor {
203+ numlines_only_natural,
204+ ..self
205+ }
206+ }
207+
184208 /// Use custom syntax for highlighting
185209 ///
186210 /// **Default: Rust**
@@ -244,21 +268,32 @@ impl CodeEditor {
244268 } else {
245269 text. lines ( ) . count ( )
246270 }
247- . max ( self . rows ) ;
248- let max_indent = total. to_string ( ) . len ( ) ;
271+ . max ( self . rows ) as isize ;
272+ let max_indent = total
273+ . to_string ( )
274+ . len ( )
275+ . max ( !self . numlines_only_natural as usize * self . numlines_shift . to_string ( ) . len ( ) ) ;
249276 let mut counter = ( 1 ..=total)
250277 . map ( |i| {
251- let label = i. to_string ( ) ;
252- format ! (
253- "{}{label}" ,
254- " " . repeat( max_indent. saturating_sub( label. len( ) ) )
255- )
278+ let num = i + self . numlines_shift ;
279+ if num <= 0 && self . numlines_only_natural {
280+ String :: new ( )
281+ } else {
282+ let label = num. to_string ( ) ;
283+ format ! (
284+ "{}{label}" ,
285+ " " . repeat( max_indent. saturating_sub( label. len( ) ) )
286+ )
287+ }
256288 } )
257289 . collect :: < Vec < String > > ( )
258290 . join ( "\n " ) ;
259291
260292 #[ allow( clippy:: cast_precision_loss) ]
261- let width = max_indent as f32 * self . fontsize * 0.5 ;
293+ let width = max_indent as f32
294+ * self . fontsize
295+ * 0.5
296+ * !( total + self . numlines_shift <= 0 && self . numlines_only_natural ) as u8 as f32 ;
262297
263298 let mut layouter = |ui : & egui:: Ui , string : & str , _wrap_width : f32 | {
264299 let layout_job = egui:: text:: LayoutJob :: single_section (
0 commit comments