feat: add Fortran support and tab controls

This commit is contained in:
shenw
2026-06-22 18:07:34 +08:00
parent 2f480e7c8f
commit 4113236128
10 changed files with 301 additions and 41 deletions
+44 -4
View File
@@ -57,6 +57,29 @@ def main() -> int:
empty_window.set_project_root(project_root, save=False)
if not empty_window.has_project or empty_window.project_root != project_root.resolve() or not empty_window.entries:
checks.append("set_project_root")
switch_root = project_root / "__gui_smoke_switch_root__"
try:
switch_root.mkdir(exist_ok=True)
(switch_root / "first.md").write_text("# First\n", encoding="utf-8")
empty_window.open_document(project_root / "sample.md", add_history=False)
if not empty_window.set_project_root(switch_root, save=False):
checks.append("switch_project_root")
if empty_window.current_file is not None:
checks.append("switch_project_clears_current_file")
if empty_window.tabs.count() != 1 or not bool(empty_window.tabs.currentWidget().property("is_welcome")):
checks.append("switch_project_closes_tabs")
(switch_root / "second.md").write_text("# Second\n", encoding="utf-8")
empty_window._poll_directory_changes()
if not any(entry.path == (switch_root / "second.md").resolve() for entry in empty_window.entries):
checks.append("directory_poll_refresh")
finally:
for cleanup_path in (switch_root / "first.md", switch_root / "second.md"):
cleanup_path.unlink(missing_ok=True)
try:
switch_root.rmdir()
except OSError:
pass
empty_window.set_project_root(project_root, save=False)
supported_suffixes = {".md", ".markdown", ".html", ".htm", ".xhtml"}
if any(entry.path.suffix.lower() not in supported_suffixes for entry in empty_window.entries):
checks.append("project_supported_files_only")
@@ -65,9 +88,9 @@ def main() -> int:
if any(entry.path == (project_root / "requirements.txt").resolve() for entry in empty_window.entries):
checks.append("project_hidden_unsupported_files")
original_file_groups = empty_window.document_file_groups
empty_window.document_file_groups = ("markdown", "python", "cpp")
empty_window.document_file_groups = ("markdown", "python", "cpp", "fortran")
empty_window._refresh_document_entries(force=True)
code_suffixes = {".py", ".c", ".cc", ".cxx", ".cpp", ".h", ".hh", ".hxx", ".hpp"}
code_suffixes = {".py", ".c", ".cc", ".cxx", ".cpp", ".h", ".hh", ".hxx", ".hpp", ".f", ".for", ".f90", ".f95"}
if not any(entry.path.suffix.lower() in code_suffixes for entry in empty_window.entries):
checks.append("project_optional_code_files")
empty_window.open_document(project_root / "sample.md", add_history=False)
@@ -162,8 +185,24 @@ def main() -> int:
checks.append("tabs_non_expanding")
if hasattr(tab_bar, "changeCurrentOnDrag") and tab_bar.changeCurrentOnDrag():
checks.append("tabs_no_drag_switch")
if tab_bar.contextMenuPolicy() != Qt.ContextMenuPolicy.CustomContextMenu:
checks.append("tabs_context_menu_policy")
if window._welcome_tab_index() >= 0:
checks.append("welcome_closed_for_document")
window.open_document(project_root / "docs" / "user_manual.md", new_tab=True, add_history=False)
user_manual_index = window.tabs.currentIndex()
if sum(1 for index in range(window.tabs.count()) if window._is_content_tab_index(index)) < 2:
checks.append("tab_context_multiple_tabs")
if not window._close_other_tabs(user_manual_index):
checks.append("tab_context_close_others")
if window.tabs.count() != 1 or window.current_file != (project_root / "docs" / "user_manual.md").resolve():
checks.append("tab_context_close_others_result")
window.open_document(project_root / "sample.md", new_tab=True, add_history=False)
if not window._close_tab_indexes(list(range(window.tabs.count()))):
checks.append("tab_context_close_all")
if window.tabs.count() != 1 or not bool(window.tabs.currentWidget().property("is_welcome")):
checks.append("tab_context_close_all_result")
window.open_document(project_root / "sample.md", add_history=False)
if window.windowIcon().isNull():
checks.append("window_icon")
if app.windowIcon().isNull():
@@ -240,12 +279,15 @@ def main() -> int:
)
if abs(settings_dialog.wheel_scroll_speed_spin.value() - 1.4) > 0.01:
checks.append("settings_wheel_scroll_speed")
settings_dialog.fortran_files_check.setChecked(True)
settings_dialog.wheel_scroll_speed_spin.setValue(2.2)
prefs = settings_dialog.preferences()
if abs(prefs.wheel_scroll_speed - 2.2) > 0.01:
checks.append("settings_wheel_scroll_speed_value")
if abs(prefs.reader_width_scale - 1.5) > 0.01:
checks.append("settings_reader_width_scale")
if "fortran" not in prefs.document_file_groups:
checks.append("settings_fortran_file_group")
if not prefs.reader_embedded_toc or prefs.reader_external_toc or not prefs.reader_embedded_progress:
checks.append("settings_reader_navigation")
external_settings_dialog = SettingsDialog(
@@ -948,5 +990,3 @@ def main() -> int:
if __name__ == "__main__":
raise SystemExit(main())
+23 -4
View File
@@ -262,12 +262,13 @@ def test_document_discovery_filters_optional_code_groups(tmp_path: Path) -> None
(tmp_path / "guide.md").write_text("# Guide\n", encoding="utf-8")
(tmp_path / "tool.py").write_text("def run():\n return 1\n", encoding="utf-8")
(tmp_path / "engine.cpp").write_text("int solve() { return 1; }\n", encoding="utf-8")
(tmp_path / "solver.f90").write_text("program demo\nend program demo\n", encoding="utf-8")
default_entries = discover_documents(tmp_path)
all_entries = discover_documents(tmp_path, ("markdown", "python", "cpp"))
all_entries = discover_documents(tmp_path, ("markdown", "python", "cpp", "fortran"))
assert [entry.path.name for entry in default_entries] == ["guide.md"]
assert {entry.path.name for entry in all_entries} == {"guide.md", "tool.py", "engine.cpp"}
assert {entry.path.name for entry in all_entries} == {"guide.md", "tool.py", "engine.cpp", "solver.f90"}
def test_document_discovery_prunes_excluded_directories(tmp_path: Path) -> None:
@@ -310,26 +311,46 @@ def test_code_file_rendering_uses_highlighting_and_line_anchors(tmp_path: Path)
renderer = MarkdownRenderer(tmp_path)
html = renderer.render_code_source(source, code_path, language="python")
fortran_html = renderer.render_code_source("program demo\nend program demo\n", tmp_path / "solver.f90", language="fortran")
assert "code-file-view" in html
assert "language-python" in html
assert "line-1" in html
assert "Runner" in html
assert "language-fortran" in fortran_html
assert "line-1" in fortran_html
assert "program" in fortran_html.lower()
def test_code_headings_extract_python_and_cpp_symbols(tmp_path: Path) -> None:
python_path = tmp_path / "tool.py"
cpp_path = tmp_path / "engine.cpp"
fortran_path = tmp_path / "solver.f90"
python_source = "class Runner:\n def run(self):\n return 1\n\nasync def main():\n return None\n"
cpp_source = "struct Node { int value; };\n\nint solve(int value) { return value; }\n"
fortran_source = (
"module solver_mod\n"
"type state\nend type state\n"
"contains\n"
"subroutine advance(dt)\nend subroutine advance\n"
"real function energy(x)\nend function energy\n"
"end module solver_mod\n"
)
python_headings = extract_code_headings(python_path, python_source)
cpp_headings = extract_code_headings(cpp_path, cpp_source)
fortran_headings = extract_code_headings(fortran_path, fortran_source)
assert [heading.title for heading in python_headings] == ["class Runner", "def run", "async def main"]
assert [heading.slug for heading in python_headings] == ["line-1", "line-2", "line-5"]
assert any(heading.title == "struct Node" for heading in cpp_headings)
assert any(heading.title == "function solve" for heading in cpp_headings)
assert [heading.title for heading in fortran_headings] == [
"module solver_mod",
"type state",
"subroutine advance",
"function energy",
]
def test_search_index_reads_optional_code_documents(tmp_path: Path) -> None:
@@ -373,5 +394,3 @@ def test_code_headings_read_gbk_source_when_source_not_supplied(tmp_path: Path)
assert headings[0].title == "class 中文类"
assert headings[1].title == "def run"