-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
PR: Make Pygments to work correctly with QSyntaxHighlighter #3491
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6f3b385
Make Pygments work correctly with QSyntaxHighlighter
ccordoba12 215f6a2
Add a threade worker manager for PythonWorkers and ProcessWorkers
goanpeca 1b4eb2f
Add timer to highlight whole file with pygments when using PygmentsSH
goanpeca f2df6da
Add threaded worker on PygmentsSH to run whole file pygments
goanpeca 9e56c23
Merge with 3.x
goanpeca 3847cf8
Skip print check
goanpeca 738f8e1
Add highlighting on startup
goanpeca 299200d
Add comments and refresh highlighting on startup
goanpeca 93137c8
Fix typos in comments and update docstrings
goanpeca 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
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 |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
from spyder.config.main import CONF | ||
from spyder.py3compat import builtins, is_text_string, to_text_string | ||
from spyder.utils.sourcecode import CELL_LANGUAGES | ||
from spyder.utils.workers import WorkerManager | ||
|
||
|
||
PYGMENTS_REQVER = '>=2.0' | ||
|
@@ -63,7 +64,6 @@ | |
CUSTOM_EXTENSION_LEXER = {'.ipynb': 'json', | ||
'.txt': 'text', | ||
'.nt': 'bat', | ||
'.scss': 'css', | ||
'.m': 'matlab', | ||
('.properties', '.session', '.inf', '.reg', '.url', | ||
'.cfg', '.cnf', '.aut', '.iss'): 'ini'} | ||
|
@@ -1052,6 +1052,7 @@ class PygmentsSH(BaseSH): | |
# Store the language name and a ref to the lexer | ||
_lang_name = None | ||
_lexer = None | ||
_charlist = [] | ||
# Syntax highlighting states (from one text block to another): | ||
NORMAL = 0 | ||
def __init__(self, parent, font=None, color_scheme=None): | ||
|
@@ -1074,33 +1075,91 @@ def __init__(self, parent, font=None, color_scheme=None): | |
# Load Pygments' Lexer | ||
if self._lang_name is not None: | ||
self._lexer = get_lexer_by_name(self._lang_name) | ||
|
||
BaseSH.__init__(self, parent, font, color_scheme) | ||
|
||
def get_fmt(self, typ): | ||
""" Get the format code for this type """ | ||
# Exact matches first | ||
for key in self._tokmap: | ||
if typ is key: | ||
return self._tokmap[key] | ||
# Partial (parent-> child) matches | ||
for key in self._tokmap: | ||
if typ in key.subtypes: | ||
return self._tokmap[key] | ||
return 'normal' | ||
# This worker runs in a thread to avoid blocking when doing full file | ||
# parsing | ||
self._worker_manager = WorkerManager() | ||
|
||
# Store the format for all the tokens after pygments parsing | ||
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.
|
||
self._charlist = [] | ||
|
||
# Flag variable to avoid unnecessary highlights if the worker has not | ||
# yet finish processing | ||
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.
|
||
self._allow_highlight = True | ||
|
||
def make_charlist(self): | ||
"""Parses the complete text and stores format for each character.""" | ||
|
||
def worker_output(worker, output, error): | ||
"""Worker finished callback.""" | ||
self._charlist = output | ||
if error is None and output: | ||
self._allow_highlight = True | ||
self.rehighlight() | ||
self._allow_highlight = False | ||
|
||
text = to_text_string(self.document().toPlainText()) | ||
tokens = self._lexer.get_tokens(text) | ||
|
||
# Before starting a new worker process make sure to end previous | ||
# incarnations | ||
self._worker_manager.terminate_all() | ||
|
||
worker = self._worker_manager.create_python_worker( | ||
self._make_charlist, | ||
tokens, | ||
self._tokmap, | ||
self.formats, | ||
) | ||
worker.sig_finished.connect(worker_output) | ||
worker.start() | ||
|
||
def _make_charlist(self, tokens, tokmap, formats): | ||
""" | ||
Parses the complete text and stores format for each character. | ||
|
||
Uses the attached lexer to parse into a list of tokens and Pygments | ||
token types. Then breaks tokens into individual letters, each with a | ||
Spyder token type attached. Stores this list as self._charlist. | ||
|
||
It's attached to the contentsChange signal of the parent QTextDocument | ||
so that the charlist is updated whenever the document changes. | ||
""" | ||
|
||
def _get_fmt(typ): | ||
"""Get the Spyder format code for the given Pygments token type.""" | ||
# Exact matches first | ||
if typ in tokmap: | ||
return tokmap[typ] | ||
# Partial (parent-> child) matches | ||
for key, val in tokmap.items(): | ||
if typ in key: # Checks if typ is a subtype of key. | ||
return val | ||
|
||
return 'normal' | ||
|
||
charlist = [] | ||
for typ, token in tokens: | ||
fmt = formats[_get_fmt(typ)] | ||
for letter in token: | ||
charlist.append((fmt, letter)) | ||
|
||
return charlist | ||
|
||
def highlightBlock(self, text): | ||
""" Actually highlight the block """ | ||
text = to_text_string(text) | ||
lextree = self._lexer.get_tokens(text) | ||
ct = 0 | ||
for item in lextree: | ||
typ, val = item | ||
key = self.get_fmt(typ) | ||
start = ct | ||
ct += len(val) | ||
self.setFormat(start, ct-start, self.formats[key]) | ||
|
||
self.highlight_spaces(text) | ||
""" Actually highlight the block""" | ||
# Note that an undefined blockstate is equal to -1, so the first block | ||
# will have the correct behaviour of starting at 0. | ||
if self._allow_highlight: | ||
start = self.previousBlockState() + 1 | ||
end = start + len(text) | ||
for i, (fmt, letter) in enumerate(self._charlist[start:end]): | ||
self.setFormat(i, 1, fmt) | ||
self.setCurrentBlockState(end) | ||
self.highlight_spaces(text) | ||
|
||
|
||
def guess_pygments_highlighter(filename): | ||
"""Factory to generate syntax highlighter for the given filename. | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to be needed because
_charlist
is also initialized in__init__
below.