-
Notifications
You must be signed in to change notification settings - Fork 68
Console Widget: Add PythonSyntaxHighlighter #1651
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
Open
vincentullmann
wants to merge
8
commits into
ynput:develop
Choose a base branch
from
vincentullmann:add_syntax_highlighter_to_console
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+269
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f1295e
add PythonSyntaxHighlighter to the PythonCodeEditor Widget
vincentullmann 1fc2240
add background color from theme
vincentullmann 05e2864
fix linter errors
vincentullmann 58e5500
Apply dedicated AYON code style (matching Github dark style closely),…
BigRoy a51d9b3
Make imports explicit
BigRoy bc1f0ce
Move `AYONStyle` -> `AYONCodeStyle` into ui folder
BigRoy 7337e96
move PythonSyntaxHighlighter to its own class and safe import it. Imp…
vincentullmann e48d7b4
add pygments to client dependencies
vincentullmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
client/ayon_core/tools/console_interpreter/ui/code_style.py
This file contains hidden or 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,149 @@ | ||
| from pygments.style import Style | ||
| from pygments.token import ( | ||
| Comment, | ||
| Error, | ||
| Generic, | ||
| Keyword, | ||
| Literal, | ||
| Name, | ||
| Number, | ||
| Operator, | ||
| Other, | ||
| Punctuation, | ||
| String, | ||
| Text, | ||
| Whitespace, | ||
| ) | ||
|
|
||
|
|
||
| pl = { | ||
| "syntax-comment": "#8b949e", | ||
| "syntax-constant": "#79c0ff", | ||
| "syntax-entity": "#d2a8ff", | ||
| "syntax-storage-modifier-import": "#c9d1d9", | ||
| "syntax-entity-tag": "#7ee787", | ||
| "syntax-keyword": "#ff7b72", | ||
| "syntax-string": "#a5d6ff", | ||
| "syntax-variable": "#ffa657", | ||
| "syntax-brackethighlighter-unmatched": "#f85149", | ||
| "syntax-invalid-illegal-text": "#f0f6fc", | ||
| "syntax-invalid-illegal-bg": "#8e1519", | ||
| "syntax-carriage-return-text": "#f0f6fc", | ||
| "syntax-carriage-return-bg": "#b62324", | ||
| "syntax-string-regexp": "#7ee787", | ||
| "syntax-markup-list": "#f2cc60", | ||
| "syntax-markup-heading": "#1f6feb", | ||
| "syntax-markup-italic": "#c9d1d9", | ||
| "syntax-markup-bold": "#e6edf3", | ||
| "syntax-markup-deleted-text": "#ffdcd7", | ||
| "syntax-markup-deleted-bg": "#67060c", | ||
| "syntax-markup-inserted-text": "#aff5b4", | ||
| "syntax-markup-inserted-bg": "#033a16", | ||
| "syntax-markup-changed-text": "#ffdfb6", | ||
| "syntax-markup-changed-bg": "#5a1e02", | ||
| "syntax-markup-ignored-text": "#c9d1d9", | ||
| "syntax-markup-ignored-bg": "#1158c7", | ||
| "syntax-meta-diff-range": "#d2a8ff", | ||
| "syntax-brackethighlighter-angle": "#8b949e", | ||
| "syntax-sublimelinter-gutter-mark": "#484f58", | ||
| "syntax-constant-other-reference-link": "#a5d6ff", | ||
| } | ||
|
|
||
|
|
||
| class AYONCodeStyle(Style): | ||
| """A Pygments style similar to GitHub's pretty lights dark theme""" | ||
|
|
||
| background_color = None | ||
| default_style = '' | ||
|
|
||
| styles = { | ||
| Whitespace: "#f0f6fc", | ||
|
|
||
| Comment: pl["syntax-comment"], | ||
| Comment.Hashbang: pl["syntax-comment"], | ||
| Comment.Multiline: pl["syntax-comment"], | ||
| Comment.Preproc: pl["syntax-comment"], | ||
| Comment.Single: pl["syntax-comment"], | ||
| Comment.Special: pl["syntax-comment"], | ||
|
|
||
| Generic: "#f0f6fc", | ||
| Generic.Deleted: "#8b080b", | ||
| Generic.Emph: "#f8f8f2 underline", | ||
| Generic.Error: "#f8f8f2", | ||
| Generic.Heading: "#f8f8f2 bold", | ||
| Generic.Inserted: "#f8f8f2 bold", | ||
| Generic.Output: "#adaeb6", | ||
| Generic.Prompt: "#f8f8f2", | ||
| Generic.Strong: "#f8f8f2", | ||
| Generic.Subheading: "#f8f8f2 bold", | ||
| Generic.Traceback: "#f8f8f2", | ||
| Error: "#f8f8f2", | ||
|
|
||
| Keyword: pl["syntax-keyword"], | ||
| Keyword.Constant: pl["syntax-constant"], # Ex. None | ||
| Keyword.Declaration: pl["syntax-keyword"], | ||
| Keyword.Namespace: pl["syntax-keyword"], | ||
| Keyword.Pseudo: pl["syntax-entity"], | ||
| Keyword.Reserved: pl["syntax-constant"], | ||
| Keyword.Type: pl["syntax-constant"], | ||
|
|
||
| Literal: "#f8f8f2", | ||
| Literal.Date: "#f8f8f2", | ||
| Literal.String.Affix: "#f8f8f2", | ||
| Literal.String.Doc: "#f8f8f2", | ||
| Literal.String.Double: "#f8f8f2", | ||
| Literal.String.Interpol: "#f8f8f2", | ||
| Literal.String.Single: "#f8f8f2", | ||
|
|
||
| Name: pl["syntax-markup-bold"], | ||
| Name.Variable: pl["syntax-markup-bold"], | ||
| Name.Attribute: pl["syntax-markup-bold"], | ||
| Name.Builtin.Pseudo: pl["syntax-markup-bold"], # Ex. self | ||
| Name.Builtin: pl["syntax-entity"], # Ex. print() | ||
| Name.Class: pl["syntax-variable"], | ||
| Name.Constant: pl["syntax-constant"], | ||
| Name.Decorator: pl["syntax-entity"], | ||
| Name.Entity: pl["syntax-entity"], | ||
| Name.Exception: pl["syntax-variable"], | ||
| Name.Function: pl["syntax-entity"], | ||
| Name.Function.Magic: pl["syntax-entity"], | ||
| # Name.Label: "#8be9fd italic", | ||
| Name.Namespace: pl["syntax-markup-bold"], | ||
| Name.Other: pl["syntax-markup-bold"], | ||
| # Name.Other: pl["syntax-variable"], | ||
| # Name.Tag: "#ff79c6", | ||
| Name.Variable.Class: pl["syntax-variable"], | ||
| Name.Variable.Global: pl["syntax-variable"], | ||
| Name.Variable.Instance: pl["syntax-markup-bold"], | ||
| Name.Variable.Magic: pl["syntax-markup-bold"], | ||
|
|
||
| Number: pl["syntax-constant"], | ||
| Number.Bin: pl["syntax-constant"], | ||
| Number.Float: pl["syntax-constant"], | ||
| Number.Hex: pl["syntax-constant"], | ||
| Number.Integer: pl["syntax-constant"], | ||
| Number.Integer.Long: pl["syntax-constant"], | ||
| Number.Oct: pl["syntax-constant"], | ||
| Operator: pl["syntax-constant"], | ||
| Operator.Word: pl["syntax-constant"], | ||
|
|
||
| # Other: "#f8f8f2", | ||
| Other.Constant: pl["syntax-constant"], | ||
| Punctuation: "#f8f8f2", | ||
| Punctuation.Definition.Comment: pl["syntax-comment"], | ||
| String: pl["syntax-string"], | ||
| String.Affix: pl["syntax-string"], | ||
| String.Backtick: pl["syntax-string"], | ||
| String.Char: pl["syntax-string"], | ||
| String.Comment: pl["syntax-comment"], | ||
| String.Doc: pl["syntax-string"], | ||
| String.Double: pl["syntax-string"], | ||
| String.Escape: pl["syntax-string"], | ||
| String.Heredoc: pl["syntax-string"], | ||
| String.Interpol: pl["syntax-string"], | ||
| String.Other: pl["syntax-string"], | ||
| String.Regex: pl["syntax-string"], | ||
| String.Single: pl["syntax-string"], | ||
| String.Symbol: pl["syntax-string"], | ||
| Text: pl["syntax-markup-bold"], | ||
| } | ||
93 changes: 93 additions & 0 deletions
93
client/ayon_core/tools/console_interpreter/ui/syntax_highlight.py
This file contains hidden or 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,93 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| from qtpy import QtGui | ||
| import pygments.lexers | ||
| import pygments.styles | ||
| import pygments.token | ||
| from pygments.util import ClassNotFound | ||
|
|
||
| from .code_style import AYONCodeStyle | ||
|
|
||
|
|
||
| class PythonSyntaxHighlighter(QtGui.QSyntaxHighlighter): | ||
| def __init__(self, document, style_name: str = ""): | ||
| self.lexer = pygments.lexers.PythonLexer() | ||
| self.style = AYONCodeStyle | ||
|
|
||
| # Allow to override style by environment variable | ||
| override_style_name = os.getenv("AYON_CONSOLE_INTERPRETER_STYLE") | ||
| if override_style_name: | ||
|
|
||
| try: | ||
| style = pygments.styles.get_style_by_name(override_style_name) | ||
| except ClassNotFound: | ||
| all_styles = ", ".join(pygments.styles.get_all_styles()) | ||
| raise ValueError( | ||
| f"'{override_style_name}' not found. " | ||
| f"Installed styles: {all_styles}." | ||
| ) | ||
| else: | ||
| self.style = style | ||
|
|
||
| self.formats = self.style_to_formats(self.style) | ||
|
|
||
| # init after validating and loading the style | ||
| super().__init__(document) | ||
|
|
||
| def highlightBlock(self, text: str | None) -> None: | ||
| if text is None: | ||
| return | ||
|
|
||
| index = 0 | ||
| for token, value in self.lexer.get_tokens(text): | ||
| length = len(value) | ||
| if fmt := self.formats.get(token): | ||
| self.setFormat(index, length, fmt) | ||
|
|
||
| index += length | ||
|
|
||
| def _format_for_token(self, token): | ||
| if token is None: | ||
| return None | ||
| if token in self.formats: | ||
| return self.formats[token] | ||
| if token.parent: | ||
| return self._format_for_token(token.parent) | ||
| return None | ||
|
|
||
| def style_to_formats( | ||
| self, | ||
| style: pygments.styles.Style | ||
| ) -> dict[pygments.token.Token, QtGui.QTextCharFormat]: | ||
| """Convert a Pygments style to a dictionary of Qt formats. | ||
|
|
||
| Args: | ||
| style: The Pygments style to convert. | ||
|
|
||
| Returns: | ||
| token_format = QTextCharFormat for each token in the style. | ||
|
|
||
| """ | ||
| formats = {} | ||
| for token, _ in style.styles.items(): | ||
|
|
||
| token_style = style.style_for_token(token) | ||
| if not token_style: | ||
| continue | ||
|
|
||
| token_format = QtGui.QTextCharFormat() | ||
| if color := token_style.get("color"): | ||
| color = f"#{color}" | ||
| token_format.setForeground(QtGui.QColor(color)) | ||
| if token_style.get("bold"): | ||
| token_format.setFontWeight(QtGui.QFont.Bold) | ||
| if token_style.get("italic"): | ||
| token_format.setFontItalic(True) | ||
| if token_style.get("underline"): | ||
| token_format.setFontUnderline(True) | ||
|
|
||
| formats[token] = token_format | ||
|
|
||
| return formats |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| from code import InteractiveInterpreter | ||
| from __future__ import annotations | ||
|
|
||
| from code import InteractiveInterpreter | ||
| from qtpy import QtCore, QtWidgets, QtGui | ||
|
|
||
| try: | ||
| from .syntax_highlight import PythonSyntaxHighlighter | ||
| except ImportError: | ||
| PythonSyntaxHighlighter = None # type: ignore | ||
|
|
||
|
|
||
| class PythonCodeEditor(QtWidgets.QPlainTextEdit): | ||
| execute_requested = QtCore.Signal() | ||
|
|
@@ -12,6 +18,25 @@ def __init__(self, parent): | |
| self.setObjectName("PythonCodeEditor") | ||
|
|
||
| self._indent = 4 | ||
| self._apply_syntax_highlighter() | ||
|
|
||
| def _apply_syntax_highlighter(self): | ||
| if PythonSyntaxHighlighter is None: | ||
| return | ||
|
|
||
| try: | ||
| highlighter = PythonSyntaxHighlighter(self.document()) | ||
| except ValueError as e: | ||
| print(f"Error applying syntax highlighter: {e!s}") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return | ||
|
|
||
| # Apply background color from the style | ||
| if background_color := highlighter.style.background_color: | ||
| self.setStyleSheet(( | ||
| "QPlainTextEdit {" | ||
| f"background-color: {background_color};" | ||
| "}" | ||
| )) | ||
BigRoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def _tab_shift_right(self): | ||
| cursor = self.textCursor() | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.