Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions rust/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,8 @@ impl<M> Editor<M> {
mode(String) = "mode";
/// The text is code in this language.
lang(String) = "lang";
/// Set false to use `decor` colors without native syntax highlighting (default true).
highlight(bool) = "highlight";
/// Not editable.
readonly(bool) = "readonly";
/// Accepts an atomic `send`.
Expand All @@ -1428,6 +1430,16 @@ impl<M> Editor<M> {
typeahead(bool) = "typeahead";
/// Cap the height at this many lines.
max_lines(u32) = "maxLines";
/// Clip long lines instead of soft-wrapping them (default false).
nowrap(bool) = "nowrap";
/// Zero-based line at the top; takes precedence over cursor following.
topline(u32) = "topline";
/// Follow caret movement when `topline` is absent (default true); false preserves scroll.
follow_cursor(bool) = "followCursor";
/// One entry per logical line, in a shared gutter; not part of the text or its offsets.
gutter(Vec<Text>) = "gutter";
/// One entry per logical line, at its end; not part of the text or its offsets.
virt(Vec<Text>) = "virt";
/// The look (`field` frames it as a prompt input).
variant(InputVariant) = "variant";
/// Buttons attached inside the ring, in order.
Expand Down Expand Up @@ -2987,6 +2999,45 @@ mod tests {
);
}

#[test]
fn editor_viewport_props_serialize() {
let source = "let café = \"🦆\";\nprint(café)\n";
assert_eq!(
wire(
editor::<()>()
.text(source)
.cursor(4usize)
.highlight(false)
.nowrap(true)
.topline(0u32)
.follow_cursor(false)
.gutter(vec!["1 │ ".into(), span("2 │ ").style("accent").into(), "".into()])
.virt(vec!["".into(), span(" : number").style("muted").into(), "end".into()])
)["p"],
json!({
"text": source, "cursor": 4, "highlight": false, "nowrap": true,
"topline": 0, "followCursor": false,
"gutter": ["1 │ ", [{"t": "2 │ ", "s": "accent"}], ""],
"virt": ["", [{"t": " : number", "s": "muted"}], "end"]
})
);
}

#[test]
fn editor_viewport_props_are_optional_and_can_clear_line_annotations() {
assert_eq!(wire(editor::<()>())["p"], json!({}));
assert_eq!(
wire(
editor::<()>()
.gutter(vec!["1 │ ".into()])
.virt(vec![" : number".into()])
.gutter(vec![])
.virt(vec![])
)["p"],
json!({"gutter": [], "virt": []})
);
}

#[test]
fn typed_values_and_escape_hatches_serialize() {
let c = card::<()>()
Expand Down
12 changes: 12 additions & 0 deletions typescript/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,19 @@ export interface InputProps extends FieldTextProps {

/** Props of `editor`. */
export interface EditorProps extends FieldTextProps {
/** False disables syntax highlighting while keeping `lang`'s code presentation; default true. */
readonly highlight?: boolean;
readonly maxLines?: number;
/** Clip long lines instead of soft-wrapping; default false. */
readonly nowrap?: boolean;
/** Zero-based logical line at the top; overrides automatic caret scrolling. */
readonly topline?: number;
/** False preserves scrolling when the caret moves; default true. `topline` takes precedence. */
readonly followCursor?: boolean;
/** One entry per logical line in a shared gutter column; not part of the text. */
readonly gutter?: readonly Spans[];
/** One entry per logical line, drawn at its end; not part of the text. */
readonly virt?: readonly Spans[];
}

/** Props of `status`. */
Expand Down
35 changes: 35 additions & 0 deletions typescript/test/jsx.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,41 @@ describe('TSX', () => {
});
});

test('editor viewport props and per-line spans survive typed JSX', () => {
const tree = (
<editor
text={'const π = "🌍";\nπ'}
cursor={0}
lang="javascript"
highlight={false}
nowrap={false}
topline={0}
followCursor={false}
gutter={['1 │ ', [span('●', 'error'), ' 2 ']] as const}
virt={[' → 🌍', [span('✓', 'success'), ' 值']] as const}
/>
);
expect(json(tree)).toEqual({
k: 'editor',
p: {
text: 'const π = "🌍";\nπ',
cursor: 0,
lang: 'javascript',
highlight: false,
nowrap: false,
topline: 0,
followCursor: false,
gutter: ['1 │ ', [{ t: '●', s: 'error' }, ' 2 ']],
virt: [' → 🌍', [{ t: '✓', s: 'success' }, ' 值']],
},
});
});

test('omitted editor viewport props leave defaults to Tern', () => {
expect(json(<editor text="draft" />)).toEqual({ k: 'editor', p: { text: 'draft' } });
expect(json(ui.editor())).toEqual({ k: 'editor' });
});

test('meter cells carry the required meter wrapper on the wire', () => {
const cells = { usage: { meter: { value: 0.5, title: 'Half' } } };
const table = ui.table({ cols: [{ id: 'usage' }], rows: [{ id: 'a', cells }] });
Expand Down