Skip to content

Commit 5853330

Browse files
committed
Add options support to yapf
1 parent 9085112 commit 5853330

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

pylsp/plugins/yapf_format.py

+40-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,50 @@ def pylsp_format_range(document, range, options=None): # pylint: disable=redefi
3434

3535

3636
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+
3776
new_source, changed = FormatCode(
3877
document.source,
3978
lines=lines,
4079
filename=document.filename,
41-
style_config=file_resources.GetDefaultStyleForDir(
42-
os.path.dirname(document.path)
43-
)
80+
style_config=style_config
4481
)
4582

4683
if not changed:

test/plugins/test_yapf_format.py

+27
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,30 @@ def test_config_file(tmpdir, workspace):
5858

5959
# A was split on multiple lines because of column_limit from config file
6060
assert pylsp_format_document(doc)[0]['newText'] == "A = [\n 'h', 'w',\n 'a'\n]\n\nB = ['h', 'w']\n"
61+
62+
FOUR_SPACE_DOC = """def hello():
63+
pass
64+
"""
65+
66+
def test_format_with_tab_size_option(workspace):
67+
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
68+
res = pyls_format_document(doc, { "tabSize": "8" })
69+
70+
assert len(res) == 1
71+
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", " ")
72+
73+
74+
def test_format_with_insert_spaces_option(workspace):
75+
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
76+
res = pyls_format_document(doc, { "insertSpaces": False })
77+
78+
assert len(res) == 1
79+
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", "\t")
80+
81+
82+
def test_format_with_yapf_specific_option(workspace):
83+
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
84+
res = pyls_format_document(doc, { "USE_TABS": True })
85+
86+
assert len(res) == 1
87+
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", "\t")

0 commit comments

Comments
 (0)