summaryrefslogtreecommitdiffstats
path: root/src/debputy/lsp/style_prefs.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/debputy/lsp/style_prefs.py')
-rw-r--r--src/debputy/lsp/style_prefs.py96
1 files changed, 92 insertions, 4 deletions
diff --git a/src/debputy/lsp/style_prefs.py b/src/debputy/lsp/style_prefs.py
index 5dfdac2..40d850b 100644
--- a/src/debputy/lsp/style_prefs.py
+++ b/src/debputy/lsp/style_prefs.py
@@ -14,13 +14,15 @@ from typing import (
Mapping,
Self,
Dict,
+ Iterable,
+ Any,
)
from debputy.lsp.lsp_reference_keyword import ALL_PUBLIC_NAMED_STYLES
from debputy.lsp.vendoring._deb822_repro.types import FormatterCallback
from debputy.lsp.vendoring.wrap_and_sort import wrap_and_sort_formatter
from debputy.packages import SourcePackage
-from debputy.util import _error
+from debputy.util import _error, _info
from debputy.yaml import MANIFEST_YAML
from debputy.yaml.compat import CommentedMap
@@ -32,6 +34,29 @@ BUILTIN_STYLES = os.path.join(os.path.dirname(__file__), "style-preferences.yaml
_NORMALISE_FIELD_CONTENT_KEY = ["formatting", "deb822", "normalize-field-content"]
_UPLOADER_SPLIT_RE = re.compile(r"(?<=>)\s*,")
+_WAS_OPTIONS = {
+ "-a": ("formatting_deb822_always_wrap", True),
+ "--always-wrap": ("formatting_deb822_always_wrap", True),
+ "-s": ("formatting_deb822_short_indent", True),
+ "--short-indent": ("formatting_deb822_short_indent", True),
+ "-t": ("formatting_deb822_trailing_separator", True),
+ "--trailing-separator": ("formatting_deb822_trailing_separator", True),
+ # Noise option for us; we do not accept `--no-keep-first` though
+ "-k": (None, True),
+ "--keep-first": (None, True),
+ "--no-keep-first": ("DISABLE_NORMALIZE_STANZA_ORDER", True),
+ "-b": ("formatting_deb822_normalize_stanza_order", True),
+ "--sort-binary-packages": ("formatting_deb822_normalize_stanza_order", True),
+}
+
+_WAS_DEFAULTS = {
+ "formatting_deb822_always_wrap": False,
+ "formatting_deb822_short_indent": False,
+ "formatting_deb822_trailing_separator": False,
+ "formatting_deb822_normalize_stanza_order": False,
+ "formatting_deb822_normalize_field_content": True,
+}
+
@dataclasses.dataclass(slots=True, frozen=True, kw_only=True)
class PreferenceOption(Generic[PT]):
@@ -400,7 +425,6 @@ class EffectivePreference:
a_value = getattr(a, attr_name)
b_value = getattr(b, attr_name)
if a_value != b_value:
- print(f"{attr_name} was misaligned")
return None
return a
@@ -415,6 +439,9 @@ class EffectivePreference:
),
)
+ def replace(self, /, **changes: Any) -> Self:
+ return dataclasses.replace(self, **changes)
+
@dataclasses.dataclass(slots=True, frozen=True)
class MaintainerPreference(EffectivePreference):
@@ -539,14 +566,45 @@ def extract_maint_email(maint: str) -> str:
def determine_effective_style(
style_preference_table: StylePreferenceTable,
- source_package: SourcePackage,
+ source_package: Optional[SourcePackage],
+ salsa_ci: Optional[CommentedMap],
) -> Optional[EffectivePreference]:
- style = source_package.fields.get("X-Style")
+ style = source_package.fields.get("X-Style") if source_package is not None else None
if style is not None:
if style not in ALL_PUBLIC_NAMED_STYLES:
return None
return style_preference_table.named_styles.get(style)
+ if salsa_ci:
+ disable_wrap_and_sort = salsa_ci.mlget(
+ ["variables", "SALSA_CI_DISABLE_WRAP_AND_SORT"],
+ list_ok=True,
+ default=True,
+ )
+
+ if isinstance(disable_wrap_and_sort, str):
+ disable_wrap_and_sort = disable_wrap_and_sort in ("yes", "1", "true")
+ elif not isinstance(disable_wrap_and_sort, (int, bool)):
+ disable_wrap_and_sort = True
+ else:
+ disable_wrap_and_sort = (
+ disable_wrap_and_sort is True or disable_wrap_and_sort == 1
+ )
+ if not disable_wrap_and_sort:
+ wrap_and_sort_options = salsa_ci.mlget(
+ ["variables", "SALSA_CI_WRAP_AND_SORT_ARGS"],
+ list_ok=True,
+ default=None,
+ )
+ if wrap_and_sort_options is None:
+ wrap_and_sort_options = ""
+ elif not isinstance(wrap_and_sort_options, str):
+ return None
+ return parse_salsa_ci_wrap_and_sort_args(wrap_and_sort_options)
+
+ if source_package is None:
+ return None
+
maint = source_package.fields.get("Maintainer")
if maint is None:
return None
@@ -580,3 +638,33 @@ def determine_effective_style(
if isinstance(r, MaintainerPreference):
return r.as_effective_pref()
return r
+
+
+def _split_options(args: Iterable[str]) -> Iterable[str]:
+ for arg in args:
+ if arg.startswith("--"):
+ yield arg
+ continue
+ if not arg.startswith("-") or len(arg) < 2:
+ yield arg
+ continue
+ for sarg in arg[1:]:
+ yield f"-{sarg}"
+
+
+@functools.lru_cache
+def parse_salsa_ci_wrap_and_sort_args(args: str) -> Optional[EffectivePreference]:
+ options = dict(_WAS_DEFAULTS)
+ for arg in _split_options(args.split()):
+ v = _WAS_OPTIONS.get(arg)
+ if v is None:
+ return None
+ varname, value = v
+ if varname is None:
+ continue
+ options[varname] = value
+ if "DISABLE_NORMALIZE_STANZA_ORDER" in options:
+ del options["DISABLE_NORMALIZE_STANZA_ORDER"]
+ options["formatting_deb822_normalize_stanza_order"] = False
+
+ return EffectivePreference(**options) # type: ignore