Improve code flow in completer class
This commit is contained in:
@@ -964,9 +964,6 @@ class GuiDocEditor(QPlainTextEdit):
|
|||||||
if not self.wcTimerDoc.isActive():
|
if not self.wcTimerDoc.isActive():
|
||||||
self.wcTimerDoc.start()
|
self.wcTimerDoc.start()
|
||||||
|
|
||||||
if added != 1:
|
|
||||||
return
|
|
||||||
|
|
||||||
block = self._qDocument.findBlock(pos)
|
block = self._qDocument.findBlock(pos)
|
||||||
if not block.isValid():
|
if not block.isValid():
|
||||||
return
|
return
|
||||||
@@ -982,7 +979,7 @@ class GuiDocEditor(QPlainTextEdit):
|
|||||||
self._completer.move(self.viewport().mapToGlobal(point))
|
self._completer.move(self.viewport().mapToGlobal(point))
|
||||||
self._completer.show()
|
self._completer.show()
|
||||||
|
|
||||||
elif self._doReplace:
|
elif self._doReplace and added == 1:
|
||||||
self._docAutoReplace(text)
|
self._docAutoReplace(text)
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -1936,42 +1933,43 @@ class MetaCompleter(QMenu):
|
|||||||
def updateText(self, text: str, pos: int) -> bool:
|
def updateText(self, text: str, pos: int) -> bool:
|
||||||
"""Update the menu options based on the line of text."""
|
"""Update the menu options based on the line of text."""
|
||||||
self.clear()
|
self.clear()
|
||||||
options = []
|
|
||||||
kw, sep, _ = text.partition(":")
|
kw, sep, _ = text.partition(":")
|
||||||
if pos <= len(kw):
|
if pos <= len(kw):
|
||||||
options = list(filter(lambda x: x.startswith(kw.rstrip()), nwKeyWords.VALID_KEYS))
|
offset = 0
|
||||||
if not options:
|
length = len(kw.rstrip())
|
||||||
return False
|
suffix = "" if sep else ":"
|
||||||
for value in options:
|
options = list(filter(
|
||||||
n = len(kw.rstrip())
|
lambda x: x.startswith(kw.rstrip()), nwKeyWords.VALID_KEYS
|
||||||
rep = value if sep else f"{value}:"
|
))
|
||||||
action = self.addAction(value)
|
else:
|
||||||
action.triggered.connect(lambda _, r=rep: self._emitComplete(0, n, r))
|
|
||||||
|
|
||||||
elif pos > len(kw):
|
|
||||||
status, tBits, tPos = SHARED.project.index.scanThis(text)
|
status, tBits, tPos = SHARED.project.index.scanThis(text)
|
||||||
if not status:
|
if not status:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
index = bisect.bisect_right(tPos, pos) - 1
|
index = bisect.bisect_right(tPos, pos) - 1
|
||||||
lookup = tBits[index].lower() if index > 0 else ""
|
lookup = tBits[index].lower() if index > 0 else ""
|
||||||
offset = tPos[index] if lookup else pos
|
offset = tPos[index] if lookup else pos
|
||||||
itemClass = nwKeyWords.KEY_CLASS.get(kw.strip(), nwItemClass.NO_CLASS)
|
length = len(lookup)
|
||||||
|
suffix = ""
|
||||||
options = list(filter(
|
options = list(filter(
|
||||||
lambda x: lookup in x.lower(), SHARED.project.index.getTags(itemClass)
|
lambda x: lookup in x.lower(), SHARED.project.index.getTags(
|
||||||
))
|
nwKeyWords.KEY_CLASS.get(kw.strip(), nwItemClass.NO_CLASS)
|
||||||
if not options:
|
)
|
||||||
return False
|
))[:15]
|
||||||
for value in sorted(options[:15]):
|
|
||||||
n = len(lookup)
|
|
||||||
action = self.addAction(value)
|
|
||||||
action.triggered.connect(lambda _, r=value: self._emitComplete(offset, n, r))
|
|
||||||
|
|
||||||
else:
|
if not options:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
for value in sorted(options):
|
||||||
|
rep = value + suffix
|
||||||
|
action = self.addAction(value)
|
||||||
|
action.triggered.connect(lambda _, r=rep: self._emitComplete(offset, length, r))
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
##
|
||||||
|
# Events
|
||||||
|
##
|
||||||
|
|
||||||
def keyPressEvent(self, event: QKeyEvent) -> None:
|
def keyPressEvent(self, event: QKeyEvent) -> None:
|
||||||
"""Capture keypresses and forward most of them to the editor."""
|
"""Capture keypresses and forward most of them to the editor."""
|
||||||
parent = self.parent()
|
parent = self.parent()
|
||||||
@@ -1981,6 +1979,10 @@ class MetaCompleter(QMenu):
|
|||||||
parent.keyPressEvent(event)
|
parent.keyPressEvent(event)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
##
|
||||||
|
# Internal Functions
|
||||||
|
##
|
||||||
|
|
||||||
def _emitComplete(self, pos: int, length: int, value: str):
|
def _emitComplete(self, pos: int, length: int, value: str):
|
||||||
"""Emit the signal to indicate a selection has been made."""
|
"""Emit the signal to indicate a selection has been made."""
|
||||||
self.complete.emit(pos, length, value)
|
self.complete.emit(pos, length, value)
|
||||||
|
|||||||
Reference in New Issue
Block a user