gpui-component icon indicating copy to clipboard operation
gpui-component copied to clipboard

input: Single line code editor

Open zanmato opened this issue 1 month ago • 1 comments

Description

I want to be able to use syntax highlighting in single line inputs, I solved this by adding InputMode::SingleLineCodeEditor. But there are more ways it could be solved, for example max_rows for code_editor. But this is more complex because of all the areas we need to check for max_rows and compare it to rows. So I'm not sure how and if this is something you want to add?

I haven't added any docs or anything yet because of this.

Screenshot

I have two use cases for this,

  1. In my API client, I have a tree sitter language for the URL input,
Image
  1. In my SQL editor I want to be able to allow JSON syntax highlighting for inline editing of json columns,
Image

Break Changes

None

How to Test

Add .single_line_code_editor("json") for example

Checklist

  • [x] I have read the CONTRIBUTING document and followed the guidelines.
  • [x] Reviewed the changes in this PR and confirmed AI generated code (If any) is accurate.
  • [x] Passed cargo run for story tests related to the changes.
  • [ ] Tested macOS, Windows and Linux platforms performance (if the change is platform-specific)

zanmato avatar Nov 27 '25 14:11 zanmato

Could also allow set_highlighter for InputMode::SingleLine

zanmato avatar Nov 27 '25 14:11 zanmato

We need example for next time test.

huacnlee avatar Nov 28 '25 03:11 huacnlee

And about the API, I think we may need to consider to change to like this:

InputState::new()
    .code_editor() // default multi_line: true
    .multi_line(false)

InputState::new()  // Default is plain text, single line.
    .multi_line(true)

enum InputMode {
    Plain {
        multi_line: bool,
    },
    CodeEditor {
        multi_line: bool,
    },
    AutoGrow
}

huacnlee avatar Nov 28 '25 03:11 huacnlee

And about the API, I think we may need to consider to change to like this:

InputState::new()
    .code_editor() // default multi_line: true
    .multi_line(false)

InputState::new()  // Default is plain text, single line.
    .multi_line(true)

enum InputMode {
    Plain {
        multi_line: bool,
    },
    CodeEditor {
        multi_line: bool,
    },
    AutoGrow
}

Good idea! I will try to do it like this

zanmato avatar Nov 28 '25 06:11 zanmato

I'd like to introduce this feature, but we need improve the API design.

Updated! This is a much nicer API. Let me know if you want it backwards compatible with .multi_line()

zanmato avatar Nov 28 '25 08:11 zanmato

Notable crash, but this is may not be caused by your changes.

➜  gpui-component git:(single-line-code-editor) RUST_BACKTRACE=1 cargo run -- input
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.67s
     Running `target/debug/gpui-component-story input`
2025-11-28T09:56:22.128324Z  INFO gpui_component_story::themes: Load themes...
2025-11-28T09:56:22.250714Z  INFO gpui_component::theme::registry: Themes reloaded successfully.
2025-11-28T09:56:22.250796Z  INFO gpui_component::theme::registry: Reload active theme: "macOS Classic Light"...

thread 'main' panicked at /Users/jason/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/gpui-0.2.2/src/platform/mac/text_system.rs:448:57:
byte index 2 is out of bounds of `}`
stack backtrace:
   0: __rustc::rust_begin_unwind

huacnlee avatar Nov 28 '25 09:11 huacnlee

Notable crash, but this is may not be caused by your changes.

➜  gpui-component git:(single-line-code-editor) RUST_BACKTRACE=1 cargo run -- input
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.67s
     Running `target/debug/gpui-component-story input`
2025-11-28T09:56:22.128324Z  INFO gpui_component_story::themes: Load themes...
2025-11-28T09:56:22.250714Z  INFO gpui_component::theme::registry: Themes reloaded successfully.
2025-11-28T09:56:22.250796Z  INFO gpui_component::theme::registry: Reload active theme: "macOS Classic Light"...

thread 'main' panicked at /Users/jason/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/gpui-0.2.2/src/platform/mac/text_system.rs:448:57:
byte index 2 is out of bounds of `}`
stack backtrace:
   0: __rustc::rust_begin_unwind

Is it reproducible? I don't get any crashes on Linux, but I will try it on my Mac

zanmato avatar Nov 28 '25 10:11 zanmato

Never mind, I have fixed that. You can read my commit to learn of this detail.

huacnlee avatar Nov 28 '25 10:11 huacnlee

Never mind, I have fixed that. You can read my commit to learn of this detail.

Ah, I was just trying it on my Mac and you already fixed it :smile: Thanks!

zanmato avatar Nov 28 '25 10:11 zanmato

I have also updated docs, and enabled auto-merge.

Thank you for PR.

huacnlee avatar Nov 28 '25 11:11 huacnlee

I have also updated docs, and enabled auto-merge.

Thank you for PR.

Thanks for all the additional fixes! Sorry I missed some of the editor docs

zanmato avatar Nov 28 '25 11:11 zanmato