Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always wrap kwargs for certain functions #39

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions cmake_format/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,37 @@
ONE_OR_MORE = '+'


def decl_command(fn_spec, command_name, pargs=None, flags=None, kwargs=None):
if pargs is None:
pargs = 0
if flags is None:
flags = []
if kwargs is None:
kwargs = {}

decl = dict(kwargs)
decl['pargs'] = pargs
for flag in flags:
decl[flag] = 0
fn_spec[command_name] = decl
class FnSpec:
def __init__(self, pargs=None, flags=None, kwargs=None, always_wrap=False):
if pargs is None:
pargs = 0
if flags is None:
flags = []
if kwargs is None:
kwargs = {}

self._kwargs = dict(kwargs)
self._pargs = pargs
self._flags = flags
self._always_wrap = always_wrap

@property
def always_wrap(self):
"""Return true if this function's kwargs should always be wrapped."""
return self._always_wrap

def is_flag(self, arg):
"""Return true if the given argument is a flag."""
return arg in self._flags

def is_kwarg(self, arg):
"""Return true if the given argument is a kwarg."""
return arg in self._kwargs


def decl_command(fn_spec, command_name, pargs=None, flags=None, kwargs=None,
always_wrap=False):
fn_spec[command_name] = FnSpec(pargs, flags, kwargs, always_wrap)


def get_fn_spec():
Expand Down
1 change: 1 addition & 0 deletions cmake_format/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __init__(self, line_width=80, tab_size=2,

self.additional_commands = get_default(additional_commands, {
'foo': {
'always_wrap': False,
'flags': ['BAR', 'BAZ'],
'kwargs': {
'HEADERS': '*',
Expand Down
18 changes: 18 additions & 0 deletions cmake_format/format_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ def test_lots_of_args_command_split(self):
source_g.cc)
""")

def test_always_wrap(self):
commands.decl_command(self.config.fn_spec,
'always_wrap', always_wrap=True,
flags=['BAR', 'BAZ'],
kwargs={
"HEADERS": '*',
"SOURCES": '*',
"DEPENDS": '*'
})
self.do_format_test(u"""\
# This short command should be split to multiple lines
always_wrap(HEADERS a.h b.h c.h SOURCES a.c b.c c.c)
""", u"""\
# This short command should be split to multiple lines
always_wrap(HEADERS a.h b.h c.h
SOURCES a.c b.c c.c)
""")

# TODO(josh): figure out why this test elicits different behavior than the
# whole-file demo.
def test_string_preserved_during_split(self):
Expand Down
21 changes: 17 additions & 4 deletions cmake_format/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ def format_comment_block(config, line_width, # pylint: disable=unused-argument
for line in markup.format_items(config, line_width - 2, items)]


def always_wrap(fn_spec, command_name):
"""Return true if this function's kwargs should always be wrapped."""
if command_name in fn_spec:
return fn_spec[command_name].always_wrap
return False


def is_flag(fn_spec, command_name, arg):
"""Return true if the given argument is a flag."""
return fn_spec.get(command_name, {}).get(arg, 1) == 0
if command_name in fn_spec:
return fn_spec[command_name].is_flag(arg)
return False


def is_kwarg(fn_spec, command_name, arg):
"""Return true if the given argument is a kwarg."""
return fn_spec.get(command_name, {}).get(arg, 0) != 0
if command_name in fn_spec:
return fn_spec[command_name].is_kwarg(arg)
return False


def split_args_by_kwargs(fn_spec, command_name, args):
Expand Down Expand Up @@ -295,8 +306,10 @@ def format_args(config, line_width, command_name, args):
"""Format arguments into a block with at most line_width chars."""

# If there are no arguments that contain a comment, then attempt to
# pack all of the arguments onto a single line
if not arg_exists_with_comment(args):
# pack all of the arguments onto a single line if our config allows
# that for this command.
if not arg_exists_with_comment(args) \
and not always_wrap(config.fn_spec, command_name):
single_line = u' '.join(join_parens([arg.contents for arg in args]))
if len(single_line) < line_width:
return [single_line]
Expand Down