Skip to content

Commit 60cbf53

Browse files
committed
feat: add embedded plugins config source
1 parent 8b3be53 commit 60cbf53

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

pylsp/plugins/config.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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]

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pylint = "pylsp.plugins.pylint_lint"
8585
rope_completion = "pylsp.plugins.rope_completion"
8686
rope_autoimport = "pylsp.plugins.rope_autoimport"
8787
yapf = "pylsp.plugins.yapf_format"
88+
config_sources = "pylsp.plugins.config"
8889

8990
[project.scripts]
9091
pylsp = "pylsp.__main__:main"

0 commit comments

Comments
 (0)