3
3
4
4
import contextlib
5
5
import logging
6
- import os
7
6
import re
8
7
import sys
8
+ from pathlib import Path
9
9
10
10
import pydocstyle
11
11
17
17
pydocstyle_logger = logging .getLogger (pydocstyle .utils .__name__ )
18
18
pydocstyle_logger .setLevel (logging .INFO )
19
19
20
- DEFAULT_MATCH_RE = pydocstyle .config .ConfigurationParser .DEFAULT_MATCH_RE
21
20
DEFAULT_MATCH_DIR_RE = pydocstyle .config .ConfigurationParser .DEFAULT_MATCH_DIR_RE
22
21
23
22
@@ -33,30 +32,36 @@ def pylsp_lint(config, workspace, document):
33
32
settings = config .plugin_settings ("pydocstyle" , document_path = document .path )
34
33
log .debug ("Got pydocstyle settings: %s" , settings )
35
34
36
- # Explicitly passing a path to pydocstyle means it doesn't respect the --match flag, so do it ourselves
37
- filename_match_re = re .compile (settings .get ("match" , DEFAULT_MATCH_RE ) + "$" )
38
- if not filename_match_re .match (os .path .basename (document .path )):
39
- return []
40
-
41
- # Likewise with --match-dir
35
+ # We explicitly pass the path to `pydocstyle`, so it ignores `--match-dir`. But
36
+ # we can match the directory ourselves.
42
37
dir_match_re = re .compile (settings .get ("matchDir" , DEFAULT_MATCH_DIR_RE ) + "$" )
43
- if not dir_match_re .match (os . path . basename ( os . path . dirname ( document .path )) ):
38
+ if not dir_match_re .match (Path ( document .path ). parent . name ):
44
39
return []
45
40
46
41
args = [document .path ]
47
42
48
- if settings .get ("convention" ):
49
- args .append ("--convention=" + settings ["convention" ])
43
+ def append_if_exists (setting_name , arg_name = None ):
44
+ """Append an argument if it exists in `settings`."""
45
+ if setting_name not in settings :
46
+ return False
47
+
48
+ if isinstance (settings [setting_name ], str ):
49
+ value = settings [setting_name ]
50
+ else :
51
+ value = "," .join (settings [setting_name ])
52
+
53
+ args .append (f"--{ arg_name or setting_name } ={ value } " )
54
+ return True
50
55
51
- if settings .get ("addSelect" ):
52
- args .append ("--add-select=" + "," .join (settings ["addSelect" ]))
53
- if settings .get ("addIgnore" ):
54
- args .append ("--add-ignore=" + "," .join (settings ["addIgnore" ]))
56
+ if append_if_exists ("convention" ):
57
+ append_if_exists ("addSelect" , "add-select" )
58
+ append_if_exists ("addIgnore" , "add-ignore" )
59
+ elif append_if_exists ("select" ):
60
+ pass
61
+ else :
62
+ append_if_exists ("ignore" )
55
63
56
- elif settings .get ("select" ):
57
- args .append ("--select=" + "," .join (settings ["select" ]))
58
- elif settings .get ("ignore" ):
59
- args .append ("--ignore=" + "," .join (settings ["ignore" ]))
64
+ append_if_exists ("match" )
60
65
61
66
log .info ("Using pydocstyle args: %s" , args )
62
67
0 commit comments