@@ -6,6 +6,7 @@ use std::time::{Duration, Instant}; |
| 6 | 6 | |
| 7 | 7 | use crate::buffer::Buffer; |
| 8 | 8 | use crate::input::{Key, Modifiers, Mouse, Button}; |
| 9 | +use crate::lsp::{CompletionItem, Diagnostic, HoverInfo, Location, ServerManagerPanel}; |
| 9 | 10 | use crate::render::{PaneBounds as RenderPaneBounds, PaneInfo, Screen, TabInfo}; |
| 10 | 11 | use crate::workspace::{PaneDirection, Tab, Workspace}; |
| 11 | 12 | |
@@ -34,6 +35,36 @@ enum TextInputAction { |
| 34 | 35 | GitCommit, |
| 35 | 36 | /// Create a git tag |
| 36 | 37 | GitTag, |
| 38 | + /// LSP rename symbol |
| 39 | + LspRename { path: String, line: u32, col: u32 }, |
| 40 | +} |
| 41 | + |
| 42 | +/// LSP UI state |
| 43 | +#[derive(Debug, Default)] |
| 44 | +struct LspState { |
| 45 | + /// Current hover information to display |
| 46 | + hover: Option<HoverInfo>, |
| 47 | + /// Whether hover popup is visible |
| 48 | + hover_visible: bool, |
| 49 | + /// Current completion list |
| 50 | + completions: Vec<CompletionItem>, |
| 51 | + /// Selected completion index |
| 52 | + completion_index: usize, |
| 53 | + /// Whether completion popup is visible |
| 54 | + completion_visible: bool, |
| 55 | + /// Current diagnostics for the active file |
| 56 | + diagnostics: Vec<Diagnostic>, |
| 57 | + /// Go-to-definition results (for multi-result navigation) |
| 58 | + definition_locations: Vec<Location>, |
| 59 | + /// Pending request IDs (to match responses) |
| 60 | + pending_hover: Option<i64>, |
| 61 | + pending_completion: Option<i64>, |
| 62 | + pending_definition: Option<i64>, |
| 63 | + pending_references: Option<i64>, |
| 64 | + /// Last known buffer hash (to detect changes) |
| 65 | + last_buffer_hash: Option<u64>, |
| 66 | + /// Last file path that was synced to LSP |
| 67 | + last_synced_path: Option<PathBuf>, |
| 37 | 68 | } |
| 38 | 69 | |
| 39 | 70 | /// Main editor state |
@@ -56,6 +87,10 @@ pub struct Editor { |
| 56 | 87 | prompt: PromptState, |
| 57 | 88 | /// Last time we wrote backups |
| 58 | 89 | last_backup: Instant, |
| 90 | + /// LSP-related UI state |
| 91 | + lsp_state: LspState, |
| 92 | + /// LSP server manager panel |
| 93 | + server_manager: ServerManagerPanel, |
| 59 | 94 | } |
| 60 | 95 | |
| 61 | 96 | impl Editor { |
@@ -97,6 +132,8 @@ impl Editor { |
| 97 | 132 | escape_time, |
| 98 | 133 | prompt: PromptState::None, |
| 99 | 134 | last_backup: Instant::now(), |
| 135 | + lsp_state: LspState::default(), |
| 136 | + server_manager: ServerManagerPanel::new(), |
| 100 | 137 | }; |
| 101 | 138 | |
| 102 | 139 | // If there are backups, show restore prompt |
@@ -208,6 +245,21 @@ impl Editor { |
| 208 | 245 | tab.panes[pane_idx].viewport_line = line; |
| 209 | 246 | } |
| 210 | 247 | |
| 248 | + /// Get current viewport column (horizontal scroll offset) |
| 249 | + #[inline] |
| 250 | + fn viewport_col(&self) -> usize { |
| 251 | + let tab = self.workspace.active_tab(); |
| 252 | + tab.panes[tab.active_pane].viewport_col |
| 253 | + } |
| 254 | + |
| 255 | + /// Set current viewport column (horizontal scroll offset) |
| 256 | + #[inline] |
| 257 | + fn set_viewport_col(&mut self, col: usize) { |
| 258 | + let tab = self.workspace.active_tab_mut(); |
| 259 | + let pane_idx = tab.active_pane; |
| 260 | + tab.panes[pane_idx].viewport_col = col; |
| 261 | + } |
| 262 | + |
| 211 | 263 | /// Get current filename |
| 212 | 264 | #[inline] |
| 213 | 265 | fn filename(&self) -> Option<PathBuf> { |
@@ -222,19 +274,12 @@ impl Editor { |
| 222 | 274 | self.render()?; |
| 223 | 275 | |
| 224 | 276 | while self.running { |
| 225 | | - // Block until an event is available (no busy polling) |
| 226 | | - match event::read()? { |
| 227 | | - Event::Key(key_event) => self.process_key(key_event)?, |
| 228 | | - Event::Mouse(mouse_event) => self.process_mouse(mouse_event)?, |
| 229 | | - Event::Resize(cols, rows) => { |
| 230 | | - self.screen.cols = cols; |
| 231 | | - self.screen.rows = rows; |
| 232 | | - } |
| 233 | | - _ => {} |
| 234 | | - } |
| 277 | + // Track whether we need to re-render |
| 278 | + let mut needs_render = false; |
| 235 | 279 | |
| 236 | | - // Process any additional queued events before rendering |
| 237 | | - while event::poll(Duration::from_millis(0))? { |
| 280 | + // Poll with a short timeout to allow LSP processing |
| 281 | + // This balances responsiveness with CPU usage |
| 282 | + if event::poll(Duration::from_millis(50))? { |
| 238 | 283 | match event::read()? { |
| 239 | 284 | Event::Key(key_event) => self.process_key(key_event)?, |
| 240 | 285 | Event::Mouse(mouse_event) => self.process_mouse(mouse_event)?, |
@@ -244,14 +289,40 @@ impl Editor { |
| 244 | 289 | } |
| 245 | 290 | _ => {} |
| 246 | 291 | } |
| 292 | + needs_render = true; |
| 293 | + |
| 294 | + // Process any additional queued events before rendering |
| 295 | + while event::poll(Duration::from_millis(0))? { |
| 296 | + match event::read()? { |
| 297 | + Event::Key(key_event) => self.process_key(key_event)?, |
| 298 | + Event::Mouse(mouse_event) => self.process_mouse(mouse_event)?, |
| 299 | + Event::Resize(cols, rows) => { |
| 300 | + self.screen.cols = cols; |
| 301 | + self.screen.rows = rows; |
| 302 | + } |
| 303 | + _ => {} |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + // Process LSP messages from language servers |
| 309 | + if self.process_lsp_messages() { |
| 310 | + needs_render = true; |
| 311 | + } |
| 312 | + |
| 313 | + // Poll for completed server installations |
| 314 | + if self.server_manager.poll_installs() { |
| 315 | + needs_render = true; |
| 247 | 316 | } |
| 248 | 317 | |
| 249 | 318 | // Check if it's time to backup modified buffers |
| 250 | 319 | self.maybe_backup(); |
| 251 | 320 | |
| 252 | | - // Only render after processing events |
| 253 | | - self.screen.refresh_size()?; |
| 254 | | - self.render()?; |
| 321 | + // Only render if something changed |
| 322 | + if needs_render { |
| 323 | + self.screen.refresh_size()?; |
| 324 | + self.render()?; |
| 325 | + } |
| 255 | 326 | } |
| 256 | 327 | |
| 257 | 328 | self.screen.leave_raw_mode()?; |
@@ -268,6 +339,518 @@ impl Editor { |
| 268 | 339 | } |
| 269 | 340 | } |
| 270 | 341 | |
| 342 | + /// Process LSP messages. Returns true if any messages were processed. |
| 343 | + fn process_lsp_messages(&mut self) -> bool { |
| 344 | + use crate::lsp::LspResponse; |
| 345 | + |
| 346 | + // Process pending messages from language servers |
| 347 | + self.workspace.lsp.process_messages(); |
| 348 | + |
| 349 | + let mut had_response = false; |
| 350 | + |
| 351 | + // Handle any responses that came in |
| 352 | + while let Some(response) = self.workspace.lsp.poll_response() { |
| 353 | + had_response = true; |
| 354 | + match response { |
| 355 | + LspResponse::Completions(id, items) => { |
| 356 | + if self.lsp_state.pending_completion == Some(id) { |
| 357 | + self.lsp_state.completions = items; |
| 358 | + self.lsp_state.completion_index = 0; |
| 359 | + self.lsp_state.completion_visible = !self.lsp_state.completions.is_empty(); |
| 360 | + self.lsp_state.pending_completion = None; |
| 361 | + } |
| 362 | + } |
| 363 | + LspResponse::Hover(id, info) => { |
| 364 | + if self.lsp_state.pending_hover == Some(id) { |
| 365 | + self.lsp_state.hover = info; |
| 366 | + self.lsp_state.hover_visible = self.lsp_state.hover.is_some(); |
| 367 | + self.lsp_state.pending_hover = None; |
| 368 | + if self.lsp_state.hover.is_none() { |
| 369 | + self.message = Some("No hover info available".to_string()); |
| 370 | + } |
| 371 | + } |
| 372 | + } |
| 373 | + LspResponse::Definition(id, locations) => { |
| 374 | + if self.lsp_state.pending_definition == Some(id) { |
| 375 | + self.lsp_state.definition_locations = locations.clone(); |
| 376 | + self.lsp_state.pending_definition = None; |
| 377 | + // Jump to first definition |
| 378 | + if let Some(loc) = locations.first() { |
| 379 | + self.goto_location(loc); |
| 380 | + } else { |
| 381 | + self.message = Some("No definition found".to_string()); |
| 382 | + } |
| 383 | + } |
| 384 | + } |
| 385 | + LspResponse::References(id, locations) => { |
| 386 | + if self.lsp_state.pending_references == Some(id) { |
| 387 | + self.lsp_state.pending_references = None; |
| 388 | + if locations.is_empty() { |
| 389 | + self.message = Some("No references found".to_string()); |
| 390 | + } else if locations.len() == 1 { |
| 391 | + self.goto_location(&locations[0]); |
| 392 | + } else { |
| 393 | + // Multiple references - show count and go to first |
| 394 | + self.message = Some(format!("Found {} references", locations.len())); |
| 395 | + self.goto_location(&locations[0]); |
| 396 | + } |
| 397 | + } |
| 398 | + } |
| 399 | + LspResponse::Symbols(id, symbols) => { |
| 400 | + // TODO: Show symbols panel |
| 401 | + let _ = (id, symbols); |
| 402 | + } |
| 403 | + LspResponse::Formatting(id, edits) => { |
| 404 | + // Apply formatting edits |
| 405 | + let _ = (id, edits); |
| 406 | + // TODO: Apply text edits to buffer |
| 407 | + } |
| 408 | + LspResponse::Rename(_id, workspace_edit) => { |
| 409 | + // Apply rename edits across all affected files |
| 410 | + let mut total_edits = 0; |
| 411 | + let mut files_changed = 0; |
| 412 | + |
| 413 | + for (uri, edits) in &workspace_edit.changes { |
| 414 | + if let Some(path_str) = crate::lsp::uri_to_path(uri) { |
| 415 | + // Check if we have this file open |
| 416 | + let path = std::path::PathBuf::from(&path_str); |
| 417 | + if let Some(tab_idx) = self.workspace.find_tab_by_path(&path) { |
| 418 | + // Apply edits to the open buffer (in reverse order to preserve positions) |
| 419 | + let mut sorted_edits = edits.clone(); |
| 420 | + sorted_edits.sort_by(|a, b| { |
| 421 | + // Sort by start position, descending |
| 422 | + b.range.start.line.cmp(&a.range.start.line) |
| 423 | + .then(b.range.start.character.cmp(&a.range.start.character)) |
| 424 | + }); |
| 425 | + |
| 426 | + for edit in sorted_edits { |
| 427 | + self.workspace.apply_text_edit(tab_idx, &edit); |
| 428 | + total_edits += 1; |
| 429 | + } |
| 430 | + files_changed += 1; |
| 431 | + } else { |
| 432 | + // File not open - would need to open, edit, and save |
| 433 | + self.message = Some(format!("Note: {} not open, skipped", path_str)); |
| 434 | + } |
| 435 | + } |
| 436 | + } |
| 437 | + |
| 438 | + if total_edits > 0 { |
| 439 | + self.message = Some(format!("Renamed: {} edits in {} file(s)", total_edits, files_changed)); |
| 440 | + } else { |
| 441 | + self.message = Some("No rename edits to apply".to_string()); |
| 442 | + } |
| 443 | + } |
| 444 | + LspResponse::CodeActions(id, actions) => { |
| 445 | + // TODO: Show code actions menu |
| 446 | + let _ = (id, actions); |
| 447 | + } |
| 448 | + LspResponse::Error(id, message) => { |
| 449 | + // Clear any pending state for this request |
| 450 | + if self.lsp_state.pending_completion == Some(id) { |
| 451 | + self.lsp_state.pending_completion = None; |
| 452 | + } |
| 453 | + if self.lsp_state.pending_hover == Some(id) { |
| 454 | + self.lsp_state.pending_hover = None; |
| 455 | + } |
| 456 | + if self.lsp_state.pending_definition == Some(id) { |
| 457 | + self.lsp_state.pending_definition = None; |
| 458 | + } |
| 459 | + if self.lsp_state.pending_references == Some(id) { |
| 460 | + self.lsp_state.pending_references = None; |
| 461 | + } |
| 462 | + // Optionally show error |
| 463 | + if !message.is_empty() { |
| 464 | + self.message = Some(format!("LSP: {}", message)); |
| 465 | + } |
| 466 | + } |
| 467 | + } |
| 468 | + } |
| 469 | + |
| 470 | + // Update diagnostics for current file |
| 471 | + if let Some(path) = self.filename() { |
| 472 | + let path_str = path.to_string_lossy(); |
| 473 | + self.lsp_state.diagnostics = self.workspace.lsp.get_diagnostics(&path_str); |
| 474 | + } |
| 475 | + |
| 476 | + // Sync document changes to LSP if buffer has changed |
| 477 | + self.sync_document_to_lsp(); |
| 478 | + |
| 479 | + had_response |
| 480 | + } |
| 481 | + |
| 482 | + /// Sync document changes to LSP server |
| 483 | + fn sync_document_to_lsp(&mut self) { |
| 484 | + let current_path = self.filename(); |
| 485 | + let current_hash = self.buffer().content_hash(); |
| 486 | + |
| 487 | + // Check if we switched files |
| 488 | + let file_changed = current_path != self.lsp_state.last_synced_path; |
| 489 | + |
| 490 | + // Check if buffer content changed |
| 491 | + let content_changed = self.lsp_state.last_buffer_hash != Some(current_hash); |
| 492 | + |
| 493 | + if file_changed { |
| 494 | + // Close the old document if we had one open |
| 495 | + if let Some(ref old_path) = self.lsp_state.last_synced_path { |
| 496 | + let old_path_str = old_path.to_string_lossy(); |
| 497 | + let _ = self.workspace.lsp.close_document(&old_path_str); |
| 498 | + } |
| 499 | + |
| 500 | + // Open the new document |
| 501 | + if let Some(ref path) = current_path { |
| 502 | + let tab = self.workspace.active_tab(); |
| 503 | + let pane = &tab.panes[tab.active_pane]; |
| 504 | + let buffer_entry = &tab.buffers[pane.buffer_idx]; |
| 505 | + |
| 506 | + let full_path = if buffer_entry.is_orphan { |
| 507 | + path.clone() |
| 508 | + } else { |
| 509 | + self.workspace.root.join(path) |
| 510 | + }; |
| 511 | + let path_str = full_path.to_string_lossy(); |
| 512 | + let content = self.buffer().contents(); |
| 513 | + let _ = self.workspace.lsp.open_document(&path_str, &content); |
| 514 | + } |
| 515 | + |
| 516 | + self.lsp_state.last_synced_path = current_path; |
| 517 | + self.lsp_state.last_buffer_hash = Some(current_hash); |
| 518 | + } else if content_changed { |
| 519 | + // Content changed - send didChange notification |
| 520 | + if let Some(ref path) = current_path { |
| 521 | + let tab = self.workspace.active_tab(); |
| 522 | + let pane = &tab.panes[tab.active_pane]; |
| 523 | + let buffer_entry = &tab.buffers[pane.buffer_idx]; |
| 524 | + |
| 525 | + let full_path = if buffer_entry.is_orphan { |
| 526 | + path.clone() |
| 527 | + } else { |
| 528 | + self.workspace.root.join(path) |
| 529 | + }; |
| 530 | + let path_str = full_path.to_string_lossy(); |
| 531 | + let content = self.buffer().contents(); |
| 532 | + let _ = self.workspace.lsp.document_changed(&path_str, &content); |
| 533 | + } |
| 534 | + |
| 535 | + self.lsp_state.last_buffer_hash = Some(current_hash); |
| 536 | + } |
| 537 | + } |
| 538 | + |
| 539 | + /// Navigate to an LSP location |
| 540 | + fn goto_location(&mut self, location: &Location) { |
| 541 | + use crate::lsp::uri_to_path; |
| 542 | + |
| 543 | + if let Some(path) = uri_to_path(&location.uri) { |
| 544 | + let path_buf = PathBuf::from(&path); |
| 545 | + // Open the file if not already open |
| 546 | + if let Err(e) = self.workspace.open_file(&path_buf) { |
| 547 | + self.message = Some(format!("Failed to open {}: {}", path, e)); |
| 548 | + return; |
| 549 | + } |
| 550 | + |
| 551 | + // Move cursor to the location |
| 552 | + let line = location.range.start.line as usize; |
| 553 | + let col = location.range.start.character as usize; |
| 554 | + |
| 555 | + self.cursors_mut().collapse_to_primary(); |
| 556 | + self.cursor_mut().line = line.min(self.buffer().line_count().saturating_sub(1)); |
| 557 | + self.cursor_mut().col = col.min(self.buffer().line_len(self.cursor().line)); |
| 558 | + self.cursor_mut().desired_col = self.cursor().col; |
| 559 | + self.cursor_mut().clear_selection(); |
| 560 | + self.scroll_to_cursor(); |
| 561 | + } |
| 562 | + } |
| 563 | + |
| 564 | + /// Get the full path to the current file |
| 565 | + fn current_file_path(&self) -> Option<PathBuf> { |
| 566 | + let tab = self.workspace.active_tab(); |
| 567 | + let pane = &tab.panes[tab.active_pane]; |
| 568 | + let buffer_entry = &tab.buffers[pane.buffer_idx]; |
| 569 | + |
| 570 | + buffer_entry.path.as_ref().map(|p| { |
| 571 | + if buffer_entry.is_orphan { |
| 572 | + p.clone() |
| 573 | + } else { |
| 574 | + self.workspace.root.join(p) |
| 575 | + } |
| 576 | + }) |
| 577 | + } |
| 578 | + |
| 579 | + /// LSP: Go to definition |
| 580 | + fn lsp_goto_definition(&mut self) { |
| 581 | + if let Some(path) = self.current_file_path() { |
| 582 | + let path_str = path.to_string_lossy().to_string(); |
| 583 | + let line = self.cursor().line as u32; |
| 584 | + let col = self.cursor().col as u32; |
| 585 | + |
| 586 | + match self.workspace.lsp.request_definition(&path_str, line, col) { |
| 587 | + Ok(id) => { |
| 588 | + self.lsp_state.pending_definition = Some(id); |
| 589 | + self.message = Some("Finding definition...".to_string()); |
| 590 | + } |
| 591 | + Err(e) => { |
| 592 | + self.message = Some(format!("LSP error: {}", e)); |
| 593 | + } |
| 594 | + } |
| 595 | + } else { |
| 596 | + self.message = Some("No file open".to_string()); |
| 597 | + } |
| 598 | + } |
| 599 | + |
| 600 | + /// LSP: Find references |
| 601 | + fn lsp_find_references(&mut self) { |
| 602 | + if let Some(path) = self.current_file_path() { |
| 603 | + let path_str = path.to_string_lossy().to_string(); |
| 604 | + let line = self.cursor().line as u32; |
| 605 | + let col = self.cursor().col as u32; |
| 606 | + |
| 607 | + match self.workspace.lsp.request_references(&path_str, line, col, true) { |
| 608 | + Ok(id) => { |
| 609 | + self.lsp_state.pending_references = Some(id); |
| 610 | + self.message = Some("Finding references...".to_string()); |
| 611 | + } |
| 612 | + Err(e) => { |
| 613 | + self.message = Some(format!("LSP error: {}", e)); |
| 614 | + } |
| 615 | + } |
| 616 | + } else { |
| 617 | + self.message = Some("No file open".to_string()); |
| 618 | + } |
| 619 | + } |
| 620 | + |
| 621 | + /// LSP: Show hover information |
| 622 | + fn lsp_hover(&mut self) { |
| 623 | + if let Some(path) = self.current_file_path() { |
| 624 | + let path_str = path.to_string_lossy().to_string(); |
| 625 | + let line = self.cursor().line as u32; |
| 626 | + let col = self.cursor().col as u32; |
| 627 | + |
| 628 | + match self.workspace.lsp.request_hover(&path_str, line, col) { |
| 629 | + Ok(id) => { |
| 630 | + self.lsp_state.pending_hover = Some(id); |
| 631 | + self.message = Some("Loading hover info...".to_string()); |
| 632 | + } |
| 633 | + Err(e) => { |
| 634 | + self.message = Some(format!("LSP error: {}", e)); |
| 635 | + } |
| 636 | + } |
| 637 | + } else { |
| 638 | + self.message = Some("No file open".to_string()); |
| 639 | + } |
| 640 | + } |
| 641 | + |
| 642 | + /// LSP: Trigger completion |
| 643 | + fn lsp_complete(&mut self) { |
| 644 | + if let Some(path) = self.current_file_path() { |
| 645 | + let path_str = path.to_string_lossy().to_string(); |
| 646 | + let line = self.cursor().line as u32; |
| 647 | + let col = self.cursor().col as u32; |
| 648 | + |
| 649 | + match self.workspace.lsp.request_completions(&path_str, line, col) { |
| 650 | + Ok(id) => { |
| 651 | + self.lsp_state.pending_completion = Some(id); |
| 652 | + self.message = Some("Loading completions...".to_string()); |
| 653 | + } |
| 654 | + Err(e) => { |
| 655 | + self.message = Some(format!("LSP error: {}", e)); |
| 656 | + } |
| 657 | + } |
| 658 | + } else { |
| 659 | + self.message = Some("No file open".to_string()); |
| 660 | + } |
| 661 | + } |
| 662 | + |
| 663 | + /// Toggle the LSP server manager panel |
| 664 | + fn toggle_server_manager(&mut self) { |
| 665 | + if self.server_manager.visible { |
| 666 | + self.server_manager.hide(); |
| 667 | + } else { |
| 668 | + self.server_manager.show(); |
| 669 | + } |
| 670 | + } |
| 671 | + |
| 672 | + /// Handle key input when server manager panel is visible |
| 673 | + fn handle_server_manager_key(&mut self, key: Key, mods: Modifiers) -> Result<()> { |
| 674 | + let max_visible = 10; // Should match screen.rs |
| 675 | + |
| 676 | + // Alt+M toggles the panel closed |
| 677 | + if key == Key::Char('m') && mods.alt { |
| 678 | + self.server_manager.hide(); |
| 679 | + return Ok(()); |
| 680 | + } |
| 681 | + |
| 682 | + // Handle confirm mode |
| 683 | + if self.server_manager.confirm_mode { |
| 684 | + match key { |
| 685 | + Key::Char('y') | Key::Char('Y') => { |
| 686 | + // Start install in background thread (non-blocking) |
| 687 | + self.server_manager.start_install(); |
| 688 | + } |
| 689 | + Key::Char('n') | Key::Char('N') | Key::Escape => { |
| 690 | + self.server_manager.cancel_confirm(); |
| 691 | + } |
| 692 | + _ => {} |
| 693 | + } |
| 694 | + return Ok(()); |
| 695 | + } |
| 696 | + |
| 697 | + // Handle manual info mode |
| 698 | + if self.server_manager.manual_info_mode { |
| 699 | + match key { |
| 700 | + Key::Char('c') | Key::Char('C') => { |
| 701 | + // Copy install instructions to clipboard |
| 702 | + if let Some(text) = self.server_manager.get_manual_install_text() { |
| 703 | + if let Some(ref mut clip) = self.clipboard { |
| 704 | + if clip.set_text(&text).is_ok() { |
| 705 | + self.server_manager.mark_copied(); |
| 706 | + } else { |
| 707 | + self.server_manager.status_message = Some("Failed to copy".to_string()); |
| 708 | + } |
| 709 | + } else { |
| 710 | + // Fall back to internal clipboard |
| 711 | + self.internal_clipboard = text; |
| 712 | + self.server_manager.mark_copied(); |
| 713 | + } |
| 714 | + } |
| 715 | + } |
| 716 | + Key::Escape | Key::Char('q') => { |
| 717 | + self.server_manager.cancel_confirm(); |
| 718 | + } |
| 719 | + _ => {} |
| 720 | + } |
| 721 | + return Ok(()); |
| 722 | + } |
| 723 | + |
| 724 | + // Normal panel navigation |
| 725 | + match key { |
| 726 | + Key::Up | Key::Char('k') => { |
| 727 | + self.server_manager.move_up(); |
| 728 | + } |
| 729 | + Key::Down | Key::Char('j') => { |
| 730 | + self.server_manager.move_down(max_visible); |
| 731 | + } |
| 732 | + Key::Enter => { |
| 733 | + self.server_manager.enter_confirm_mode(); |
| 734 | + } |
| 735 | + Key::Char('r') | Key::Char('R') => { |
| 736 | + self.server_manager.refresh(); |
| 737 | + } |
| 738 | + Key::Escape | Key::Char('q') => { |
| 739 | + self.server_manager.hide(); |
| 740 | + } |
| 741 | + _ => {} |
| 742 | + } |
| 743 | + |
| 744 | + Ok(()) |
| 745 | + } |
| 746 | + |
| 747 | + /// LSP: Rename symbol - opens prompt for new name |
| 748 | + fn lsp_rename(&mut self) { |
| 749 | + if let Some(path) = self.current_file_path() { |
| 750 | + let path_str = path.to_string_lossy().to_string(); |
| 751 | + let line = self.cursor().line as u32; |
| 752 | + let col = self.cursor().col as u32; |
| 753 | + |
| 754 | + // Get the word under cursor to show in prompt |
| 755 | + let buffer = self.buffer(); |
| 756 | + let cursor = self.cursor(); |
| 757 | + let current_word = if let Some(line_slice) = buffer.line(cursor.line) { |
| 758 | + let line_text: String = line_slice.chars().collect(); |
| 759 | + let mut start = cursor.col; |
| 760 | + let mut end = cursor.col; |
| 761 | + |
| 762 | + // Find word boundaries |
| 763 | + while start > 0 { |
| 764 | + let ch = line_text.chars().nth(start - 1).unwrap_or(' '); |
| 765 | + if ch.is_alphanumeric() || ch == '_' { |
| 766 | + start -= 1; |
| 767 | + } else { |
| 768 | + break; |
| 769 | + } |
| 770 | + } |
| 771 | + while end < line_text.len() { |
| 772 | + let ch = line_text.chars().nth(end).unwrap_or(' '); |
| 773 | + if ch.is_alphanumeric() || ch == '_' { |
| 774 | + end += 1; |
| 775 | + } else { |
| 776 | + break; |
| 777 | + } |
| 778 | + } |
| 779 | + line_text[start..end].to_string() |
| 780 | + } else { |
| 781 | + String::new() |
| 782 | + }; |
| 783 | + |
| 784 | + self.prompt = PromptState::TextInput { |
| 785 | + label: "Rename to: ".to_string(), |
| 786 | + buffer: current_word.clone(), |
| 787 | + action: TextInputAction::LspRename { path: path_str, line, col }, |
| 788 | + }; |
| 789 | + self.message = Some(format!("Rename '{}' to: {}", current_word, current_word)); |
| 790 | + } else { |
| 791 | + self.message = Some("No file open".to_string()); |
| 792 | + } |
| 793 | + } |
| 794 | + |
| 795 | + /// Accept the currently selected completion and insert it |
| 796 | + fn accept_completion(&mut self) { |
| 797 | + if self.lsp_state.completions.is_empty() { |
| 798 | + return; |
| 799 | + } |
| 800 | + |
| 801 | + let completion = self.lsp_state.completions[self.lsp_state.completion_index].clone(); |
| 802 | + |
| 803 | + // Determine the text to insert |
| 804 | + let insert_text = if let Some(ref text_edit) = completion.text_edit { |
| 805 | + // Use text edit if provided (includes range to replace) |
| 806 | + // For now, just use the new text - proper range replacement would be more complex |
| 807 | + text_edit.new_text.clone() |
| 808 | + } else if let Some(ref insert) = completion.insert_text { |
| 809 | + insert.clone() |
| 810 | + } else { |
| 811 | + completion.label.clone() |
| 812 | + }; |
| 813 | + |
| 814 | + // Find the start of the word being completed (walk back from cursor) |
| 815 | + let buffer = self.buffer(); |
| 816 | + let cursor = self.cursor(); |
| 817 | + let line_idx = cursor.line; |
| 818 | + let cursor_col = cursor.col; |
| 819 | + let mut word_start = cursor_col; |
| 820 | + |
| 821 | + // Walk back to find word start (alphanumeric or underscore) |
| 822 | + if let Some(line_slice) = buffer.line(line_idx) { |
| 823 | + let line_text: String = line_slice.chars().collect(); |
| 824 | + while word_start > 0 { |
| 825 | + let prev_char = line_text.chars().nth(word_start - 1).unwrap_or(' '); |
| 826 | + if prev_char.is_alphanumeric() || prev_char == '_' { |
| 827 | + word_start -= 1; |
| 828 | + } else { |
| 829 | + break; |
| 830 | + } |
| 831 | + } |
| 832 | + } |
| 833 | + |
| 834 | + // Delete the partial word and insert completion |
| 835 | + if word_start < cursor_col { |
| 836 | + // Select from word start to cursor |
| 837 | + let cursor = self.cursor_mut(); |
| 838 | + cursor.anchor_line = cursor.line; |
| 839 | + cursor.anchor_col = word_start; |
| 840 | + cursor.selecting = true; |
| 841 | + } |
| 842 | + |
| 843 | + // Insert the completion text (this will replace selection if any) |
| 844 | + for ch in insert_text.chars() { |
| 845 | + self.insert_char(ch); |
| 846 | + } |
| 847 | + |
| 848 | + // Clear completion state |
| 849 | + self.lsp_state.completion_visible = false; |
| 850 | + self.lsp_state.completions.clear(); |
| 851 | + self.lsp_state.completion_index = 0; |
| 852 | + } |
| 853 | + |
| 271 | 854 | /// Process a key event, handling ESC as potential Alt prefix |
| 272 | 855 | fn process_key(&mut self, key_event: KeyEvent) -> Result<()> { |
| 273 | 856 | use crossterm::event::KeyCode; |
@@ -404,7 +987,11 @@ impl Editor { |
| 404 | 987 | } |
| 405 | 988 | Mouse::ScrollDown { .. } => { |
| 406 | 989 | // Scroll down 3 lines |
| 407 | | - let max_viewport = self.buffer().line_count().saturating_sub(1); |
| 990 | + // Calculate visible rows (accounting for tab bar, gap, and status bar) |
| 991 | + let top_offset = if self.workspace.tabs.len() > 1 { 1 } else { 0 }; |
| 992 | + let visible_rows = (self.screen.rows as usize).saturating_sub(2 + top_offset); |
| 993 | + // Max viewport is when the last line is at the bottom of visible area |
| 994 | + let max_viewport = self.buffer().line_count().saturating_sub(visible_rows).max(0); |
| 408 | 995 | let new_line = (self.viewport_line() + 3).min(max_viewport); |
| 409 | 996 | self.set_viewport_line(new_line); |
| 410 | 997 | } |
@@ -492,29 +1079,93 @@ impl Editor { |
| 492 | 1079 | top_offset, |
| 493 | 1080 | ) |
| 494 | 1081 | } else { |
| 495 | | - // Single pane - use simpler render path |
| 1082 | + // Single pane - use simpler render path with syntax highlighting |
| 496 | 1083 | let pane = &tab.panes[tab.active_pane]; |
| 497 | 1084 | let buffer_entry = &tab.buffers[pane.buffer_idx]; |
| 498 | 1085 | let buffer = &buffer_entry.buffer; |
| 499 | 1086 | let cursors = &pane.cursors; |
| 500 | 1087 | let viewport_line = pane.viewport_line; |
| 1088 | + let viewport_col = pane.viewport_col; |
| 501 | 1089 | let is_modified = buffer_entry.is_modified(); |
| 1090 | + let highlighter = &buffer_entry.highlighter; |
| 502 | 1091 | |
| 503 | 1092 | // Find matching bracket for primary cursor |
| 504 | 1093 | let cursor = cursors.primary(); |
| 505 | 1094 | let bracket_match = buffer.find_matching_bracket(cursor.line, cursor.col); |
| 506 | 1095 | |
| 507 | | - self.screen.render_with_offset( |
| 1096 | + self.screen.render_with_syntax( |
| 508 | 1097 | buffer, |
| 509 | 1098 | cursors, |
| 510 | 1099 | viewport_line, |
| 1100 | + viewport_col, |
| 511 | 1101 | filename, |
| 512 | 1102 | self.message.as_deref(), |
| 513 | 1103 | bracket_match, |
| 514 | 1104 | fuss_width, |
| 515 | 1105 | top_offset, |
| 516 | 1106 | is_modified, |
| 517 | | - ) |
| 1107 | + highlighter, |
| 1108 | + )?; |
| 1109 | + |
| 1110 | + // Render diagnostics markers in gutter |
| 1111 | + if !self.lsp_state.diagnostics.is_empty() { |
| 1112 | + self.screen.render_diagnostics_gutter( |
| 1113 | + &self.lsp_state.diagnostics, |
| 1114 | + viewport_line, |
| 1115 | + fuss_width, |
| 1116 | + top_offset, |
| 1117 | + )?; |
| 1118 | + } |
| 1119 | + |
| 1120 | + // Render completion popup if visible |
| 1121 | + if self.lsp_state.completion_visible && !self.lsp_state.completions.is_empty() { |
| 1122 | + let cursor = cursors.primary(); |
| 1123 | + // Calculate cursor screen position |
| 1124 | + let cursor_row = (cursor.line.saturating_sub(viewport_line)) as u16 + top_offset; |
| 1125 | + let line_num_width = self.screen.line_number_width(buffer.line_count()) as u16; |
| 1126 | + let cursor_col = cursor.col as u16 + line_num_width + 1; |
| 1127 | + |
| 1128 | + self.screen.render_completion_popup( |
| 1129 | + &self.lsp_state.completions, |
| 1130 | + self.lsp_state.completion_index, |
| 1131 | + cursor_row, |
| 1132 | + cursor_col, |
| 1133 | + fuss_width, |
| 1134 | + )?; |
| 1135 | + } |
| 1136 | + |
| 1137 | + // Render hover popup if visible |
| 1138 | + if self.lsp_state.hover_visible { |
| 1139 | + if let Some(ref hover) = self.lsp_state.hover { |
| 1140 | + let cursor = cursors.primary(); |
| 1141 | + let cursor_row = (cursor.line.saturating_sub(viewport_line)) as u16 + top_offset; |
| 1142 | + let line_num_width = self.screen.line_number_width(buffer.line_count()) as u16; |
| 1143 | + let cursor_col = cursor.col as u16 + line_num_width + 1; |
| 1144 | + |
| 1145 | + self.screen.render_hover_popup( |
| 1146 | + hover, |
| 1147 | + cursor_row, |
| 1148 | + cursor_col, |
| 1149 | + fuss_width, |
| 1150 | + )?; |
| 1151 | + } |
| 1152 | + } |
| 1153 | + |
| 1154 | + // Render server manager panel if visible (on top of everything) |
| 1155 | + if self.server_manager.visible { |
| 1156 | + self.screen.render_server_manager_panel(&self.server_manager)?; |
| 1157 | + } |
| 1158 | + |
| 1159 | + // After all overlays are rendered, reposition cursor to the correct location |
| 1160 | + // (overlays may have moved the terminal cursor position) |
| 1161 | + let cursor = cursors.primary(); |
| 1162 | + let cursor_row = (cursor.line.saturating_sub(viewport_line)) as u16 + top_offset; |
| 1163 | + let line_num_width = self.screen.line_number_width(buffer.line_count()) as u16; |
| 1164 | + // Account for horizontal scroll offset |
| 1165 | + let cursor_screen_col = fuss_width + line_num_width + 1 + (cursor.col.saturating_sub(viewport_col)) as u16; |
| 1166 | + self.screen.show_cursor_at(cursor_screen_col, cursor_row)?; |
| 1167 | + |
| 1168 | + Ok(()) |
| 518 | 1169 | } |
| 519 | 1170 | } |
| 520 | 1171 | |
@@ -524,6 +1175,11 @@ impl Editor { |
| 524 | 1175 | return self.handle_prompt_key(key); |
| 525 | 1176 | } |
| 526 | 1177 | |
| 1178 | + // Handle server manager panel when visible |
| 1179 | + if self.server_manager.visible { |
| 1180 | + return self.handle_server_manager_key(key, mods); |
| 1181 | + } |
| 1182 | + |
| 527 | 1183 | // Clear message on any key |
| 528 | 1184 | self.message = None; |
| 529 | 1185 | |
@@ -538,6 +1194,58 @@ impl Editor { |
| 538 | 1194 | return self.handle_fuss_key(key, mods); |
| 539 | 1195 | } |
| 540 | 1196 | |
| 1197 | + // Handle completion popup navigation when visible |
| 1198 | + if self.lsp_state.completion_visible { |
| 1199 | + match (&key, &mods) { |
| 1200 | + // Navigate up in completion list |
| 1201 | + (Key::Up, _) => { |
| 1202 | + if self.lsp_state.completion_index > 0 { |
| 1203 | + self.lsp_state.completion_index -= 1; |
| 1204 | + } else { |
| 1205 | + // Wrap to bottom |
| 1206 | + self.lsp_state.completion_index = self.lsp_state.completions.len().saturating_sub(1); |
| 1207 | + } |
| 1208 | + return Ok(()); |
| 1209 | + } |
| 1210 | + // Navigate down in completion list |
| 1211 | + (Key::Down, _) => { |
| 1212 | + if self.lsp_state.completion_index < self.lsp_state.completions.len().saturating_sub(1) { |
| 1213 | + self.lsp_state.completion_index += 1; |
| 1214 | + } else { |
| 1215 | + // Wrap to top |
| 1216 | + self.lsp_state.completion_index = 0; |
| 1217 | + } |
| 1218 | + return Ok(()); |
| 1219 | + } |
| 1220 | + // Select completion with Enter or Tab |
| 1221 | + (Key::Enter, _) | (Key::Tab, _) => { |
| 1222 | + self.accept_completion(); |
| 1223 | + return Ok(()); |
| 1224 | + } |
| 1225 | + // Dismiss completion popup with Escape |
| 1226 | + (Key::Escape, _) => { |
| 1227 | + self.lsp_state.completion_visible = false; |
| 1228 | + self.lsp_state.completions.clear(); |
| 1229 | + return Ok(()); |
| 1230 | + } |
| 1231 | + // Any other key dismisses popup and continues normally |
| 1232 | + _ => { |
| 1233 | + self.lsp_state.completion_visible = false; |
| 1234 | + self.lsp_state.completions.clear(); |
| 1235 | + } |
| 1236 | + } |
| 1237 | + } |
| 1238 | + |
| 1239 | + // Dismiss hover popup on any key press |
| 1240 | + if self.lsp_state.hover_visible { |
| 1241 | + self.lsp_state.hover_visible = false; |
| 1242 | + self.lsp_state.hover = None; |
| 1243 | + // Let Escape just dismiss the popup without doing anything else |
| 1244 | + if matches!(key, Key::Escape) { |
| 1245 | + return Ok(()); |
| 1246 | + } |
| 1247 | + } |
| 1248 | + |
| 541 | 1249 | // Break undo group on any non-character key (movement, commands, etc.) |
| 542 | 1250 | // This ensures each "typing session" is its own undo unit |
| 543 | 1251 | let is_typing = matches!( |
@@ -717,6 +1425,20 @@ impl Editor { |
| 717 | 1425 | // New tab: Alt+T |
| 718 | 1426 | (Key::Char('t'), Modifiers { alt: true, .. }) => self.workspace.new_tab(), |
| 719 | 1427 | |
| 1428 | + // === LSP operations === |
| 1429 | + // Go to definition: F12 |
| 1430 | + (Key::F(12), Modifiers { shift: false, .. }) => self.lsp_goto_definition(), |
| 1431 | + // Find references: Shift+F12 |
| 1432 | + (Key::F(12), Modifiers { shift: true, .. }) => self.lsp_find_references(), |
| 1433 | + // Hover info: F1 |
| 1434 | + (Key::F(1), _) => self.lsp_hover(), |
| 1435 | + // Code completion: Ctrl+Space |
| 1436 | + (Key::Char(' '), Modifiers { ctrl: true, .. }) => self.lsp_complete(), |
| 1437 | + // Rename: F2 |
| 1438 | + (Key::F(2), _) => self.lsp_rename(), |
| 1439 | + // Server manager: Alt+M |
| 1440 | + (Key::Char('m'), Modifiers { alt: true, .. }) => self.toggle_server_manager(), |
| 1441 | + |
| 720 | 1442 | _ => {} |
| 721 | 1443 | } |
| 722 | 1444 | |
@@ -1024,8 +1746,13 @@ impl Editor { |
| 1024 | 1746 | } |
| 1025 | 1747 | |
| 1026 | 1748 | fn select_word(&mut self) { |
| 1027 | | - // If no selection, select word at cursor |
| 1028 | | - // If already have selection, this could expand to next occurrence (future enhancement) |
| 1749 | + // If primary cursor has a selection, find next occurrence and add cursor there |
| 1750 | + if self.cursor().has_selection() { |
| 1751 | + self.select_next_occurrence(); |
| 1752 | + return; |
| 1753 | + } |
| 1754 | + |
| 1755 | + // No selection - select word at cursor |
| 1029 | 1756 | if let Some(line_str) = self.buffer().line_str(self.cursor().line) { |
| 1030 | 1757 | let chars: Vec<char> = line_str.chars().collect(); |
| 1031 | 1758 | let col = self.cursor().col.min(chars.len()); |
@@ -1063,6 +1790,120 @@ impl Editor { |
| 1063 | 1790 | } |
| 1064 | 1791 | } |
| 1065 | 1792 | |
| 1793 | + /// Find the next occurrence of the selected text and add a cursor there |
| 1794 | + fn select_next_occurrence(&mut self) { |
| 1795 | + // Get the selected text from primary cursor |
| 1796 | + let selected_text = { |
| 1797 | + let cursor = self.cursor(); |
| 1798 | + if !cursor.has_selection() { |
| 1799 | + return; |
| 1800 | + } |
| 1801 | + let (start, end) = cursor.selection().ordered(); |
| 1802 | + let buffer = self.buffer(); |
| 1803 | + |
| 1804 | + // Extract selected text |
| 1805 | + let mut text = String::new(); |
| 1806 | + for line_idx in start.line..=end.line { |
| 1807 | + if let Some(line) = buffer.line_str(line_idx) { |
| 1808 | + let line_start = if line_idx == start.line { start.col } else { 0 }; |
| 1809 | + let line_end = if line_idx == end.line { end.col } else { line.len() }; |
| 1810 | + if line_start < line_end && line_end <= line.len() { |
| 1811 | + text.push_str(&line[line_start..line_end]); |
| 1812 | + } |
| 1813 | + if line_idx < end.line { |
| 1814 | + text.push('\n'); |
| 1815 | + } |
| 1816 | + } |
| 1817 | + } |
| 1818 | + text |
| 1819 | + }; |
| 1820 | + |
| 1821 | + if selected_text.is_empty() { |
| 1822 | + return; |
| 1823 | + } |
| 1824 | + |
| 1825 | + // Find the position to start searching from (after the last cursor with this selection) |
| 1826 | + let search_start = { |
| 1827 | + let cursors = self.cursors(); |
| 1828 | + let mut max_pos = (0usize, 0usize); |
| 1829 | + for cursor in cursors.all() { |
| 1830 | + if cursor.has_selection() { |
| 1831 | + let (_, end) = cursor.selection().ordered(); |
| 1832 | + if (end.line, end.col) > max_pos { |
| 1833 | + max_pos = (end.line, end.col); |
| 1834 | + } |
| 1835 | + } |
| 1836 | + } |
| 1837 | + max_pos |
| 1838 | + }; |
| 1839 | + |
| 1840 | + // Search for next occurrence |
| 1841 | + let buffer = self.buffer(); |
| 1842 | + let line_count = buffer.line_count(); |
| 1843 | + let search_text = &selected_text; |
| 1844 | + |
| 1845 | + // Start searching from the line after the last selection end |
| 1846 | + for line_idx in search_start.0..line_count { |
| 1847 | + if let Some(line) = buffer.line_str(line_idx) { |
| 1848 | + let start_col = if line_idx == search_start.0 { search_start.1 } else { 0 }; |
| 1849 | + |
| 1850 | + // Search for the text in this line (only works for single-line selections for now) |
| 1851 | + if !search_text.contains('\n') { |
| 1852 | + if let Some(found_col) = line[start_col..].find(search_text) { |
| 1853 | + let match_start = start_col + found_col; |
| 1854 | + let match_end = match_start + search_text.len(); |
| 1855 | + |
| 1856 | + // Add a new cursor with selection at this location |
| 1857 | + self.cursors_mut().add_with_selection( |
| 1858 | + line_idx, |
| 1859 | + match_end, |
| 1860 | + line_idx, |
| 1861 | + match_start, |
| 1862 | + ); |
| 1863 | + return; |
| 1864 | + } |
| 1865 | + } |
| 1866 | + } |
| 1867 | + } |
| 1868 | + |
| 1869 | + // Wrap around to beginning if not found |
| 1870 | + for line_idx in 0..=search_start.0 { |
| 1871 | + if let Some(line) = buffer.line_str(line_idx) { |
| 1872 | + let end_col = if line_idx == search_start.0 { |
| 1873 | + // Don't search past where we started |
| 1874 | + search_start.1.saturating_sub(search_text.len()) |
| 1875 | + } else { |
| 1876 | + line.len() |
| 1877 | + }; |
| 1878 | + |
| 1879 | + if !search_text.contains('\n') { |
| 1880 | + if let Some(found_col) = line[..end_col].find(search_text) { |
| 1881 | + let match_start = found_col; |
| 1882 | + let match_end = match_start + search_text.len(); |
| 1883 | + |
| 1884 | + // Check if this position already has a cursor |
| 1885 | + let already_has_cursor = self.cursors().all().iter().any(|c| { |
| 1886 | + c.line == line_idx && c.col == match_end |
| 1887 | + }); |
| 1888 | + |
| 1889 | + if !already_has_cursor { |
| 1890 | + self.cursors_mut().add_with_selection( |
| 1891 | + line_idx, |
| 1892 | + match_end, |
| 1893 | + line_idx, |
| 1894 | + match_start, |
| 1895 | + ); |
| 1896 | + return; |
| 1897 | + } |
| 1898 | + } |
| 1899 | + } |
| 1900 | + } |
| 1901 | + } |
| 1902 | + |
| 1903 | + // No more occurrences found |
| 1904 | + self.message = Some("No more occurrences".to_string()); |
| 1905 | + } |
| 1906 | + |
| 1066 | 1907 | // === Bracket/Quote Operations === |
| 1067 | 1908 | |
| 1068 | 1909 | fn jump_to_matching_bracket(&mut self) { |
@@ -2146,17 +2987,48 @@ impl Editor { |
| 2146 | 2987 | // === Viewport === |
| 2147 | 2988 | |
| 2148 | 2989 | fn scroll_to_cursor(&mut self) { |
| 2149 | | - let visible_rows = self.screen.rows.saturating_sub(1) as usize; |
| 2990 | + // Calculate top offset (tab bar takes 1 row if multiple tabs) |
| 2991 | + let top_offset = if self.workspace.tabs.len() > 1 { 1 } else { 0 }; |
| 2992 | + // Vertical scrolling (2 rows reserved: gap + status bar, plus top_offset for tab bar) |
| 2993 | + let visible_rows = (self.screen.rows as usize).saturating_sub(2 + top_offset); |
| 2150 | 2994 | let cursor_line = self.cursor().line; |
| 2151 | | - let viewport = self.viewport_line(); |
| 2995 | + let viewport_line = self.viewport_line(); |
| 2152 | 2996 | |
| 2153 | | - if cursor_line < viewport { |
| 2997 | + if cursor_line < viewport_line { |
| 2154 | 2998 | self.set_viewport_line(cursor_line); |
| 2155 | 2999 | } |
| 2156 | 3000 | |
| 2157 | | - if cursor_line >= viewport + visible_rows { |
| 3001 | + if cursor_line >= viewport_line + visible_rows { |
| 2158 | 3002 | self.set_viewport_line(cursor_line - visible_rows + 1); |
| 2159 | 3003 | } |
| 3004 | + |
| 3005 | + // Horizontal scrolling |
| 3006 | + let line_num_width = self.screen.line_number_width(self.buffer().line_count()); |
| 3007 | + let fuss_width = if self.workspace.fuss.active { |
| 3008 | + self.workspace.fuss.width(self.screen.cols) |
| 3009 | + } else { |
| 3010 | + 0 |
| 3011 | + }; |
| 3012 | + // Available text columns = screen width - fuss sidebar - line numbers - 1 (separator) |
| 3013 | + let visible_cols = (self.screen.cols as usize) |
| 3014 | + .saturating_sub(fuss_width as usize) |
| 3015 | + .saturating_sub(line_num_width + 1); |
| 3016 | + |
| 3017 | + let cursor_col = self.cursor().col; |
| 3018 | + let viewport_col = self.viewport_col(); |
| 3019 | + |
| 3020 | + // Keep some margin (3 chars) so cursor isn't right at the edge |
| 3021 | + let margin = 3; |
| 3022 | + |
| 3023 | + if cursor_col < viewport_col { |
| 3024 | + // Cursor is left of viewport - scroll left |
| 3025 | + self.set_viewport_col(cursor_col.saturating_sub(margin)); |
| 3026 | + } |
| 3027 | + |
| 3028 | + if cursor_col >= viewport_col + visible_cols.saturating_sub(margin) { |
| 3029 | + // Cursor is right of viewport - scroll right |
| 3030 | + self.set_viewport_col(cursor_col.saturating_sub(visible_cols.saturating_sub(margin + 1))); |
| 3031 | + } |
| 2160 | 3032 | } |
| 2161 | 3033 | |
| 2162 | 3034 | // === File operations === |
@@ -2546,6 +3418,22 @@ impl Editor { |
| 2546 | 3418 | let (_, msg) = self.workspace.fuss.git_tag(buffer); |
| 2547 | 3419 | self.message = Some(msg); |
| 2548 | 3420 | } |
| 3421 | + TextInputAction::LspRename { path, line, col } => { |
| 3422 | + if buffer.is_empty() { |
| 3423 | + self.message = Some("Rename cancelled: empty name".to_string()); |
| 3424 | + return; |
| 3425 | + } |
| 3426 | + match self.workspace.lsp.request_rename(&path, line, col, buffer) { |
| 3427 | + Ok(_id) => { |
| 3428 | + self.message = Some(format!("Renaming to '{}'...", buffer)); |
| 3429 | + // Note: The actual rename edits will be applied when we receive |
| 3430 | + // the response and implement WorkspaceEdit handling |
| 3431 | + } |
| 3432 | + Err(e) => { |
| 3433 | + self.message = Some(format!("Rename failed: {}", e)); |
| 3434 | + } |
| 3435 | + } |
| 3436 | + } |
| 2549 | 3437 | } |
| 2550 | 3438 | } |
| 2551 | 3439 | |