Skip to content

Commit 1566784

Browse files
author
Роман Чумак
committed
lines numbering shift
1 parent 0221737 commit 1566784

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "egui_code_editor"
33
authors = ["Roman Chumak <p4ymak@yandex.ru>"]
4-
version = "0.2.12"
4+
version = "0.2.13"
55
edition = "2021"
66
license = "MIT"
77
repository = "https://github.com/p4ymak/egui_code_editor"

examples/demo.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ struct CodeEditorDemo {
139139
theme: ColorTheme,
140140
syntax: Syntax,
141141
example: bool,
142+
shift: isize,
143+
numlines_only_natural: bool,
142144
}
143145
impl CodeEditorDemo {
144146
fn new(_cc: &CreationContext) -> Self {
@@ -148,6 +150,8 @@ impl CodeEditorDemo {
148150
theme: ColorTheme::GRUVBOX,
149151
syntax: rust.syntax(),
150152
example: true,
153+
shift: 0,
154+
numlines_only_natural: false,
151155
}
152156
}
153157
}
@@ -192,13 +196,20 @@ impl eframe::App for CodeEditorDemo {
192196
});
193197

194198
egui::CentralPanel::default().show(ctx, |ui| {
199+
ui.horizontal(|h| {
200+
h.label("Numbering Shift");
201+
h.add(egui::DragValue::new(&mut self.shift));
202+
h.checkbox(&mut self.numlines_only_natural, "Only Natural Numbering");
203+
});
195204
let mut editor = CodeEditor::default()
196205
.id_source("code editor")
197206
.with_rows(10)
198207
.with_fontsize(14.0)
199208
.with_theme(self.theme)
200209
.with_syntax(self.syntax.to_owned())
201210
.with_numlines(true)
211+
.with_numlines_shift(self.shift)
212+
.with_numlines_only_natural(self.numlines_only_natural)
202213
.vscroll(true);
203214
editor.show(ui, &mut self.code);
204215

src/lib.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)