Skip to content

Commit 97ae449

Browse files
author
whitequark
committedJun 6, 2015
Extend diagnostic.Diagnostic.render with only_line argument.
1 parent d63349c commit 97ae449

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed
 

Diff for: ‎pythonparser/diagnostic.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def message(self):
5656
"""
5757
return self.reason.format(**self.arguments)
5858

59-
def render(self):
59+
def render(self, only_line=False):
6060
"""
6161
Returns the human-readable location of the diagnostic in the source,
6262
the formatted message, the source line corresponding
@@ -66,9 +66,11 @@ def render(self):
6666
6767
For example: ::
6868
69-
<input>:1:8: error: cannot add integer and string
69+
<input>:1:8-9: error: cannot add integer and string
7070
x + (1 + "a")
7171
~ ^ ~~~
72+
73+
:param only_line: (bool) If true, only print line number, not line and column range
7274
"""
7375
source_line = self.location.source_line().rstrip("\n")
7476
highlight_line = bytearray(" ", "utf-8") * len(source_line)
@@ -83,11 +85,16 @@ def render(self):
8385
rgt = lft + 1
8486
highlight_line[lft:rgt] = bytearray("^", "utf-8") * (rgt - lft)
8587

88+
if only_line:
89+
location = "%s:%s" % (self.location.source_buffer.name, self.location.line())
90+
else:
91+
location = str(self.location)
92+
8693
return [
87-
"%s: %s: %s" % (str(self.location), self.level, self.message()),
94+
"%s: %s: %s" % (location, self.level, self.message()),
8895
source_line,
8996
highlight_line.decode("utf-8")
90-
] + reduce(list.__add__, [note.render() for note in self.notes], [])
97+
] + reduce(list.__add__, [note.render(only_line) for note in self.notes], [])
9198

9299

93100
class Error(Exception):

0 commit comments

Comments
 (0)
Please sign in to comment.