@@ -230,9 +230,25 @@ contains |
| 230 | 230 | success = .false. |
| 231 | 231 | workspace_file = trim(dir_path) // "/.fac/workspace.json" |
| 232 | 232 | |
| 233 | + ! DEBUG: Write to stderr (unit 0) so it doesn't interfere |
| 234 | + write(0, '(A)') '[DEBUG SAVE] workspace_save_state called' |
| 235 | + write(0, '(A)') '[DEBUG SAVE] Workspace file: ' // trim(workspace_file) |
| 236 | + if (allocated(editor%tabs)) then |
| 237 | + write(0, '(A,I0)') '[DEBUG SAVE] Tabs allocated, size: ', size(editor%tabs) |
| 238 | + if (size(editor%tabs) > 0) then |
| 239 | + write(0, '(A)') '[DEBUG SAVE] First tab filename: ' // trim(editor%tabs(1)%filename) |
| 240 | + end if |
| 241 | + else |
| 242 | + write(0, '(A)') '[DEBUG SAVE] ERROR: Tabs not allocated!' |
| 243 | + end if |
| 244 | + write(0, '(A,I0)') '[DEBUG SAVE] Active tab index: ', editor%active_tab_index |
| 245 | + |
| 233 | 246 | ! Open file for writing |
| 234 | 247 | open(newunit=unit, file=workspace_file, status='replace', iostat=ios) |
| 235 | | - if (ios /= 0) return |
| 248 | + if (ios /= 0) then |
| 249 | + write(0, '(A,I0)') '[DEBUG SAVE] ERROR: Failed to open file, ios=', ios |
| 250 | + return |
| 251 | + end if |
| 236 | 252 | |
| 237 | 253 | ! Get current timestamp (simplified) |
| 238 | 254 | call date_and_time(timestamp) |
@@ -246,6 +262,7 @@ contains |
| 246 | 262 | |
| 247 | 263 | ! Write tabs |
| 248 | 264 | if (allocated(editor%tabs)) then |
| 265 | + write(0, '(A,I0)') '[DEBUG SAVE] Writing ', size(editor%tabs), ' tabs' |
| 249 | 266 | ws_len = len_trim(dir_path) |
| 250 | 267 | do i = 1, size(editor%tabs) |
| 251 | 268 | ! Write tab start - must be on its own line for parser |
@@ -410,13 +427,20 @@ contains |
| 410 | 427 | character(len=20) :: value_str |
| 411 | 428 | character(len=512) :: warning_msg |
| 412 | 429 | |
| 430 | + ! DEBUG: Start of restoration |
| 431 | + write(0, '(A)') '[DEBUG RESTORE WS] workspace_restore_state called' |
| 432 | + write(0, '(A)') '[DEBUG RESTORE WS] Dir path: ' // trim(dir_path) |
| 433 | + |
| 413 | 434 | success = .false. |
| 414 | 435 | workspace_file = trim(dir_path) // "/.fac/workspace.json" |
| 415 | 436 | |
| 437 | + write(0, '(A)') '[DEBUG RESTORE WS] Workspace file: ' // trim(workspace_file) |
| 438 | + |
| 416 | 439 | ! Open workspace file |
| 417 | 440 | open(newunit=unit, file=workspace_file, status='old', iostat=ios) |
| 418 | 441 | if (ios /= 0) then |
| 419 | 442 | ! Workspace file doesn't exist or can't be read (Phase 7: error handling) |
| 443 | + write(0, '(A,I0)') '[DEBUG RESTORE WS] ERROR: Failed to open workspace.json, ios=', ios |
| 420 | 444 | warning_msg = "Warning: Could not open workspace.json - using empty workspace" |
| 421 | 445 | call terminal_write(trim(warning_msg)) |
| 422 | 446 | ! Brief pause so user can see the warning |
@@ -426,6 +450,8 @@ contains |
| 426 | 450 | return |
| 427 | 451 | end if |
| 428 | 452 | |
| 453 | + write(0, '(A)') '[DEBUG RESTORE WS] Workspace file opened successfully' |
| 454 | + |
| 429 | 455 | ! Parse JSON line by line (simple parser for our specific format) |
| 430 | 456 | in_tabs_array = .false. |
| 431 | 457 | reading_tab = .false. |
@@ -435,6 +461,7 @@ contains |
| 435 | 461 | pane_filename = "" |
| 436 | 462 | is_orphan = .false. |
| 437 | 463 | pane_count = 0 |
| 464 | + editor%active_tab_index = 1 ! Default to first tab |
| 438 | 465 | |
| 439 | 466 | |
| 440 | 467 | do |
@@ -449,6 +476,23 @@ contains |
| 449 | 476 | cycle |
| 450 | 477 | end if |
| 451 | 478 | |
| 479 | + ! Parse active_tab index (outside tabs array) |
| 480 | + if (.not. in_tabs_array .and. index(line, '"active_tab":') > 0) then |
| 481 | + colon_pos = index(line, ':') |
| 482 | + comma_pos = index(line, ',') |
| 483 | + if (colon_pos > 0) then |
| 484 | + if (comma_pos > colon_pos) then |
| 485 | + value_str = adjustl(line(colon_pos+1:comma_pos-1)) |
| 486 | + else |
| 487 | + value_str = adjustl(line(colon_pos+1:)) |
| 488 | + end if |
| 489 | + read(value_str, *, iostat=ios) editor%active_tab_index |
| 490 | + ! Ensure it's at least 1 if tabs were restored |
| 491 | + if (editor%active_tab_index < 1) editor%active_tab_index = 1 |
| 492 | + end if |
| 493 | + cycle |
| 494 | + end if |
| 495 | + |
| 452 | 496 | ! Check if we're exiting the tabs array |
| 453 | 497 | if (in_tabs_array .and. index(line, '],') > 0 .and. .not. in_panes_array) then |
| 454 | 498 | in_tabs_array = .false. |
@@ -567,14 +611,17 @@ contains |
| 567 | 611 | end if |
| 568 | 612 | |
| 569 | 613 | ! Create tab |
| 614 | + write(0, '(A)') '[DEBUG RESTORE WS] Creating tab for file: ' // trim(full_path) |
| 570 | 615 | call create_tab(editor, trim(full_path)) |
| 571 | 616 | tab_idx = editor%active_tab_index |
| 572 | 617 | |
| 573 | 618 | ! Set orphan flag and load file |
| 574 | 619 | if (allocated(editor%tabs) .and. tab_idx > 0) then |
| 620 | + write(0, '(A,I0)') '[DEBUG RESTORE WS] Tab created successfully, index: ', tab_idx |
| 575 | 621 | editor%tabs(tab_idx)%is_orphan = is_orphan |
| 576 | 622 | |
| 577 | 623 | call buffer_load_file(editor%tabs(tab_idx)%buffer, trim(full_path), load_status) |
| 624 | + write(0, '(A,I0)') '[DEBUG RESTORE WS] Buffer loaded, status: ', load_status |
| 578 | 625 | |
| 579 | 626 | ! Set cursor and viewport in first pane |
| 580 | 627 | if (allocated(editor%tabs(tab_idx)%panes) .and. size(editor%tabs(tab_idx)%panes) > 0) then |
@@ -739,6 +786,29 @@ contains |
| 739 | 786 | |
| 740 | 787 | close(unit) |
| 741 | 788 | |
| 789 | + write(0, '(A)') '[DEBUG RESTORE WS] Finished parsing JSON' |
| 790 | + |
| 791 | + ! Clamp active_tab_index to valid range |
| 792 | + if (allocated(editor%tabs)) then |
| 793 | + write(0, '(A,I0)') '[DEBUG RESTORE WS] Tabs allocated, count: ', size(editor%tabs) |
| 794 | + if (size(editor%tabs) > 0) then |
| 795 | + if (editor%active_tab_index > size(editor%tabs)) then |
| 796 | + editor%active_tab_index = size(editor%tabs) |
| 797 | + end if |
| 798 | + if (editor%active_tab_index < 1) then |
| 799 | + editor%active_tab_index = 1 |
| 800 | + end if |
| 801 | + else |
| 802 | + write(0, '(A)') '[DEBUG RESTORE WS] No tabs in array!' |
| 803 | + editor%active_tab_index = 0 ! No tabs |
| 804 | + end if |
| 805 | + else |
| 806 | + write(0, '(A)') '[DEBUG RESTORE WS] Tabs NOT allocated!' |
| 807 | + editor%active_tab_index = 0 ! No tabs |
| 808 | + end if |
| 809 | + |
| 810 | + write(0, '(A,I0)') '[DEBUG RESTORE WS] Final active_tab_index: ', editor%active_tab_index |
| 811 | + |
| 742 | 812 | ! Sync the active pane to editor state so status bar shows correct filename |
| 743 | 813 | if (allocated(editor%tabs) .and. editor%active_tab_index > 0) then |
| 744 | 814 | if (editor%active_tab_index <= size(editor%tabs)) then |
@@ -746,6 +816,7 @@ contains |
| 746 | 816 | if (editor%tabs(editor%active_tab_index)%active_pane_index > 0 .and. & |
| 747 | 817 | editor%tabs(editor%active_tab_index)%active_pane_index <= & |
| 748 | 818 | size(editor%tabs(editor%active_tab_index)%panes)) then |
| 819 | + write(0, '(A)') '[DEBUG RESTORE WS] Syncing active pane to editor' |
| 749 | 820 | call sync_pane_to_editor(editor, editor%active_tab_index, & |
| 750 | 821 | editor%tabs(editor%active_tab_index)%active_pane_index) |
| 751 | 822 | end if |
@@ -754,6 +825,7 @@ contains |
| 754 | 825 | end if |
| 755 | 826 | |
| 756 | 827 | success = .true. |
| 828 | + write(0, '(A)') '[DEBUG RESTORE WS] workspace_restore_state completed successfully' |
| 757 | 829 | end subroutine workspace_restore_state |
| 758 | 830 | |
| 759 | 831 | !> Track workspace in recents (helper function) |