input: Fix resetting highlighting for single line code editor (#1742)

## Description

When using `set_value` with a `code_editor("lang").multi_line(false)`
the highlighting and lsp will not be reset.

## Video

### Before

https://github.com/user-attachments/assets/5fef5b6e-478e-4233-a911-e087f889040a

### After

https://github.com/user-attachments/assets/3ea99060-8dba-4604-9d3f-c151b3aadcf2

## How to Test

`cargo run --release -- input`, enter some text in "Single line code
editor", click "Reset"

## Checklist

- [x] I have read the [CONTRIBUTING](../CONTRIBUTING.md) 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)
This commit is contained in:
Andreas Johansson 2025-12-05 02:53:07 +01:00 committed by GitHub
parent 3b2746e0ba
commit 8fcf5f337d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 6 deletions

View file

@ -1,11 +1,13 @@
use gpui::{ use gpui::{
App, AppContext as _, Context, Entity, InteractiveElement, IntoElement, ParentElement as _, App, AppContext as _, ClickEvent, Context, Entity, InteractiveElement, IntoElement,
Render, Styled, Subscription, Window, div, ParentElement as _, Render, Styled, Subscription, Window, div,
}; };
use crate::section; use crate::section;
use gpui_component::{button::*, input::*, *}; use gpui_component::{button::*, input::*, *};
const CODE_EXAMPLE: &str = r#"{"single_line":"code editor"}"#;
pub fn init(_: &mut App) {} pub fn init(_: &mut App) {}
pub struct InputStory { pub struct InputStory {
@ -93,7 +95,7 @@ impl InputStory {
InputState::new(window, cx) InputState::new(window, cx)
.code_editor("json") .code_editor("json")
.multi_line(false) .multi_line(false)
.default_value(r#"{"single_line":"code editor"}"#) .default_value(CODE_EXAMPLE)
}); });
let _subscriptions = vec![ let _subscriptions = vec![
@ -151,6 +153,12 @@ impl InputStory {
InputEvent::Blur => println!("Blur"), InputEvent::Blur => println!("Blur"),
}; };
} }
fn on_click_reset(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
self.code_input.update(cx, |input_state, cx| {
input_state.set_value(CODE_EXAMPLE, window, cx);
});
}
} }
impl Render for InputStory { impl Render for InputStory {
@ -262,9 +270,15 @@ impl Render for InputStory {
), ),
) )
.child( .child(
section("Single line code editor") section("Single line code editor").max_w_md().child(
.max_w_md() Input::new(&self.code_input).suffix(
.child(Input::new(&self.code_input)), Button::new("code-reset")
.ghost()
.label("Reset")
.xsmall()
.on_click(cx.listener(Self::on_click_reset)),
),
),
) )
} }
} }

View file

@ -606,15 +606,19 @@ impl InputState {
self.replace_text(value, window, cx); self.replace_text(value, window, cx);
self.disabled = was_disabled; self.disabled = was_disabled;
self.history.ignore = false; self.history.ignore = false;
// Ensure cursor to start when set text // Ensure cursor to start when set text
if self.mode.is_single_line() { if self.mode.is_single_line() {
self.selected_range = (self.text.len()..self.text.len()).into(); self.selected_range = (self.text.len()..self.text.len()).into();
} else { } else {
self.selected_range.clear(); self.selected_range.clear();
}
if self.mode.is_code_editor() {
self._pending_update = true; self._pending_update = true;
self.lsp.reset(); self.lsp.reset();
} }
// Move scroll to top // Move scroll to top
self.scroll_handle.set_offset(point(px(0.), px(0.))); self.scroll_handle.set_offset(point(px(0.), px(0.)));