Skip to content

Commit 1ed18a6

Browse files
committed
Add options support to yapf
1 parent 0e70e72 commit 1ed18a6

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

pylsp/plugins/yapf_format.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,50 @@ def _format(document, lines=None, options=None):
4646
replace_cr = True
4747
source = source.replace('\r', '\n')
4848

49+
# Get the default styles as a string
50+
# for a preset configuration, i.e. "pep8"
51+
style_config = file_resources.GetDefaultStyleForDir(
52+
os.path.dirname(document.path)
53+
)
54+
if options is not None:
55+
# We have options passed from LSP format request
56+
# let's pass them to the formatter.
57+
# First we want to get a dictionary of the preset style
58+
# to pass instead of a string so that we can modify it
59+
style_config = style.CreateStyleFromConfig(style_config)
60+
61+
use_tabs = style_config['USE_TABS']
62+
indent_width = style_config['INDENT_WIDTH']
63+
64+
if options.get('tabSize') is not None:
65+
indent_width = max(int(options.get('tabSize')), 1)
66+
67+
if options.get('insertSpaces') is not None:
68+
# TODO is it guaranteed to be a boolean, or can it be a string?
69+
use_tabs = not options.get('insertSpaces')
70+
71+
if use_tabs:
72+
# Indent width doesn't make sense when using tabs
73+
# the specifications state: "Size of a tab in spaces"
74+
indent_width = 1
75+
76+
style_config['USE_TABS'] = use_tabs
77+
style_config['INDENT_WIDTH'] = indent_width
78+
style_config['CONTINUATION_INDENT_WIDTH'] = indent_width
79+
80+
for style_option, value in options.items():
81+
# Apply arbitrary options passed as formatter options
82+
if style_option not in style_config:
83+
# ignore if it's not a known yapf config
84+
continue
85+
86+
style_config[style_option] = value
87+
4988
new_source, changed = FormatCode(
5089
source,
5190
lines=lines,
5291
filename=document.filename,
53-
style_config=file_resources.GetDefaultStyleForDir(
54-
os.path.dirname(document.path)
55-
)
92+
style_config=style_config
5693
)
5794

5895
if not changed:

test/plugins/test_yapf_format.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,30 @@ def test_cr_line_endings(workspace):
6565
res = pylsp_format_document(doc)
6666

6767
assert res[0]['newText'] == 'import os\rimport sys\r\rdict(a=1)\r'
68+
69+
FOUR_SPACE_DOC = """def hello():
70+
pass
71+
"""
72+
73+
def test_format_with_tab_size_option(workspace):
74+
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
75+
res = pyls_format_document(doc, { "tabSize": "8" })
76+
77+
assert len(res) == 1
78+
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", " ")
79+
80+
81+
def test_format_with_insert_spaces_option(workspace):
82+
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
83+
res = pyls_format_document(doc, { "insertSpaces": False })
84+
85+
assert len(res) == 1
86+
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", "\t")
87+
88+
89+
def test_format_with_yapf_specific_option(workspace):
90+
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
91+
res = pyls_format_document(doc, { "USE_TABS": True })
92+
93+
assert len(res) == 1
94+
assert res[0]['newText'] == FOUR_SPACE_DOC.replace(" ", "\t")

0 commit comments

Comments
 (0)