aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Wendling <isanbard@gmail.com>2021-12-24 01:24:05 -0600
committerBill Wendling <isanbard@gmail.com>2021-12-24 01:27:30 -0600
commit5bc5a7b3e723e9b6b7555f659400a7ae106adb85 (patch)
tree16c933f1389a29c8865b5320862ff8e87fae923c
parentcb0bb0eb81ee113b017e2a64bab7b582f2ce4886 (diff)
downloadyapf-5bc5a7b3e723e9b6b7555f659400a7ae106adb85.tar.gz
Fix up the "name" and "is_docstring" bits of a format_token
-rw-r--r--yapf/yapflib/format_token.py12
-rw-r--r--yapftests/format_token_test.py10
2 files changed, 14 insertions, 8 deletions
diff --git a/yapf/yapflib/format_token.py b/yapf/yapflib/format_token.py
index 42e524e..9d61662 100644
--- a/yapf/yapflib/format_token.py
+++ b/yapf/yapflib/format_token.py
@@ -141,6 +141,7 @@ class FormatToken(object):
self.type = node.type
self.column = node.column
self.lineno = node.lineno
+ self.name = pytree_utils.NodeName(node)
self.spaces_required_before = 0
if self.is_comment:
@@ -153,9 +154,7 @@ class FormatToken(object):
subtypes = pytree_utils.GetNodeAnnotation(node,
pytree_utils.Annotation.SUBTYPE)
self.subtypes = [Subtype.NONE] if subtypes is None else subtypes
- self.name = pytree_utils.NodeName(node)
self.is_pseudo = hasattr(node, 'is_pseudo') and node.is_pseudo
- self.is_docstring = self.is_multiline_string and not node.prev_sibling
@property
def formatted_whitespace_prefix(self):
@@ -250,8 +249,9 @@ class FormatToken(object):
return self.value in pytree_utils.CLOSING_BRACKETS
def __repr__(self):
- msg = 'FormatToken(name={0}, value={1}, lineno={2}'.format(
- self.name, self.value, self.lineno)
+ msg = 'FormatToken(name={0}, value={1}, column={2}, lineno={3}'.format(
+ 'DOCSTRING' if self.is_docstring else self.name, self.value,
+ self.column, self.lineno)
msg += ', pseudo)' if self.is_pseudo else ')'
return msg
@@ -328,6 +328,10 @@ class FormatToken(object):
return self.is_string and self.value.endswith(('"""', "'''"))
@property
+ def is_docstring(self):
+ return self.is_string and self.previous_token is None
+
+ @property
def is_pylint_comment(self):
return self.is_comment and re.match(r'#.*\bpylint:\s*(disable|enable)=',
self.value)
diff --git a/yapftests/format_token_test.py b/yapftests/format_token_test.py
index b4c7151..8c7151a 100644
--- a/yapftests/format_token_test.py
+++ b/yapftests/format_token_test.py
@@ -67,13 +67,15 @@ class FormatTokenTest(unittest.TestCase):
def testSimple(self):
tok = format_token.FormatToken(pytree.Leaf(token.STRING, "'hello world'"))
- self.assertEqual("FormatToken(name=STRING, value='hello world', lineno=0)",
- str(tok))
+ self.assertEqual(
+ "FormatToken(name=DOCSTRING, value='hello world', column=0, lineno=0)",
+ str(tok))
self.assertTrue(tok.is_string)
tok = format_token.FormatToken(pytree.Leaf(token.COMMENT, '# A comment'))
- self.assertEqual('FormatToken(name=COMMENT, value=# A comment, lineno=0)',
- str(tok))
+ self.assertEqual(
+ 'FormatToken(name=COMMENT, value=# A comment, column=0, lineno=0)',
+ str(tok))
self.assertTrue(tok.is_comment)
def testIsMultilineString(self):