From 28959d9c4d0bf264e269fd0535b3cb3d78ef1782 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Wed, 4 Feb 2026 20:06:47 +0000 Subject: [PATCH] Add detector modules --- confsync/detectors/editor.py | 161 +++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 confsync/detectors/editor.py diff --git a/confsync/detectors/editor.py b/confsync/detectors/editor.py new file mode 100644 index 0000000..193f18b --- /dev/null +++ b/confsync/detectors/editor.py @@ -0,0 +1,161 @@ +"""Editor configuration file detectors.""" + +from pathlib import Path +from typing import List, TYPE_CHECKING + +from confsync.detectors.base import BaseDetector, detector +from confsync.models.config_models import ConfigCategory + +if TYPE_CHECKING: + from confsync.models.config_models import ConfigFile + + +@detector +class VSCodeDetector(BaseDetector): + """Detector for Visual Studio Code configurations.""" + + category = ConfigCategory.EDITOR + tool_name = "vscode" + + def get_default_locations(self) -> List[str]: + """Get VS Code config locations.""" + locations = [ + Path.home() / ".vscode", + Path.home() / ".config" / "Code", + Path.home() / "Library" / "Application Support" / "Code", + ] + if "APPDATA" in __import__('os').environ: + locations.append(Path(__import__('os').environ['APPDATA']) / "Code") + return [str(loc) for loc in locations] + + def get_file_patterns(self) -> List[str]: + """Get VS Code file patterns.""" + return ["settings.json", "keybindings.json"] + + def detect(self) -> List["ConfigFile"]: + """Detect VS Code configuration files.""" + configs = [] + home = Path.home() + + vscode_paths = [ + home / ".vscode" / "settings.json", + home / ".vscode" / "keybindings.json", + home / ".config" / "Code" / "User" / "settings.json", + home / ".config" / "Code" / "User" / "keybindings.json", + home / "Library" / "Application Support" / "Code" / "User" / "settings.json", + home / "Library" / "Application Support" / "Code" / "User" / "keybindings.json", + ] + + if "APPDATA" in __import__('os').environ: + appdata = Path(__import__('os').environ['APPDATA']) + vscode_paths.extend([ + appdata / "Code" / "User" / "settings.json", + appdata / "Code" / "User" / "keybindings.json", + ]) + + for vscode_path in vscode_paths: + if vscode_path.exists(): + config = self._create_config_file(str(vscode_path)) + config.metadata = {"editor": "vscode"} + configs.append(config) + + return configs + + +@detector +class VimDetector(BaseDetector): + """Detector for Vim configurations.""" + + category = ConfigCategory.EDITOR + tool_name = "vim" + + def get_default_locations(self) -> List[str]: + """Get Vim config locations.""" + return [str(Path.home() / ".vim"), str(Path.home() / ".vimrc")] + + def get_file_patterns(self) -> List[str]: + """Get Vim file patterns.""" + return [".vimrc", ".vimrc.*", "vimrc", "*.vim"] + + def detect(self) -> List["ConfigFile"]: + """Detect Vim configuration files.""" + configs = [] + home = Path.home() + + vimrc_paths = [home / ".vimrc", home / ".vim" / "vimrc", home / "_vimrc"] + + for vimrc_path in vimrc_paths: + if vimrc_path.exists(): + config = self._create_config_file(str(vimrc_path)) + config.metadata = {"editor": "vim"} + configs.append(config) + + vim_dir = home / ".vim" + if vim_dir.exists() and vim_dir.is_dir(): + vim_files = ["colors", "plugin", "autoload", "ftplugin", "after"] + for vf in vim_files: + vf_path = vim_dir / vf + if vf_path.exists(): + if vf_path.is_file(): + config = self._create_config_file(str(vf_path)) + config.metadata = {"editor": "vim", "subdirectory": vf} + configs.append(config) + + return configs + + +@detector +class NeovimDetector(BaseDetector): + """Detector for Neovim configurations.""" + + category = ConfigCategory.EDITOR + tool_name = "neovim" + + def get_default_locations(self) -> List[str]: + """Get Neovim config locations.""" + return [ + str(Path.home() / ".config" / "nvim"), + str(Path.home() / ".nvim"), + str(Path.home() / ".nvimrc"), + ] + + def get_file_patterns(self) -> List[str]: + """Get Neovim file patterns.""" + return ["init.vim", "init.lua", "init.nvim"] + + def detect(self) -> List["ConfigFile"]: + """Detect Neovim configuration files.""" + configs = [] + home = Path.home() + + xdg_config = home / ".config" + nvim_xdg = xdg_config / "nvim" if xdg_config.exists() else None + nvim_home = home / ".nvim" + nvimrc = home / ".nvimrc" + + nvim_paths = [ + nvim_xdg / "init.lua" if nvim_xdg else None, + nvim_xdg / "init.vim" if nvim_xdg else None, + nvim_home / "init.lua" if nvim_home.exists() else None, + nvim_home / "init.vim" if nvim_home.exists() else None, + nvimrc if nvimrc.exists() else None, + ] + + for nvim_path in filter(None, nvim_paths): + if nvim_path.exists(): + config = self._create_config_file(str(nvim_path)) + config.metadata = {"editor": "neovim"} + configs.append(config) + + nvim_dir = nvim_xdg or nvim_home + if nvim_dir and nvim_dir.exists() and nvim_dir.is_dir(): + for subdir in ["after", "plugin", "autoload", "ftplugin"]: + subdir_path = nvim_dir / subdir + if subdir_path.exists() and subdir_path.is_dir(): + for file_path in subdir_path.iterdir(): + if file_path.is_file(): + config = self._create_config_file(str(file_path)) + config.metadata = {"editor": "neovim", "subdirectory": subdir} + configs.append(config) + + return configs