-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolution.py
More file actions
101 lines (79 loc) · 3.4 KB
/
Copy pathresolution.py
File metadata and controls
101 lines (79 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Config instantiation and public language resolution helpers."""
from __future__ import annotations
from pathlib import Path
from . import registry_state
from .base.types import LangConfig
from .contract_validation import validate_lang_contract
from .discovery import load_all
_MARKER_GLOB_CHARS = ("*", "?", "[")
def make_lang_config(name: str, cfg_cls: type) -> LangConfig:
"""Instantiate and validate a language config."""
try:
cfg = cfg_cls()
except (TypeError, ValueError, AttributeError, RuntimeError, OSError) as ex:
raise ValueError(
f"Failed to instantiate language config '{name}': {ex}"
) from ex
validate_lang_contract(name, cfg)
return cfg
def get_lang(name: str) -> LangConfig:
"""Get a language config by name.
All plugins (full and generic) store LangConfig instances in the registry.
Test doubles that store plain classes are instantiated on demand as a fallback.
"""
if not registry_state.is_registered(name):
load_all()
if not registry_state.is_registered(name):
available = ", ".join(sorted(registry_state.all_keys()))
raise ValueError(f"Unknown language: {name!r}. Available: {available}")
obj = registry_state.get(name)
if isinstance(obj, LangConfig):
return obj
return make_lang_config(name, obj) # fallback for test doubles
def auto_detect_lang(project_root: Path) -> str | None:
"""Auto-detect language from project files.
When multiple config files are present (e.g. package.json + pyproject.toml),
counts actual source files to pick the dominant language instead of relying
on first-match ordering.
"""
load_all()
candidates: list[str] = []
configs: dict[str, LangConfig] = {}
for lang_name, obj in registry_state.all_items():
cfg = obj if isinstance(obj, LangConfig) else make_lang_config(lang_name, obj)
configs[lang_name] = cfg
markers = getattr(cfg, "detect_markers", []) or []
if markers and any(_detect_marker_exists(project_root, marker) for marker in markers):
candidates.append(lang_name)
if not candidates:
# Marker-less fallback: pick language with most source files.
best, best_count = None, 0
for lang_name, cfg in configs.items():
count = len(cfg.file_finder(project_root))
if count > best_count:
best, best_count = lang_name, count
return best if best_count > 0 else None
if len(candidates) == 1:
return candidates[0]
# Multiple candidates: choose language with most source files.
best, best_count = None, -1
for lang_name in candidates:
count = len(configs[lang_name].file_finder(project_root))
if count > best_count:
best, best_count = lang_name, count
return best
def _detect_marker_exists(project_root: Path, marker: str) -> bool:
marker_text = str(marker).strip()
if not marker_text:
return False
# Fast path for literal markers.
if (project_root / marker_text).exists():
return True
# Wildcard markers (for example "*.fsproj") are matched at project root.
if any(ch in marker_text for ch in _MARKER_GLOB_CHARS):
return any(project_root.glob(marker_text))
return False
def available_langs() -> list[str]:
"""Return list of registered language names."""
load_all()
return sorted(registry_state.all_keys())