Skip to content

input: Add text decorations to multi-line text - #3498

Draft
WundurAi wants to merge 1 commit into
longbridge:mainfrom
WundurAi:input-textarea-decorations
Draft

WundurAi wants to merge 1 commit into
longbridge:mainfrom
WundurAi:input-textarea-decorations

Conversation

@WundurAi

Copy link
Copy Markdown

Description

TextareaState (ordinary multi-line text) can now hold text decoration collections, the same API EditorState already has: create_decorations_collection, with ranges that follow edits.

The renderer already composes decoration layers for every mode (the non-code-editor branch in element.rs calls extras.decoration_layers()), but TextareaMode's extras were (), so there was nowhere to put them. Switching to EditorState to get decorations is not an option for inputs that use inline tokens, which EditorMode does not support.

Use case: light Markdown highlighting in a chat composer (bold, italic, code spans, quieter syntax characters) that also shows pasted text as inline tokens.

What changed:

  • TextareaMode gets TextareaExtras { decorations: DecorationCollections }, with reset_annotations / adjust_annotations doing what EditorMode's do for text decorations.
  • TextDecorationCollection takes the owning mode as a type parameter, defaulting to EditorMode. Its methods and create_decorations_collection are implemented for EditorMode and TextareaMode by one macro, following the token_api! pattern in inline_tokens.rs. Most of the diff in decorations.rs is the existing methods moving into that macro unchanged (re-indented).
  • Tests: test_textarea_decorations_follow_typing (mirrors test_editor_decorations_follow_typing) and test_textarea_decorations_beside_inline_tokens.

Checked: cargo test -p gpui-base --lib (1338 passed), cargo clippy -p gpui-base -p gpui-component --locked -- --deny warnings, cargo fmt --check, cargo check -p gpui-component-story.

Screenshot

A Textarea with inline-token support, highlighted through the new API:

[screenshot]

Public API

gpui_base::input (re-exported from gpui_component::input):

  • InputBaseState<TextareaMode>::create_decorations_collection(&mut self, decorations: Vec<TextDecoration>, cx: &mut Context<Self>) -> TextDecorationCollection<TextareaMode> — new: text decorations for multi-line text (TextareaState).
  • pub struct TextDecorationCollection<M: InputModeKind = EditorMode> — gains the mode type parameter; set, append, clear and get_ranges exist for EditorMode and TextareaMode.
  • pub struct TextareaExtras — new: TextareaMode's extras, holding its decorations.

Breaking Changes

  • <TextareaMode as InputModeKind>::Extras is now TextareaExtras instead of (). Code naming TextDecorationCollection without a parameter keeps compiling (it defaults to EditorMode).

TextareaState can now hold text decoration collections, like EditorState:
create_decorations_collection, with ranges that follow edits. The renderer
already composed decoration layers for every mode; ordinary multi-line text
had no extras to hold them. Inline tokens keep working beside them, which
EditorState cannot offer.

TextDecorationCollection takes the mode as a type parameter, defaulting to
EditorMode, so existing code is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant