def get_tag_at_cursor(self): """Called when encountering a colon. Finds out which tag was entered and validates it. """ right = self.get_buffer().get_property('cursor-position') text = self.get_full_text() left = text.rfind(' ', 0, right) tag = text[left + 1:right] # Abrreviated tags need to be converted to the full form. if len(tag) is 1: tag = query_abbrev_to_full(tag) # Only process valid tags. if is_valid_query_tag(tag): return tag
def apply_colors(self): """Called once the buffer contents are changed. Live-highlighting would be possible, but would be tedious to implement. """ # Color the parantheses red: text = self.get_full_text() for idx, char in enumerate(text): if char in '()': self.apply_tag('red', idx, idx + 1) # Color the text between quotes green: for match in self._patterns['qte'].finditer(text): self.apply_tag('green', *match.span()) # Color all operators slightly blueish: for match in self._patterns['ops'].finditer(text): self.apply_tag('blue', *match.span()) # Color all valid tags turquoise, invalid will get red: for match in self._patterns['tag'].finditer(text): tag = match.group()[:-1] is_tag = is_valid_query_tag(tag) or query_abbrev_to_full(tag) self.apply_tag('tag' if is_tag else 'red', *match.span())