Refactor doc search class to allow single document search
This commit is contained in:
@@ -337,32 +337,32 @@ class DocSearch:
|
|||||||
"""Iteratively search through documents in a project."""
|
"""Iteratively search through documents in a project."""
|
||||||
self._regEx.setPattern(self._buildPattern(search))
|
self._regEx.setPattern(self._buildPattern(search))
|
||||||
logger.debug("Searching with pattern '%s'", self._regEx.pattern())
|
logger.debug("Searching with pattern '%s'", self._regEx.pattern())
|
||||||
|
|
||||||
num = len(search)
|
|
||||||
storage = project.storage
|
storage = project.storage
|
||||||
for item in project.tree:
|
for item in project.tree:
|
||||||
if item.isFileType():
|
if item.isFileType():
|
||||||
text = storage.getDocumentText(item.itemHandle)
|
results, capped = self.searchText(storage.getDocumentText(item.itemHandle))
|
||||||
rxItt = self._regEx.globalMatch(text)
|
|
||||||
count = 0
|
|
||||||
capped = False
|
|
||||||
results = []
|
|
||||||
while rxItt.hasNext():
|
|
||||||
rxMatch = rxItt.next()
|
|
||||||
pos = rxMatch.capturedStart()
|
|
||||||
num = rxMatch.capturedLength()
|
|
||||||
context = text[pos:pos+100].partition("\n")[0]
|
|
||||||
if context:
|
|
||||||
results.append((pos, num, context))
|
|
||||||
count += 1
|
|
||||||
if count >= nwConst.MAX_SEARCH_RESULT:
|
|
||||||
capped = True
|
|
||||||
break
|
|
||||||
|
|
||||||
yield item, results, capped
|
yield item, results, capped
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def searchText(self, text: str) -> tuple[list[tuple[int, int, str]], bool]:
|
||||||
|
"""Search a piece of text for RegEx matches."""
|
||||||
|
rxItt = self._regEx.globalMatch(text)
|
||||||
|
count = 0
|
||||||
|
capped = False
|
||||||
|
results = []
|
||||||
|
while rxItt.hasNext():
|
||||||
|
rxMatch = rxItt.next()
|
||||||
|
pos = rxMatch.capturedStart()
|
||||||
|
num = rxMatch.capturedLength()
|
||||||
|
context = text[pos:pos+100].partition("\n")[0]
|
||||||
|
if context:
|
||||||
|
results.append((pos, num, context))
|
||||||
|
count += 1
|
||||||
|
if count >= nwConst.MAX_SEARCH_RESULT:
|
||||||
|
capped = True
|
||||||
|
break
|
||||||
|
return results, capped
|
||||||
|
|
||||||
##
|
##
|
||||||
# Internal Functions
|
# Internal Functions
|
||||||
##
|
##
|
||||||
|
|||||||
Reference in New Issue
Block a user