mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Improvements to paren matching algorithm.
This commit is contained in:
parent
6ff2d3c346
commit
e624da286e
@ -28,6 +28,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
|
|||||||
Config = {}
|
Config = {}
|
||||||
Rules = []
|
Rules = []
|
||||||
Formats = {}
|
Formats = {}
|
||||||
|
BN_FACTOR = 1000
|
||||||
|
|
||||||
KEYWORDS = ["program"]
|
KEYWORDS = ["program"]
|
||||||
|
|
||||||
@ -95,10 +96,8 @@ class TemplateHighlighter(QSyntaxHighlighter):
|
|||||||
self.Formats[name] = format
|
self.Formats[name] = format
|
||||||
|
|
||||||
def find_paren(self, bn, pos):
|
def find_paren(self, bn, pos):
|
||||||
for pp in self.paren_positions:
|
dex = bn * self.BN_FACTOR + pos
|
||||||
if pp.block == bn and pp.pos == pos:
|
return self.paren_pos_map.get(dex, None)
|
||||||
return pp
|
|
||||||
return None
|
|
||||||
|
|
||||||
def highlightBlock(self, text):
|
def highlightBlock(self, text):
|
||||||
bn = self.currentBlock().blockNumber()
|
bn = self.currentBlock().blockNumber()
|
||||||
@ -127,24 +126,27 @@ class TemplateHighlighter(QSyntaxHighlighter):
|
|||||||
if self.generate_paren_positions:
|
if self.generate_paren_positions:
|
||||||
t = unicode(text)
|
t = unicode(text)
|
||||||
i = 0
|
i = 0
|
||||||
first = True
|
foundQuote = False
|
||||||
while i < len(t):
|
while i < len(t):
|
||||||
c = t[i]
|
c = t[i]
|
||||||
if c == ':':
|
if c == ':':
|
||||||
if first and i+1 < len(t) and t[i+1] == "'":
|
# Deal with the funky syntax of template program mode.
|
||||||
|
# This won't work if there are more than one template
|
||||||
|
# expression in the document.
|
||||||
|
if not foundQuote and i+1 < len(t) and t[i+1] == "'":
|
||||||
i += 2
|
i += 2
|
||||||
elif c in ["'", '"']:
|
elif c in ["'", '"']:
|
||||||
first = False
|
foundQuote = True
|
||||||
i += 1
|
i += 1
|
||||||
j = t[i:].find(c)
|
j = t[i:].find(c)
|
||||||
if j < 0:
|
if j < 0:
|
||||||
i = len(t)
|
i = len(t)
|
||||||
else:
|
else:
|
||||||
i = i + j
|
i = i + j
|
||||||
elif c == '(':
|
elif c in ['(', ')']:
|
||||||
self.paren_positions.append(ParenPosition(bn, i, '('))
|
pp = ParenPosition(bn, i, c)
|
||||||
elif c == ')':
|
self.paren_positions.append(pp)
|
||||||
self.paren_positions.append(ParenPosition(bn, i, ')'))
|
self.paren_pos_map[bn*self.BN_FACTOR+i] = pp
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
def rehighlight(self):
|
def rehighlight(self):
|
||||||
@ -186,6 +188,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
|
|||||||
def regenerate_paren_positions(self):
|
def regenerate_paren_positions(self):
|
||||||
self.generate_paren_positions = True
|
self.generate_paren_positions = True
|
||||||
self.paren_positions = []
|
self.paren_positions = []
|
||||||
|
self.paren_pos_map = {}
|
||||||
self.rehighlight()
|
self.rehighlight()
|
||||||
self.generate_paren_positions = False
|
self.generate_paren_positions = False
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user