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

Make blank space less apparent. #2501

Merged
merged 11 commits into from
Jul 9, 2015
14 changes: 14 additions & 0 deletions spyderlib/widgets/sourcecode/syntaxhighlighters.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,20 @@ def highlightBlock(self, text):

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

# Make blank space less apparent by setting the foreground alpha.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this should have an if statement tied to the option of showing spaces, no?

Also to make it work with other syntax highlighters it should be refactored as a separate method inside BaseSH

# This only has an effect when "Show blank space" is turned on.
re_blank = re.compile("\s+")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should perhaps be a class variable (look on top for)

     # Syntax highlighting rules:
     PROG = re.compile(make_python_patterns(), re.S)
     IDPROG = re.compile(r"\s+(\w+)", re.S)

This way the compilation is done only once when the class is instantiated, and the code would be more uniform, as you can see no regex is compiled within the highlighting method.

match = re_blank.search(text, offset)
while match:
start, end = match.span()
start = max([0, start+offset])
end = max([0, end+offset])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which cases start+offset or end+offset could be negative ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start+offset and end+offset are negative when a line in a multiline string starts with spaces.

That code is copied from the loop earlier in the function. Interpreting the code (and some experimentation) taught me that highlightBlock() is called for each line of text, which might be part of a multiline string. The relevant quotes are temporarily prefixed to the line. The number of added characters is tracked by the negative valued offset variable.

format = self.format(start)
color_foreground = format.foreground().color()
color_foreground.setAlpha(80)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps since you are hard coding the alpha value, it would be better to have it in a more "accessible/set-able" place? @Nodd suggestions on this?

maybe

            color_foreground = format.foreground().color()
            color_foreground.setAlpha(self.SPACE_TAB_ALPHA)

And SPACE_TAB_ALPHA can be also a class variable?

self.setFormat(start, end-start, color_foreground)
match = re_blank.search(text, match.end())

self.setCurrentBlockState(state)

if oedata is not None:
Expand Down