Help for syntax highlighting

  • I did a little bit of research in Google and the forum, but can’t find a sollution so far.

    I often work with files like these:


    It would be really helpful if I could split these strings in different blocks and also colour them differently, to make it more readable.
    Best would be, if it would also stretch it out a little bit, that it becomes a little bit like a table.
    Is that possible with the UDL?

    my proposal

  • Hello Marco Bimbös,

    afaik, UDL cannot help you here.
    What could be done, in regards to formatting, is to use the Python Script plugin and a little script.
    I’have written here what needs to be done first.

    Regarding colorizing, it could be done but I don’t think a python script would be a good solution.
    From what I understand, colorizing is done when you activate a new document, repeatedly but
    a script runs once, normally.
    If you use callbacks like BUFFERACTIVATED then you need to take care that there is a logical
    way when the script should be executed and when not.
    Lastly, the addStyledText function is not that fast. Colorizing 100 lines of your provided sample
    takes up to 3 seconds.

    So, a python script could look like this

    a = [[] for x in editor.getLine(0).split(";")]   # create a list of lists using the first line in the doc
    def ssv_to_list(cont, l_num, tot_lines):        # convert semicolon separated values to a list
        cont = cont.split(";") 
        for n in range(len(cont)): 
            a[n].append(cont[n].replace("\r\n", ""))
    editor.forEachLine(ssv_to_list)     # well, self explaining I guess
    aLens = [max([len(x) for x in a[y]]) for y in range(len(a))]    # find out what the biggest string is
    def format_line(l_num):        # format the lines
        txt = ''
        spacer = 1     # can be used to increace space between columns
        for x in range(len(aLens) - 1):   # loop
            if x == 4 :     # your example used one left justified column
                txt += "{0:<{1}}".format(' ',spacer) + a[x][l_num].ljust(aLens[x])
            else:      # right justified
                txt += a[x][l_num].rjust(aLens[x]+spacer)
        return txt + '\r\n'   # add line breaks     # open a new doc
    big_txt = ''
    for l in range(len(a[0])):  # loop through the list of lists saved in ssv_to_list
        big_txt += format_line(l)     # create the new formatted text
    editor.addText(big_txt)     # add it to editor.


  • Nice job Claudia. I actually tried this out. The only (slight) negative is the x==4 part, which binds it to the data set rather tightly (but that was what the user wanted). I could see this being used to do a quick reformat on CSV data with a few tweaks. Again, good job.

  • Hello Scott,

    thank you for your kind words, you are right, this script, as it is, is only useful
    if you have a data set similar to the one Marco has.
    For those who want to have a more general solution replace the format_line
    function with this one

    def format_line(l_num):
        txt = ''
        spacer = 1
        for x in range(len(aLens) - 1):
            txt += a[x][l_num].rjust(aLens[x]+spacer)
        return txt + '\r\n'