-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change linting highlighting implementation a bit
- split out code into `darker.highlighting.*` modules - register custom lexers as setuptools plugin entry points - refer to built-in and custom Pygments lexers with names instead of object instances - use `PythonLexer` directly instead of using the legacy `Python3Lexer` backwards-compatibility name - separate dummy and real `colorize()` implementations (for use without and with Pygments, respectively) - no need for "fake Pygments" module - more unit tests
- Loading branch information
Showing
9 changed files
with
116 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""Highlighting of terminal output""" | ||
|
||
try: | ||
import pygments # noqa: F401 | ||
except ImportError: | ||
from darker.highlighting import without_pygments | ||
|
||
colorize = without_pygments.colorize | ||
else: | ||
from darker.highlighting import with_pygments | ||
|
||
colorize = with_pygments.colorize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Linter output highlighting helper to be used when Pygments is installed""" | ||
|
||
import sys | ||
from typing import cast | ||
|
||
from pygments import highlight | ||
from pygments.formatters.terminal import TerminalFormatter | ||
from pygments.lexers import get_lexer_by_name | ||
|
||
|
||
def colorize(output: str, lexer_name: str) -> str: | ||
"""Return the output highlighted for terminal if Pygments is available""" | ||
if not highlight or not sys.stdout.isatty(): | ||
return output | ||
lexer = get_lexer_by_name(lexer_name) | ||
highlighted = highlight(output, lexer, TerminalFormatter()) | ||
if "\n" not in output: | ||
# see https://github.com/pygments/pygments/issues/1107 | ||
highlighted = highlighted.rstrip("\n") | ||
return cast(str, highlighted) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"""Linter output highlighting helper to be used when Pygments is not installed""" | ||
|
||
|
||
def colorize(output: str, lexer_name: str) -> str: # pylint: disable=unused-argument | ||
"""Return the output unaltered""" | ||
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters