Skip to content

Commit 63f9b19

Browse files
committed
shrink diff
1 parent 4a8b86f commit 63f9b19

22 files changed

Lines changed: 86 additions & 317 deletions

File tree

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ jobs:
2020
with:
2121
all-features: false
2222
no-default-features: true
23-
features: dioxus-code/all-languages dioxus-code-editor/web dioxus-code-docsite/web dioxus-code-live-input/web
23+
features: dioxus-code/all-languages dioxus-code-docsite/web dioxus-code-live-input/web
2424

2525
test:
2626
uses: dioxuslabs/dioxus-ci/.github/workflows/test.yml@v0.1.0
2727
with:
2828
no-default-features: true
29-
features: dioxus-code/all-languages dioxus-code-editor/web dioxus-code-docsite/web dioxus-code-live-input/web
29+
features: dioxus-code/all-languages dioxus-code-docsite/web dioxus-code-live-input/web
3030

3131
fmt:
3232
uses: dioxuslabs/dioxus-ci/.github/workflows/fmt.yml@v0.1.0
@@ -36,11 +36,11 @@ jobs:
3636
with:
3737
all-features: false
3838
no-default-features: true
39-
features: dioxus-code/all-languages dioxus-code-editor/web dioxus-code-docsite/web dioxus-code-live-input/web
39+
features: dioxus-code/all-languages dioxus-code-docsite/web dioxus-code-live-input/web
4040

4141
clippy:
4242
uses: dioxuslabs/dioxus-ci/.github/workflows/clippy.yml@v0.1.0
4343
with:
4444
all-features: false
4545
no-default-features: true
46-
features: dioxus-code/all-languages dioxus-code-editor/web dioxus-code-docsite/web dioxus-code-live-input/web
46+
features: dioxus-code/all-languages dioxus-code-docsite/web dioxus-code-live-input/web

.github/workflows/nightly.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ jobs:
1919
toolchain: nightly
2020
all-features: false
2121
no-default-features: true
22-
features: dioxus-code/all-languages dioxus-code-editor/web dioxus-code-docsite/web dioxus-code-live-input/web
22+
features: dioxus-code/all-languages dioxus-code-docsite/web dioxus-code-live-input/web
2323

2424
test:
2525
uses: dioxuslabs/dioxus-ci/.github/workflows/test.yml@v0.1.0
2626
with:
2727
toolchain: nightly
2828
no-default-features: true
29-
features: dioxus-code/all-languages dioxus-code-editor/web dioxus-code-docsite/web dioxus-code-live-input/web
29+
features: dioxus-code/all-languages dioxus-code-docsite/web dioxus-code-live-input/web
3030

3131
clippy:
3232
uses: dioxuslabs/dioxus-ci/.github/workflows/clippy.yml@v0.1.0
3333
with:
3434
toolchain: nightly
3535
all-features: false
3636
no-default-features: true
37-
features: dioxus-code/all-languages dioxus-code-editor/web dioxus-code-docsite/web dioxus-code-live-input/web
37+
features: dioxus-code/all-languages dioxus-code-docsite/web dioxus-code-live-input/web
3838

3939
web-demo:
4040
uses: dioxuslabs/dioxus-ci/.github/workflows/web-build.yml@v0.1.0

Cargo.toml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ categories = ["gui", "web-programming"]
2323
[workspace.dependencies]
2424
dioxus-code = { version = "0.1.1", path = "." }
2525
dioxus-code-editor = { version = "0.1.2", path = "code-editor", default-features = false }
26-
betlang = { version = "0.1.0" }
2726

2827
[package]
2928
name = "dioxus-code"
@@ -44,14 +43,8 @@ rustdoc-args = ["--cfg", "docsrs"]
4443
[features]
4544
default = ["macro"]
4645
macro = ["dep:dioxus-code-macro", "dioxus-code-macro/lang-rust"]
47-
runtime = [
48-
"arborium/lang-rust",
49-
"dep:arborium-tree-sitter",
50-
]
51-
detection = [
52-
"runtime",
53-
"dep:betlang",
54-
]
46+
runtime = ["arborium/lang-rust", "dep:arborium-tree-sitter"]
47+
detection = ["runtime", "dep:betlang"]
5548
all-languages = [
5649
"runtime",
5750
"arborium/all-languages",
@@ -268,7 +261,7 @@ arborium-theme = "2.16.0"
268261
arborium-tree-sitter = { version = "2.16.0", optional = true }
269262
dioxus = { version = "0.7.0", default-features = false, features = ["lib"] }
270263
dioxus-code-macro = { version = "0.1.0", path = "dioxus-code-macro", default-features = false, optional = true }
271-
betlang = { workspace = true, optional = true }
264+
betlang = { version = "0.1.0", optional = true }
272265

273266
[build-dependencies]
274267
arborium = { version = "2.16.0", default-features = false }

README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ A small Dioxus component for rendering source code with proper highlighting. Par
2121

2222
Two ways to highlight:
2323

24-
- **[`code!`] macro** — parses at compile time. The runtime ships only the spans, no parser. Default.
25-
- **[`SourceCode`]** — parses at runtime. Opt in with the `runtime` feature when the source isn't known until the user types it.
24+
- **[`code!`] macro** — parses at compile time and embeds the highlighted spans. Default.
25+
- **[`SourceCode`]** — parses at runtime. Opt in with the `runtime` feature for dynamic source text.
2626

2727
## Quick start
2828

@@ -51,35 +51,32 @@ When the file extension is ambiguous, pass [`CodeOptions::builder`] with [`CodeO
5151

5252
## Runtime highlighting
5353

54-
For editor-style use cases where the source isn't known at compile time:
54+
For editor-style use cases with dynamic source text:
5555

5656
```toml
5757
[dependencies]
58-
dioxus-code = { version = "0.1", features = ["runtime", "detection"] }
58+
dioxus-code = { version = "0.1", features = ["runtime"] }
5959
```
6060

6161
```rust
62-
# #[cfg(feature = "detection")]
63-
# {
6462
# use dioxus::prelude::*;
6563
use dioxus_code::{Code, Language, SourceCode, Theme};
6664
# let user_input = String::new();
6765
# let _ =
6866
rsx! {
6967
Code {
70-
src: SourceCode::builder(user_input).with_language(Language::Auto),
68+
src: SourceCode::new(Language::Rust, user_input),
7169
theme: Theme::GITHUB_LIGHT,
7270
}
7371
}
7472
# ;
75-
# }
7673
```
7774

78-
`Language::Auto` is available only with the `detection` feature. Without detection, pass a concrete language such as `Language::Rust`. The default `runtime` feature includes Rust; pass `lang-python`, `lang-toml`, or `all-languages` for the rest.
75+
Pass a [`Language`] variant when building [`SourceCode`]. The `runtime` feature includes Rust; enable the matching `lang-*` feature, or `all-languages`, for additional grammars.
7976

8077
## Editor
8178

82-
[`dioxus-code-editor`] is a sibling crate that pairs the highlighter with a `contenteditable` input layer:
79+
[`dioxus-code-editor`] is a sibling crate that pairs the highlighter with a textarea input layer:
8380

8481
```rust
8582
# use dioxus::prelude::*;
@@ -133,7 +130,7 @@ Code {
133130

134131
```sh
135132
dx serve --example dioxus-code-basic # macro + runtime side by side
136-
dx serve --example dioxus-code-macro-only # compile-time only, no parser in the binary
133+
dx serve --example dioxus-code-macro-only # compile-time highlighted spans
137134
dx serve --example dioxus-code-live-input # textarea bound to runtime highlighter
138135
```
139136

code-editor/Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
1717

1818
[dependencies]
1919
dioxus = { version = "0.7.0", default-features = false, features = ["lib"] }
20-
dioxus-code = { workspace = true, features = ["detection", "lang-toml", "lang-python"] }
20+
dioxus-code = { workspace = true, features = ["runtime"] }
2121

2222
[target.'cfg(target_arch = "wasm32")'.dependencies]
2323
wasm-bindgen = "0.2"
@@ -30,6 +30,4 @@ web-sys = { version = "0.3", features = [
3030
] }
3131

3232
[features]
33-
default = ["desktop"]
34-
desktop = ["dioxus/desktop", "dioxus/launch"]
35-
web = ["dioxus/web", "dioxus/launch"]
33+
all-languages = ["dioxus-code/all-languages"]

code-editor/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The component is controlled — drive [`CodeEditorProps::value`] from your own s
5353
| prop | description |
5454
|---|---|
5555
| [`CodeEditorProps::value`] | Current editor contents. |
56-
| [`CodeEditorProps::language`] | Tree-sitter grammar selection. Pass a [`Language`] variant (for example [`Language::Rust`]) or use [`Language::from_slug`] for custom slugs. |
56+
| [`CodeEditorProps::language`] | Syntax grammar selection. Pass a [`Language`] variant (for example [`Language::Rust`]) or use [`Language::from_slug`] for runtime slugs. |
5757
| [`CodeEditorProps::theme`] | Syntax theme selection shared with [`dioxus-code`](https://crates.io/crates/dioxus-code); accepts [`Theme`] or [`CodeTheme`]. |
5858
| [`CodeEditorProps::line_numbers`] | Show a one-based line gutter. Defaults to `true`. |
5959
| [`CodeEditorProps::read_only`] | Disable editing while preserving highlighting. |

code-editor/src/lib.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use dioxus::prelude::*;
55
pub use dioxus_code::Language;
66
#[cfg(test)]
77
use dioxus_code::Theme;
8-
use dioxus_code::advanced::{Buffer, CodeThemeStyles, TokenSpan};
8+
use dioxus_code::advanced::{Buffer, CodeThemeStyles, HighlightError, TokenSpan};
99
#[cfg(test)]
1010
use dioxus_code::advanced::{HighlightSegment, HighlightedSource};
1111
use dioxus_code::{CodeTheme, SourceCode};
@@ -71,6 +71,11 @@ pub struct CodeEditorProps {
7171
pub oninput: EventHandler<String>,
7272
}
7373

74+
struct EditorBuffer {
75+
buffer: Option<Buffer>,
76+
language: Language,
77+
}
78+
7479
/// Editable syntax-highlighted code surface.
7580
///
7681
/// The component is controlled by [`CodeEditorProps::value`]; update that value
@@ -96,10 +101,15 @@ pub struct CodeEditorProps {
96101
/// ```
97102
#[component]
98103
pub fn CodeEditor(props: CodeEditorProps) -> Element {
99-
let buffer = use_hook({
104+
let state = use_hook({
100105
let value = props.value.clone();
101106
let language = props.language;
102-
move || Rc::new(RefCell::new(Buffer::new(language, value).ok()))
107+
move || {
108+
Rc::new(RefCell::new(EditorBuffer {
109+
buffer: Buffer::new(language, value).ok(),
110+
language,
111+
}))
112+
}
103113
});
104114
let edit_tracker = use_hook(|| {
105115
Rc::new(RefCell::new(edit_capture::InputEditTracker::new(
@@ -109,28 +119,30 @@ pub fn CodeEditor(props: CodeEditorProps) -> Element {
109119

110120
let edit = edit_tracker.borrow_mut().take_for_render(&props.value);
111121
let snapshot = {
112-
let mut buffer_slot = buffer.borrow_mut();
113-
if buffer_slot.is_none() {
114-
*buffer_slot = Buffer::new(props.language, props.value.clone()).ok();
122+
let mut slot = state.borrow_mut();
123+
if slot.language != props.language {
124+
slot.buffer = Buffer::new(props.language, props.value.clone()).ok();
125+
slot.language = props.language;
115126
}
116127

117-
match buffer_slot.as_mut() {
128+
match slot.buffer.as_mut() {
118129
Some(buffer) => {
119-
if buffer.language() != props.language {
120-
let _ = buffer.set_language(props.language);
121-
}
122130
if buffer.source() != props.value {
123131
let result = match edit {
124-
Some(edit) => buffer.edit(edit, props.value.clone()),
132+
Some(edit) => match buffer.edit(edit, props.value.clone()) {
133+
Ok(()) => Ok(()),
134+
Err(HighlightError::InvalidEdit { .. }) => {
135+
buffer.replace(props.value.clone())
136+
}
137+
Err(error) => Err(error),
138+
},
125139
None => buffer.replace(props.value.clone()),
126140
};
127141
let _ = result;
128142
}
129143
buffer.highlighted()
130144
}
131-
None => SourceCode::builder(props.value.clone())
132-
.with_language(props.language)
133-
.into(),
145+
None => SourceCode::new(props.language, props.value.clone()).into(),
134146
}
135147
};
136148
let lines = snapshot.lines();

code-editor/src/main.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

dioxus-code-macro/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
Implementation crate for the [`code!`] macro re-exported by [`dioxus-code`](https://crates.io/crates/dioxus-code) under its default `macro` feature. You usually depend on `dioxus-code` instead of pulling this in directly.
2121

22-
The macro reads a source file at compile time, parses it with [`arborium`](https://crates.io/crates/arborium), and expands to a static span tree. The runtime binary ships only the spans — no parser.
22+
The macro reads a source file at compile time, parses it with [`arborium`](https://crates.io/crates/arborium), and expands to a static span tree that can be rendered by `dioxus-code`.
2323

2424
```rust
2525
use dioxus_code::code;

docsite/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ publish = false
1111

1212
[dependencies]
1313
dioxus = { version = "0.7.0", features = ["router"] }
14-
dioxus-code = { workspace = true, features = ["all-languages", "detection"] }
14+
dioxus-code = { workspace = true, features = ["runtime", "lang-toml"] }
1515
dioxus-code-editor = { workspace = true }
1616
dioxus-primitives = { git = "https://github.com/DioxusLabs/components", version = "0.0.1", default-features = false, features = ["router"] }
1717

0 commit comments

Comments
 (0)