@@ -34,13 +34,50 @@ def pylsp_format_range(document, range, options=None): # pylint: disable=redefi
34
34
35
35
36
36
def _format (document , lines = None , options = None ):
37
+ # Get the default styles as a string
38
+ # for a preset configuration, i.e. "pep8"
39
+ style_config = file_resources .GetDefaultStyleForDir (
40
+ os .path .dirname (document .path )
41
+ )
42
+ if options is not None :
43
+ # We have options passed from LSP format request
44
+ # let's pass them to the formatter.
45
+ # First we want to get a dictionary of the preset style
46
+ # to pass instead of a string so that we can modify it
47
+ style_config = style .CreateStyleFromConfig (style_config )
48
+
49
+ use_tabs = style_config ['USE_TABS' ]
50
+ indent_width = style_config ['INDENT_WIDTH' ]
51
+
52
+ if options .get ('tabSize' ) is not None :
53
+ indent_width = max (int (options .get ('tabSize' )), 1 )
54
+
55
+ if options .get ('insertSpaces' ) is not None :
56
+ # TODO is it guaranteed to be a boolean, or can it be a string?
57
+ use_tabs = not options .get ('insertSpaces' )
58
+
59
+ if use_tabs :
60
+ # Indent width doesn't make sense when using tabs
61
+ # the specifications state: "Size of a tab in spaces"
62
+ indent_width = 1
63
+
64
+ style_config ['USE_TABS' ] = use_tabs
65
+ style_config ['INDENT_WIDTH' ] = indent_width
66
+ style_config ['CONTINUATION_INDENT_WIDTH' ] = indent_width
67
+
68
+ for style_option , value in options .items ():
69
+ # Apply arbitrary options passed as formatter options
70
+ if style_option not in style_config :
71
+ # ignore if it's not a known yapf config
72
+ continue
73
+
74
+ style_config [style_option ] = value
75
+
37
76
new_source , changed = FormatCode (
38
77
document .source ,
39
78
lines = lines ,
40
79
filename = document .filename ,
41
- style_config = file_resources .GetDefaultStyleForDir (
42
- os .path .dirname (document .path )
43
- )
80
+ style_config = style_config
44
81
)
45
82
46
83
if not changed :
0 commit comments