我正在为 Gedit 编写一个插件,它根据正则表达式对某些单词进行更改。在某些情况下,这会将标签应用到预期单词之外的几个字符。
因此 match.start() 和 match.end() 返回的值在 get_iter_at_offset 中使用无效。
def on_save(self, doc, location, *args, **kwargs):
"""called when document is saved"""
for match in WORD_RE.finditer(get_text(doc)):
if not self._checker.check(match.group().strip()):
self.apply_tag(doc, match.start(), match.end())
def apply_tag(self, doc, start, end):
"""apply the tag to the text between start and end"""
istart = doc.get_iter_at_offset(start)
iend = doc.get_iter_at_offset(end)
doc.apply_tag(self._spell_error_tag, istart, iend)