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

Emphasizing Error-Inducing Code Chunks for Text-Based Reporters #970

Merged
merged 15 commits into from
Oct 17, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Enhancements

- Both PlainReporter and ColorReporter emphasize specific code chunks by using overline characters under any part that is highlighted as ERROR.

## [2.6.3] - 2023-10-09

### Bug fixes
Expand Down
6 changes: 4 additions & 2 deletions python_ta/reporters/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class PythonTaReporter(BaseReporter):
_BREAK = "\n"
_COLOURING = {}
_PRE_LINE_NUM_SPACES = 2
_NUM_LENGTH_SPACES = 3
_AFTER_NUM_SPACES = 2

# The error messages to report, mapping filename to a list of messages.
messages: Dict[str, List[Message]]
Expand Down Expand Up @@ -192,11 +194,11 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str =
def _add_line_number(self, lineno: int, linetype: LineType) -> str:
"""Return a formatted string displaying a line number."""
pre_spaces = self._PRE_LINE_NUM_SPACES * self._SPACE
spaces = 2 * self._SPACE
spaces = self._AFTER_NUM_SPACES * self._SPACE
if lineno is not None:
number = "{:>3}".format(lineno)
else:
number = 3 * self._SPACE
number = self._NUM_LENGTH_SPACES * self._SPACE

if linetype == LineType.ERROR:
return pre_spaces + self._colourify("gbold-line", number) + spaces
Expand Down
29 changes: 29 additions & 0 deletions python_ta/reporters/plain_reporter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict, List

from .core import NewMessage, PythonTaReporter
from .node_printers import LineType


class PlainReporter(PythonTaReporter):
Expand Down Expand Up @@ -81,3 +82,31 @@ def _colour_messages_by_type(self, messages: Dict[str, List[NewMessage]]) -> str
result += self._BREAK

return result

def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = "") -> str:
"""Format given source code line as specified and return as str.

Called by _build_snippet, relies on _colourify.
"""
lineno_spaces = self._PRE_LINE_NUM_SPACES + self._NUM_LENGTH_SPACES + self._AFTER_NUM_SPACES
snippet_so_far = super()._add_line(lineno, linetype, slice_, text)
if linetype == LineType.ERROR:
start = slice_.start or 0
prespace = (
lineno_spaces + start
) * self._SPACE # number 7 for prespaces included for adding line number
snippet_so_far += self._overline_helper(text[slice_], prespace)

elif linetype == LineType.DOCSTRING:
prespace = (lineno_spaces + len(text) - len(text.lstrip(" "))) * self._SPACE
snippet_so_far += self._overline_helper(text.lstrip(" "), prespace)

return snippet_so_far

def _overline_helper(self, text: str, prespace: str) -> str:
"""
Helper method _add_line. Adds the Unicode U+203E (the overline character "‾") under any
part that is highlighted as ERROR. Returns the corresponding snippet as a result.
"""
overline = "‾" * len(text)
return prespace + overline + self._BREAK