| 1 | //! Workspace state management |
| 2 | //! |
| 3 | //! The Workspace is the defining unit of fackr. Every editing session |
| 4 | //! operates within a workspace context. |
| 5 | |
| 6 | #![allow(dead_code)] |
| 7 | |
| 8 | use anyhow::Result; |
| 9 | use serde::{Deserialize, Serialize}; |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | |
| 12 | use crate::buffer::Buffer; |
| 13 | use crate::editor::{Cursor, Cursors, History}; |
| 14 | use crate::fuss::FussMode; |
| 15 | use crate::lsp::LspClient; |
| 16 | use crate::syntax::Highlighter; |
| 17 | |
| 18 | // ============================================================================ |
| 19 | // Serializable state structures for workspace persistence |
| 20 | // ============================================================================ |
| 21 | |
| 22 | /// Serializable workspace state for persistence |
| 23 | #[derive(Debug, Serialize, Deserialize)] |
| 24 | struct WorkspaceState { |
| 25 | active_tab: usize, |
| 26 | tabs: Vec<TabState>, |
| 27 | } |
| 28 | |
| 29 | /// Serializable tab state |
| 30 | #[derive(Debug, Serialize, Deserialize)] |
| 31 | struct TabState { |
| 32 | /// Files open in this tab (by path) |
| 33 | files: Vec<FileState>, |
| 34 | /// Active pane index |
| 35 | active_pane: usize, |
| 36 | /// Pane configurations |
| 37 | panes: Vec<PaneState>, |
| 38 | } |
| 39 | |
| 40 | /// Serializable file reference |
| 41 | #[derive(Debug, Serialize, Deserialize)] |
| 42 | struct FileState { |
| 43 | /// Path to file (None for unsaved) |
| 44 | path: Option<PathBuf>, |
| 45 | /// Whether file is outside workspace |
| 46 | is_orphan: bool, |
| 47 | } |
| 48 | |
| 49 | /// Serializable pane state |
| 50 | #[derive(Debug, Serialize, Deserialize)] |
| 51 | struct PaneState { |
| 52 | /// Index into tab's files |
| 53 | buffer_idx: usize, |
| 54 | /// Primary cursor position |
| 55 | cursor_line: usize, |
| 56 | cursor_col: usize, |
| 57 | /// Viewport scroll position |
| 58 | viewport_line: usize, |
| 59 | viewport_col: usize, |
| 60 | /// Pane bounds (normalized 0.0-1.0) |
| 61 | bounds: BoundsState, |
| 62 | } |
| 63 | |
| 64 | /// Serializable pane bounds |
| 65 | #[derive(Debug, Serialize, Deserialize)] |
| 66 | struct BoundsState { |
| 67 | x_start: f32, |
| 68 | y_start: f32, |
| 69 | x_end: f32, |
| 70 | y_end: f32, |
| 71 | } |
| 72 | |
| 73 | /// Normalized pane bounds (0.0 to 1.0) |
| 74 | /// Converted to screen coordinates at render time |
| 75 | #[derive(Debug, Clone)] |
| 76 | pub struct PaneBounds { |
| 77 | pub x_start: f32, |
| 78 | pub y_start: f32, |
| 79 | pub x_end: f32, |
| 80 | pub y_end: f32, |
| 81 | } |
| 82 | |
| 83 | impl Default for PaneBounds { |
| 84 | fn default() -> Self { |
| 85 | Self { |
| 86 | x_start: 0.0, |
| 87 | y_start: 0.0, |
| 88 | x_end: 1.0, |
| 89 | y_end: 1.0, |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /// A buffer entry in a tab (file content with its undo history) |
| 95 | #[derive(Debug)] |
| 96 | pub struct BufferEntry { |
| 97 | /// File path (relative to workspace for workspace files, absolute for orphans) |
| 98 | /// None means unsaved new file |
| 99 | pub path: Option<PathBuf>, |
| 100 | /// The text buffer |
| 101 | pub buffer: Buffer, |
| 102 | /// Undo/redo history for this buffer |
| 103 | pub history: History, |
| 104 | /// Syntax highlighter for this buffer |
| 105 | pub highlighter: Highlighter, |
| 106 | /// File is outside workspace directory |
| 107 | pub is_orphan: bool, |
| 108 | /// Hash of buffer content at last save (None for new unsaved buffers) |
| 109 | saved_hash: Option<u64>, |
| 110 | /// Length of buffer at last save (sentinel for quick modified check) |
| 111 | saved_len: Option<usize>, |
| 112 | /// Whether current modifications have been backed up (reset on save) |
| 113 | pub backed_up: bool, |
| 114 | } |
| 115 | |
| 116 | impl BufferEntry { |
| 117 | pub fn new() -> Self { |
| 118 | let mut buffer = Buffer::new(); |
| 119 | let saved_hash = Some(buffer.content_hash()); // Empty buffer is "saved" |
| 120 | let saved_len = Some(buffer.len_chars()); |
| 121 | Self { |
| 122 | path: None, |
| 123 | buffer, |
| 124 | history: History::new(), |
| 125 | highlighter: Highlighter::new(), |
| 126 | is_orphan: false, |
| 127 | saved_hash, |
| 128 | saved_len, |
| 129 | backed_up: false, // Will backup on first edit |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// Create a buffer from string content (for diff views, etc.) |
| 134 | /// The buffer is considered "saved" so it won't prompt for save on close |
| 135 | pub fn from_content(content: &str, display_name: Option<&str>) -> Self { |
| 136 | let mut buffer = Buffer::from_str(content); |
| 137 | let saved_hash = Some(buffer.content_hash()); |
| 138 | let saved_len = Some(buffer.len_chars()); |
| 139 | |
| 140 | // Detect language from display name for syntax highlighting |
| 141 | let mut highlighter = Highlighter::new(); |
| 142 | if let Some(name) = display_name { |
| 143 | highlighter.detect_language(name); |
| 144 | } |
| 145 | |
| 146 | Self { |
| 147 | path: display_name.map(PathBuf::from), |
| 148 | buffer, |
| 149 | history: History::new(), |
| 150 | highlighter, |
| 151 | is_orphan: true, // Mark as orphan so path isn't prefixed with workspace root |
| 152 | saved_hash, |
| 153 | saved_len, |
| 154 | backed_up: true, // Content buffers (like diffs) don't need backup |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// Create an empty buffer for a new file that doesn't exist yet |
| 159 | pub fn new_file(path: &Path, workspace_root: &Path) -> Self { |
| 160 | let buffer = Buffer::new(); |
| 161 | let is_orphan = !path.starts_with(workspace_root); |
| 162 | |
| 163 | // Store relative path for workspace files, absolute for orphans |
| 164 | let stored_path = if is_orphan { |
| 165 | path.to_path_buf() |
| 166 | } else { |
| 167 | path.strip_prefix(workspace_root) |
| 168 | .unwrap_or(path) |
| 169 | .to_path_buf() |
| 170 | }; |
| 171 | |
| 172 | // Detect language for syntax highlighting |
| 173 | let mut highlighter = Highlighter::new(); |
| 174 | if let Some(filename) = path.file_name().and_then(|n| n.to_str()) { |
| 175 | highlighter.detect_language(filename); |
| 176 | } |
| 177 | |
| 178 | Self { |
| 179 | path: Some(stored_path), |
| 180 | buffer, |
| 181 | history: History::new(), |
| 182 | highlighter, |
| 183 | is_orphan, |
| 184 | saved_hash: None, // Not saved yet - will prompt on close |
| 185 | saved_len: None, |
| 186 | backed_up: false, // Will backup on first edit |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | pub fn from_file(path: &Path, workspace_root: &Path) -> Result<Self> { |
| 191 | let mut buffer = Buffer::load(path)?; |
| 192 | let saved_hash = Some(buffer.content_hash()); // Hash at load time |
| 193 | let saved_len = Some(buffer.len_chars()); |
| 194 | let is_orphan = !path.starts_with(workspace_root); |
| 195 | |
| 196 | // Store relative path for workspace files, absolute for orphans |
| 197 | let stored_path = if is_orphan { |
| 198 | path.to_path_buf() |
| 199 | } else { |
| 200 | path.strip_prefix(workspace_root) |
| 201 | .unwrap_or(path) |
| 202 | .to_path_buf() |
| 203 | }; |
| 204 | |
| 205 | // Detect language for syntax highlighting |
| 206 | let mut highlighter = Highlighter::new(); |
| 207 | if let Some(filename) = path.file_name().and_then(|n| n.to_str()) { |
| 208 | highlighter.detect_language(filename); |
| 209 | } |
| 210 | |
| 211 | Ok(Self { |
| 212 | path: Some(stored_path), |
| 213 | buffer, |
| 214 | history: History::new(), |
| 215 | highlighter, |
| 216 | is_orphan, |
| 217 | saved_hash, |
| 218 | saved_len, |
| 219 | backed_up: false, // Will backup on first edit |
| 220 | }) |
| 221 | } |
| 222 | |
| 223 | /// Get the display name for the tab bar |
| 224 | pub fn display_name(&self) -> String { |
| 225 | match &self.path { |
| 226 | Some(p) => p.file_name() |
| 227 | .and_then(|n| n.to_str()) |
| 228 | .unwrap_or("[unknown]") |
| 229 | .to_string(), |
| 230 | None => "[new]".to_string(), |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /// Check if buffer has been modified since last save |
| 235 | pub fn is_modified(&mut self) -> bool { |
| 236 | match (self.saved_hash, self.saved_len) { |
| 237 | (Some(hash), Some(len)) => { |
| 238 | // Quick check: if length differs, definitely modified |
| 239 | if self.buffer.len_chars() != len { |
| 240 | return true; |
| 241 | } |
| 242 | // Length matches - need to check content hash (uses cache) |
| 243 | self.buffer.content_hash() != hash |
| 244 | }, |
| 245 | _ => true, // No saved state means never saved |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /// Mark the buffer as saved (updates hash and length for change detection) |
| 250 | pub fn mark_saved(&mut self) { |
| 251 | self.saved_hash = Some(self.buffer.content_hash()); |
| 252 | self.saved_len = Some(self.buffer.len_chars()); |
| 253 | self.backed_up = false; // Reset - will backup on next edit |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | impl Default for BufferEntry { |
| 258 | fn default() -> Self { |
| 259 | Self::new() |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /// A pane is a view into a buffer with its own cursor and viewport |
| 264 | #[derive(Debug)] |
| 265 | pub struct Pane { |
| 266 | /// Index into the tab's buffers vector |
| 267 | pub buffer_idx: usize, |
| 268 | /// Cursor positions within this pane |
| 269 | pub cursors: Cursors, |
| 270 | /// First visible line |
| 271 | pub viewport_line: usize, |
| 272 | /// First visible column (for horizontal scrolling) |
| 273 | pub viewport_col: usize, |
| 274 | /// Normalized bounds within the tab area |
| 275 | pub bounds: PaneBounds, |
| 276 | } |
| 277 | |
| 278 | impl Default for Pane { |
| 279 | fn default() -> Self { |
| 280 | Self { |
| 281 | buffer_idx: 0, |
| 282 | cursors: Cursors::new(), |
| 283 | viewport_line: 0, |
| 284 | viewport_col: 0, |
| 285 | bounds: PaneBounds::default(), |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | impl Pane { |
| 291 | pub fn new() -> Self { |
| 292 | Self::default() |
| 293 | } |
| 294 | |
| 295 | pub fn with_buffer_idx(buffer_idx: usize) -> Self { |
| 296 | Self { |
| 297 | buffer_idx, |
| 298 | ..Default::default() |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /// A tab represents a view group with one or more panes viewing buffers |
| 304 | #[derive(Debug)] |
| 305 | pub struct Tab { |
| 306 | /// All buffers open in this tab (panes reference these by index) |
| 307 | pub buffers: Vec<BufferEntry>, |
| 308 | /// Views into buffers |
| 309 | pub panes: Vec<Pane>, |
| 310 | /// Which pane is active (index into panes) |
| 311 | pub active_pane: usize, |
| 312 | } |
| 313 | |
| 314 | impl Tab { |
| 315 | /// Create a new empty tab |
| 316 | pub fn new() -> Self { |
| 317 | Self { |
| 318 | buffers: vec![BufferEntry::new()], |
| 319 | panes: vec![Pane::new()], |
| 320 | active_pane: 0, |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /// Create a tab from a file |
| 325 | pub fn from_file(path: &Path, workspace_root: &Path) -> Result<Self> { |
| 326 | let buffer_entry = BufferEntry::from_file(path, workspace_root)?; |
| 327 | Ok(Self { |
| 328 | buffers: vec![buffer_entry], |
| 329 | panes: vec![Pane::new()], |
| 330 | active_pane: 0, |
| 331 | }) |
| 332 | } |
| 333 | |
| 334 | /// Create a tab for a new file that doesn't exist yet |
| 335 | pub fn new_file(path: &Path, workspace_root: &Path) -> Self { |
| 336 | let buffer_entry = BufferEntry::new_file(path, workspace_root); |
| 337 | Self { |
| 338 | buffers: vec![buffer_entry], |
| 339 | panes: vec![Pane::new()], |
| 340 | active_pane: 0, |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /// Create a tab from string content (for diff views, etc.) |
| 345 | pub fn from_content(content: &str, display_name: &str) -> Self { |
| 346 | let buffer_entry = BufferEntry::from_content(content, Some(display_name)); |
| 347 | Self { |
| 348 | buffers: vec![buffer_entry], |
| 349 | panes: vec![Pane::new()], |
| 350 | active_pane: 0, |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /// Get the display name for the tab bar (uses primary buffer's name) |
| 355 | pub fn display_name(&self) -> String { |
| 356 | self.buffers.first() |
| 357 | .map(|b| b.display_name()) |
| 358 | .unwrap_or_else(|| "[new]".to_string()) |
| 359 | } |
| 360 | |
| 361 | /// Check if any buffer has been modified |
| 362 | pub fn is_modified(&mut self) -> bool { |
| 363 | self.buffers.iter_mut().any(|b| b.is_modified()) |
| 364 | } |
| 365 | |
| 366 | /// Get the active pane |
| 367 | pub fn active_pane(&self) -> &Pane { |
| 368 | &self.panes[self.active_pane] |
| 369 | } |
| 370 | |
| 371 | /// Get mutable reference to active pane |
| 372 | pub fn active_pane_mut(&mut self) -> &mut Pane { |
| 373 | &mut self.panes[self.active_pane] |
| 374 | } |
| 375 | |
| 376 | /// Get the buffer for the active pane |
| 377 | pub fn active_buffer(&self) -> &BufferEntry { |
| 378 | let buffer_idx = self.panes[self.active_pane].buffer_idx; |
| 379 | &self.buffers[buffer_idx] |
| 380 | } |
| 381 | |
| 382 | /// Get mutable reference to the buffer for the active pane |
| 383 | pub fn active_buffer_mut(&mut self) -> &mut BufferEntry { |
| 384 | let buffer_idx = self.panes[self.active_pane].buffer_idx; |
| 385 | &mut self.buffers[buffer_idx] |
| 386 | } |
| 387 | |
| 388 | /// Get the buffer for a specific pane |
| 389 | pub fn buffer_for_pane(&self, pane_idx: usize) -> &BufferEntry { |
| 390 | let buffer_idx = self.panes[pane_idx].buffer_idx; |
| 391 | &self.buffers[buffer_idx] |
| 392 | } |
| 393 | |
| 394 | /// Get mutable buffer for a specific pane |
| 395 | pub fn buffer_for_pane_mut(&mut self, pane_idx: usize) -> &mut BufferEntry { |
| 396 | let buffer_idx = self.panes[pane_idx].buffer_idx; |
| 397 | &mut self.buffers[buffer_idx] |
| 398 | } |
| 399 | |
| 400 | /// Split the active pane vertically (new pane to the right, same buffer) |
| 401 | pub fn split_vertical(&mut self) { |
| 402 | let active = &self.panes[self.active_pane]; |
| 403 | let buffer_idx = active.buffer_idx; |
| 404 | let old_bounds = active.bounds.clone(); |
| 405 | let mid_x = (old_bounds.x_start + old_bounds.x_end) / 2.0; |
| 406 | |
| 407 | // Shrink active pane |
| 408 | self.panes[self.active_pane].bounds.x_end = mid_x; |
| 409 | |
| 410 | // Create new pane to the right |
| 411 | let mut new_pane = Pane::with_buffer_idx(buffer_idx); |
| 412 | new_pane.bounds = PaneBounds { |
| 413 | x_start: mid_x, |
| 414 | y_start: old_bounds.y_start, |
| 415 | x_end: old_bounds.x_end, |
| 416 | y_end: old_bounds.y_end, |
| 417 | }; |
| 418 | |
| 419 | self.panes.push(new_pane); |
| 420 | self.active_pane = self.panes.len() - 1; |
| 421 | } |
| 422 | |
| 423 | /// Split the active pane horizontally (new pane below, same buffer) |
| 424 | pub fn split_horizontal(&mut self) { |
| 425 | let active = &self.panes[self.active_pane]; |
| 426 | let buffer_idx = active.buffer_idx; |
| 427 | let old_bounds = active.bounds.clone(); |
| 428 | let mid_y = (old_bounds.y_start + old_bounds.y_end) / 2.0; |
| 429 | |
| 430 | // Shrink active pane |
| 431 | self.panes[self.active_pane].bounds.y_end = mid_y; |
| 432 | |
| 433 | // Create new pane below |
| 434 | let mut new_pane = Pane::with_buffer_idx(buffer_idx); |
| 435 | new_pane.bounds = PaneBounds { |
| 436 | x_start: old_bounds.x_start, |
| 437 | y_start: mid_y, |
| 438 | x_end: old_bounds.x_end, |
| 439 | y_end: old_bounds.y_end, |
| 440 | }; |
| 441 | |
| 442 | self.panes.push(new_pane); |
| 443 | self.active_pane = self.panes.len() - 1; |
| 444 | } |
| 445 | |
| 446 | /// Split vertical with a new file in the new pane |
| 447 | pub fn split_vertical_with_file(&mut self, path: &Path, workspace_root: &Path) -> Result<()> { |
| 448 | let buffer_entry = BufferEntry::from_file(path, workspace_root)?; |
| 449 | let new_buffer_idx = self.buffers.len(); |
| 450 | self.buffers.push(buffer_entry); |
| 451 | |
| 452 | let active = &self.panes[self.active_pane]; |
| 453 | let old_bounds = active.bounds.clone(); |
| 454 | let mid_x = (old_bounds.x_start + old_bounds.x_end) / 2.0; |
| 455 | |
| 456 | // Shrink active pane |
| 457 | self.panes[self.active_pane].bounds.x_end = mid_x; |
| 458 | |
| 459 | // Create new pane to the right with the new buffer |
| 460 | let mut new_pane = Pane::with_buffer_idx(new_buffer_idx); |
| 461 | new_pane.bounds = PaneBounds { |
| 462 | x_start: mid_x, |
| 463 | y_start: old_bounds.y_start, |
| 464 | x_end: old_bounds.x_end, |
| 465 | y_end: old_bounds.y_end, |
| 466 | }; |
| 467 | |
| 468 | self.panes.push(new_pane); |
| 469 | self.active_pane = self.panes.len() - 1; |
| 470 | Ok(()) |
| 471 | } |
| 472 | |
| 473 | /// Split horizontal with a new file in the new pane |
| 474 | pub fn split_horizontal_with_file(&mut self, path: &Path, workspace_root: &Path) -> Result<()> { |
| 475 | let buffer_entry = BufferEntry::from_file(path, workspace_root)?; |
| 476 | let new_buffer_idx = self.buffers.len(); |
| 477 | self.buffers.push(buffer_entry); |
| 478 | |
| 479 | let active = &self.panes[self.active_pane]; |
| 480 | let old_bounds = active.bounds.clone(); |
| 481 | let mid_y = (old_bounds.y_start + old_bounds.y_end) / 2.0; |
| 482 | |
| 483 | // Shrink active pane |
| 484 | self.panes[self.active_pane].bounds.y_end = mid_y; |
| 485 | |
| 486 | // Create new pane below with the new buffer |
| 487 | let mut new_pane = Pane::with_buffer_idx(new_buffer_idx); |
| 488 | new_pane.bounds = PaneBounds { |
| 489 | x_start: old_bounds.x_start, |
| 490 | y_start: mid_y, |
| 491 | x_end: old_bounds.x_end, |
| 492 | y_end: old_bounds.y_end, |
| 493 | }; |
| 494 | |
| 495 | self.panes.push(new_pane); |
| 496 | self.active_pane = self.panes.len() - 1; |
| 497 | Ok(()) |
| 498 | } |
| 499 | |
| 500 | /// Close the active pane |
| 501 | /// Returns true if the tab should be closed (no panes left) |
| 502 | pub fn close_active_pane(&mut self) -> bool { |
| 503 | if self.panes.len() <= 1 { |
| 504 | return true; // Last pane - tab should close |
| 505 | } |
| 506 | |
| 507 | // Remove the pane |
| 508 | self.panes.remove(self.active_pane); |
| 509 | if self.active_pane >= self.panes.len() { |
| 510 | self.active_pane = self.panes.len() - 1; |
| 511 | } |
| 512 | |
| 513 | // Recalculate bounds - for now just expand remaining panes equally |
| 514 | // This is a simplified approach; a proper tiling system would be more complex |
| 515 | self.recalculate_pane_bounds(); |
| 516 | false |
| 517 | } |
| 518 | |
| 519 | /// Recalculate pane bounds after closing a pane |
| 520 | fn recalculate_pane_bounds(&mut self) { |
| 521 | // Simple approach: split screen equally among remaining panes |
| 522 | let n = self.panes.len(); |
| 523 | if n == 1 { |
| 524 | self.panes[0].bounds = PaneBounds::default(); |
| 525 | } else { |
| 526 | // Arrange panes horizontally for now |
| 527 | for (i, pane) in self.panes.iter_mut().enumerate() { |
| 528 | let width = 1.0 / n as f32; |
| 529 | pane.bounds = PaneBounds { |
| 530 | x_start: i as f32 * width, |
| 531 | y_start: 0.0, |
| 532 | x_end: (i + 1) as f32 * width, |
| 533 | y_end: 1.0, |
| 534 | }; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /// Navigate to the next pane |
| 540 | pub fn next_pane(&mut self) { |
| 541 | self.active_pane = (self.active_pane + 1) % self.panes.len(); |
| 542 | } |
| 543 | |
| 544 | /// Navigate to the previous pane |
| 545 | pub fn prev_pane(&mut self) { |
| 546 | if self.active_pane == 0 { |
| 547 | self.active_pane = self.panes.len() - 1; |
| 548 | } else { |
| 549 | self.active_pane -= 1; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /// Navigate to pane in direction (for vim-style navigation) |
| 554 | pub fn navigate_pane(&mut self, direction: PaneDirection) { |
| 555 | if self.panes.len() <= 1 { |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | let current = &self.panes[self.active_pane]; |
| 560 | let current_center_x = (current.bounds.x_start + current.bounds.x_end) / 2.0; |
| 561 | let current_center_y = (current.bounds.y_start + current.bounds.y_end) / 2.0; |
| 562 | |
| 563 | let mut best_idx = None; |
| 564 | let mut best_score = f32::MAX; |
| 565 | |
| 566 | for (i, pane) in self.panes.iter().enumerate() { |
| 567 | if i == self.active_pane { |
| 568 | continue; |
| 569 | } |
| 570 | |
| 571 | let center_x = (pane.bounds.x_start + pane.bounds.x_end) / 2.0; |
| 572 | let center_y = (pane.bounds.y_start + pane.bounds.y_end) / 2.0; |
| 573 | |
| 574 | let (is_valid, score) = match direction { |
| 575 | PaneDirection::Left => (center_x < current_center_x, current_center_x - center_x), |
| 576 | PaneDirection::Right => (center_x > current_center_x, center_x - current_center_x), |
| 577 | PaneDirection::Up => (center_y < current_center_y, current_center_y - center_y), |
| 578 | PaneDirection::Down => (center_y > current_center_y, center_y - current_center_y), |
| 579 | }; |
| 580 | |
| 581 | if is_valid && score < best_score { |
| 582 | best_score = score; |
| 583 | best_idx = Some(i); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if let Some(idx) = best_idx { |
| 588 | self.active_pane = idx; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /// Get number of panes |
| 593 | pub fn pane_count(&self) -> usize { |
| 594 | self.panes.len() |
| 595 | } |
| 596 | |
| 597 | /// Find which pane contains a screen coordinate |
| 598 | /// Returns the pane index, or active_pane if no match found |
| 599 | pub fn pane_at_screen_position(&self, col: u16, row: u16, screen_cols: u16, screen_rows: u16, left_offset: u16, top_offset: u16) -> usize { |
| 600 | // Available space for panes (excluding fuss width and tab bar) |
| 601 | let available_width = screen_cols.saturating_sub(left_offset) as f32; |
| 602 | let available_height = screen_rows.saturating_sub(2 + top_offset) as f32; // -2 for gap + status bar |
| 603 | |
| 604 | // Adjust click coordinates for offsets |
| 605 | let adj_col = col.saturating_sub(left_offset) as f32; |
| 606 | let adj_row = row.saturating_sub(top_offset) as f32; |
| 607 | |
| 608 | // Normalize coordinates to 0.0-1.0 range |
| 609 | let norm_x = adj_col / available_width; |
| 610 | let norm_y = adj_row / available_height; |
| 611 | |
| 612 | // Find pane containing this normalized position |
| 613 | for (i, pane) in self.panes.iter().enumerate() { |
| 614 | if norm_x >= pane.bounds.x_start && norm_x < pane.bounds.x_end |
| 615 | && norm_y >= pane.bounds.y_start && norm_y < pane.bounds.y_end |
| 616 | { |
| 617 | return i; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // Default to active pane if no match |
| 622 | self.active_pane |
| 623 | } |
| 624 | |
| 625 | /// Get the path of the primary buffer (for tab display and workspace tracking) |
| 626 | pub fn path(&self) -> Option<&PathBuf> { |
| 627 | self.buffers.first().and_then(|b| b.path.as_ref()) |
| 628 | } |
| 629 | |
| 630 | /// Check if the primary buffer is an orphan |
| 631 | pub fn is_orphan(&self) -> bool { |
| 632 | self.buffers.first().map(|b| b.is_orphan).unwrap_or(false) |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /// Direction for pane navigation |
| 637 | #[derive(Debug, Clone, Copy)] |
| 638 | pub enum PaneDirection { |
| 639 | Left, |
| 640 | Right, |
| 641 | Up, |
| 642 | Down, |
| 643 | } |
| 644 | |
| 645 | impl Default for Tab { |
| 646 | fn default() -> Self { |
| 647 | Self::new() |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /// Workspace configuration |
| 652 | #[derive(Debug, Clone)] |
| 653 | pub struct WorkspaceConfig { |
| 654 | /// Tab width in spaces |
| 655 | pub tab_width: usize, |
| 656 | /// Use spaces instead of tabs |
| 657 | pub use_spaces: bool, |
| 658 | // Add more config options as needed |
| 659 | } |
| 660 | |
| 661 | impl Default for WorkspaceConfig { |
| 662 | fn default() -> Self { |
| 663 | Self { |
| 664 | tab_width: 4, |
| 665 | use_spaces: true, |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /// The Workspace - defining unit of fackr |
| 671 | /// |
| 672 | /// Every editing session operates within a workspace context. |
| 673 | /// A workspace is tied to a directory and persists state in .fackr/ |
| 674 | pub struct Workspace { |
| 675 | /// Root directory of the workspace |
| 676 | pub root: PathBuf, |
| 677 | /// All open tabs |
| 678 | pub tabs: Vec<Tab>, |
| 679 | /// Currently active tab index |
| 680 | pub active_tab: usize, |
| 681 | /// Fuss mode (file tree) state |
| 682 | pub fuss: FussMode, |
| 683 | /// Workspace configuration |
| 684 | pub config: WorkspaceConfig, |
| 685 | /// LSP client for language server support |
| 686 | pub lsp: LspClient, |
| 687 | } |
| 688 | |
| 689 | impl Workspace { |
| 690 | /// Create a new workspace for a directory |
| 691 | pub fn new(root: PathBuf) -> Self { |
| 692 | let mut fuss = FussMode::new(); |
| 693 | fuss.init(&root); |
| 694 | let root_str = root.to_string_lossy().to_string(); |
| 695 | let lsp = LspClient::new(&root_str); |
| 696 | Self { |
| 697 | root, |
| 698 | tabs: vec![Tab::new()], |
| 699 | active_tab: 0, |
| 700 | fuss, |
| 701 | config: WorkspaceConfig::default(), |
| 702 | lsp, |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | /// Initialize workspace directory structure (.fackr/) |
| 707 | pub fn init(&self) -> Result<()> { |
| 708 | let fackr_dir = self.root.join(".fackr"); |
| 709 | if !fackr_dir.exists() { |
| 710 | std::fs::create_dir_all(&fackr_dir)?; |
| 711 | std::fs::create_dir_all(fackr_dir.join("backups"))?; |
| 712 | } |
| 713 | Ok(()) |
| 714 | } |
| 715 | |
| 716 | /// Check if a directory has an existing workspace |
| 717 | pub fn exists(dir: &Path) -> bool { |
| 718 | dir.join(".fackr").join("workspace.json").exists() |
| 719 | } |
| 720 | |
| 721 | /// Detect workspace from a file path (searches parent directories) |
| 722 | pub fn detect_from_file(file_path: &Path) -> Option<PathBuf> { |
| 723 | let mut current = file_path.parent()?; |
| 724 | loop { |
| 725 | if Self::exists(current) { |
| 726 | return Some(current.to_path_buf()); |
| 727 | } |
| 728 | match current.parent() { |
| 729 | Some(parent) => current = parent, |
| 730 | None => return None, |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /// Open a workspace, creating .fackr/ if needed |
| 736 | pub fn open(root: PathBuf) -> Result<Self> { |
| 737 | let mut workspace = Self::new(root); |
| 738 | workspace.init()?; |
| 739 | |
| 740 | // Try to load existing state |
| 741 | if let Err(_e) = workspace.load() { |
| 742 | // No existing state or failed to load - start fresh |
| 743 | // (workspace already has default empty tab) |
| 744 | } |
| 745 | |
| 746 | Ok(workspace) |
| 747 | } |
| 748 | |
| 749 | /// Open a workspace with a specific file |
| 750 | pub fn open_with_file(file_path: &Path) -> Result<Self> { |
| 751 | // Canonicalize the path to handle relative paths |
| 752 | let abs_path = file_path.canonicalize() |
| 753 | .unwrap_or_else(|_| file_path.to_path_buf()); |
| 754 | |
| 755 | // If path is a directory, use it as the workspace root directly |
| 756 | if abs_path.is_dir() { |
| 757 | return Self::open(abs_path); |
| 758 | } |
| 759 | |
| 760 | // Determine workspace root |
| 761 | let root = Self::detect_from_file(&abs_path) |
| 762 | .or_else(|| abs_path.parent().map(|p| p.to_path_buf())) |
| 763 | .unwrap_or_else(|| std::env::current_dir().unwrap_or_default()); |
| 764 | |
| 765 | let mut workspace = Self::open(root)?; |
| 766 | |
| 767 | // Open the file in a tab (or create new file if it doesn't exist) |
| 768 | if abs_path.exists() { |
| 769 | workspace.open_file(&abs_path)?; |
| 770 | } else if abs_path.extension().is_some() || abs_path.file_name().is_some() { |
| 771 | // Path looks like a file (has extension or filename) - create new file buffer |
| 772 | workspace.open_new_file(&abs_path)?; |
| 773 | } |
| 774 | |
| 775 | Ok(workspace) |
| 776 | } |
| 777 | |
| 778 | /// Load workspace state from .fackr/workspace.json |
| 779 | pub fn load(&mut self) -> Result<()> { |
| 780 | let state_path = self.root.join(".fackr").join("workspace.json"); |
| 781 | if !state_path.exists() { |
| 782 | return Ok(()); |
| 783 | } |
| 784 | |
| 785 | // Read and parse JSON |
| 786 | let json = std::fs::read_to_string(&state_path)?; |
| 787 | let state: WorkspaceState = match serde_json::from_str(&json) { |
| 788 | Ok(s) => s, |
| 789 | Err(e) => { |
| 790 | // If JSON is corrupted, log and continue with empty workspace |
| 791 | eprintln!("Warning: Failed to parse workspace.json: {}", e); |
| 792 | return Ok(()); |
| 793 | } |
| 794 | }; |
| 795 | |
| 796 | // Restore tabs from state |
| 797 | let mut restored_tabs = Vec::new(); |
| 798 | for tab_state in state.tabs { |
| 799 | // Try to open each file in the tab |
| 800 | let mut buffers = Vec::new(); |
| 801 | let mut valid_buffer_map: Vec<Option<usize>> = Vec::new(); // Maps old index to new index |
| 802 | |
| 803 | for file_state in &tab_state.files { |
| 804 | if let Some(ref path) = file_state.path { |
| 805 | // Resolve path (relative or absolute based on orphan status) |
| 806 | let full_path = if file_state.is_orphan { |
| 807 | path.clone() |
| 808 | } else { |
| 809 | self.root.join(path) |
| 810 | }; |
| 811 | |
| 812 | // Only restore if file still exists and is not a directory |
| 813 | if full_path.exists() && !full_path.is_dir() { |
| 814 | match BufferEntry::from_file(&full_path, &self.root) { |
| 815 | Ok(entry) => { |
| 816 | valid_buffer_map.push(Some(buffers.len())); |
| 817 | buffers.push(entry); |
| 818 | } |
| 819 | Err(_) => { |
| 820 | valid_buffer_map.push(None); |
| 821 | } |
| 822 | } |
| 823 | } else { |
| 824 | valid_buffer_map.push(None); |
| 825 | } |
| 826 | } else { |
| 827 | // Unsaved file - skip it (can't restore without content) |
| 828 | valid_buffer_map.push(None); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // Skip tab if no files could be restored |
| 833 | if buffers.is_empty() { |
| 834 | continue; |
| 835 | } |
| 836 | |
| 837 | // Restore panes, mapping buffer indices |
| 838 | let mut panes = Vec::new(); |
| 839 | for pane_state in &tab_state.panes { |
| 840 | // Check if this pane's buffer was successfully loaded |
| 841 | if let Some(Some(new_idx)) = valid_buffer_map.get(pane_state.buffer_idx) { |
| 842 | let mut pane = Pane::with_buffer_idx(*new_idx); |
| 843 | |
| 844 | // Restore cursor position (clamped to buffer bounds) |
| 845 | let buffer = &buffers[*new_idx].buffer; |
| 846 | let line = pane_state.cursor_line.min(buffer.line_count().saturating_sub(1)); |
| 847 | let col = if line < buffer.line_count() { |
| 848 | pane_state.cursor_col.min(buffer.line_len(line)) |
| 849 | } else { |
| 850 | 0 |
| 851 | }; |
| 852 | pane.cursors = Cursors::from_cursor(Cursor { |
| 853 | line, |
| 854 | col, |
| 855 | desired_col: col, |
| 856 | anchor_line: line, |
| 857 | anchor_col: col, |
| 858 | selecting: false, |
| 859 | }); |
| 860 | |
| 861 | // Restore viewport |
| 862 | pane.viewport_line = pane_state.viewport_line.min(buffer.line_count().saturating_sub(1)); |
| 863 | pane.viewport_col = pane_state.viewport_col; |
| 864 | |
| 865 | // Restore bounds |
| 866 | pane.bounds = PaneBounds { |
| 867 | x_start: pane_state.bounds.x_start, |
| 868 | y_start: pane_state.bounds.y_start, |
| 869 | x_end: pane_state.bounds.x_end, |
| 870 | y_end: pane_state.bounds.y_end, |
| 871 | }; |
| 872 | |
| 873 | panes.push(pane); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | // Ensure at least one pane exists |
| 878 | if panes.is_empty() { |
| 879 | panes.push(Pane::default()); |
| 880 | } |
| 881 | |
| 882 | // Clamp active_pane to valid range |
| 883 | let active_pane = tab_state.active_pane.min(panes.len().saturating_sub(1)); |
| 884 | |
| 885 | restored_tabs.push(Tab { |
| 886 | buffers, |
| 887 | panes, |
| 888 | active_pane, |
| 889 | }); |
| 890 | } |
| 891 | |
| 892 | // Only replace tabs if we successfully restored at least one |
| 893 | if !restored_tabs.is_empty() { |
| 894 | self.tabs = restored_tabs; |
| 895 | self.active_tab = state.active_tab.min(self.tabs.len().saturating_sub(1)); |
| 896 | } |
| 897 | |
| 898 | Ok(()) |
| 899 | } |
| 900 | |
| 901 | /// Save workspace state to .fackr/workspace.json |
| 902 | pub fn save(&self) -> Result<()> { |
| 903 | self.init()?; // Ensure .fackr/ exists |
| 904 | |
| 905 | let state_path = self.root.join(".fackr").join("workspace.json"); |
| 906 | |
| 907 | // Build serializable state |
| 908 | let mut tabs = Vec::new(); |
| 909 | for tab in &self.tabs { |
| 910 | // Collect file states |
| 911 | let files: Vec<FileState> = tab.buffers.iter().map(|b| { |
| 912 | FileState { |
| 913 | path: b.path.clone(), |
| 914 | is_orphan: b.is_orphan, |
| 915 | } |
| 916 | }).collect(); |
| 917 | |
| 918 | // Only save tabs that have at least one saved file |
| 919 | if files.iter().all(|f| f.path.is_none()) { |
| 920 | continue; |
| 921 | } |
| 922 | |
| 923 | // Collect pane states |
| 924 | let panes: Vec<PaneState> = tab.panes.iter().map(|p| { |
| 925 | let cursor = p.cursors.primary(); |
| 926 | PaneState { |
| 927 | buffer_idx: p.buffer_idx, |
| 928 | cursor_line: cursor.line, |
| 929 | cursor_col: cursor.col, |
| 930 | viewport_line: p.viewport_line, |
| 931 | viewport_col: p.viewport_col, |
| 932 | bounds: BoundsState { |
| 933 | x_start: p.bounds.x_start, |
| 934 | y_start: p.bounds.y_start, |
| 935 | x_end: p.bounds.x_end, |
| 936 | y_end: p.bounds.y_end, |
| 937 | }, |
| 938 | } |
| 939 | }).collect(); |
| 940 | |
| 941 | tabs.push(TabState { |
| 942 | files, |
| 943 | active_pane: tab.active_pane, |
| 944 | panes, |
| 945 | }); |
| 946 | } |
| 947 | |
| 948 | // Don't save if there's nothing meaningful to save |
| 949 | if tabs.is_empty() { |
| 950 | // Remove old state file if it exists |
| 951 | if state_path.exists() { |
| 952 | let _ = std::fs::remove_file(&state_path); |
| 953 | } |
| 954 | return Ok(()); |
| 955 | } |
| 956 | |
| 957 | let state = WorkspaceState { |
| 958 | active_tab: self.active_tab.min(tabs.len().saturating_sub(1)), |
| 959 | tabs, |
| 960 | }; |
| 961 | |
| 962 | // Serialize and write |
| 963 | let json = serde_json::to_string_pretty(&state)?; |
| 964 | std::fs::write(&state_path, json)?; |
| 965 | |
| 966 | Ok(()) |
| 967 | } |
| 968 | |
| 969 | /// Get the active tab |
| 970 | pub fn active_tab(&self) -> &Tab { |
| 971 | &self.tabs[self.active_tab] |
| 972 | } |
| 973 | |
| 974 | /// Get mutable reference to active tab |
| 975 | pub fn active_tab_mut(&mut self) -> &mut Tab { |
| 976 | &mut self.tabs[self.active_tab] |
| 977 | } |
| 978 | |
| 979 | /// Check if a tab is an empty default tab (no path, not modified, empty content) |
| 980 | fn is_empty_default_tab(tab: &mut Tab) -> bool { |
| 981 | if tab.buffers.len() != 1 || tab.panes.len() != 1 { |
| 982 | return false; |
| 983 | } |
| 984 | let buf = &mut tab.buffers[0]; |
| 985 | buf.path.is_none() && !buf.is_modified() && buf.buffer.len_chars() == 0 |
| 986 | } |
| 987 | |
| 988 | /// Open a file in a new tab |
| 989 | pub fn open_file(&mut self, path: &Path) -> Result<()> { |
| 990 | // Check if file is already open in any tab's primary buffer |
| 991 | let abs_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf()); |
| 992 | for (i, tab) in self.tabs.iter().enumerate() { |
| 993 | if let Some(tab_path) = tab.path() { |
| 994 | let full_path = if tab.is_orphan() { |
| 995 | tab_path.clone() |
| 996 | } else { |
| 997 | self.root.join(tab_path) |
| 998 | }; |
| 999 | if full_path.canonicalize().ok() == Some(abs_path.clone()) { |
| 1000 | // File already open - switch to it |
| 1001 | self.active_tab = i; |
| 1002 | return Ok(()); |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | // Open new tab |
| 1008 | let tab = Tab::from_file(path, &self.root)?; |
| 1009 | |
| 1010 | // Notify LSP server of newly opened file |
| 1011 | if let Some(file_path) = tab.path() { |
| 1012 | let full_path = if tab.is_orphan() { |
| 1013 | file_path.clone() |
| 1014 | } else { |
| 1015 | self.root.join(file_path) |
| 1016 | }; |
| 1017 | let path_str = full_path.to_string_lossy(); |
| 1018 | let content = tab.buffers[0].buffer.contents(); |
| 1019 | let _ = self.lsp.open_document(&path_str, &content); |
| 1020 | } |
| 1021 | |
| 1022 | // If we have exactly one empty default tab, replace it instead of adding |
| 1023 | if self.tabs.len() == 1 && Self::is_empty_default_tab(&mut self.tabs[0]) { |
| 1024 | self.tabs[0] = tab; |
| 1025 | self.active_tab = 0; |
| 1026 | } else { |
| 1027 | self.tabs.push(tab); |
| 1028 | self.active_tab = self.tabs.len() - 1; |
| 1029 | } |
| 1030 | Ok(()) |
| 1031 | } |
| 1032 | |
| 1033 | /// Open a new file (doesn't exist yet) in a new tab |
| 1034 | pub fn open_new_file(&mut self, path: &Path) -> Result<()> { |
| 1035 | let tab = Tab::new_file(path, &self.root); |
| 1036 | |
| 1037 | // If we have exactly one empty default tab, replace it instead of adding |
| 1038 | if self.tabs.len() == 1 && Self::is_empty_default_tab(&mut self.tabs[0]) { |
| 1039 | self.tabs[0] = tab; |
| 1040 | self.active_tab = 0; |
| 1041 | } else { |
| 1042 | self.tabs.push(tab); |
| 1043 | self.active_tab = self.tabs.len() - 1; |
| 1044 | } |
| 1045 | Ok(()) |
| 1046 | } |
| 1047 | |
| 1048 | /// Open a file in a vertical split pane in the current tab |
| 1049 | pub fn open_file_in_vsplit(&mut self, path: &Path) -> Result<()> { |
| 1050 | self.tabs[self.active_tab].split_vertical_with_file(path, &self.root) |
| 1051 | } |
| 1052 | |
| 1053 | /// Open a file in a horizontal split pane in the current tab |
| 1054 | pub fn open_file_in_hsplit(&mut self, path: &Path) -> Result<()> { |
| 1055 | self.tabs[self.active_tab].split_horizontal_with_file(path, &self.root) |
| 1056 | } |
| 1057 | |
| 1058 | /// Create a new empty tab |
| 1059 | pub fn new_tab(&mut self) { |
| 1060 | self.tabs.push(Tab::new()); |
| 1061 | self.active_tab = self.tabs.len() - 1; |
| 1062 | } |
| 1063 | |
| 1064 | /// Open a content tab (for diff views, etc.) |
| 1065 | pub fn open_content_tab(&mut self, content: &str, display_name: &str) { |
| 1066 | let tab = Tab::from_content(content, display_name); |
| 1067 | self.tabs.push(tab); |
| 1068 | self.active_tab = self.tabs.len() - 1; |
| 1069 | } |
| 1070 | |
| 1071 | /// Close the active tab |
| 1072 | /// Returns true if the workspace should close (no tabs left) |
| 1073 | pub fn close_active_tab(&mut self) -> bool { |
| 1074 | if self.tabs.len() <= 1 { |
| 1075 | return true; // Last tab - workspace should close |
| 1076 | } |
| 1077 | |
| 1078 | self.tabs.remove(self.active_tab); |
| 1079 | if self.active_tab >= self.tabs.len() { |
| 1080 | self.active_tab = self.tabs.len() - 1; |
| 1081 | } |
| 1082 | false |
| 1083 | } |
| 1084 | |
| 1085 | /// Switch to tab by index (0-based) |
| 1086 | pub fn switch_to_tab(&mut self, index: usize) { |
| 1087 | if index < self.tabs.len() { |
| 1088 | self.active_tab = index; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /// Switch to next tab (wraps around) |
| 1093 | pub fn next_tab(&mut self) { |
| 1094 | self.active_tab = (self.active_tab + 1) % self.tabs.len(); |
| 1095 | } |
| 1096 | |
| 1097 | /// Switch to previous tab (wraps around) |
| 1098 | pub fn prev_tab(&mut self) { |
| 1099 | if self.active_tab == 0 { |
| 1100 | self.active_tab = self.tabs.len() - 1; |
| 1101 | } else { |
| 1102 | self.active_tab -= 1; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /// Get number of tabs |
| 1107 | pub fn tab_count(&self) -> usize { |
| 1108 | self.tabs.len() |
| 1109 | } |
| 1110 | |
| 1111 | // === Backup functionality === |
| 1112 | |
| 1113 | /// Get the backups directory path |
| 1114 | fn backups_dir(&self) -> PathBuf { |
| 1115 | self.root.join(".fackr").join("backups") |
| 1116 | } |
| 1117 | |
| 1118 | /// Generate a backup filename for a buffer path |
| 1119 | /// Uses a hash of the path to create a unique but deterministic name |
| 1120 | fn backup_filename(&self, path: &Path) -> String { |
| 1121 | use std::collections::hash_map::DefaultHasher; |
| 1122 | use std::hash::{Hash, Hasher}; |
| 1123 | |
| 1124 | let mut hasher = DefaultHasher::new(); |
| 1125 | path.hash(&mut hasher); |
| 1126 | format!("{:016x}.bak", hasher.finish()) |
| 1127 | } |
| 1128 | |
| 1129 | /// Write a backup for a modified buffer |
| 1130 | pub fn write_backup(&self, path: &Path, content: &str) -> Result<()> { |
| 1131 | let backups_dir = self.backups_dir(); |
| 1132 | std::fs::create_dir_all(&backups_dir)?; |
| 1133 | |
| 1134 | let backup_path = backups_dir.join(self.backup_filename(path)); |
| 1135 | |
| 1136 | // Store as simple format: first line is original path, rest is content |
| 1137 | let backup_content = format!("{}\n{}", path.display(), content); |
| 1138 | std::fs::write(&backup_path, backup_content)?; |
| 1139 | |
| 1140 | Ok(()) |
| 1141 | } |
| 1142 | |
| 1143 | /// Delete backup for a buffer (called after successful save) |
| 1144 | pub fn delete_backup(&self, path: &Path) -> Result<()> { |
| 1145 | let backup_path = self.backups_dir().join(self.backup_filename(path)); |
| 1146 | if backup_path.exists() { |
| 1147 | std::fs::remove_file(backup_path)?; |
| 1148 | } |
| 1149 | Ok(()) |
| 1150 | } |
| 1151 | |
| 1152 | /// Delete all backups (called on discard) |
| 1153 | pub fn delete_all_backups(&self) -> Result<()> { |
| 1154 | let backups_dir = self.backups_dir(); |
| 1155 | if backups_dir.exists() { |
| 1156 | for entry in std::fs::read_dir(&backups_dir)? { |
| 1157 | let entry = entry?; |
| 1158 | if entry.path().extension().map_or(false, |e| e == "bak") { |
| 1159 | std::fs::remove_file(entry.path())?; |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | Ok(()) |
| 1164 | } |
| 1165 | |
| 1166 | /// Check if there are any backups to restore |
| 1167 | pub fn has_backups(&self) -> bool { |
| 1168 | let backups_dir = self.backups_dir(); |
| 1169 | if !backups_dir.exists() { |
| 1170 | return false; |
| 1171 | } |
| 1172 | if let Ok(entries) = std::fs::read_dir(&backups_dir) { |
| 1173 | for entry in entries.flatten() { |
| 1174 | if entry.path().extension().map_or(false, |e| e == "bak") { |
| 1175 | return true; |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | false |
| 1180 | } |
| 1181 | |
| 1182 | /// Get list of backup info (original path, backup path) |
| 1183 | pub fn list_backups(&self) -> Vec<(PathBuf, PathBuf)> { |
| 1184 | let mut backups = Vec::new(); |
| 1185 | let backups_dir = self.backups_dir(); |
| 1186 | |
| 1187 | if !backups_dir.exists() { |
| 1188 | return backups; |
| 1189 | } |
| 1190 | |
| 1191 | if let Ok(entries) = std::fs::read_dir(&backups_dir) { |
| 1192 | for entry in entries.flatten() { |
| 1193 | let backup_path = entry.path(); |
| 1194 | if backup_path.extension().map_or(false, |e| e == "bak") { |
| 1195 | // Read first line to get original path |
| 1196 | if let Ok(content) = std::fs::read_to_string(&backup_path) { |
| 1197 | if let Some(first_line) = content.lines().next() { |
| 1198 | backups.push((PathBuf::from(first_line), backup_path)); |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | backups |
| 1206 | } |
| 1207 | |
| 1208 | /// Restore a backup into its buffer |
| 1209 | /// Returns the original path and content |
| 1210 | pub fn read_backup(&self, backup_path: &Path) -> Result<(PathBuf, String)> { |
| 1211 | let content = std::fs::read_to_string(backup_path)?; |
| 1212 | let mut lines = content.lines(); |
| 1213 | |
| 1214 | let original_path = lines.next() |
| 1215 | .ok_or_else(|| anyhow::anyhow!("Invalid backup file: missing path"))?; |
| 1216 | |
| 1217 | let content: String = lines.collect::<Vec<_>>().join("\n"); |
| 1218 | |
| 1219 | Ok((PathBuf::from(original_path), content)) |
| 1220 | } |
| 1221 | |
| 1222 | /// Check if any buffer in the workspace has unsaved changes |
| 1223 | pub fn has_unsaved_changes(&mut self) -> bool { |
| 1224 | for tab in &mut self.tabs { |
| 1225 | for buffer_entry in &mut tab.buffers { |
| 1226 | if buffer_entry.is_modified() { |
| 1227 | return true; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | false |
| 1232 | } |
| 1233 | |
| 1234 | /// Get list of modified buffer paths |
| 1235 | pub fn modified_buffers(&mut self) -> Vec<PathBuf> { |
| 1236 | let mut modified = Vec::new(); |
| 1237 | for tab in &mut self.tabs { |
| 1238 | for buffer_entry in &mut tab.buffers { |
| 1239 | if buffer_entry.is_modified() { |
| 1240 | if let Some(path) = &buffer_entry.path { |
| 1241 | // Convert relative path to absolute |
| 1242 | let full_path = if buffer_entry.is_orphan { |
| 1243 | path.clone() |
| 1244 | } else { |
| 1245 | self.root.join(path) |
| 1246 | }; |
| 1247 | modified.push(full_path); |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | modified |
| 1253 | } |
| 1254 | |
| 1255 | /// Save all modified buffers |
| 1256 | pub fn save_all(&mut self) -> Result<()> { |
| 1257 | // Collect paths to save first to avoid borrow issues |
| 1258 | let mut to_save: Vec<(usize, usize, PathBuf)> = Vec::new(); |
| 1259 | |
| 1260 | for (tab_idx, tab) in self.tabs.iter_mut().enumerate() { |
| 1261 | for (buf_idx, buffer_entry) in tab.buffers.iter_mut().enumerate() { |
| 1262 | if buffer_entry.is_modified() { |
| 1263 | if let Some(path) = &buffer_entry.path { |
| 1264 | let full_path = if buffer_entry.is_orphan { |
| 1265 | path.clone() |
| 1266 | } else { |
| 1267 | self.root.join(path) |
| 1268 | }; |
| 1269 | to_save.push((tab_idx, buf_idx, full_path)); |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | // Now save each buffer |
| 1276 | for (tab_idx, buf_idx, full_path) in to_save { |
| 1277 | self.tabs[tab_idx].buffers[buf_idx].buffer.save(&full_path)?; |
| 1278 | self.tabs[tab_idx].buffers[buf_idx].mark_saved(); |
| 1279 | // Delete backup after successful save |
| 1280 | let _ = self.delete_backup(&full_path); |
| 1281 | } |
| 1282 | |
| 1283 | Ok(()) |
| 1284 | } |
| 1285 | |
| 1286 | /// Write backups for all modified buffers |
| 1287 | pub fn backup_all_modified(&mut self) -> Result<()> { |
| 1288 | // Collect backup info first to avoid borrow issues |
| 1289 | let mut to_backup: Vec<(PathBuf, String)> = Vec::new(); |
| 1290 | |
| 1291 | for tab in &mut self.tabs { |
| 1292 | for buffer_entry in &mut tab.buffers { |
| 1293 | if buffer_entry.is_modified() { |
| 1294 | if let Some(path) = &buffer_entry.path { |
| 1295 | let full_path = if buffer_entry.is_orphan { |
| 1296 | path.clone() |
| 1297 | } else { |
| 1298 | self.root.join(path) |
| 1299 | }; |
| 1300 | let content = buffer_entry.buffer.contents(); |
| 1301 | to_backup.push((full_path, content)); |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | for (full_path, content) in to_backup { |
| 1308 | self.write_backup(&full_path, &content)?; |
| 1309 | } |
| 1310 | Ok(()) |
| 1311 | } |
| 1312 | |
| 1313 | /// Get the workspace directory name (repo name) |
| 1314 | pub fn repo_name(&self) -> String { |
| 1315 | self.root |
| 1316 | .file_name() |
| 1317 | .and_then(|n| n.to_str()) |
| 1318 | .unwrap_or("workspace") |
| 1319 | .to_string() |
| 1320 | } |
| 1321 | |
| 1322 | /// Get the current git branch name, if in a git repo |
| 1323 | pub fn git_branch(&self) -> Option<String> { |
| 1324 | use std::process::Command; |
| 1325 | |
| 1326 | let output = Command::new("git") |
| 1327 | .arg("-C") |
| 1328 | .arg(&self.root) |
| 1329 | .arg("branch") |
| 1330 | .arg("--show-current") |
| 1331 | .output() |
| 1332 | .ok()?; |
| 1333 | |
| 1334 | if output.status.success() { |
| 1335 | let branch = String::from_utf8_lossy(&output.stdout) |
| 1336 | .trim() |
| 1337 | .to_string(); |
| 1338 | if branch.is_empty() { |
| 1339 | // Detached HEAD - try to get short SHA |
| 1340 | let sha_output = Command::new("git") |
| 1341 | .arg("-C") |
| 1342 | .arg(&self.root) |
| 1343 | .arg("rev-parse") |
| 1344 | .arg("--short") |
| 1345 | .arg("HEAD") |
| 1346 | .output() |
| 1347 | .ok()?; |
| 1348 | if sha_output.status.success() { |
| 1349 | let sha = String::from_utf8_lossy(&sha_output.stdout) |
| 1350 | .trim() |
| 1351 | .to_string(); |
| 1352 | return Some(format!("({})", sha)); |
| 1353 | } |
| 1354 | None |
| 1355 | } else { |
| 1356 | Some(branch) |
| 1357 | } |
| 1358 | } else { |
| 1359 | None // Not a git repo |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | /// Check if this workspace is a git repository |
| 1364 | pub fn is_git_repo(&self) -> bool { |
| 1365 | self.root.join(".git").exists() |
| 1366 | } |
| 1367 | |
| 1368 | /// Find a tab by file path, returns tab index if found |
| 1369 | pub fn find_tab_by_path(&self, path: &std::path::Path) -> Option<usize> { |
| 1370 | for (tab_idx, tab) in self.tabs.iter().enumerate() { |
| 1371 | for buffer_entry in &tab.buffers { |
| 1372 | if let Some(buf_path) = &buffer_entry.path { |
| 1373 | // Get full path for comparison |
| 1374 | let full_path = if buffer_entry.is_orphan { |
| 1375 | buf_path.clone() |
| 1376 | } else { |
| 1377 | self.root.join(buf_path) |
| 1378 | }; |
| 1379 | if full_path == path { |
| 1380 | return Some(tab_idx); |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | None |
| 1386 | } |
| 1387 | |
| 1388 | /// Apply a text edit to a buffer in a specific tab |
| 1389 | pub fn apply_text_edit(&mut self, tab_idx: usize, edit: &crate::lsp::TextEdit) { |
| 1390 | if tab_idx >= self.tabs.len() { |
| 1391 | return; |
| 1392 | } |
| 1393 | |
| 1394 | let tab = &mut self.tabs[tab_idx]; |
| 1395 | if tab.buffers.is_empty() { |
| 1396 | return; |
| 1397 | } |
| 1398 | |
| 1399 | let buffer = &mut tab.buffers[0].buffer; |
| 1400 | |
| 1401 | // Convert LSP range to buffer char indices |
| 1402 | let start_line = edit.range.start.line as usize; |
| 1403 | let start_col = edit.range.start.character as usize; |
| 1404 | let end_line = edit.range.end.line as usize; |
| 1405 | let end_col = edit.range.end.character as usize; |
| 1406 | |
| 1407 | let start_char = buffer.line_col_to_char(start_line, start_col); |
| 1408 | let end_char = buffer.line_col_to_char(end_line, end_col); |
| 1409 | |
| 1410 | // Delete the old text first (if range is non-empty) |
| 1411 | if start_char < end_char { |
| 1412 | buffer.delete(start_char, end_char); |
| 1413 | } |
| 1414 | |
| 1415 | // Insert the new text at start position |
| 1416 | if !edit.new_text.is_empty() { |
| 1417 | buffer.insert(start_char, &edit.new_text); |
| 1418 | } |
| 1419 | // Buffer automatically tracks modifications via content hash |
| 1420 | } |
| 1421 | |
| 1422 | /// Find which pane in the active tab contains a screen coordinate |
| 1423 | /// Returns the pane index |
| 1424 | pub fn pane_at_position(&self, col: u16, row: u16, screen_cols: u16, screen_rows: u16) -> usize { |
| 1425 | // Calculate offsets for fuss mode and tab bar |
| 1426 | let fuss_width = if self.fuss.active { |
| 1427 | self.fuss.width(screen_cols) |
| 1428 | } else { |
| 1429 | 0 |
| 1430 | }; |
| 1431 | // Tab bar is always rendered (takes 1 row) |
| 1432 | let top_offset = 1u16; |
| 1433 | |
| 1434 | self.tabs[self.active_tab].pane_at_screen_position( |
| 1435 | col, row, screen_cols, screen_rows, fuss_width, top_offset |
| 1436 | ) |
| 1437 | } |
| 1438 | } |
| 1439 |