Skip to content

Commit

Permalink
Fix dangle parens after comment
Browse files Browse the repository at this point in the history
* Add missing numpy dependency to setup.py
* Fix arg comments dont force vpack
* Fix arg comments dont force dangle parenthesis
* Add some missing function specifications

Fixes #54
Closes #54
Fixes #55
Fixes #56
Fixes #59
  • Loading branch information
cheshirekow committed Jul 14, 2018
1 parent 54ad65d commit afd9c6b
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmake_format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"""
from __future__ import unicode_literals

VERSION = '0.4.0'
VERSION = '0.4.1'
47 changes: 43 additions & 4 deletions cmake_format/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@
else:
STRING_TYPES = (str,)

CONDITIONAL_FLAGS = [
"COMMAND",
"DEFINED",
"EQUAL",
"EXISTS",
"GREATER",
"LESS",
"IS_ABSOLUTE",
"IS_DIRECTORY",
"IS_NEWER_THAN",
"IS_SYMLINK",
"MATCHES",
"NOT",
"POLICY",
"STRLESS",
"STRGREATER",
"STREQUAL",
"TARGET",
"TEST",
"VERSION_EQUAL",
"VERSION_GREATER",
"VERSION_LESS",
]


class CommandSpec(dict):
"""
Expand Down Expand Up @@ -188,6 +212,18 @@ def get_fn_spec():
"WORKING_DIRECTORY": ZERO_OR_MORE
})

fn_spec.add(
"export",
flags=["APPEND", "EXPORT_LINK_INTERFACE_LIBRARIES"],
kwargs={
"ANDROID_MK": 1,
"EXPORT": 1,
"FILE": 1,
"NAMESPACE": 1,
"PACKAGE": 1,
"TARGETS": ONE_OR_MORE
})

fn_spec.add(
"file",
flags=["FOLLOW_SYMLINKS", "GENERATE", "HEX", "NEWLINE_CONSUME",
Expand Down Expand Up @@ -355,6 +391,7 @@ def get_fn_spec():
kwargs={
"ARCHIVE": subspec,
"BUNDLE": subspec,
"DESTINATION": 1,
"DIRECTORY": ZERO_OR_MORE,
"DIRECTORY_PERMISSIONS": ZERO_OR_MORE,
"EXCLUDE": ONE_OR_MORE,
Expand All @@ -366,6 +403,7 @@ def get_fn_spec():
"INCLUDES": ZERO_OR_MORE,
"INCLUDESPERMISSIONS": ZERO_OR_MORE,
"LIBRARY": subspec,
"NAMESPACE": 1,
"PATTERN": ZERO_OR_MORE,
"PRIVATE_HEADER": subspec,
"PROGRAMS": ZERO_OR_MORE,
Expand Down Expand Up @@ -512,10 +550,11 @@ def get_fn_spec():
})

fn_spec.add(
"if", flags=[], kwargs={
"NOT": ONE_OR_MORE,
"AND": ONE_OR_MORE,
"OR": ONE_OR_MORE
"if",
flags=list(CONDITIONAL_FLAGS),
kwargs={
"AND": ONE_OR_MORE,
"OR": ONE_OR_MORE,
}
)

Expand Down
21 changes: 21 additions & 0 deletions cmake_format/doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@
Changelog
=========

------
v0.4.1
------

* Add missing numpy dependency to setup.py
* Fix arg comments dont force vpack
* Fix arg comments dont force dangle parenthesis
* Add some missing function specifications

* Fixes `_cheshirekow/cmake_format#53`
* Closes `_cheshirekow/cmake_format#54`
* Fixes `_cheshirekow/cmake_format#55`
* Fixes `_cheshirekow/cmake_format#56`
* Fixes `_cheshirekow/cmake_format#59`

.. _cheshirekow/cmake_format#53: https://github.com/cheshirekow/cmake_format/issues/53
.. _cheshirekow/cmake_format#54: https://github.com/cheshirekow/cmake_format/issues/54
.. _cheshirekow/cmake_format#55: https://github.com/cheshirekow/cmake_format/issues/55
.. _cheshirekow/cmake_format#56: https://github.com/cheshirekow/cmake_format/issues/56
.. _cheshirekow/cmake_format#59: https://github.com/cheshirekow/cmake_format/issues/59

------
v0.4.0
------
Expand Down
9 changes: 4 additions & 5 deletions cmake_format/doc/todo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ TODO
comments. Argument comments must be reflowed to (config.linewidth-1) if
dangle_parens is false, while block level and statement comments may reflow
up to (config.linewidth). Can probably just make this a flag in the
CommentNode class rather than implementing a separate class for it. Throw
an exception from CommentNode._reflow() if passno is zero and .isarg=True
CommentNode class rather than implementing a separate class for it. This
can help to eliminate the need for, or at least simplify, the
has_terminal_comment() hack.
* Deal with the case that the command name is so long or that the statement is
nested so far that the open paren doesn't fit on the line and needs to be
wrapped.

Where you left off:
* Restore the old console command heuristic layout
* Improve error messages for exceptions/assertions caused by malformed input.
16 changes: 16 additions & 0 deletions cmake_format/format_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ def test_argcomment_preserved_and_reflowed(self):
header_d.h)
""")

def test_argcomments_force_reflow(self):
self.config.line_width = 140
self.do_format_test("""\
cmake_parse_arguments(ARG
"SILENT" # optional keywords
"" # one value keywords
"" # multi value keywords
${ARGN})
""", """\
cmake_parse_arguments(ARG
"SILENT" # optional keywords
"" # one value keywords
"" # multi value keywords
${ARGN})
""")

def test_format_off(self):
self.do_format_test("""\
# This part of the comment should
Expand Down
36 changes: 36 additions & 0 deletions cmake_format/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def __repr__(self):
self.position[0], self.position[1],
self.colextent)

def has_terminal_comment(self):
return False

def get_depth(self):
"""
Compute and return the depth of the subtree rooted at this node. The
Expand Down Expand Up @@ -297,6 +300,10 @@ def _write(self, config, ctx):

class ScalarNode(LayoutNode):

def has_terminal_comment(self):
return (self.children
and self.children[-1].pnode.children[0].type == TokenType.COMMENT)

def _reflow(self, config, cursor, passno):
"""
Update the size of a single-token box node by computing the size of the
Expand Down Expand Up @@ -326,6 +333,11 @@ def _reflow(self, config, cursor, passno):
assert child.type == NodeType.COMMENT
cursor = child.reflow(config, cursor, passno)
self._colextent = max(self._colextent, child.colextent)
# NOTE(josh): if we have a line-comment child associated with this
# argument, then we cannot vpack
if (child.pnode.children[0].type != TokenType.BRACKET_COMMENT
and self.wrap == WrapAlgo.HPACK):
self._colextent = config.linewidth + 100

assert not children
return cursor
Expand Down Expand Up @@ -358,6 +370,9 @@ def _write(self, config, ctx):

class OnOffSwitchNode(LayoutNode):

def has_terminal_comment(self):
return True

def _reflow(self, config, cursor, passno):
"""
Update the size of a single-token comment token for format on/off
Expand Down Expand Up @@ -552,6 +567,10 @@ def _reflow(self, config, cursor, passno):
elif prev.type == NodeType.COMMENT:
column_cursor[0] += 1
cursor = np.array(column_cursor)
elif prev.has_terminal_comment():
column_cursor[0] += 1
cursor = np.array(column_cursor)


cursor = child.reflow(config, cursor, passno)
self._colextent = max(self._colextent, child.colextent)
Expand Down Expand Up @@ -581,6 +600,9 @@ def _write(self, config, ctx):

class KwargGroupNode(LayoutNode):

def has_terminal_comment(self):
return self.children[-1].has_terminal_comment()

def _reflow(self, config, cursor, passno):
"""
Compute the size of a keyword argument group which is nominally allocated
Expand Down Expand Up @@ -670,6 +692,16 @@ def _reflow(self, config, cursor, passno):

class ArgGroupNode(LayoutNode):

def has_terminal_comment(self):
children = list(self.children)
while children and children[0].type != NodeType.RPAREN:
children.pop(0)
children.pop(0)

return (children
and children[-1].pnode.children[0].type == TokenType.COMMENT)


def _reflow(self, config, cursor, passno):
"""
Compute the size of a parenthtetical argument group which is nominally
Expand Down Expand Up @@ -774,6 +806,9 @@ def _reflow(self, config, cursor, passno):
elif prev.type == NodeType.COMMENT:
column_cursor[0] += 1
cursor = np.array(column_cursor)
elif prev.has_terminal_comment():
column_cursor[0] += 1
cursor = np.array(column_cursor)

cursor = child.reflow(config, cursor, passno)
self._colextent = max(self._colextent, child.colextent)
Expand Down Expand Up @@ -889,6 +924,7 @@ def _reflow(self, config, cursor, passno):
allocation = config.linewidth - cursor[1]
lines = list(format_comment_lines(self.pnode, config, allocation))
self._colextent = cursor[1] + max(len(line) for line in lines)

return cursor + (len(lines) - 1, len(lines[-1]))

def _write(self, config, ctx):
Expand Down
4 changes: 2 additions & 2 deletions cmake_format/layout_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def test_argcomment_preserved_and_reflowed(self):
(NodeType.LPAREN, WrapAlgo.HPACK, 0, 3, 4, []),
(NodeType.ARGUMENT, WrapAlgo.HPACK, 0, 4, 11, []),
(NodeType.ARGUMENT, WrapAlgo.HPACK, 1, 4, 14, []),
(NodeType.ARGUMENT, WrapAlgo.HPACK, 2, 4, 78, [
(NodeType.ARGUMENT, WrapAlgo.VPACK, 2, 4, 78, [
(NodeType.COMMENT, WrapAlgo.HPACK, 2, 15, 78, []),
]),
(NodeType.ARGUMENT, WrapAlgo.HPACK, 4, 4, 14, []),
Expand Down Expand Up @@ -272,7 +272,7 @@ def test_complex_nested_stuff(self):
(NodeType.LPAREN, WrapAlgo.HPACK, 3, 15, 16, []),
(NodeType.ARGUMENT, WrapAlgo.HPACK, 3, 16, 27, []),
(NodeType.ARGUMENT, WrapAlgo.HPACK, 4, 16, 22, []),
(NodeType.ARGUMENT, WrapAlgo.HPACK, 5, 16, 76, [
(NodeType.ARGUMENT, WrapAlgo.VPACK, 5, 16, 76, [
(NodeType.COMMENT, WrapAlgo.HPACK, 5, 23, 76, []),
]),
(NodeType.ARGUMENT, WrapAlgo.HPACK, 7, 16, 22, []),
Expand Down
2 changes: 1 addition & 1 deletion cmake_format/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class FlowType(common.EnumObject):


def make_conditional_spec():
flags = ['NOT', 'EXISTS', 'MATCHES', 'VERSION_LESS']
flags = list(commands.CONDITIONAL_FLAGS)
spec = commands.CommandSpec('<conditional>', pargs='+', flags=list(flags))
return commands.CommandSpec('<conditional>', pargs='+', flags=list(flags),
kwargs={'AND': spec, 'OR': spec})
Expand Down
2 changes: 1 addition & 1 deletion cmake_format/pypi/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
'YAML': ["pyyaml"],
},
install_requires=[
'sortedcontainers'
'numpy'
]
)

0 comments on commit afd9c6b

Please sign in to comment.