Edit book: Fix detection of class names containing hyphens/underscores

This commit is contained in:
Kovid Goyal 2021-03-29 11:01:31 +05:30
parent 176ad16b04
commit 97bf4c1773
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -716,7 +716,7 @@ class TextEdit(PlainTextEdit):
c = self.textCursor() c = self.textCursor()
c.setPosition(block.position() + r.start) c.setPosition(block.position() + r.start)
c.setPosition(c.position() + r.length, QTextCursor.MoveMode.KeepAnchor) c.setPosition(c.position() + r.length, QTextCursor.MoveMode.KeepAnchor)
return unicode_type(c.selectedText()) return self.selected_text_from_cursor(c)
def spellcheck_locale_for_cursor(self, c): def spellcheck_locale_for_cursor(self, c):
with store_locale: with store_locale:
@ -767,12 +767,41 @@ class TextEdit(PlainTextEdit):
if r is not None and r.format.property(LINK_PROPERTY): if r is not None and r.format.property(LINK_PROPERTY):
return self.text_for_range(c.block(), r) return self.text_for_range(c.block(), r)
def select_class_name_at_cursor(self, cursor):
valid = re.compile(r'[\w_0-9\-]+', flags=re.UNICODE)
def keep_going():
q = cursor.selectedText()
m = valid.match(q)
return m is not None and m.group() == q
def run_loop(forward=True):
cursor.setPosition(pos)
n, p = QTextCursor.MoveOperation.NextCharacter, QTextCursor.MoveOperation.PreviousCharacter
if not forward:
n, p = p, n
while True:
if not cursor.movePosition(n, QTextCursor.MoveMode.KeepAnchor):
break
if not keep_going():
cursor.movePosition(p, QTextCursor.MoveMode.KeepAnchor)
break
ans = cursor.position()
cursor.setPosition(pos)
return ans
pos = cursor.position()
forwards_limit = run_loop()
backwards_limit = run_loop(forward=False)
cursor.setPosition(backwards_limit)
cursor.setPosition(forwards_limit, QTextCursor.MoveMode.KeepAnchor)
return self.selected_text_from_cursor(cursor)
def class_for_position(self, pos): def class_for_position(self, pos):
c = self.cursorForPosition(pos) c = self.cursorForPosition(pos)
r = self.syntax_range_for_cursor(c) r = self.syntax_range_for_cursor(c)
if r is not None and r.format.property(CLASS_ATTRIBUTE_PROPERTY): if r is not None and r.format.property(CLASS_ATTRIBUTE_PROPERTY):
c.select(QTextCursor.SelectionType.WordUnderCursor) class_name = self.select_class_name_at_cursor(c)
class_name = c.selectedText()
if class_name: if class_name:
tags = self.current_tag(for_position_sync=False, cursor=c) tags = self.current_tag(for_position_sync=False, cursor=c)
return {'class': class_name, 'sourceline_address': tags} return {'class': class_name, 'sourceline_address': tags}