Skip to content

Commit

Permalink
highlighter: Add dark theme variants for standard colors
Browse files Browse the repository at this point in the history
To make the tests pass, use the old approach from highlighter for
reading colors from settings.

Together with the previous commits, this fixes #255.
  • Loading branch information
mitya57 committed Mar 4, 2021
1 parent 7e718aa commit 9003444
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
28 changes: 22 additions & 6 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import weakref

from markups import MarkdownMarkup, ReStructuredTextMarkup, TextileMarkup
from ReText import globalSettings, tablemode, readFromSettings
from ReText import globalSettings, settings, tablemode

from PyQt5.QtCore import pyqtSignal, QFileInfo, QPoint, QRect, QSize, Qt
from PyQt5.QtGui import QColor, QImage, QKeyEvent, QMouseEvent, QPainter, \
Expand All @@ -34,24 +34,40 @@
ReTextFakeVimHandler = None

colors = {
# Editor
'marginLine': {'light': '#dcd2dc', 'dark': '#3daee9'},
'currentLineHighlight': {'light': '#ffffc8', 'dark': '#31363b'},
'infoArea': {'light': '#aaaaff55', 'dark': '#aa557f2a'},
'statsArea': {'light': '#aaffaa55', 'dark': '#aa7f552a'},
'lineNumberArea': {'light': '#00ffff', 'dark': '#31363b'},
'lineNumberAreaText': {'light': '#008080', 'dark': '#bdc3c7'},
# Highlighter
'htmlTags': {'light': '#800080', 'dark': '#d070d0'},
'htmlSymbols': {'light': '#008080', 'dark': '#70d0a0'},
'htmlStrings': {'light': '#808000', 'dark': '#d0d070'},
'htmlComments': {'light': '#a0a0a4', 'dark': '#b0b0aa'},
'codeSpans': {'light': '#505050', 'dark': '#afafaf'},
'markdownLinks': {'light': '#000090', 'dark': '#8080ff'},
'blockquotes': {'light': '#808080', 'dark': '#b0b0b0'},
'restDirectives': {'light': '#800080', 'dark': '#d070d0'},
'restRoles': {'light': '#800000', 'dark': '#d07070'},
'whitespaceOnEnd': {'light': '#80e1e1a5', 'dark': '#8096966e'},
}

colorValues = {}

def getColor(colorName):
if colorName not in colorValues:
def getColor(colorName, settings=settings):
if not colorValues:
palette = QApplication.palette()
windowColor = palette.color(QPalette.ColorRole.Window)
themeVariant = 'light' if windowColor.lightness() > 150 else 'dark'
defaultColor = QColor(colors[colorName][themeVariant])
colorValues[colorName] = readFromSettings('ColorScheme/' + colorName, QColor,
default=defaultColor)
settings.beginGroup('ColorScheme')
for key in colors:
if settings.contains(key):
colorValues[key] = settings.value(key, type=QColor)
else:
colorValues[key] = QColor(colors[key][themeVariant])
settings.endGroup()
return colorValues[colorName]

def documentIndentMore(document, cursor, globalSettings=globalSettings):
Expand Down
33 changes: 4 additions & 29 deletions ReText/highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from ReText import settings
from ReText.editor import getColor
from enum import IntFlag, auto
import re

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QFont, QSyntaxHighlighter, QTextCharFormat
from PyQt5.QtGui import QFont, QSyntaxHighlighter, QTextCharFormat

reHtmlTags = re.compile('<[^<>@]*>')
reHtmlSymbols = re.compile(r'&#?\w+;')
Expand Down Expand Up @@ -48,30 +48,6 @@
reWords = re.compile('[^_\\W]+')
reSpacesOnEnd = re.compile(r'\s+$')

defaultColorScheme = {
'htmlTags': Qt.GlobalColor.darkMagenta,
'htmlSymbols': Qt.GlobalColor.darkCyan,
'htmlStrings': Qt.GlobalColor.darkYellow,
'htmlComments': Qt.GlobalColor.gray,
'codeSpans': QColor(0x50, 0x50, 0x50),
'markdownLinks': QColor(0, 0, 0x90),
'blockquotes': Qt.GlobalColor.darkGray,
'restDirectives': Qt.GlobalColor.darkMagenta,
'restRoles': Qt.GlobalColor.darkRed,
'whitespaceOnEnd': QColor(0xe1, 0xe1, 0xa5, 0x80)
}
colorScheme = {}

def updateColorScheme(settings=settings):
settings.beginGroup('ColorScheme')
for key in defaultColorScheme:
if settings.contains(key):
colorScheme[key] = settings.value(key, type=QColor)
else:
colorScheme[key] = defaultColorScheme[key]
settings.endGroup()

updateColorScheme()

class Formatter:
def __init__(self, funcs=None):
Expand All @@ -94,8 +70,7 @@ def format(self, charFormat):
UNDL = Formatter([lambda f: f.setFontUnderline(True)])

def FG(colorName):
color = colorScheme[colorName]
func = lambda f: f.setForeground(color)
func = lambda f: f.setForeground(getColor(colorName))
return Formatter([func])

def QString_length(text):
Expand Down Expand Up @@ -182,7 +157,7 @@ def highlightBlock(self, text):
charFormat)
for match in reSpacesOnEnd.finditer(text):
charFormat = QTextCharFormat()
charFormat.setBackground(colorScheme['whitespaceOnEnd'])
charFormat.setBackground(getColor('whitespaceOnEnd'))
self.setFormat(QString_length(text[:match.start()]),
QString_length(match.group(0)),
charFormat)
Expand Down
11 changes: 5 additions & 6 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from PyQt5.QtWidgets import QApplication
from ReText import readListFromSettings, writeListToSettings, \
readFromSettings, writeToSettings
from ReText.highlighter import colorScheme, updateColorScheme
from ReText.editor import getColor

# For this particular test, QCoreApplication is enough. However, we should
# only have one QCoreApplication instance for all tests in a process. As
Expand Down Expand Up @@ -83,11 +83,10 @@ def test_storingColors(self):
self.settings.setValue('ColorScheme/htmlTags', 'green')
self.settings.setValue('ColorScheme/htmlSymbols', '#ff8800')
self.settings.setValue('ColorScheme/htmlComments', '#abc')
updateColorScheme(self.settings)
self.assertEqual(colorScheme['htmlTags'], QColor(0x00, 0x80, 0x00))
self.assertEqual(colorScheme['htmlSymbols'], QColor(0xff, 0x88, 0x00))
self.assertEqual(colorScheme['htmlStrings'], Qt.GlobalColor.darkYellow) # default
self.assertEqual(colorScheme['htmlComments'], QColor(0xaa, 0xbb, 0xcc))
self.assertEqual(getColor('htmlTags', self.settings), QColor(0x00, 0x80, 0x00))
self.assertEqual(getColor('htmlSymbols', self.settings), QColor(0xff, 0x88, 0x00))
self.assertEqual(getColor('htmlStrings', self.settings), Qt.GlobalColor.darkYellow) # default
self.assertEqual(getColor('htmlComments', self.settings), QColor(0xaa, 0xbb, 0xcc))

if __name__ == '__main__':
unittest.main()

0 comments on commit 9003444

Please sign in to comment.