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

If it is a new line character that make this,you can try this quick fix.

operatorless = [item.rstrip() for item in operatorless]
return operatorless

Use print statement for troubleshoothing i think many forget in wxpython.
It will show result in stdout window.

Then you can see if as example if problem come from "wordlist"

spaceles=wordlist.split(" ")
print spaceles  #Test print

Or as many test print you need to track down the problem.

This is really odd, their happens something with the newlines which I can't get
an example:
I type:

x==False:
d

and I printed operatorless each time:

darragh@kruptools:~/d-cm/trunk$ ./deditor.py
[u'x']
[u'x', '=']
[u'x', '=', u'F']
[u'x', '=', u'Fa']
[u'x', '=', u'Fal']
[u'x', '=', u'False']
[u'x', '=', u'False']
[u'x', '=', u'False', ':']
[u'x=False:', '\n']
[u'x=False:', '\n', u'd']

So from the moment I press the enter, the x=False isn't split anymore...
what is the reason for this?

P.S.: With snippsat's solution it's just the same...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.