Skip to content

Commit

Permalink
Merge pull request #2501 from hugobuddel/spacesyntaxautorebase
Browse files Browse the repository at this point in the history
Make blank space less apparent.
  • Loading branch information
goanpeca committed Jul 9, 2015
2 parents c217ed0 + 95fcd87 commit b5373f7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
2 changes: 2 additions & 0 deletions spyderlib/widgets/sourcecode/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@ def set_blanks_enabled(self, state):
else:
option.setFlags(option.flags() & ~QTextOption.ShowTabsAndSpaces)
self.document().setDefaultTextOption(option)
# Rehighlight to make the spaces less apparent.
self.rehighlight()

#-----scrollflagarea
def set_scrollflagarea_enabled(self, state):
Expand Down
67 changes: 59 additions & 8 deletions spyderlib/widgets/sourcecode/syntaxhighlighters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import keyword

from spyderlib.qt.QtGui import (QColor, QApplication, QFont,
QSyntaxHighlighter, QCursor, QTextCharFormat)
QSyntaxHighlighter, QCursor, QTextCharFormat,
QTextOption)
from spyderlib.qt.QtCore import Qt

# Local imports
Expand Down Expand Up @@ -84,8 +85,12 @@ class BaseSH(QSyntaxHighlighter):
"""Base Syntax Highlighter Class"""
# Syntax highlighting rules:
PROG = None
BLANKPROG = re.compile("\s+")
# Syntax highlighting states (from one text block to another):
NORMAL = 0
# Syntax highlighting parameters.
BLANK_ALPHA_FACTOR = 0.31

def __init__(self, parent, font=None, color_scheme='Spyder'):
QSyntaxHighlighter.__init__(self, parent)

Expand Down Expand Up @@ -190,7 +195,37 @@ def set_color_scheme(self, color_scheme):

def highlightBlock(self, text):
raise NotImplementedError


def highlight_spaces(self, text, offset=0):
"""
Make blank space less apparent by setting the foreground alpha.
This only has an effect when 'Show blank space' is turned on.
Derived classes could call this function at the end of
highlightBlock().
"""
flags_text = self.document().defaultTextOption().flags()
show_blanks = flags_text & QTextOption.ShowTabsAndSpaces
if show_blanks:
format_leading = self.formats.get("leading", None)
format_trailing = self.formats.get("trailing", None)
match = self.BLANKPROG.search(text, offset)
while match:
start, end = match.span()
start = max([0, start+offset])
end = max([0, end+offset])
# Format trailing spaces at the end of the line.
if end == len(text) and format_trailing is not None:
self.setFormat(start, end, format_trailing)
# Format leading spaces, e.g. indentation.
if start == 0 and format_leading is not None:
self.setFormat(start, end, format_leading)
format = self.format(start)
color_foreground = format.foreground().color()
alpha_new = self.BLANK_ALPHA_FACTOR * color_foreground.alphaF()
color_foreground.setAlphaF(alpha_new)
self.setFormat(start, end-start, color_foreground)
match = self.BLANKPROG.search(text, match.end())

def get_outlineexplorer_data(self):
return self.outlineexplorer_data

Expand All @@ -202,9 +237,9 @@ def rehighlight(self):


class TextSH(BaseSH):
"""Simple Text Syntax Highlighter Class (do nothing)"""
"""Simple Text Syntax Highlighter Class (only highlight spaces)"""
def highlightBlock(self, text):
pass
self.highlight_spaces(text)


class GenericSH(BaseSH):
Expand All @@ -225,6 +260,8 @@ def highlightBlock(self, text):
self.setFormat(start, end-start, self.formats[key])

match = self.PROG.search(text, match.end())

self.highlight_spaces(text)


#==============================================================================
Expand Down Expand Up @@ -413,9 +450,14 @@ def highlightBlock(self, text):
self.formats["keyword"])

match = self.PROG.search(text, match.end())

self.setCurrentBlockState(state)

# Use normal format for indentation and trailing spaces.
self.formats['leading'] = self.formats['normal']
self.formats['trailing'] = self.formats['normal']
self.highlight_spaces(text, offset)

if oedata is not None:
block_nb = self.currentBlock().blockNumber()
self.outlineexplorer_data[block_nb] = oedata
Expand Down Expand Up @@ -536,7 +578,9 @@ def highlightBlock(self, text):
self.setFormat(start, end-start, self.formats[key])

match = self.PROG.search(text, match.end())


self.highlight_spaces(text)

last_state = self.INSIDE_COMMENT if inside_comment else self.NORMAL
self.setCurrentBlockState(last_state)

Expand Down Expand Up @@ -611,13 +655,16 @@ def highlightBlock(self, text):
self.formats["definition"])

match = self.PROG.search(text, match.end())

self.highlight_spaces(text)

class Fortran77SH(FortranSH):
"""Fortran 77 Syntax Highlighter"""
def highlightBlock(self, text):
text = to_text_string(text)
if text.startswith(("c", "C")):
self.setFormat(0, len(text), self.formats["comment"])
self.highlight_spaces(text)
else:
FortranSH.highlightBlock(self, text)
self.setFormat(0, 5, self.formats["comment"])
Expand Down Expand Up @@ -673,7 +720,8 @@ def highlightBlock(self, text):
self.setFormat(0, len(text), self.formats["number"])
elif text.startswith("@"):
self.setFormat(0, len(text), self.formats["builtin"])


self.highlight_spaces(text)

#==============================================================================
# NSIS highlighter
Expand Down Expand Up @@ -803,6 +851,8 @@ def highlightBlock(self, text):

match = self.PROG.search(text, match.end())
match_count += 1

self.highlight_spaces(text)

def make_html_patterns():
"""Strongly inspired from idlelib.ColorDelegator.make_pat """
Expand Down Expand Up @@ -885,7 +935,8 @@ def highlightBlock(self, text):
start = ct
ct += len(val)
self.setFormat(start, ct-start, self.formats[key])


self.highlight_spaces(text)

def guess_pygments_highlighter(filename):
"""Factory to generate syntax highlighter for the given filename.
Expand Down

0 comments on commit b5373f7

Please sign in to comment.