Skip to content

Commit d6ac53d

Browse files
authored
Completer
* completion popup * egui 0.33
1 parent 6909880 commit d6ac53d

8 files changed

Lines changed: 416 additions & 28 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "egui_code_editor"
33
authors = ["Roman Chumak <[email protected]>"]
4-
version = "0.2.17"
5-
edition = "2021"
4+
version = "0.2.18"
5+
edition = "2024"
66
license = "MIT"
77
repository = "https://github.com/p4ymak/egui_code_editor"
88
description = "egui Code Editor widget with numbered lines and syntax highlighting.."
@@ -11,7 +11,7 @@ categories = ["gui", "text-editors"]
1111
keywords = ["egui", "GUI", "editor", "syntax", "highlighting"]
1212

1313
[dependencies]
14-
egui = { version = "0.32", optional = true }
14+
egui = { version = "0.33", optional = true }
1515
serde = { version = "1", optional = true}
1616

1717
[lib]
@@ -33,5 +33,5 @@ name = "tui"
3333
test = true
3434

3535
[dev-dependencies]
36-
eframe = "0.32"
36+
eframe = "0.33"
3737
colorful = "0.3"

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
# Egui Code Editor
22
[![Latest version](https://img.shields.io/crates/v/egui_code_editor.svg)](https://crates.io/crates/egui_code_editor)
33

4-
Text Editor Widget for [egui](https://github.com/emilk/egui) with numbered lines and simple syntax highlighting based on keywords sets.
4+
![Completer](screenshots/completer.gif)
5+
6+
Text Editor Widget for [egui](https://github.com/emilk/egui) with numbered lines, simple syntax highlighting based on keywords sets and auto-completion feature.
7+
8+
### Auto-completion
9+
![Completer](screenshots/completer_demo.gif)
10+
11+
Offers completions from the syntax dictionary and optionally from words previously entered by the user.
12+
13+
*Usage:*
14+
- UP/DOWN Arrows: Select
15+
- TAB: Complete
16+
- ESC: Hide
517

618
## Usage with egui
719

820
```rust
9-
use egui_code_editor::{CodeEditor, ColorTheme, Syntax};
21+
use egui_code_editor::{CodeEditor, Completer, ColorTheme, Syntax};
22+
let syntax = Syntax::rust();
23+
let mut completer = Completer::new_with_syntax(&syntax).with_user_words();
1024

1125
CodeEditor::default()
1226
.id_source("code editor")
1327
.with_rows(12)
1428
.with_fontsize(14.0)
1529
.with_theme(ColorTheme::GRUVBOX)
16-
.with_syntax(Syntax::rust())
30+
.with_syntax(syntax)
1731
.with_numlines(true)
18-
.show(ui, &mut self.code);
32+
.show_with_completer(ui, &mut self.code, &mut completer);
33+
// .show(ui, &mut self.code); // to use without completer
1934
```
2035

2136
## Usage as lexer without egui

examples/demo.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
22

3-
use eframe::{self, egui, CreationContext};
4-
use egui_code_editor::{self, highlighting::Token, CodeEditor, ColorTheme, Syntax};
3+
use eframe::{self, CreationContext, egui};
4+
use egui_code_editor::{self, CodeEditor, ColorTheme, Completer, Syntax, highlighting::Token};
55

66
const THEMES: [ColorTheme; 8] = [
77
ColorTheme::AYU,
@@ -114,7 +114,7 @@ impl SyntaxDemo {
114114

115115
fn main() -> Result<(), eframe::Error> {
116116
#[cfg(debug_assertions)]
117-
{
117+
unsafe {
118118
std::env::set_var("RUST_BACKTRACE", "1");
119119
}
120120

@@ -143,6 +143,7 @@ struct CodeEditorDemo {
143143
code: String,
144144
theme: ColorTheme,
145145
syntax: Syntax,
146+
completer: Completer,
146147
example: bool,
147148
shift: isize,
148149
numlines_only_natural: bool,
@@ -154,6 +155,7 @@ impl CodeEditorDemo {
154155
code: rust.example.to_string(),
155156
theme: ColorTheme::GRUVBOX,
156157
syntax: rust.syntax(),
158+
completer: Completer::new_with_syntax(&rust.syntax()).with_user_words(),
157159
example: true,
158160
shift: 0,
159161
numlines_only_natural: false,
@@ -192,6 +194,8 @@ impl eframe::App for CodeEditorDemo {
192194
.clicked()
193195
{
194196
self.syntax = syntax.syntax();
197+
self.completer =
198+
Completer::new_with_syntax(&syntax.syntax()).with_user_words();
195199
if self.example {
196200
self.code = syntax.example.to_string()
197201
}
@@ -206,6 +210,7 @@ impl eframe::App for CodeEditorDemo {
206210
h.add(egui::DragValue::new(&mut self.shift));
207211
h.checkbox(&mut self.numlines_only_natural, "Only Natural Numbering");
208212
});
213+
209214
let mut editor = CodeEditor::default()
210215
.id_source("code editor")
211216
.with_rows(10)
@@ -216,7 +221,7 @@ impl eframe::App for CodeEditorDemo {
216221
.with_numlines_shift(self.shift)
217222
.with_numlines_only_natural(self.numlines_only_natural)
218223
.vscroll(true);
219-
editor.show(ui, &mut self.code);
224+
editor.show_with_completer(ui, &mut self.code, &mut self.completer);
220225

221226
ui.separator();
222227

@@ -225,7 +230,7 @@ impl eframe::App for CodeEditorDemo {
225230
.show(ui, |ui| {
226231
for token in Token::default().tokens(&self.syntax, &self.code) {
227232
ui.horizontal(|h| {
228-
let fmt = editor.format(token.ty());
233+
let fmt = editor.format_token(token.ty());
229234
h.label(egui::text::LayoutJob::single_section(
230235
format!("{:?}", token.ty()),
231236
fmt,

screenshots/completer.gif

198 KB
Loading

screenshots/completer_demo.gif

529 KB
Loading

0 commit comments

Comments
 (0)