diff --git a/docs/architecture.md b/docs/architecture.md index 96401bf..da79830 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -31,6 +31,12 @@ `markdown_editor.py` 组装编辑界面并连接文档会话。`editor_widgets.py` 负责源码控件、行号和语法高亮;`EditorFindController` 管理查找位置及可撤销替换;`EditorPreviewController` 管理预览刷新、滚动映射、桥接信号和计时器。预览控制器接收明确的控件与渲染回调,不读取主窗口或文档存储。关闭编辑器时同时停止预览刷新与滚动计时器。 +## 项目目录与文件监听 + +`ProjectCatalog` 管理文档列表、目录签名和搜索索引。主窗口只负责把结果放到目录树中,仍在接收后台扫描结果时检查项目与代次,避免旧结果覆盖新项目。 + +`FileWatchService` 管理监听路径、变更签名和合并刷新计时器,通过 `WatchContext` 请求界面刷新。它不访问标签控件。内部保存完成后更新监听签名;切换或关闭项目会清理待处理事件和计时器。编辑中的内容仍由窗口的文档会话规则保护,不被磁盘刷新覆盖。 + ## 验证约定 每个重构阶段独立提交。运行单元测试,并按改动范围运行 `gui_smoke.py`、`improvements_smoke.py` 和 `reliability_smoke.py`。涉及浏览器生命周期的调整必须检查欢迎页、首次打开文件、标签切换以及预览滚动;涉及保存的调整必须检查保存失败、取消、另存为和后台标签。 diff --git a/mdhelper/file_watch.py b/mdhelper/file_watch.py new file mode 100644 index 0000000..1641d46 --- /dev/null +++ b/mdhelper/file_watch.py @@ -0,0 +1,241 @@ +"""File monitoring and refresh decisions, with explicit UI callbacks.""" +from __future__ import annotations +from collections.abc import Callable +from dataclasses import dataclass +from pathlib import Path +from PySide6.QtCore import QObject, QFileSystemWatcher, QTimer +from .document_resources import analyze_document, resolve_local_reference + + +@dataclass(frozen=True) +class WatchContext: + root: Callable[[], Path | None] + mode: Callable[[], str] + open_paths: Callable[[], set[Path]] + is_document: Callable[[Path], bool] + renaming: Callable[[], bool] + refresh_tabs: Callable[[set[Path]], None] + refresh_tree: Callable[[], None] + + +class FileWatchService(QObject): + def __init__(self, parent: QObject, context: WatchContext, signature: Callable): + super().__init__(parent) + self.context, self.signature = context, signature + self.internal_save_paths: set[Path] = set() + self.watched_file_signatures: dict[Path, tuple[int, int] | None] = {} + self.pending_changed_files: set[Path] = set() + self.pending_directory_refresh = False + self.file_watcher = QFileSystemWatcher(self) + self.file_watcher.fileChanged.connect(self.on_file_changed) + self.file_watcher.directoryChanged.connect(self.on_directory_changed) + self.refresh_timer = QTimer(self) + self.refresh_timer.setSingleShot(True) + self.refresh_timer.setInterval(1000) + self.refresh_timer.timeout.connect(self.refresh_changed_documents) + self.tree_refresh_timer = QTimer(self) + self.tree_refresh_timer.setSingleShot(True) + self.tree_refresh_timer.setInterval(1000) + self.tree_refresh_timer.timeout.connect(context.refresh_tree) + + def reset(self) -> None: + self.refresh_timer.stop() + self.tree_refresh_timer.stop() + self.pending_changed_files.clear() + self.pending_directory_refresh = False + self.internal_save_paths.clear() + self.watched_file_signatures.clear() + paths = self.file_watcher.files() + self.file_watcher.directories() + if paths: + self.file_watcher.removePaths(paths) + + def update_paths(self) -> None: + watched_files = self.file_watcher.files() + watched_dirs = self.file_watcher.directories() + if watched_files: + self.file_watcher.removePaths(watched_files) + if watched_dirs: + self.file_watcher.removePaths(watched_dirs) + + root = self.context.root() + if root is None or self.context.mode() == "off": + return + + paths: set[Path] = set() + directories: set[Path] = set() + + docs_dir = root / "docs" + media_dir = root / "media" + if self.allows("directory"): + directories.add(root) + if self.allows("directory") and docs_dir.exists(): + directories.add(docs_dir) + if self.allows("resource") and media_dir.exists(): + directories.add(media_dir) + + sample = root / "sample.md" + if sample.exists(): + paths.add(sample.resolve()) + + open_paths = self.context.open_paths() + + for open_path in open_paths: + paths.add(open_path) + if ( + self.allows("current") + or self.allows("resource") + or self.allows("directory") + ): + directories.add(open_path.parent) + if self.allows("resource") and open_path.suffix.lower() in {".md", ".markdown"}: + paths.update(self.referenced_local_paths(open_path)) + + normalized_paths = {path.resolve() for path in paths if path.exists() and path.is_file()} + self.watched_file_signatures = { + path: self.watched_file_signatures.get(path, self.signature(path)) + for path in normalized_paths + } + file_paths = [str(path) for path in normalized_paths] + dir_paths = [str(path) for path in directories if path.exists() and path.is_dir()] + if file_paths: + self.file_watcher.addPaths(file_paths) + if dir_paths: + self.file_watcher.addPaths(dir_paths) + + def file_changed_since_last_check(self, path: Path) -> bool: + had_previous = path in self.watched_file_signatures + previous = self.watched_file_signatures.get(path) + current = self.signature(path) + self.watched_file_signatures[path] = current + if not had_previous: + return False + return previous != current + + def allows(self, target: str) -> bool: + if self.context.mode() == "off": + return False + if target == "current": + return self.context.mode() in {"current", "resources", "all"} + if target == "resource": + return self.context.mode() in {"resources", "all"} + if target == "directory": + return self.context.mode() == "all" + return False + + def references_for_documents(self, document_paths: set[Path]) -> dict[Path, set[Path]]: + if not self.allows("resource"): + return {} + return { + path: self.referenced_local_paths(path) + for path in document_paths + if path.suffix.lower() in {".md", ".markdown"} and path.exists() + } + + def affected_documents( + self, + changed_paths: set[Path], + open_paths: set[Path] | None = None, + reference_paths_by_document: dict[Path, set[Path]] | None = None, + ) -> set[Path]: + if not changed_paths: + return set() + active_open_paths = open_paths or self.context.open_paths() + references = ( + reference_paths_by_document + if reference_paths_by_document is not None + else self.references_for_documents(active_open_paths) + ) + affected = active_open_paths & changed_paths + if self.allows("resource"): + for document_path, reference_paths in references.items(): + if reference_paths & changed_paths: + affected.add(document_path) + return affected + + @staticmethod + def referenced_local_paths(markdown_path: Path) -> set[Path]: + try: + source = markdown_path.read_text(encoding="utf-8") + except (OSError, UnicodeError): + return set() + paths = {resolve_local_reference(markdown_path, reference.url) + for reference in analyze_document(source).references} + return {path for path in paths if path is not None and path.is_file()} + + def on_file_changed(self, changed_path: str) -> None: + if self.context.mode() == "off": + return + changed = Path(changed_path).resolve() + if changed in self.internal_save_paths: + self.internal_save_paths.discard(changed) + self.watched_file_signatures[changed] = self.signature(changed) + self.update_paths() + return + if not self.file_changed_since_last_check(changed): + self.update_paths() + return + self.pending_changed_files.add(changed) + if self.affected_documents({changed}): + self.refresh_timer.start() + elif self.allows("directory") and self.context.is_document(changed): + self.pending_directory_refresh = True + self.tree_refresh_timer.start() + + def on_directory_changed(self, changed_path: str) -> None: + root = self.context.root() + if self.context.mode() == "off" or root is None: + return + if self.context.renaming(): + return + changed = Path(changed_path).resolve() + docs_dir = (root / "docs").resolve() + if self.allows("directory") and changed in {root, docs_dir}: + self.pending_directory_refresh = True + self.tree_refresh_timer.start() + open_paths = self.context.open_paths() + watched_content_dirs = {path.parent.resolve() for path in open_paths} + if self.allows("resource"): + watched_content_dirs.add((root / "media").resolve()) + if changed in watched_content_dirs: + self.refresh_timer.start() + + def refresh_changed_documents(self) -> None: + open_paths = {path for path in self.context.open_paths() if path.exists()} + + if not open_paths: + self.pending_changed_files.clear() + self.update_paths() + return + + reference_paths_by_document = self.references_for_documents(open_paths) + refresh_paths = set(open_paths) + for reference_paths in reference_paths_by_document.values(): + refresh_paths.update(reference_paths) + + signature_changed_paths = { + path + for path in refresh_paths + if path.exists() and self.file_changed_since_last_check(path) + } + changed_paths = { + path.resolve() + for path in self.pending_changed_files + if path.exists() + } | signature_changed_paths + affected_documents = self.affected_documents( + changed_paths, + open_paths=open_paths, + reference_paths_by_document=reference_paths_by_document, + ) + self.pending_changed_files.clear() + if affected_documents: + self.context.refresh_tabs(affected_documents) + if ( + self.allows("directory") + and any(self.context.is_document(path) for path in affected_documents) + ): + self.context.refresh_tree() + else: + self.update_paths() + + diff --git a/mdhelper/main_window.py b/mdhelper/main_window.py index da4086d..0a2bbee 100644 --- a/mdhelper/main_window.py +++ b/mdhelper/main_window.py @@ -12,7 +12,7 @@ from pathlib import Path from dataclasses import fields, asdict from urllib.parse import unquote, urlsplit -from PySide6.QtCore import QEventLoop, QFile, QFileSystemWatcher, QPoint, QProcess, QSettings, QSignalBlocker, QSize, QThreadPool, QTimer, QUrl, Qt, Slot +from PySide6.QtCore import QEventLoop, QFile, QPoint, QProcess, QSettings, QSignalBlocker, QSize, QThreadPool, QTimer, QUrl, Qt, Slot from PySide6.QtGui import ( QAction, QActionGroup, @@ -39,10 +39,7 @@ from .document_tree import ( DEFAULT_DOCUMENT_FILE_GROUPS, DocumentEntry, code_language_for_path, - discover_documents, - document_directory_signature, document_extensions_for_groups, - first_heading, is_code_document, normalized_document_file_groups, ) @@ -51,12 +48,13 @@ from .markdown_utils import Heading, extract_headings, extract_headings_from_tex from .markdown_editor import MarkdownEditorDialog as RichMarkdownEditorDialog from .markdown_renderer import MarkdownRenderer from .media import is_markdown_url, is_qt_video_player_url, is_webpage_url, media_kind, media_mime_type -from .search import SearchIndex from .settings_dialog import AppPreferences, SettingsDialog from .style_dialog import DocumentStyleEditorDialog, build_document_style_css from .style_sync import normalize_payload, render_css from .text_io import DEFAULT_TEXT_PREVIEW_BYTES, read_text_with_fallback from .startup_scan import StartupScan +from .project_catalog import ProjectCatalog +from .file_watch import FileWatchService, WatchContext from .export_workflow import ExportController, ExportContext, ExportSnapshot from .render_options import RenderOptions, render_document from .preferences import ReaderPreferences, ApplicationConfiguration, ConfigurationStore, normalize_custom_styles @@ -120,18 +118,6 @@ def re_find_markdown_links(source: str) -> list[str]: return [reference.url for reference in analyze_document(source).references] - - - - - - - - - - - - class MainWindow(QMainWindow): def __init__( self, @@ -162,8 +148,7 @@ class MainWindow(QMainWindow): self.document_closer = DocumentCloseController(self.drafts, self._choose_editor_close, lambda editor: self._save_editor_tab(editor, editor.path, editor.source_text(), editor.file_signature)) self.document_file_groups = DEFAULT_DOCUMENT_FILE_GROUPS self.renderer = MarkdownRenderer(self.project_root) - self.entries: list[DocumentEntry] = [] - self.search_index = SearchIndex(self.entries) + self.catalog = ProjectCatalog() self.current_file: Path | None = None self.current_fragment = "" self.pending_fragment = "" @@ -195,28 +180,15 @@ class MainWindow(QMainWindow): self.path_to_item: dict[Path, QTreeWidgetItem] = {} self.file_tree_icon_cache: dict[tuple[str, str], QIcon] = {} self.folder_tree_icon_cache: dict[str, QIcon] = {} - self.internal_save_paths: set[Path] = set() - self.watched_file_signatures: dict[Path, tuple[int, int] | None] = {} - self.pending_changed_files: set[Path] = set() - self.pending_directory_refresh = False self._new_file_rename_item: QTreeWidgetItem | None = None self._new_file_rename_path: Path | None = None self._tree_rename_is_new = False - self.document_entries_signature = self._document_entries_signature(self.entries) - self.document_directory_signature = tuple() - self.file_watcher = QFileSystemWatcher(self) - self.file_watcher.fileChanged.connect(self._on_watched_file_changed) - self.file_watcher.directoryChanged.connect(self._on_watched_directory_changed) - - self.refresh_timer = QTimer(self) - self.refresh_timer.setSingleShot(True) - self.refresh_timer.setInterval(self.auto_refresh_delay_ms) - self.refresh_timer.timeout.connect(self._auto_refresh_current_document) - - self.tree_refresh_timer = QTimer(self) - self.tree_refresh_timer.setSingleShot(True) - self.tree_refresh_timer.setInterval(self.auto_refresh_delay_ms) - self.tree_refresh_timer.timeout.connect(self._refresh_document_entries) + self.watch = FileWatchService(self, WatchContext( + lambda: self.project_root if self.has_project else None, + lambda: self.auto_refresh_mode if self.auto_refresh_enabled else "off", + self._watched_open_paths, self._is_supported_document_path, + lambda: self._new_file_rename_item is not None, + self._refresh_open_document_tabs, self._refresh_document_entries), self.documents.signature) self.directory_poll_timer = QTimer(self) self.directory_poll_timer.setInterval(DIRECTORY_POLL_INTERVAL_MS) @@ -231,10 +203,7 @@ class MainWindow(QMainWindow): self._load_preferences() if self.has_project and not defer_startup: - self.entries = discover_documents(self.project_root, self.document_file_groups) - self.document_entries_signature = self._document_entries_signature(self.entries) - self.document_directory_signature = document_directory_signature(self.project_root, self.document_file_groups) - self.search_index.rebuild(self.entries) + self.catalog.scan(self.project_root, self.document_file_groups, force=True) self._apply_auto_refresh_timing() self.setWindowTitle("MDHelper") icon_path = self.base_window_icon_path @@ -284,10 +253,7 @@ class MainWindow(QMainWindow): LOGGER.warning("Initial directory scan failed: %s", error) self.status_label.setText(f"目录加载失败:{error}") else: - self.entries = entries - self.document_entries_signature = self._document_entries_signature(entries) - self.document_directory_signature = signature - self.search_index.rebuild(entries) + self.catalog.adopt(entries, signature) if self.current_file and self.current_file.exists() and self._is_supported_document_path(self.current_file): self._ensure_document_entry(self.current_file) self._populate_tree() @@ -1884,10 +1850,8 @@ class MainWindow(QMainWindow): self.has_project = False self.project_root = self.app_root self.renderer = MarkdownRenderer(self.project_root) - self.entries = [] - self.document_entries_signature = self._document_entries_signature(self.entries) - self.document_directory_signature = tuple() - self.search_index = SearchIndex(self.entries) + self.catalog.adopt([]) + self.watch.reset() self.path_to_item.clear() self.current_file = None self.current_fragment = "" @@ -1936,13 +1900,11 @@ class MainWindow(QMainWindow): self._startup_generation += 1 self._startup_scan_pending = False + self.watch.reset() self.project_root = project_root self.has_project = True self.renderer = MarkdownRenderer(self.project_root) - self.entries = discover_documents(self.project_root, self.document_file_groups) - self.document_entries_signature = self._document_entries_signature(self.entries) - self.document_directory_signature = document_directory_signature(self.project_root, self.document_file_groups) - self.search_index.rebuild(self.entries) + self.catalog.scan(self.project_root, self.document_file_groups, force=True) self.path_to_item.clear() self._populate_tree() self._filter_tree(self.search_box.text()) @@ -2156,12 +2118,10 @@ class MainWindow(QMainWindow): def _refresh_after_markdown_save(self, path: Path, reload_current: bool = True) -> None: previous_current = self.current_file previous_fragment = self.current_fragment - self.entries = discover_documents(self.project_root, self.document_file_groups) - self.document_entries_signature = self._document_entries_signature(self.entries) + self.catalog.scan(self.project_root, self.document_file_groups, force=True) if path.exists() and all(entry.path != path for entry in self.entries): self._ensure_document_entry(path) else: - self.search_index.rebuild(self.entries) self._populate_tree() self._filter_tree(self.search_box.text()) @@ -3863,22 +3823,9 @@ class MainWindow(QMainWindow): self.open_document(Path(data)) def _ensure_document_entry(self, path: Path) -> None: - if not self._is_supported_document_path(path): - return - if any(entry.path == path for entry in self.entries): - return - - title = first_heading(path) or path.stem.replace("_", " ").title() - try: - relative_path = path.relative_to(self.project_root).as_posix() - except ValueError: - relative_path = str(path) - - self.entries.append(DocumentEntry(path=path, title=title, relative_path=relative_path)) - self.document_entries_signature = self._document_entries_signature(self.entries) - self.search_index.rebuild(self.entries) - self._populate_tree() - self._filter_tree(self.search_box.text()) + if self._is_supported_document_path(path) and self.catalog.ensure(path, self.project_root): + self._populate_tree() + self._filter_tree(self.search_box.text()) def _display_path(self, path: Path) -> str: try: @@ -4027,59 +3974,87 @@ class MainWindow(QMainWindow): script = f"(() => {{ window.scrollTo({{top: {max(0, scroll_y)}, behavior: 'instant'}}); }})();" self.web_view.page().runJavaScript(script) - def _update_watched_paths(self) -> None: - watched_files = self.file_watcher.files() - watched_dirs = self.file_watcher.directories() - if watched_files: - self.file_watcher.removePaths(watched_files) - if watched_dirs: - self.file_watcher.removePaths(watched_dirs) - if not self.has_project or not self.auto_refresh_enabled or self.auto_refresh_mode == "off": - return - - paths: set[Path] = set() - directories: set[Path] = set() - - docs_dir = self.project_root / "docs" - media_dir = self.project_root / "media" - if self._auto_refresh_allows("directory"): - directories.add(self.project_root) - if self._auto_refresh_allows("directory") and docs_dir.exists(): - directories.add(docs_dir) - if self._auto_refresh_allows("resource") and media_dir.exists(): - directories.add(media_dir) - - sample = self.project_root / "sample.md" - if sample.exists(): - paths.add(sample.resolve()) - - open_paths = self._open_tab_file_paths() + def _watched_open_paths(self) -> set[Path]: + paths = self._open_tab_file_paths() if self.current_file is not None: - open_paths.add(self.current_file) + paths.add(self.current_file) + return paths - for open_path in open_paths: - paths.add(open_path) - if ( - self._auto_refresh_allows("current") - or self._auto_refresh_allows("resource") - or self._auto_refresh_allows("directory") - ): - directories.add(open_path.parent) - if self._auto_refresh_allows("resource") and open_path.suffix.lower() in {".md", ".markdown"}: - paths.update(self._referenced_local_paths(open_path)) + @property + def entries(self): + return self.catalog.entries - normalized_paths = {path.resolve() for path in paths if path.exists() and path.is_file()} - self.watched_file_signatures = { - path: self.watched_file_signatures.get(path, self._file_signature(path)) - for path in normalized_paths - } - file_paths = [str(path) for path in normalized_paths] - dir_paths = [str(path) for path in directories if path.exists() and path.is_dir()] - if file_paths: - self.file_watcher.addPaths(file_paths) - if dir_paths: - self.file_watcher.addPaths(dir_paths) + @property + def search_index(self): + return self.catalog.search_index + + @property + def document_entries_signature(self): + return self.catalog.entries_signature + + @property + def document_directory_signature(self): + return self.catalog.directory_signature + + @property + def file_watcher(self): + return self.watch.file_watcher + + @property + def refresh_timer(self): + return self.watch.refresh_timer + + @property + def tree_refresh_timer(self): + return self.watch.tree_refresh_timer + + @property + def internal_save_paths(self): + return self.watch.internal_save_paths + + @property + def watched_file_signatures(self): + return self.watch.watched_file_signatures + + @property + def pending_changed_files(self): + return self.watch.pending_changed_files + + @property + def pending_directory_refresh(self): + return self.watch.pending_directory_refresh + + @pending_directory_refresh.setter + def pending_directory_refresh(self, value): + self.watch.pending_directory_refresh = value + + def _update_watched_paths(self): + return self.watch.update_paths() + + def _watched_file_really_changed(self, path): + return self.watch.file_changed_since_last_check(path) + + def _auto_refresh_allows(self, target): + return self.watch.allows(target) + + def _open_document_reference_paths(self, document_paths): + return self.watch.references_for_documents(document_paths) + + def _documents_affected_by_changed_paths(self, changed_paths, open_paths=None, reference_paths_by_document=None): + return self.watch.affected_documents(changed_paths, open_paths, reference_paths_by_document) + + def _referenced_local_paths(self, markdown_path): + return self.watch.referenced_local_paths(markdown_path) + + def _on_watched_file_changed(self, changed_path): + return self.watch.on_file_changed(changed_path) + + def _on_watched_directory_changed(self, changed_path): + return self.watch.on_directory_changed(changed_path) + + def _auto_refresh_current_document(self): + return self.watch.refresh_changed_documents() def _poll_directory_changes(self) -> None: if self._startup_scan_pending: @@ -4088,34 +4063,12 @@ class MainWindow(QMainWindow): return if self._new_file_rename_item is not None: return - signature = document_directory_signature(self.project_root, self.document_file_groups) - if signature == self.document_directory_signature: + signature = self.catalog.directory_changed(self.project_root, self.document_file_groups) + if signature is None: return self.pending_directory_refresh = True self._refresh_document_entries(directory_signature=signature) - def _document_entries_signature(self, entries: list[DocumentEntry]) -> tuple[tuple[str, str], ...]: - return tuple(sorted((str(entry.path), entry.title) for entry in entries)) - - def _watched_file_really_changed(self, path: Path) -> bool: - had_previous = path in self.watched_file_signatures - previous = self.watched_file_signatures.get(path) - current = self._file_signature(path) - self.watched_file_signatures[path] = current - if not had_previous: - return False - return previous != current - - def _auto_refresh_allows(self, target: str) -> bool: - if not self.auto_refresh_enabled or self.auto_refresh_mode == "off": - return False - if target == "current": - return self.auto_refresh_mode in {"current", "resources", "all"} - if target == "resource": - return self.auto_refresh_mode in {"resources", "all"} - if target == "directory": - return self.auto_refresh_mode == "all" - return False def _open_tab_file_paths(self) -> set[Path]: paths: set[Path] = set() @@ -4155,35 +4108,6 @@ class MainWindow(QMainWindow): tabs.append(web_view) return tabs - def _open_document_reference_paths(self, document_paths: set[Path]) -> dict[Path, set[Path]]: - if not self._auto_refresh_allows("resource"): - return {} - return { - path: self._referenced_local_paths(path) - for path in document_paths - if path.suffix.lower() in {".md", ".markdown"} and path.exists() - } - - def _documents_affected_by_changed_paths( - self, - changed_paths: set[Path], - open_paths: set[Path] | None = None, - reference_paths_by_document: dict[Path, set[Path]] | None = None, - ) -> set[Path]: - if not changed_paths: - return set() - active_open_paths = open_paths or self._open_tab_file_paths() - references = ( - reference_paths_by_document - if reference_paths_by_document is not None - else self._open_document_reference_paths(active_open_paths) - ) - affected = active_open_paths & changed_paths - if self._auto_refresh_allows("resource"): - for document_path, reference_paths in references.items(): - if reference_paths & changed_paths: - affected.add(document_path) - return affected def _refresh_open_document_tabs(self, document_paths: set[Path]) -> None: if not document_paths: @@ -4206,92 +4130,6 @@ class MainWindow(QMainWindow): if not refreshed_current: self._update_watched_paths() - def _referenced_local_paths(self, markdown_path: Path) -> set[Path]: - try: - source = markdown_path.read_text(encoding="utf-8") - except (OSError, UnicodeError): - return set() - paths = {resolve_local_reference(markdown_path, reference.url) - for reference in analyze_document(source).references} - return {path for path in paths if path is not None and path.is_file()} - - def _on_watched_file_changed(self, changed_path: str) -> None: - if not self.auto_refresh_enabled or self.auto_refresh_mode == "off": - return - changed = Path(changed_path).resolve() - if changed in self.internal_save_paths: - self.internal_save_paths.discard(changed) - self._update_watched_paths() - return - if not self._watched_file_really_changed(changed): - self._update_watched_paths() - return - self.pending_changed_files.add(changed) - if self._documents_affected_by_changed_paths({changed}): - self.refresh_timer.start() - elif self._auto_refresh_allows("directory") and self._is_supported_document_path(changed): - self.pending_directory_refresh = True - self.tree_refresh_timer.start() - - def _on_watched_directory_changed(self, changed_path: str) -> None: - if not self.auto_refresh_enabled or self.auto_refresh_mode == "off" or not self.has_project: - return - if self._new_file_rename_item is not None: - return - changed = Path(changed_path).resolve() - docs_dir = (self.project_root / "docs").resolve() - if self._auto_refresh_allows("directory") and changed in {self.project_root, docs_dir}: - self.pending_directory_refresh = True - self.tree_refresh_timer.start() - open_paths = self._open_tab_file_paths() - if self.current_file is not None: - open_paths.add(self.current_file) - watched_content_dirs = {path.parent.resolve() for path in open_paths} - if self._auto_refresh_allows("resource"): - watched_content_dirs.add((self.project_root / "media").resolve()) - if changed in watched_content_dirs: - self.refresh_timer.start() - - def _auto_refresh_current_document(self) -> None: - open_paths = {path for path in self._open_tab_file_paths() if path.exists()} - if self.current_file is not None and self.current_file.exists(): - open_paths.add(self.current_file) - - if not open_paths: - self.pending_changed_files.clear() - self._update_watched_paths() - return - - reference_paths_by_document = self._open_document_reference_paths(open_paths) - refresh_paths = set(open_paths) - for reference_paths in reference_paths_by_document.values(): - refresh_paths.update(reference_paths) - - signature_changed_paths = { - path - for path in refresh_paths - if path.exists() and self._watched_file_really_changed(path) - } - changed_paths = { - path.resolve() - for path in self.pending_changed_files - if path.exists() - } | signature_changed_paths - affected_documents = self._documents_affected_by_changed_paths( - changed_paths, - open_paths=open_paths, - reference_paths_by_document=reference_paths_by_document, - ) - self.pending_changed_files.clear() - if affected_documents: - self._refresh_open_document_tabs(affected_documents) - if ( - self._auto_refresh_allows("directory") - and any(self._is_supported_document_path(path) for path in affected_documents) - ): - self._refresh_document_entries() - else: - self._update_watched_paths() def _refresh_document_entries(self, force: bool = False, directory_signature: tuple[tuple[str, int, int], ...] | None = None) -> None: if self._startup_scan_pending: @@ -4303,18 +4141,12 @@ class MainWindow(QMainWindow): if not self.has_project: return previous_current = self.current_file - entries = discover_documents(self.project_root, self.document_file_groups) - signature = self._document_entries_signature(entries) - if not force and not self.pending_directory_refresh and signature == self.document_entries_signature: - self.document_directory_signature = directory_signature or document_directory_signature(self.project_root, self.document_file_groups) + changed = self.catalog.scan(self.project_root, self.document_file_groups, + force=force or self.pending_directory_refresh, directory_signature=directory_signature) + if not changed: self._update_watched_paths() return - self.pending_directory_refresh = False - self.document_entries_signature = signature - self.document_directory_signature = directory_signature or document_directory_signature(self.project_root, self.document_file_groups) - self.entries = entries - self.search_index.rebuild(self.entries) should_preserve_current = ( previous_current is not None and previous_current.exists() @@ -4333,6 +4165,8 @@ class MainWindow(QMainWindow): event.ignore() return self.exports.cancel_all() + self.watch.reset() + self.directory_poll_timer.stop() self._startup_generation += 1 self._startup_scan_pending = False standby_host = self._standby_reader_host diff --git a/mdhelper/project_catalog.py b/mdhelper/project_catalog.py new file mode 100644 index 0000000..8f7059a --- /dev/null +++ b/mdhelper/project_catalog.py @@ -0,0 +1,48 @@ +"""Project document inventory and its search index, independent of tree widgets.""" +from pathlib import Path + +from .document_tree import DocumentEntry, discover_documents, document_directory_signature, first_heading +from .search import SearchIndex + + +class ProjectCatalog: + def __init__(self): + self.entries: list[DocumentEntry] = [] + self.entries_signature = () + self.directory_signature = () + self.search_index = SearchIndex([]) + + @staticmethod + def signature(entries): + return tuple(sorted((str(entry.path), entry.title) for entry in entries)) + + def adopt(self, entries, directory_signature=()) -> None: + self.entries = entries + self.entries_signature = self.signature(entries) + self.directory_signature = directory_signature + self.search_index.rebuild(entries) + + def scan(self, root, groups, *, force=False, directory_signature=None) -> bool: + entries = discover_documents(root, groups) + signature = self.signature(entries) + self.directory_signature = directory_signature or document_directory_signature(root, groups) + if not force and signature == self.entries_signature: + return False + self.adopt(entries, self.directory_signature) + return True + + def directory_changed(self, root, groups): + signature = document_directory_signature(root, groups) + return signature if signature != self.directory_signature else None + + def ensure(self, path: Path, root: Path) -> bool: + if any(entry.path == path for entry in self.entries): + return False + title = first_heading(path) or path.stem.replace('_', ' ').title() + try: + relative = path.relative_to(root).as_posix() + except ValueError: + relative = str(path) + self.entries.append(DocumentEntry(path=path, title=title, relative_path=relative)) + self.adopt(self.entries, self.directory_signature) + return True diff --git a/tests/startup_smoke.py b/tests/startup_smoke.py new file mode 100644 index 0000000..c4168ab --- /dev/null +++ b/tests/startup_smoke.py @@ -0,0 +1,89 @@ +"""Check window/reader identity across startup and the first document load. + +Set MDHELPER_SMOKE_NATIVE=1 to run against the Windows display backend. +""" +import os +import sys +import tempfile +import time +from pathlib import Path + +NATIVE = os.environ.get("MDHELPER_SMOKE_NATIVE") == "1" +if not NATIVE: + os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") + os.environ.setdefault("QTWEBENGINE_DISABLE_SANDBOX", "1") + os.environ.setdefault("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-gpu --no-sandbox --log-level=3") +else: + os.environ.setdefault("QTWEBENGINE_CHROMIUM_FLAGS", "--log-level=3") +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +from PySide6.QtCore import QEvent, QObject, QSettings, QTimer, Qt +from PySide6.QtWidgets import QApplication +from mdhelper import main_window + + +class WindowEvents(QObject): + def __init__(self, window): + super().__init__(window) + self.events = [] + window.installEventFilter(self) + + def eventFilter(self, watched, event): + if event.type() in (QEvent.Type.Show, QEvent.Type.Hide, QEvent.Type.WinIdChange): + self.events.append(event.type()) + return False + + +def main(): + QApplication.setAttribute(Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings, True) + app = QApplication.instance() or QApplication([]) + app.setQuitOnLastWindowClosed(False) + + def wait(condition, seconds=20): + deadline = time.monotonic() + seconds + while not condition() and time.monotonic() < deadline: + app.processEvents() + time.sleep(.01) + assert condition(), "Startup did not finish" + + with tempfile.TemporaryDirectory(prefix="mdhelper-startup-") as temporary: + root = Path(temporary) + settings = QSettings(str(root / "settings.ini"), QSettings.Format.IniFormat) + main_window.QSettings = lambda *_: settings + note = root / "first.md" + note.write_text("# First document\n\nFirst **formatted** paragraph.", encoding="utf-8") + window = main_window.MainWindow(None, app_root=ROOT, defer_startup=True) + window.prepare_reader_before_show() + host = window.tabs.currentWidget() + reader = host.reader_view + assert reader is not None + parent = reader.parent() + events = WindowEvents(window) + try: + window.show() + native_id = int(window.winId()) + # Inspect late startup callbacks as well as the immediate show. + settled = [] + QTimer.singleShot(1200, lambda: settled.append(True)) + wait(lambda: bool(settled)) + startup_events = list(events.events) + load = [] + reader.loadFinished.connect(load.append) + window.open_document(note) + wait(lambda: bool(load) and load[-1]) + assert window.isVisible() and int(window.winId()) == native_id + assert window.tabs.currentWidget() is host and host.reader_view is reader + assert reader.parent() is parent and host.stack.currentWidget() is reader + assert events.events.count(QEvent.Type.Show) == 1, events.events + assert QEvent.Type.Hide not in events.events, events.events + assert events.events == startup_events, events.events + print("Startup passed: one show, no hide, stable native window, fixed reader host and browser parent.") + finally: + window.close() + app.processEvents() + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tests/test_document_resources.py b/tests/test_document_resources.py index 68dfc50..e20b31a 100644 --- a/tests/test_document_resources.py +++ b/tests/test_document_resources.py @@ -15,7 +15,8 @@ def test_resource_consumers_agree_on_examples_and_references(tmp_path): assert [(r.url, r.line) for r in analyze_document(source).references] == [("real.png", 5)] assert check_markdown_file(path) == [] assert check_export(source, path, "html") == [] - assert MainWindow._referenced_local_paths(None, path) == {tmp_path / "real.png"} + from mdhelper.file_watch import FileWatchService + assert FileWatchService.referenced_local_paths(path) == {tmp_path / "real.png"} def test_images_inside_links_and_html_attributes_have_correct_spans(tmp_path): diff --git a/tests/test_project_services.py b/tests/test_project_services.py new file mode 100644 index 0000000..21b093f --- /dev/null +++ b/tests/test_project_services.py @@ -0,0 +1,65 @@ +from mdhelper.document_session import DocumentStore +from mdhelper.file_watch import FileWatchService, WatchContext +from mdhelper.project_catalog import ProjectCatalog + + +def test_catalog_updates_titles_search_and_removed_documents(tmp_path): + note = tmp_path / "note.md" + note.write_text("# First\nold text", encoding="utf-8") + catalog = ProjectCatalog() + assert catalog.scan(tmp_path, ("markdown",)) + assert catalog.search_index.search("old text") + assert not catalog.scan(tmp_path, ("markdown",)) + note.write_text("# New title\nnew text", encoding="utf-8") + assert catalog.scan(tmp_path, ("markdown",)) + assert catalog.entries[0].title == "New title" + assert catalog.search_index.search("new text") + note.unlink() + assert catalog.scan(tmp_path, ("markdown",)) + assert not catalog.entries and not catalog.search_index.search("new text") + + +def test_monitor_distinguishes_internal_external_and_resource_changes(qt_application, tmp_path): + note, image = tmp_path / "note.md", tmp_path / "picture.png" + note.write_text("# Note\n![image](picture.png)", encoding="utf-8") + image.write_bytes(b"image") + mode = ["resources"] + refreshed, trees = [], [] + monitor = FileWatchService(None, WatchContext( + lambda: tmp_path, lambda: mode[0], lambda: {note}, lambda p: p.suffix == ".md", + lambda: False, refreshed.append, lambda: trees.append(True)), DocumentStore.signature) + monitor.file_watcher.blockSignals(True) # Deliver deterministic events below. + try: + monitor.update_paths() + assert image in monitor.watched_file_signatures + monitor.internal_save_paths.add(note) + note.write_text("# Note saved\n![image](picture.png)", encoding="utf-8") + monitor.on_file_changed(str(note)) + monitor.refresh_changed_documents() + assert not refreshed and not monitor.pending_changed_files + + note.write_text("# Externally changed\n![image](picture.png)", encoding="utf-8") + monitor.on_file_changed(str(note)) + assert monitor.refresh_timer.isActive() + monitor.refresh_timer.stop() + monitor.refresh_changed_documents() + assert refreshed == [{note}] and not trees + + image.write_bytes(b"changed image data") + monitor.on_file_changed(str(image)) + monitor.refresh_timer.stop() + monitor.refresh_changed_documents() + assert refreshed == [{note}, {note}] + + mode[0] = "current" + monitor.update_paths() + assert image not in monitor.watched_file_signatures + assert not monitor.affected_documents({image}) + monitor.refresh_timer.start() + monitor.tree_refresh_timer.start() + monitor.reset() + assert not monitor.refresh_timer.isActive() and not monitor.tree_refresh_timer.isActive() + assert not monitor.file_watcher.files() and not monitor.file_watcher.directories() + finally: + monitor.reset() +