1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2014 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# See http://www.gnu.org/licenses/ for more information.
"""
Highlighter for the snippet editor and view.
"""
import builtins
import keyword
from PyQt5.QtGui import QSyntaxHighlighter
import app
import textformats
from ly import slexer
from . import snippets
class Highlighter(QSyntaxHighlighter):
def __init__(self, document):
super(Highlighter, self).__init__(document)
self.python = None
self._fridge = slexer.Fridge()
self.readSettings()
app.settingsChanged.connect(self.readSettingsAgain)
def readSettings(self):
self._styles = textformats.formatData('editor').defaultStyles
def readSettingsAgain(self):
self.readSettings()
self.rehighlight()
def setPython(self, python):
"""Force Python or generic snippet highlighting.
Use True for Python, False for Snippet, or None to let the highlighter
decide based on the variable lines.
"""
if self.python is not python:
self.python = python
self.rehighlight()
def highlightBlock(self, text):
prev = self.previousBlockState()
python = self.python if self.python is not None else prev == -2
if text.startswith('-*- '):
# the line defines variables, highlight them and check if 'python' is defined
self.setFormat(0, 3, self._styles['keyword'])
for m in snippets._variables_re.finditer(text):
self.setFormat(m.start(1), m.end(1)-m.start(1), self._styles['variable'])
if m.group(2):
self.setFormat(m.start(2), m.end(2)-m.start(2), self._styles['value'])
python = python or m.group(1) == 'python'
self.setCurrentBlockState(-2 if python else -1)
else:
# we are in the snippet text
state = self._fridge.thaw(prev) or slexer.State(Python if python else Snippet)
for t in state.tokens(text):
if isinstance(t, Stylable):
self.setFormat(t.pos, len(t), self._styles[t.style])
self.setCurrentBlockState(self._fridge.freeze(state))
# Basic types:
class Stylable(slexer.Token):
"""A token type with style ;-) to highlight."""
style = '<set style here>'
class String(Stylable):
style = 'string'
class Comment(Stylable):
style = 'comment'
class Escape(Stylable):
style = 'escape'
class Function(Stylable):
style = 'function'
class Keyword(Stylable):
style = 'keyword'
class Variable(Stylable):
style = 'variable'
class Value(Stylable):
style = 'value'
# Snippet types:
class StringStart(String):
rx = '"'
def update_state(self, state):
state.enter(StringParser())
class StringEnd(StringStart):
def update_state(self, state):
state.leave()
class StringEscape(Escape):
rx = r'\\["\\]'
class Expansion(Escape):
rx = snippets._expansions_re.pattern
# Python types:
class PyKeyword(Keyword):
rx = r"\b({0})\b".format('|'.join(keyword.kwlist))
class PyBuiltin(Function):
rx = r"\b({0})\b".format('|'.join(builtins.__dict__))
class PyStringStartDQ1(String):
rx = '[uUbB]?"'
def update_state(self, state):
state.enter(PyStringParserDQ1())
class PyStringStartDQ3(String):
rx = '[uUbB]?"""'
def update_state(self, state):
state.enter(PyStringParserDQ3())
class PyStringStartSQ1(String):
rx = "[uUbB]?'"
def update_state(self, state):
state.enter(PyStringParserSQ1())
class PyStringStartSQ3(String):
rx = "[uUbB]?'''"
def update_state(self, state):
state.enter(PyStringParserSQ3())
class PyStringStartDQ1R(String):
rx = '[uUbB]?[rR]"'
def update_state(self, state):
state.enter(PyStringParserDQ1R())
class PyStringStartDQ3R(String):
rx = '[uUbB]?[rR]"""'
def update_state(self, state):
state.enter(PyStringParserDQ3R())
class PyStringStartSQ1R(String):
rx = "[uUbB]?[rR]'"
def update_state(self, state):
state.enter(PyStringParserSQ1R())
class PyStringStartSQ3R(String):
rx = "[uUbB]?[rR]'''"
def update_state(self, state):
state.enter(PyStringParserSQ3R())
class PyStringEndDQ1(StringEnd):
rx = '"'
class PyStringEndDQ3(StringEnd):
rx = '"""'
class PyStringEndSQ1(StringEnd):
rx = "'"
class PyStringEndSQ3(StringEnd):
rx = "'''"
class PyStringEscape(Escape):
rx = (
r"""\\([\\'"abfnrtv]"""
r'|[0-7]{3}|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}'
r'|N\{[A-Z]+( [A-Z]+)*\})'
)
class PyStringEscapedNewline(Escape):
"""Highlights escaped newline."""
rx = r'\\$'
class PyStringDoubleBackslashAtLineEnd(PyStringEscape):
"""Use this to leave a string at line end. Prevents staying in PyStringEOL."""
rx = r'\\\\$'
def update_state(self, state):
state.leave()
class PyStringDoubleBackslashAtLineEndR(String):
"""Use this to leave a string at line end in raw. Prevents staying in PyStringEOL."""
rx = r'\\\\$'
def update_state(self, state):
state.leave()
class PyStringEOL(slexer.Token):
"""Leaves a string at unescaped Newline."""
rx = r'(?<!\\)$'
def update_state(self, state):
state.leave()
class PyComment(Comment):
rx = "#"
def update_state(self, state):
state.enter(PyCommentParser())
class LineEnd(slexer.Token):
"""Newline to leave context."""
rx = r'$'
def update_state(self, state):
state.leave()
class PyIdentifier(slexer.Token):
rx = r"\b[^\W\d]\w+"
class PySpecialVariable(Variable):
rx = r"\b(self|state|cursor|text|view|ANCHOR|CURSOR)\b"
class PyValue(Value):
rx = (
r"(0[bB][01]+|0[oO][0-7]+|0[xX][0-9A-Fa-f]+|\d+)[lL]?"
r"|(\d+\.\d*|\.\d+)([eE][+-]?\d+)?"
)
# Parsers, many because of complicated string quote types in Python
class StringParser(slexer.Parser):
default = String
items = (
StringEnd,
StringEscape,
Expansion,
)
class Python(slexer.Parser):
items = (
PyKeyword,
PyBuiltin,
PySpecialVariable,
PyIdentifier,
PyValue,
PyStringStartDQ3,
PyStringStartDQ1,
PyStringStartSQ3,
PyStringStartSQ1,
PyStringStartDQ3R,
PyStringStartDQ1R,
PyStringStartSQ3R,
PyStringStartSQ1R,
PyComment,
)
class PyStringParserDQ1(StringParser):
"""Parses string with one double quote."""
items = (
PyStringDoubleBackslashAtLineEnd,
PyStringEscape,
PyStringEscapedNewline,
PyStringEOL,
PyStringEndDQ1,
)
class PyStringParserDQ3(StringParser):
"""Parses string with three double quotes."""
items = (
PyStringEscape,
PyStringEscapedNewline,
PyStringEndDQ3,
)
class PyStringParserSQ1(StringParser):
"""Parses string with one single quote."""
items = (
PyStringDoubleBackslashAtLineEnd,
PyStringEscape,
PyStringEscapedNewline,
PyStringEOL,
PyStringEndSQ1,
)
class PyStringParserSQ3(StringParser):
"""Parses string with three single quotes."""
items = (
PyStringEscape,
PyStringEscapedNewline,
PyStringEndSQ3,
)
class PyStringParserDQ1R(StringParser):
"""Parses raw string with one double quote."""
items = (
PyStringDoubleBackslashAtLineEndR,
PyStringEscapedNewline,
PyStringEOL,
PyStringEndDQ1,
)
class PyStringParserDQ3R(StringParser):
"""Parses raw string with three double quotes."""
items = (
PyStringEscapedNewline,
PyStringEndDQ3,
)
class PyStringParserSQ1R(StringParser):
"""Parses raw string with one single quote."""
items = (
PyStringDoubleBackslashAtLineEndR,
PyStringEscapedNewline,
PyStringEOL,
PyStringEndSQ1,
)
class PyStringParserSQ3R(StringParser):
"""Parses raw string with three single quotes."""
items = (
PyStringEscapedNewline,
PyStringEndSQ3,
)
class PyCommentParser(slexer.Parser):
"""Parses comment."""
default = Comment
items = (
LineEnd,
)
class Snippet(slexer.Parser):
"""Just parses simple double-quoted string and dollar-sign expansions."""
items = (
StringStart,
Expansion,
)
|