|
| 1 | +# Copyright 2025- Python Language Server Contributors. |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +import pycodestyle |
| 7 | + |
| 8 | +from pylsp import hookimpl |
| 9 | +from pylsp.config.source import ConfigSource |
| 10 | + |
| 11 | + |
| 12 | +class Flake8Config(ConfigSource): |
| 13 | + """Parse flake8 configurations.""" |
| 14 | + |
| 15 | + CONFIG_KEY = "flake8" |
| 16 | + PROJECT_CONFIGS = [".flake8", "setup.cfg", "tox.ini"] |
| 17 | + USER_CONFIGS = ( |
| 18 | + [os.path.expanduser("~\\.flake8")] |
| 19 | + if sys.platform == "win32" else [os.path.join(ConfigSource.XDG_CONFIG_HOME, "flake8")] |
| 20 | + ) |
| 21 | + |
| 22 | + OPTIONS = [ |
| 23 | + # mccabe |
| 24 | + ("max-complexity", "plugins.mccabe.threshold", int), |
| 25 | + # pycodestyle |
| 26 | + ("exclude", "plugins.pycodestyle.exclude", list), |
| 27 | + ("filename", "plugins.pycodestyle.filename", list), |
| 28 | + ("hang-closing", "plugins.pycodestyle.hangClosing", bool), |
| 29 | + ("ignore", "plugins.pycodestyle.ignore", list), |
| 30 | + ("max-line-length", "plugins.pycodestyle.maxLineLength", int), |
| 31 | + ("indent-size", "plugins.pycodestyle.indentSize", int), |
| 32 | + ("select", "plugins.pycodestyle.select", list), |
| 33 | + # flake8 |
| 34 | + ("exclude", "plugins.flake8.exclude", list), |
| 35 | + ("extend-ignore", "plugins.flake8.extendIgnore", list), |
| 36 | + ("extend-select", "plugins.flake8.extendSelect", list), |
| 37 | + ("filename", "plugins.flake8.filename", list), |
| 38 | + ("hang-closing", "plugins.flake8.hangClosing", bool), |
| 39 | + ("ignore", "plugins.flake8.ignore", list), |
| 40 | + ("max-complexity", "plugins.flake8.maxComplexity", int), |
| 41 | + ("max-line-length", "plugins.flake8.maxLineLength", int), |
| 42 | + ("indent-size", "plugins.flake8.indentSize", int), |
| 43 | + ("select", "plugins.flake8.select", list), |
| 44 | + ("per-file-ignores", "plugins.flake8.perFileIgnores", list), |
| 45 | + ] |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def _parse_list_opt(cls, string): |
| 49 | + if string.startswith("\n"): |
| 50 | + return [s.strip().rstrip(",") for s in string.split("\n") if s.strip()] |
| 51 | + return [s.strip() for s in string.split(",") if s.strip()] |
| 52 | + |
| 53 | + |
| 54 | +class PyCodeStyleConfig(ConfigSource): |
| 55 | + CONFIG_KEY = "pycodestyle" |
| 56 | + USER_CONFIGS = [pycodestyle.USER_CONFIG] if pycodestyle.USER_CONFIG else [] |
| 57 | + PROJECT_CONFIGS = ["pycodestyle.cfg", "setup.cfg", "tox.ini"] |
| 58 | + |
| 59 | + OPTIONS = [ |
| 60 | + ("exclude", "plugins.pycodestyle.exclude", list), |
| 61 | + ("filename", "plugins.pycodestyle.filename", list), |
| 62 | + ("hang-closing", "plugins.pycodestyle.hangClosing", bool), |
| 63 | + ("ignore", "plugins.pycodestyle.ignore", list), |
| 64 | + ("max-line-length", "plugins.pycodestyle.maxLineLength", int), |
| 65 | + ("indent-size", "plugins.pycodestyle.indentSize", int), |
| 66 | + ("select", "plugins.pycodestyle.select", list), |
| 67 | + ("aggressive", "plugins.pycodestyle.aggressive", int), |
| 68 | + ] |
| 69 | + |
| 70 | + |
| 71 | +# ---- pylsp_settings hook implementation (new architecture) ---- |
| 72 | +@hookimpl |
| 73 | +def pylsp_settings(): # noqa: ARG001 (config not used yet) |
| 74 | + """Provide configuration source classes to the server. |
| 75 | +
|
| 76 | + The server will instantiate each returned class with the workspace root. |
| 77 | + Returning classes (not instances) matches the new ServerConfig collection flow. |
| 78 | + """ |
| 79 | + return [Flake8Config, PyCodeStyleConfig] |
0 commit comments