def getFoldableBlocks(self, block: QTextBlock) -> list: rootIndentation: int = len(block.text()) - len(block.text().strip()) nextBlock: QTextBlock = block.next() foldableBlocks: list = [] lastBlock = None while nextBlock.isValid(): strippedText = nextBlock.text().strip() currentBlockIndentation: int = len( nextBlock.text()) - len(strippedText) if currentBlockIndentation <= rootIndentation: if ( len(strippedText) != 0 ): # This evaluates to true when we we reach a block that is not in our scope to fold break if ( len(strippedText) != 0 ): # Last block with actual code in it, no white space contained lastBlock = nextBlock foldableBlocks.append(nextBlock) nextBlock = nextBlock.next() for index, foldableBlock in enumerate(foldableBlocks): if foldableBlocks[index] == lastBlock: return foldableBlocks[:index + 1]
def getBlockUnderCursor(self, event: QMouseEvent) -> QTextBlock: height = self.fontMetrics().height() y = event.pos().y() for array in self.currentlyVisibleBlocks: if array[0] < y < height + array[0]: return array[1] emptyBlock: QTextBlock = QTextBlock() return emptyBlock
def __init__(self, document): QSyntaxHighlighter.__init__(self, document) self.cursor = self.parent().parent().parent().textCursor() self.Line = QTextBlock(self.cursor.block()) rules = [] self.rules = [] Highlighter.temp_object = self set_first_object(Highlighter.temp_object) # Keyword, operator, and brace rules for w in Highlighter.keywords: rules += [(r'%c' % w, 0, STYLES['character'])] for o in Highlighter.operators: rules += [(r'%s' % o, 0, STYLES['line'])] # Build a QRegExp for each pattern for (pat, index, fmt) in rules: self.rules.append([(QRegExp(pat), index, fmt)])
def update_line_index(self, block: QtGui.QTextBlock) -> None: ch_str = self.chapter_keyword chapters = [Chapter()] chapter_line_numbers = [0] current_chunk_start = 0 n = 0 while block.isValid(): line = block.text() user_data = cast(LineFormatData, block.userData()) state = user_data.text if user_data is not None else '' if state == 'chapter': chapters[-1].sections[-1].line_count = n - current_chunk_start chapters.append(Chapter( title=line[len(ch_str):].strip('✓ \t'), complete=line.rstrip().endswith('✓') )) chapter_line_numbers.append(n) current_chunk_start = n elif state == 'section': chapters[-1].sections[-1].line_count = n - current_chunk_start chapters[-1].sections.append(Section( desc=line.rstrip()[2:-2].strip() )) current_chunk_start = n elif state in ('desc', 'time', 'tags'): if state == 'desc': chapters[-1].desc = line.rstrip()[2:-2].strip() elif state == 'time': chapters[-1].time = line[1:].strip() elif state == 'tags': chapters[-1].tags = {tag.strip()[1:] for tag in line.split(',') if tag.strip()} n += 1 block = block.next() chapters[-1].sections[-1].line_count = n - current_chunk_start # Shitty hack to fix the metadata line count for c in chapters: l = sum(x is not None for x in [c.title, c.desc, c.tags, c.time]) c.metadata_line_count = l c.sections[0].line_count -= l self.chapters = chapters self.chapter_line_numbers = chapter_line_numbers
def backwards(block, until=QTextBlock()): """Yields the block and all preceding blocks. If until is a valid block, yields the blocks until the specified block. """ if until.isValid(): while block.isValid() and block >= until: yield block block = block.previous() else: while block.isValid(): yield block block = block.previous()
def forwards(block, until=QTextBlock()): """Yields the block and all following blocks. If until is a valid block, yields the blocks until the specified block. """ if until.isValid(): while block.isValid() and block <= until: yield block block = block.next() else: while block.isValid(): yield block block = block.next()
class Highlighter(QSyntaxHighlighter): temp_object = "" f_text, s_text = "", "" line = [] allLine = [] onlyChar = [] keywords = ["T", "t", "s", "f"] # Python operators operators = [ "\.", ",", "!"] result = [] def __init__(self, document): QSyntaxHighlighter.__init__(self, document) self.cursor = self.parent().parent().parent().textCursor() self.Line = QTextBlock(self.cursor.block()) rules = [] self.rules = [] Highlighter.temp_object = self set_first_object(Highlighter.temp_object) # Keyword, operator, and brace rules for w in Highlighter.keywords: rules += [(r'%c' % w, 0, STYLES['character'])] for o in Highlighter.operators: rules += [(r'%s' % o, 0, STYLES['line'])] # Build a QRegExp for each pattern for (pat, index, fmt) in rules: self.rules.append([(QRegExp(pat), index, fmt)]) # Get text editor from Window def returnParent(self): return self.parent().parent().parent() def getAllLine(self,ind): self.cursor.setBlockFormat(self.Line) def findLines(self): # self.cursor.setPosition() # print("SON",self.cursor.atBlockEnd()) # print("BAŞ",self.cursor.atBlockStart()) pass def highlightBlock(self, text): self.findLines() first, second = self.parent().parent().parent().parent().returnTextWindow() if Highlighter.temp_object is self: Highlighter.f_text = first.toPlainText() text = self.returnParent().toPlainText() joinText(text, Highlighter.temp_object) if Highlighter.temp_object is not self: Highlighter.s_text = second.toPlainText() text = self.returnParent().toPlainText() joinText(text) Highlighter.result = retResult() # print(Highlighter.result) # for ind, index in Highlighter.result: # self.cursor.setBlockFormat(self.Line) # self.setCurrentBlockState(0) # self.cursor.setPosition(0) print(self.Line.blockNumber())