| 1 | use crate::input::InputModeKind; |
| 2 | use std::rc::Rc; |
| 3 | use std::{cell::RefCell, ops::Range}; |
| 4 | |
| 5 | use gpui::{Context, Window}; |
| 6 | use ropey::Rope; |
| 7 | |
| 8 | use super::DisplayMap; |
| 9 | use crate::input::{ |
| 10 | DiagnosticSet, EditorLanguage, InputEdit, InputHighlighter, InputHighlighterFactory, |
| 11 | LanguageConfig, RopeExt as _, TabSize, |
| 12 | }; |
| 13 | |
| 14 | /// What changed, handed to the syntax highlighter. |
| 15 | pub(crate) struct HighlighterUpdate<'a> { |
| 16 | pub(super) selected_range: &'a Range<usize>, |
| 17 | pub(super) old_text: &'a Rope, |
| 18 | pub(super) new_text: &'a Rope, |
| 19 | pub(super) change_text: &'a str, |
| 20 | pub(super) force: bool, |
| 21 | } |
| 22 | |
| 23 | /// How the input lays its text out: rows, growth, and the code-editor extras. |
| 24 | /// |
| 25 | /// This does not say what *kind* of input this is — that is fixed at the type |
| 26 | /// level by [`crate::input::InputModeKind`], and asking it here is what used to |
| 27 | /// let the two disagree. |
| 28 | #[derive(Clone)] |
| 29 | pub(crate) enum LayoutMode { |
| 30 | /// A plain text input mode. |
| 31 | PlainText { tab: TabSize, rows: usize }, |
| 32 | /// An auto grow input mode. |
| 33 | AutoGrow { |
| 34 | rows: usize, |
| 35 | min_rows: usize, |
| 36 | max_rows: usize, |
| 37 | }, |
| 38 | /// A code editor input mode. |
| 39 | CodeEditor { |
| 40 | tab: TabSize, |
| 41 | rows: usize, |
| 42 | /// Show line number |
| 43 | line_number: bool, |
| 44 | language: Box<EditorLanguage>, |
| 45 | indent_guides: bool, |
| 46 | folding: bool, |
| 47 | highlighter: Rc<RefCell<Option<Box<dyn InputHighlighter>>>>, |
| 48 | highlighter_factory: Option<InputHighlighterFactory>, |
| 49 | diagnostics: DiagnosticSet, |
| 50 | /// Automatic delimiter ranges, adjusted with document edits. |
| 51 | auto_closed_pairs: super::auto_close::AutoClosedPairs, |
| 52 | auto_close: bool, |
| 53 | smart_indent: bool, |
| 54 | }, |
| 55 | } |
| 56 | |
| 57 | impl Default for LayoutMode { |
| 58 | fn default() -> Self { |
| 59 | LayoutMode::plain_text() |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #[allow(unused)] |
| 64 | impl LayoutMode { |
| 65 | /// Create a plain input mode with default settings. |
| 66 | pub(super) fn plain_text() -> Self { |
| 67 | LayoutMode::PlainText { |
| 68 | tab: TabSize::default(), |
| 69 | rows: 1, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// Create a code editor input mode with default settings. |
| 74 | /// |
| 75 | /// Starts with no language; the state sets one through its own builder. |
| 76 | pub(super) fn code_editor(language: EditorLanguage) -> Self { |
| 77 | LayoutMode::CodeEditor { |
| 78 | rows: 2, |
| 79 | tab: TabSize::default(), |
| 80 | language: Box::new(language), |
| 81 | highlighter: Rc::new(RefCell::new(None)), |
| 82 | highlighter_factory: None, |
| 83 | line_number: true, |
| 84 | indent_guides: true, |
| 85 | folding: true, |
| 86 | diagnostics: DiagnosticSet::new(&Rope::new()), |
| 87 | auto_closed_pairs: Default::default(), |
| 88 | auto_close: true, |
| 89 | smart_indent: true, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// Create an auto grow input mode with given min and max rows. |
| 94 | pub(super) fn auto_grow(min_rows: usize, max_rows: usize) -> Self { |
| 95 | LayoutMode::AutoGrow { |
| 96 | rows: min_rows, |
| 97 | min_rows, |
| 98 | max_rows, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /// Return true if this layout is a code editor with folding enabled. |
| 103 | #[inline] |
| 104 | pub(crate) fn is_folding(&self) -> bool { |
| 105 | if cfg!(target_family = "wasm") { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | matches!(self, LayoutMode::CodeEditor { folding: true, .. }) |
| 110 | } |
| 111 | |
| 112 | pub(super) fn language_config(&self) -> Option<Rc<LanguageConfig>> { |
| 113 | match self { |
| 114 | Self::CodeEditor { language, .. } => Some(language.config()), |
| 115 | _ => None, |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | pub(super) fn syntax_context_at( |
| 120 | &self, |
| 121 | text: &Rope, |
| 122 | offset: usize, |
| 123 | ) -> crate::input::SyntaxContext { |
| 124 | match self { |
| 125 | Self::CodeEditor { language, .. } => language.context_at(text, offset), |
| 126 | _ => crate::input::SyntaxContext::Code, |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | pub(super) fn is_auto_close(&self) -> bool { |
| 131 | matches!( |
| 132 | self, |
| 133 | Self::CodeEditor { |
| 134 | auto_close: true, |
| 135 | .. |
| 136 | } |
| 137 | ) |
| 138 | } |
| 139 | |
| 140 | pub(super) fn is_smart_indent(&self) -> bool { |
| 141 | matches!( |
| 142 | self, |
| 143 | Self::CodeEditor { |
| 144 | smart_indent: true, |
| 145 | .. |
| 146 | } |
| 147 | ) |
| 148 | } |
| 149 | |
| 150 | pub(super) fn set_auto_close(&mut self, enabled: bool) { |
| 151 | if let Self::CodeEditor { auto_close, .. } = self { |
| 152 | *auto_close = enabled; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | pub(super) fn set_smart_indent(&mut self, enabled: bool) { |
| 157 | if let Self::CodeEditor { smart_indent, .. } = self { |
| 158 | *smart_indent = enabled; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | pub(super) fn auto_closed_pairs(&self) -> &super::auto_close::AutoClosedPairs { |
| 163 | match self { |
| 164 | Self::CodeEditor { |
| 165 | auto_closed_pairs, .. |
| 166 | } => auto_closed_pairs, |
| 167 | _ => super::auto_close::AutoClosedPairs::empty(), |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | pub(super) fn restore_auto_closed_pairs(&mut self, pairs: super::auto_close::AutoClosedPairs) { |
| 172 | if let Self::CodeEditor { |
| 173 | auto_closed_pairs, .. |
| 174 | } = self |
| 175 | { |
| 176 | *auto_closed_pairs = pairs; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | pub(super) fn track_auto_closed_pair(&mut self, open: Range<usize>, close: Range<usize>) { |
| 181 | if let Self::CodeEditor { |
| 182 | auto_closed_pairs, .. |
| 183 | } = self |
| 184 | { |
| 185 | auto_closed_pairs.record(open, close); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | pub(super) fn adjust_auto_closed_pair(&mut self, edit: &Range<usize>, new_len: usize) { |
| 190 | let Self::CodeEditor { |
| 191 | auto_closed_pairs, .. |
| 192 | } = self |
| 193 | else { |
| 194 | return; |
| 195 | }; |
| 196 | auto_closed_pairs.adjust(edit, new_len); |
| 197 | } |
| 198 | |
| 199 | #[inline] |
| 200 | pub(super) fn is_auto_grow(&self) -> bool { |
| 201 | matches!(self, LayoutMode::AutoGrow { .. }) |
| 202 | } |
| 203 | |
| 204 | pub(super) fn set_rows(&mut self, new_rows: usize) { |
| 205 | match self { |
| 206 | LayoutMode::PlainText { rows, .. } => { |
| 207 | *rows = new_rows; |
| 208 | } |
| 209 | LayoutMode::CodeEditor { rows, .. } => { |
| 210 | *rows = new_rows; |
| 211 | } |
| 212 | LayoutMode::AutoGrow { |
| 213 | rows, |
| 214 | min_rows, |
| 215 | max_rows, |
| 216 | } => { |
| 217 | *rows = new_rows.clamp(*min_rows, *max_rows); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /// Grow the row count to fit the content. |
| 223 | /// |
| 224 | /// Callers gate this on the input being multi-line; a single-line field |
| 225 | /// keeps its one row. Only auto grow follows the content: in the other |
| 226 | /// modes `rows` is the configured height. |
| 227 | pub(super) fn update_auto_grow(&mut self, display_map: &DisplayMap) { |
| 228 | if !self.is_auto_grow() { |
| 229 | return; |
| 230 | } |
| 231 | let wrapped_lines = display_map.wrap_row_count(); |
| 232 | self.set_rows(wrapped_lines); |
| 233 | } |
| 234 | |
| 235 | /// At least 1 row be return. |
| 236 | pub(super) fn rows(&self) -> usize { |
| 237 | match self { |
| 238 | LayoutMode::PlainText { rows, .. } => *rows, |
| 239 | LayoutMode::CodeEditor { rows, .. } => *rows, |
| 240 | LayoutMode::AutoGrow { rows, .. } => *rows, |
| 241 | } |
| 242 | .max(1) |
| 243 | } |
| 244 | |
| 245 | /// At least 1 row be return. |
| 246 | #[allow(unused)] |
| 247 | pub(super) fn min_rows(&self) -> usize { |
| 248 | match self { |
| 249 | LayoutMode::AutoGrow { min_rows, .. } => *min_rows, |
| 250 | _ => 1, |
| 251 | } |
| 252 | .max(1) |
| 253 | } |
| 254 | |
| 255 | #[allow(unused)] |
| 256 | pub(super) fn max_rows(&self) -> usize { |
| 257 | match self { |
| 258 | LayoutMode::AutoGrow { max_rows, .. } => *max_rows, |
| 259 | _ => usize::MAX, |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /// Return false if the mode is not [`LayoutMode::CodeEditor`]. |
| 264 | #[inline] |
| 265 | pub(super) fn line_number(&self) -> bool { |
| 266 | match self { |
| 267 | LayoutMode::CodeEditor { line_number, .. } => *line_number, |
| 268 | _ => false, |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /// Update the syntax highlighter with new text. |
| 273 | /// |
| 274 | pub(crate) fn update_highlighter<M: InputModeKind>( |
| 275 | &mut self, |
| 276 | update: HighlighterUpdate<'_>, |
| 277 | window: &mut Window, |
| 278 | cx: &mut Context<crate::input::InputBaseState<M>>, |
| 279 | ) { |
| 280 | match &self { |
| 281 | LayoutMode::CodeEditor { |
| 282 | language, |
| 283 | highlighter, |
| 284 | highlighter_factory, |
| 285 | folding, |
| 286 | .. |
| 287 | } => { |
| 288 | if !update.force && highlighter.borrow().is_some() { |
| 289 | return; |
| 290 | } |
| 291 | if !ensure_highlighter(highlighter, highlighter_factory.as_ref(), language) { |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | let edit = replacement_input_edit( |
| 296 | update.old_text, |
| 297 | update.new_text, |
| 298 | update.selected_range, |
| 299 | update.change_text, |
| 300 | ); |
| 301 | M::drive_highlighter(highlighter, edit, update.new_text, *folding, window, cx); |
| 302 | } |
| 303 | _ => {} |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /// Drive the highlighter once for several edits applied as one change, |
| 308 | /// each paired with the text right after it, in application order. |
| 309 | pub(crate) fn update_highlighter_batch<M: InputModeKind>( |
| 310 | &mut self, |
| 311 | edits: &[(InputEdit, Rope)], |
| 312 | window: &mut Window, |
| 313 | cx: &mut Context<crate::input::InputBaseState<M>>, |
| 314 | ) { |
| 315 | let LayoutMode::CodeEditor { |
| 316 | language, |
| 317 | highlighter, |
| 318 | highlighter_factory, |
| 319 | folding, |
| 320 | .. |
| 321 | } = &self |
| 322 | else { |
| 323 | return; |
| 324 | }; |
| 325 | if !ensure_highlighter(highlighter, highlighter_factory.as_ref(), language) { |
| 326 | return; |
| 327 | } |
| 328 | M::drive_highlighter_batch(highlighter, edits, *folding, window, cx); |
| 329 | } |
| 330 | |
| 331 | #[allow(unused)] |
| 332 | pub(super) fn diagnostics(&self) -> Option<&DiagnosticSet> { |
| 333 | match self { |
| 334 | LayoutMode::CodeEditor { diagnostics, .. } => Some(diagnostics), |
| 335 | _ => None, |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | pub(super) fn diagnostics_mut(&mut self) -> Option<&mut DiagnosticSet> { |
| 340 | match self { |
| 341 | LayoutMode::CodeEditor { diagnostics, .. } => Some(diagnostics), |
| 342 | _ => None, |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /// Get a reference to the highlighter (if available) |
| 347 | pub(super) fn highlighter(&self) -> Option<&Rc<RefCell<Option<Box<dyn InputHighlighter>>>>> { |
| 348 | match self { |
| 349 | LayoutMode::CodeEditor { highlighter, .. } => Some(highlighter), |
| 350 | _ => None, |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | pub(super) fn set_highlighter_factory(&mut self, factory: InputHighlighterFactory) { |
| 355 | if let LayoutMode::CodeEditor { |
| 356 | highlighter_factory, |
| 357 | highlighter, |
| 358 | .. |
| 359 | } = self |
| 360 | { |
| 361 | *highlighter_factory = Some(factory); |
| 362 | *highlighter.borrow_mut() = None; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | pub(super) fn ensure_highlighter_factory(&mut self, factory: InputHighlighterFactory) { |
| 367 | if let LayoutMode::CodeEditor { |
| 368 | highlighter_factory, |
| 369 | .. |
| 370 | } = self |
| 371 | { |
| 372 | if highlighter_factory.is_none() { |
| 373 | *highlighter_factory = Some(factory); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /// Create the highlighter from the factory if there is none yet. Returns |
| 380 | /// whether one is installed. |
| 381 | fn ensure_highlighter( |
| 382 | highlighter: &RefCell<Option<Box<dyn InputHighlighter>>>, |
| 383 | highlighter_factory: Option<&InputHighlighterFactory>, |
| 384 | language: &EditorLanguage, |
| 385 | ) -> bool { |
| 386 | let mut highlighter = highlighter.borrow_mut(); |
| 387 | if highlighter.is_none() |
| 388 | && let Some(factory) = highlighter_factory |
| 389 | { |
| 390 | *highlighter = factory(&language.name()); |
| 391 | } |
| 392 | highlighter.is_some() |
| 393 | } |
| 394 | |
| 395 | /// Builds the tree-sitter edit for a text replacement. |
| 396 | /// |
| 397 | /// Byte offsets and positions for `start`/`old_end` come from `old_text`; |
| 398 | /// `new_end` byte/position come from the post-edit `text`. |
| 399 | pub(super) fn replacement_input_edit( |
| 400 | old_text: &Rope, |
| 401 | new_text: &Rope, |
| 402 | selected_range: &Range<usize>, |
| 403 | change_text: &str, |
| 404 | ) -> InputEdit { |
| 405 | let start_byte = selected_range.start.min(old_text.len()); |
| 406 | let old_end_byte = selected_range.end.min(old_text.len()).max(start_byte); |
| 407 | let new_end_byte = (start_byte + change_text.len()).min(new_text.len()); |
| 408 | |
| 409 | InputEdit { |
| 410 | start_byte, |
| 411 | old_end_byte, |
| 412 | new_end_byte, |
| 413 | start_position: old_text.offset_to_point(start_byte), |
| 414 | old_end_position: old_text.offset_to_point(old_end_byte), |
| 415 | new_end_position: new_text.offset_to_point(new_end_byte), |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | #[cfg(test)] |
| 420 | mod tests { |
| 421 | use ropey::Rope; |
| 422 | |
| 423 | use super::replacement_input_edit; |
| 424 | use crate::input::{DiagnosticSet, Point, TabSize, mode::LayoutMode}; |
| 425 | |
| 426 | #[test] |
| 427 | fn test_replacement_input_edit_backspace_at_end_uses_old_range() { |
| 428 | let old_text = Rope::from_str("-="); |
| 429 | let text = Rope::from_str("-"); |
| 430 | let edit = replacement_input_edit(&old_text, &text, &(1..2), ""); |
| 431 | |
| 432 | assert_eq!(edit.start_byte, 1); |
| 433 | assert_eq!(edit.old_end_byte, 2); |
| 434 | assert_eq!(edit.new_end_byte, 1); |
| 435 | assert_eq!(edit.start_position, Point::new(0, 1)); |
| 436 | assert_eq!(edit.old_end_position, Point::new(0, 2)); |
| 437 | assert_eq!(edit.new_end_position, Point::new(0, 1)); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn test_code_editor() { |
| 442 | let mode = LayoutMode::code_editor(Default::default()); |
| 443 | assert_eq!(mode.line_number(), true); |
| 444 | assert_eq!(mode.has_indent_guides(), true); |
| 445 | assert_eq!(mode.max_rows(), usize::MAX); |
| 446 | assert_eq!(mode.min_rows(), 1); |
| 447 | assert_eq!(mode.is_folding(), true); |
| 448 | |
| 449 | let mode = LayoutMode::CodeEditor { |
| 450 | line_number: false, |
| 451 | indent_guides: false, |
| 452 | folding: false, |
| 453 | rows: 0, |
| 454 | tab: Default::default(), |
| 455 | language: Box::default(), |
| 456 | highlighter: Default::default(), |
| 457 | highlighter_factory: None, |
| 458 | diagnostics: DiagnosticSet::new(&Rope::new()), |
| 459 | auto_closed_pairs: Default::default(), |
| 460 | auto_close: false, |
| 461 | smart_indent: false, |
| 462 | }; |
| 463 | assert_eq!(mode.line_number(), false); |
| 464 | assert_eq!(mode.has_indent_guides(), false); |
| 465 | assert_eq!(mode.min_rows(), 1); |
| 466 | assert_eq!(mode.is_folding(), false); |
| 467 | assert_eq!(mode.is_auto_close(), false); |
| 468 | assert_eq!(mode.is_smart_indent(), false); |
| 469 | } |
| 470 | |
| 471 | #[test] |
| 472 | fn test_plain() { |
| 473 | let mode = LayoutMode::PlainText { |
| 474 | tab: TabSize::default(), |
| 475 | rows: 5, |
| 476 | }; |
| 477 | assert_eq!(mode.line_number(), false); |
| 478 | assert_eq!(mode.rows(), 5); |
| 479 | assert_eq!(mode.max_rows(), usize::MAX); |
| 480 | assert_eq!(mode.min_rows(), 1); |
| 481 | |
| 482 | let mode = LayoutMode::plain_text(); |
| 483 | assert_eq!(mode.line_number(), false); |
| 484 | assert_eq!(mode.rows(), 1); |
| 485 | assert_eq!(mode.min_rows(), 1); |
| 486 | } |
| 487 | |
| 488 | #[test] |
| 489 | fn test_auto_grow() { |
| 490 | let mut mode = LayoutMode::auto_grow(2, 5); |
| 491 | assert_eq!(mode.line_number(), false); |
| 492 | assert_eq!(mode.rows(), 2); |
| 493 | assert_eq!(mode.max_rows(), 5); |
| 494 | assert_eq!(mode.min_rows(), 2); |
| 495 | |
| 496 | mode.set_rows(4); |
| 497 | assert_eq!(mode.rows(), 4); |
| 498 | |
| 499 | mode.set_rows(1); |
| 500 | assert_eq!(mode.rows(), 2); |
| 501 | |
| 502 | mode.set_rows(10); |
| 503 | assert_eq!(mode.rows(), 5); |
| 504 | } |
| 505 | } |