Okay my syntax highlighter works very well, I only have on problem,
Sometimes GetWords() adds "\n" to operatorless although there isn't pressed an enter
I think the problem is within the GetWords() function, but I give all functions to be sure..
Main function is OnKeyUp,
colorize prints the text colored or not
getWords fetches all the words and puts them in a list
def OnKeyDown(self, event):
keycode = event.GetKeyCode()
controls=[wx.WXK_CONTROL,wx.WXK_ALT,wx.WXK_SHIFT]
if keycode in controls:
pass
else:
insp=self.text_ctrl_1.GetInsertionPoint()
words=self.getWords(self.text_ctrl_1.GetValue())
self.text_ctrl_1.SetValue("")
wordcount=0
print words
for word in words:
self.colorize(word,wordcount)
wordcount+=1
self.text_ctrl_1.SetInsertionPoint(insp)
event.Skip()
def colorize(self,word,wordcount):
'''If the word is found in the syntaxlist color it, if not, return black'''
hl=[["in","green"],["if","green"],["False","brown"],["for","green"],["self","yellow"],["elif","green"],["=","red"]] #syntax list
colorize=False
prev="" #previous words processed
for lh in hl:
if lh[0]==word:
self.text_ctrl_1.BeginTextColour(lh[1])
self.text_ctrl_1.WriteText(word)
self.text_ctrl_1.EndTextColour()
colorize=True
if not colorize:
self.text_ctrl_1.BeginTextColour("black")
self.text_ctrl_1.WriteText(word)
self.text_ctrl_1.EndTextColour()
def getWords(self,wordlist):
'''returns a list with all words,operators,spaces
f.e.: "if x==5" returns ["if"," ","x","==","5"]'''
spaceles=wordlist.split(" ")
i=0
spaceless=[]
for word in spaceles:
if i!=0:
spaceless.append(" ")
spaceless.append(word)
i+=1
operators=["\n","==","!=","<>","+","-","/","*",">=","<=","=<","=>",">","<",".","="]
operatorless=[]
for x in spaceless:
opfound=False
for operator in operators:
if x.split(operator)[0]==x:
pass
elif not opfound:
opfound=True
operatorless.append(x.split(operator)[0])
operatorless.append(operator)
operatorless.append(x.split(operator)[1])
if not opfound:
operatorless.append(x)
return operatorless