feat: add Fortran support and tab controls
This commit is contained in:
@@ -4,7 +4,7 @@ import ast
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from .document_tree import CPP_EXTENSIONS, PYTHON_EXTENSIONS
|
||||
from .document_tree import CPP_EXTENSIONS, FORTRAN_EXTENSIONS, PYTHON_EXTENSIONS
|
||||
from .markdown_utils import Heading
|
||||
from .text_io import DEFAULT_TEXT_PREVIEW_BYTES, read_text_with_fallback
|
||||
|
||||
@@ -13,6 +13,14 @@ _CPP_FUNCTION_RE = re.compile(
|
||||
r"^\s*(?:template\s*<[^;{}]+>\s*)?(?:[A-Za-z_~][\w:<>~*&\s,]*\s+)?([A-Za-z_~][\w:]*)\s*\([^;{}]*\)\s*(?:const\s*)?(?:noexcept\s*)?(?:->\s*[^{};]+\s*)?(?:\{|$)"
|
||||
)
|
||||
_CPP_CONTROL_WORDS = {"if", "for", "while", "switch", "catch", "return", "sizeof"}
|
||||
_FORTRAN_PROGRAM_RE = re.compile(r"^\s*program\s+([A-Za-z_]\w*)\b", re.IGNORECASE)
|
||||
_FORTRAN_MODULE_RE = re.compile(r"^\s*module\s+(?!procedure\b)([A-Za-z_]\w*)\b", re.IGNORECASE)
|
||||
_FORTRAN_SUBROUTINE_RE = re.compile(r"^\s*(?:(?:recursive|pure|elemental|impure)\s+)*subroutine\s+([A-Za-z_]\w*)\b", re.IGNORECASE)
|
||||
_FORTRAN_FUNCTION_RE = re.compile(
|
||||
r"^\s*(?:(?:recursive|pure|elemental|impure)\s+)*(?:(?:type|class)\s*\([^)]*\)\s+|(?:integer|real|double\s+precision|complex|logical|character)\b(?:\s*\([^)]*\))?\s+)?function\s+([A-Za-z_]\w*)\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
_FORTRAN_TYPE_RE = re.compile(r"^\s*type\s*(?:(?:,\s*[^:]*)?::\s*|\s+)([A-Za-z_]\w*)\b", re.IGNORECASE)
|
||||
|
||||
|
||||
def extract_code_headings(path: Path, source: str | None = None) -> list[Heading]:
|
||||
@@ -23,6 +31,8 @@ def extract_code_headings(path: Path, source: str | None = None) -> list[Heading
|
||||
return _extract_python_headings(source)
|
||||
if suffix in CPP_EXTENSIONS:
|
||||
return _extract_cpp_headings(source)
|
||||
if suffix in FORTRAN_EXTENSIONS:
|
||||
return _extract_fortran_headings(source)
|
||||
return []
|
||||
|
||||
|
||||
@@ -78,3 +88,27 @@ def _extract_cpp_headings(source: str) -> list[Heading]:
|
||||
headings.append(Heading(level=2, title=f"function {func_match.group(1)}", slug=f"line-{lineno}"))
|
||||
pending_template = False
|
||||
return headings
|
||||
|
||||
|
||||
def _extract_fortran_headings(source: str) -> list[Heading]:
|
||||
headings: list[Heading] = []
|
||||
matchers = (
|
||||
("program", _FORTRAN_PROGRAM_RE, 1),
|
||||
("module", _FORTRAN_MODULE_RE, 1),
|
||||
("type", _FORTRAN_TYPE_RE, 2),
|
||||
("subroutine", _FORTRAN_SUBROUTINE_RE, 2),
|
||||
("function", _FORTRAN_FUNCTION_RE, 2),
|
||||
)
|
||||
|
||||
for lineno, line in enumerate(source.splitlines(), start=1):
|
||||
stripped = line.strip()
|
||||
lowered = stripped.lower()
|
||||
if not stripped or lowered.startswith("!") or lowered.startswith("end "):
|
||||
continue
|
||||
for kind, pattern, level in matchers:
|
||||
match = pattern.match(line)
|
||||
if match:
|
||||
headings.append(Heading(level=level, title=f"{kind} {match.group(1)}", slug=f"line-{lineno}"))
|
||||
break
|
||||
return headings
|
||||
|
||||
|
||||
@@ -11,15 +11,17 @@ from .text_io import DEFAULT_TEXT_PREVIEW_BYTES, is_within_size_limit, read_text
|
||||
|
||||
PYTHON_EXTENSIONS = {".py", ".pyw", ".pyi"}
|
||||
CPP_EXTENSIONS = {".c", ".cc", ".cxx", ".cpp", ".h", ".hh", ".hxx", ".hpp"}
|
||||
FORTRAN_EXTENSIONS = {".f", ".for", ".f90", ".f95"}
|
||||
|
||||
FILE_GROUP_EXTENSIONS = {
|
||||
"markdown": MARKDOWN_EXTENSIONS | WEBPAGE_EXTENSIONS,
|
||||
"python": PYTHON_EXTENSIONS,
|
||||
"cpp": CPP_EXTENSIONS,
|
||||
"fortran": FORTRAN_EXTENSIONS,
|
||||
}
|
||||
DEFAULT_DOCUMENT_FILE_GROUPS = ("markdown",)
|
||||
SUPPORTED_DOCUMENT_EXTENSIONS = set().union(*FILE_GROUP_EXTENSIONS.values())
|
||||
CODE_DOCUMENT_EXTENSIONS = PYTHON_EXTENSIONS | CPP_EXTENSIONS
|
||||
CODE_DOCUMENT_EXTENSIONS = PYTHON_EXTENSIONS | CPP_EXTENSIONS | FORTRAN_EXTENSIONS
|
||||
MAX_DISCOVERABLE_CODE_BYTES = DEFAULT_TEXT_PREVIEW_BYTES
|
||||
|
||||
EXCLUDED_DIR_NAMES = {
|
||||
@@ -80,6 +82,8 @@ def code_language_for_path(path: Path) -> str:
|
||||
return "c"
|
||||
if suffix in CPP_EXTENSIONS:
|
||||
return "cpp"
|
||||
if suffix in FORTRAN_EXTENSIONS:
|
||||
return "fortran"
|
||||
return "text"
|
||||
|
||||
|
||||
@@ -102,6 +106,29 @@ def first_heading(path: Path) -> str | None:
|
||||
return headings[0].title if headings else None
|
||||
|
||||
|
||||
|
||||
def document_directory_signature(project_root: Path, enabled_groups: object = None) -> tuple[tuple[str, int, int], ...]:
|
||||
root = project_root.resolve()
|
||||
supported_extensions = document_extensions_for_groups(enabled_groups)
|
||||
signatures: list[tuple[str, int, int]] = []
|
||||
|
||||
for path in _iter_project_files(root):
|
||||
suffix = path.suffix.lower()
|
||||
if suffix not in supported_extensions:
|
||||
continue
|
||||
try:
|
||||
stat = path.stat()
|
||||
if suffix in CODE_DOCUMENT_EXTENSIONS and stat.st_size > MAX_DISCOVERABLE_CODE_BYTES:
|
||||
continue
|
||||
relative_path = path.relative_to(root).as_posix()
|
||||
except OSError:
|
||||
continue
|
||||
except ValueError:
|
||||
continue
|
||||
signatures.append((relative_path, stat.st_mtime_ns, stat.st_size))
|
||||
|
||||
return tuple(sorted(signatures))
|
||||
|
||||
def discover_documents(project_root: Path, enabled_groups: object = None) -> list[DocumentEntry]:
|
||||
root = project_root.resolve()
|
||||
supported_extensions = document_extensions_for_groups(enabled_groups)
|
||||
@@ -151,3 +178,4 @@ def _iter_project_files(root: Path) -> Iterator[Path]:
|
||||
|
||||
def _is_excluded_name(name: str) -> bool:
|
||||
return name.startswith(".") or name in EXCLUDED_DIR_NAMES
|
||||
|
||||
|
||||
+153
-25
@@ -71,6 +71,7 @@ from .document_tree import (
|
||||
DocumentEntry,
|
||||
code_language_for_path,
|
||||
discover_documents,
|
||||
document_directory_signature,
|
||||
document_extensions_for_groups,
|
||||
first_heading,
|
||||
is_code_document,
|
||||
@@ -93,6 +94,7 @@ SCROLL_POSITIONS_SETTING_KEY = "reader/scroll_positions"
|
||||
EXTERNAL_APPLICATIONS_SETTING_KEY = "external_apps/by_extension"
|
||||
EDITOR_DRAFTS_SETTING_KEY = "editor/drafts"
|
||||
LAST_PROJECT_ROOT_SETTING_KEY = "project/last_root"
|
||||
DIRECTORY_POLL_INTERVAL_MS = 1000
|
||||
CUSTOM_DOCUMENT_STYLES_SETTING_KEY = "reader/custom_document_styles"
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
THEME_COLOR_VALUES = {"white", "black", "dark_gray", "colorful"}
|
||||
@@ -543,6 +545,7 @@ class MainWindow(QMainWindow):
|
||||
self.pending_changed_files: set[Path] = set()
|
||||
self.pending_directory_refresh = False
|
||||
self.document_entries_signature = self._document_entries_signature(self.entries)
|
||||
self.document_directory_signature = document_directory_signature(self.project_root, self.document_file_groups) if self.has_project else 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)
|
||||
@@ -557,6 +560,10 @@ class MainWindow(QMainWindow):
|
||||
self.tree_refresh_timer.setInterval(self.auto_refresh_delay_ms)
|
||||
self.tree_refresh_timer.timeout.connect(self._refresh_document_entries)
|
||||
|
||||
self.directory_poll_timer = QTimer(self)
|
||||
self.directory_poll_timer.setInterval(DIRECTORY_POLL_INTERVAL_MS)
|
||||
self.directory_poll_timer.timeout.connect(self._poll_directory_changes)
|
||||
|
||||
self.scroll_capture_timer = QTimer(self)
|
||||
self.scroll_capture_timer.setInterval(1500)
|
||||
self.scroll_capture_timer.timeout.connect(self._save_current_scroll_position)
|
||||
@@ -570,6 +577,7 @@ class MainWindow(QMainWindow):
|
||||
if self.has_project:
|
||||
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._apply_auto_refresh_timing()
|
||||
self.setWindowTitle("MDHelper")
|
||||
@@ -862,6 +870,8 @@ class MainWindow(QMainWindow):
|
||||
tab_bar.setExpanding(False)
|
||||
tab_bar.setUsesScrollButtons(True)
|
||||
tab_bar.setElideMode(Qt.TextElideMode.ElideRight)
|
||||
tab_bar.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
tab_bar.customContextMenuRequested.connect(self._show_tab_context_menu)
|
||||
if hasattr(tab_bar, "setChangeCurrentOnDrag"):
|
||||
tab_bar.setChangeCurrentOnDrag(False)
|
||||
|
||||
@@ -1878,6 +1888,101 @@ class MainWindow(QMainWindow):
|
||||
self._update_watched_paths()
|
||||
self._update_actions()
|
||||
|
||||
def _is_content_tab_index(self, index: int) -> bool:
|
||||
if not 0 <= index < self.tabs.count():
|
||||
return False
|
||||
widget = self.tabs.widget(index)
|
||||
return widget is not None and not bool(widget.property("is_welcome"))
|
||||
|
||||
def _close_tab_indexes(self, indexes: list[int], show_welcome: bool = True) -> bool:
|
||||
target_indexes = sorted({index for index in indexes if self._is_content_tab_index(index)}, reverse=True)
|
||||
if not target_indexes:
|
||||
if show_welcome:
|
||||
self._ensure_welcome_if_empty()
|
||||
return True
|
||||
|
||||
for index in target_indexes:
|
||||
widget = self.tabs.widget(index)
|
||||
if isinstance(widget, RichMarkdownEditorDialog) and not widget.confirm_close():
|
||||
return False
|
||||
|
||||
for index in target_indexes:
|
||||
widget = self.tabs.widget(index)
|
||||
if isinstance(widget, VideoPlayerWidget):
|
||||
widget.stop()
|
||||
self.tabs.removeTab(index)
|
||||
if widget is not None:
|
||||
widget.deleteLater()
|
||||
|
||||
if show_welcome:
|
||||
self._ensure_welcome_if_empty()
|
||||
if self.tabs.count() > 0:
|
||||
self._on_tab_changed(self.tabs.currentIndex())
|
||||
else:
|
||||
self.current_file = None
|
||||
self.current_fragment = ""
|
||||
self._update_actions()
|
||||
return True
|
||||
|
||||
def _close_other_tabs(self, keep_index: int) -> bool:
|
||||
keep_widget = self.tabs.widget(keep_index) if 0 <= keep_index < self.tabs.count() else None
|
||||
close_indexes = [index for index in range(self.tabs.count()) if index != keep_index and self._is_content_tab_index(index)]
|
||||
if not self._close_tab_indexes(close_indexes, show_welcome=False):
|
||||
return False
|
||||
if keep_widget is not None:
|
||||
new_index = self.tabs.indexOf(keep_widget)
|
||||
if new_index >= 0:
|
||||
self.tabs.setCurrentIndex(new_index)
|
||||
self._ensure_welcome_if_empty()
|
||||
return True
|
||||
|
||||
def _show_tab_context_menu(self, position: QPoint) -> None:
|
||||
tab_bar = self.tabs.tabBar()
|
||||
tab_index = tab_bar.tabAt(position)
|
||||
if not self._is_content_tab_index(tab_index):
|
||||
return
|
||||
|
||||
menu = QMenu(self)
|
||||
close_action = QAction("关闭", menu)
|
||||
close_all_action = QAction("关闭所有文件", menu)
|
||||
close_others_action = QAction("关闭其他文件", menu)
|
||||
|
||||
content_tab_count = sum(1 for index in range(self.tabs.count()) if self._is_content_tab_index(index))
|
||||
close_others_action.setEnabled(content_tab_count > 1)
|
||||
|
||||
close_action.triggered.connect(lambda checked=False, index=tab_index: self._close_tab(index))
|
||||
close_all_action.triggered.connect(lambda checked=False: self._close_tab_indexes(list(range(self.tabs.count()))))
|
||||
close_others_action.triggered.connect(lambda checked=False, index=tab_index: self._close_other_tabs(index))
|
||||
|
||||
menu.addAction(close_action)
|
||||
menu.addAction(close_all_action)
|
||||
menu.addAction(close_others_action)
|
||||
menu.exec(tab_bar.mapToGlobal(position))
|
||||
|
||||
def _close_all_tabs(self, show_welcome: bool = True) -> bool:
|
||||
for index in range(self.tabs.count() - 1, -1, -1):
|
||||
widget = self.tabs.widget(index)
|
||||
if isinstance(widget, RichMarkdownEditorDialog) and not widget.confirm_close():
|
||||
return False
|
||||
|
||||
for index in range(self.tabs.count() - 1, -1, -1):
|
||||
widget = self.tabs.widget(index)
|
||||
if isinstance(widget, VideoPlayerWidget):
|
||||
widget.stop()
|
||||
self.tabs.removeTab(index)
|
||||
if widget is not None:
|
||||
widget.deleteLater()
|
||||
|
||||
self.current_file = None
|
||||
self.current_fragment = ""
|
||||
self.pending_fragment = ""
|
||||
self.pending_find_text = ""
|
||||
self.pending_scroll_y = None
|
||||
if hasattr(self, "toc_tree"):
|
||||
self.toc_tree.clear()
|
||||
if show_welcome:
|
||||
self._ensure_welcome_if_empty()
|
||||
return True
|
||||
def _close_tab(self, index: int) -> None:
|
||||
widget = self.tabs.widget(index)
|
||||
if isinstance(widget, RichMarkdownEditorDialog) and not widget.confirm_close():
|
||||
@@ -1933,7 +2038,7 @@ class MainWindow(QMainWindow):
|
||||
)
|
||||
|
||||
def _is_text_file(self, path: Path) -> bool:
|
||||
if path.suffix.lower() in {".txt", ".log", ".csv", ".tsv", ".json", ".xml", ".html", ".htm", ".py", ".pyw", ".pyi", ".c", ".cc", ".cxx", ".cpp", ".h", ".hh", ".hxx", ".hpp"}:
|
||||
if path.suffix.lower() in {".txt", ".log", ".csv", ".tsv", ".json", ".xml", ".html", ".htm", ".py", ".pyw", ".pyi", ".c", ".cc", ".cxx", ".cpp", ".h", ".hh", ".hxx", ".hpp", ".f", ".for", ".f90", ".f95"}:
|
||||
return True
|
||||
try:
|
||||
chunk = path.read_bytes()[:2048]
|
||||
@@ -1964,7 +2069,7 @@ class MainWindow(QMainWindow):
|
||||
color = "#ffb36b" if self._tree_is_dark_theme() else "#b85600"
|
||||
elif extension in {".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx"}:
|
||||
color = "#f28c8c" if self._tree_is_dark_theme() else "#b3261e"
|
||||
elif extension in {".py", ".pyw", ".pyi", ".c", ".cc", ".cxx", ".cpp", ".h", ".hh", ".hxx", ".hpp", ".java", ".go", ".rs", ".cs"}:
|
||||
elif extension in {".py", ".pyw", ".pyi", ".c", ".cc", ".cxx", ".cpp", ".h", ".hh", ".hxx", ".hpp", ".f", ".for", ".f90", ".f95", ".java", ".go", ".rs", ".cs"}:
|
||||
color = "#97d679" if self._tree_is_dark_theme() else "#417b27"
|
||||
else:
|
||||
color = "#c7cdd7" if self._tree_is_dark_theme() else "#4b5563"
|
||||
@@ -2025,6 +2130,10 @@ class MainWindow(QMainWindow):
|
||||
"cc": "C++",
|
||||
"cxx": "C++",
|
||||
"cpp": "C++",
|
||||
"f": "Fortran",
|
||||
"for": "Fortran",
|
||||
"f90": "Fortran",
|
||||
"f95": "Fortran",
|
||||
"h": "H",
|
||||
"hh": "H++",
|
||||
"hxx": "H++",
|
||||
@@ -2209,25 +2318,19 @@ class MainWindow(QMainWindow):
|
||||
folder = QFileDialog.getExistingDirectory(self, "打开文件夹", start_dir)
|
||||
if not folder:
|
||||
return
|
||||
self.set_project_root(Path(folder), save=True)
|
||||
self.status_label.setText(f"已打开文件夹:{self.project_root}")
|
||||
if self.set_project_root(Path(folder), save=True):
|
||||
self.status_label.setText(f"已打开文件夹:{self.project_root}")
|
||||
|
||||
def close_project_folder(self) -> None:
|
||||
for index in range(self.tabs.count() - 1, -1, -1):
|
||||
widget = self.tabs.widget(index)
|
||||
if isinstance(widget, RichMarkdownEditorDialog) and not widget.confirm_close():
|
||||
return
|
||||
if isinstance(widget, VideoPlayerWidget):
|
||||
widget.stop()
|
||||
self.tabs.removeTab(index)
|
||||
if widget is not None:
|
||||
widget.deleteLater()
|
||||
if not self._close_all_tabs(show_welcome=False):
|
||||
return
|
||||
|
||||
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.path_to_item.clear()
|
||||
self.current_file = None
|
||||
@@ -2243,6 +2346,7 @@ class MainWindow(QMainWindow):
|
||||
self.settings.remove(LAST_PROJECT_ROOT_SETTING_KEY)
|
||||
self._populate_tree()
|
||||
self._update_watched_paths()
|
||||
self._apply_auto_refresh_timing()
|
||||
self._show_welcome_tab()
|
||||
self.setWindowTitle("MDHelper")
|
||||
self.status_label.setText("已关闭文件夹")
|
||||
@@ -2253,11 +2357,12 @@ class MainWindow(QMainWindow):
|
||||
self,
|
||||
"打开文件",
|
||||
str(self.project_root if self.has_project else Path.home()),
|
||||
"Supported Files (*.md *.markdown *.html *.htm *.xhtml *.py *.pyw *.pyi *.c *.cc *.cxx *.cpp *.h *.hh *.hxx *.hpp);;All Files (*)",
|
||||
"Supported Files (*.md *.markdown *.html *.htm *.xhtml *.py *.pyw *.pyi *.c *.cc *.cxx *.cpp *.h *.hh *.hxx *.hpp *.f *.for *.f90 *.f95);;All Files (*)",
|
||||
)
|
||||
if file_name:
|
||||
path = Path(file_name).resolve()
|
||||
self.set_project_root(path.parent, save=True)
|
||||
if not self.set_project_root(path.parent, save=True):
|
||||
return
|
||||
if path.suffix.lower() in {".md", ".markdown"} or is_code_document(path):
|
||||
self._ensure_document_entry(path)
|
||||
self.open_document(path)
|
||||
@@ -2267,20 +2372,30 @@ class MainWindow(QMainWindow):
|
||||
else:
|
||||
self._open_local_file_now(path)
|
||||
|
||||
def set_project_root(self, project_root: Path, save: bool = True) -> None:
|
||||
def set_project_root(self, project_root: Path, save: bool = True) -> bool:
|
||||
project_root = project_root.resolve()
|
||||
root_changed = self.has_project and project_root != self.project_root.resolve()
|
||||
if root_changed and not self._close_all_tabs(show_welcome=False):
|
||||
return False
|
||||
|
||||
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.path_to_item.clear()
|
||||
self._populate_tree()
|
||||
self._filter_tree(self.search_box.text())
|
||||
self.pending_directory_refresh = False
|
||||
self._update_watched_paths()
|
||||
self._apply_auto_refresh_timing()
|
||||
if root_changed:
|
||||
self._ensure_welcome_if_empty()
|
||||
if save:
|
||||
self.settings.setValue(LAST_PROJECT_ROOT_SETTING_KEY, str(self.project_root))
|
||||
return True
|
||||
|
||||
def edit_current_markdown(self) -> None:
|
||||
path = self.current_file
|
||||
@@ -4471,13 +4586,23 @@ class MainWindow(QMainWindow):
|
||||
self.refresh_timer.setInterval(self.auto_refresh_delay_ms)
|
||||
if hasattr(self, "tree_refresh_timer"):
|
||||
self.tree_refresh_timer.setInterval(self.auto_refresh_delay_ms)
|
||||
if hasattr(self, "directory_poll_timer"):
|
||||
self.directory_poll_timer.setInterval(DIRECTORY_POLL_INTERVAL_MS)
|
||||
if not self.auto_refresh_enabled or self.auto_refresh_mode == "off":
|
||||
if hasattr(self, "refresh_timer"):
|
||||
self.refresh_timer.stop()
|
||||
if hasattr(self, "tree_refresh_timer"):
|
||||
self.tree_refresh_timer.stop()
|
||||
if hasattr(self, "directory_poll_timer"):
|
||||
self.directory_poll_timer.stop()
|
||||
self.pending_changed_files.clear()
|
||||
self.pending_directory_refresh = False
|
||||
return
|
||||
if hasattr(self, "directory_poll_timer"):
|
||||
if self.has_project and self._auto_refresh_allows("directory"):
|
||||
self.directory_poll_timer.start()
|
||||
else:
|
||||
self.directory_poll_timer.stop()
|
||||
|
||||
def _save_preferences(self) -> None:
|
||||
self.settings.setValue("reader/theme", self.theme)
|
||||
@@ -4627,6 +4752,15 @@ class MainWindow(QMainWindow):
|
||||
if dir_paths:
|
||||
self.file_watcher.addPaths(dir_paths)
|
||||
|
||||
def _poll_directory_changes(self) -> None:
|
||||
if not self.has_project or not self._auto_refresh_allows("directory"):
|
||||
return
|
||||
signature = document_directory_signature(self.project_root, self.document_file_groups)
|
||||
if signature == self.document_directory_signature:
|
||||
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))
|
||||
|
||||
@@ -4829,18 +4963,20 @@ class MainWindow(QMainWindow):
|
||||
else:
|
||||
self._update_watched_paths()
|
||||
|
||||
def _refresh_document_entries(self, force: bool = False) -> None:
|
||||
def _refresh_document_entries(self, force: bool = False, directory_signature: tuple[tuple[str, int, int], ...] | None = None) -> None:
|
||||
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)
|
||||
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 = (
|
||||
@@ -4865,11 +5001,3 @@ class MainWindow(QMainWindow):
|
||||
super().showEvent(event)
|
||||
QTimer.singleShot(0, self._apply_navigation_style)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -133,6 +133,8 @@ class SettingsDialog(QDialog):
|
||||
self.python_files_check.setChecked("python" in enabled_groups)
|
||||
self.cpp_files_check = QCheckBox("C / C++")
|
||||
self.cpp_files_check.setChecked("cpp" in enabled_groups)
|
||||
self.fortran_files_check = QCheckBox("Fortran")
|
||||
self.fortran_files_check.setChecked("fortran" in enabled_groups)
|
||||
|
||||
file_types_widget = QWidget()
|
||||
file_types_layout = QVBoxLayout(file_types_widget)
|
||||
@@ -141,6 +143,7 @@ class SettingsDialog(QDialog):
|
||||
file_types_layout.addWidget(self.markdown_files_check)
|
||||
file_types_layout.addWidget(self.python_files_check)
|
||||
file_types_layout.addWidget(self.cpp_files_check)
|
||||
file_types_layout.addWidget(self.fortran_files_check)
|
||||
|
||||
form = QFormLayout()
|
||||
form.addRow("主题颜色", self.theme_color_combo)
|
||||
@@ -175,6 +178,8 @@ class SettingsDialog(QDialog):
|
||||
document_file_groups.append("python")
|
||||
if self.cpp_files_check.isChecked():
|
||||
document_file_groups.append("cpp")
|
||||
if self.fortran_files_check.isChecked():
|
||||
document_file_groups.append("fortran")
|
||||
|
||||
return AppPreferences(
|
||||
theme="dark" if theme_color in {"black", "dark_gray"} else "light",
|
||||
@@ -205,4 +210,3 @@ class SettingsDialog(QDialog):
|
||||
if not enabled:
|
||||
self.theme_background_combo.setCurrentIndex(max(0, self.theme_background_combo.findData("none")))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user