我正在创建一个文本编辑器,我正在使用Pygments来帮助语法突出显示。当我输入关键字时,请说for
,它会变黄,很好。好吧,当我或者之后,输入一封信后,说fore
,for
仍为黄色,或,如果我退格,fo
或f
是黄色的。
为什么它没有变回黑色/清除颜色?有没有一种有效的方法来做到这一点?我的代码,只是需要,如下,没有错误,所以我将只包括突出显示功能,即使它本身可能是不需要的。
如果需要,我会包含更多内容
import sys #imports
from pygments import highlight
from pygments.lexers import PythonLexer #python syntax highlighter
from pygments import lex
major_version = sys.version_info.major
if major_version == 2: #check python version for importing tkinter
from Tkinter import *
import tkFileDialog
elif major_version == 3:
from tkinter import *
import tkinter.filedialog as tkFileDialog
else:
raise RuntimeError('Unexpected python major version: %d' % major_version)
def highlight(t, previousContent):
content = t.get("1.0", END)
lines = content.split("\n")
if(content != previousContent):
t.mark_set("range_start", "1.0")
data = t.get("1.0", "end-1c")
for token, content in lex(data, PythonLexer()):
t.mark_set("range_end", "range_start + %dc" % len(content))
t.tag_add(str(token), "range_start", "range_end")
t.mark_set("range_start", "range_end")
def highlightLine(t, previousContent):
content = t.get("1.0", END)
lines = content.split("\n")
currentCursorPosition = t.index(INSERT)
currentCursorPositionSplit = currentCursorPosition.split(".")
currentLine = currentCursorPositionSplit[0]
currentColumn = currentCursorPositionSplit[1]
if(content != previousContent):
t.mark_set("range_start", str(currentLine) + ".0")
data = t.get(str(currentLine) + ".0", str(currentLine) + "." + str(currentColumn))
for token, content in lex(data, PythonLexer()):
t.mark_set("range_end", "range_start + %dc" % len(content))
t.tag_add(str(token), "range_start", "range_end")
t.mark_set("range_start", "range_end")
def initHighlight(t):
t.tag_configure("Token.Keyword", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Constant", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Declaration", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Namespace", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Pseudo", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Reserved", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Type", foreground="#CC7A00")
t.tag_configure("Token.Name.Class", foreground="#003D99")
t.tag_configure("Token.Name.Exception", foreground="#003D99")
t.tag_configure("Token.Name.Function", foreground="#003D99")
t.tag_configure("Token.Operator.Word", foreground="#CC7A00")
t.tag_configure("Token.Comment", foreground="#B80000")
t.tag_configure("Token.Literal.String", foreground="#248F24")
此问题不询问如何从文本中删除所有标记,它询问如何检查标记的字词是否为&#39 ;不再是理想的单词,然后去掉那个单词上的标签。如果我要删除所有标签然后重新标记所有内容,那就太迟了。
答案 0 :(得分:0)
在标记之前,我在同一个函数中添加了
for tag in t.tag_names():
t.tag_remove(tag, str(currentLine) + ".0", str(currentLine) + "." + str(currentColumn))
它检查所有标签,然后删除当前行中的标签,然后,它会将它们添加回来,完整功能如下:
def highlightLine(t, previousContent):
content = t.get("1.0", END)
lines = content.split("\n")
currentCursorPosition = t.index(INSERT)
currentCursorPositionSplit = currentCursorPosition.split(".")
currentLine = currentCursorPositionSplit[0]
currentColumn = currentCursorPositionSplit[1]
if(content != previousContent):
t.mark_set("range_start", str(currentLine) + ".0")
data = t.get(str(currentLine) + ".0", str(currentLine) + "." + str(currentColumn))
for tag in t.tag_names():
t.tag_remove(tag, str(currentLine) + ".0", str(currentLine) + "." + str(currentColumn))
for token, content in lex(data, PythonLexer()):
t.mark_set("range_end", "range_start + %dc" % len(content))
t.tag_add(str(token), "range_start", "range_end")
t.mark_set("range_start", "range_end")
现在完美运作!