so what I'm doing is building my own python IDE using PyQt.
nothing can do what I need it to without a loss of something else, let alone properly.
so I'm using a QPainter and getting the best results yet.

however I'm having a problem trying to build indent guides like NotePad++ or VS2010 among other IDEs.

what I need to do is build an array specifying the indentation level as an int (basically the amount of tabs to draw) for each line

I've been trying to do it using python's tokenize, however I can't seem to get it to properly indent then dedent.
anyone know of anything better??

thanks :).

Recommended Answers

All 10 Replies

oops, forgot to select for this to go into the python board...
if an admin could please move this there, it would be much appreciated.

thanks :)

I don't see how tokenize can fail: it generates INDENT and DEDENT tokens that you could use to compute the indentation level. Also note that it is better to indent python code with space characters than tabs. A good python editor needs the 4 space indentation.

Unfortunately, I'm not using windows tools such as notepad++ and VS2010, so I don't really understand what you want to do.

You could perhaps find some ideas in the source code of the asciitree module, which produces terminal output similar to the linux tree command.

oh hello, I was not notified of your reply >.>

anyways, I came here to post my code:

    lines = this.data.splitlines(True)
    indents = [0]*(lines.__len__()+2) # 2 because of an index error with erow
    for toktype, toktext, (srow, scol), (erow, ecol), line in tokenize.generate_tokens( iter(lines).next ):

        if toktype == tokenize.INDENT:
            indents[srow-1] = 1
        if toktype == tokenize.DEDENT:
            indents[erow-1] = -1

    pos = 0
    for indent in indents: pos+=indent; this.indents.append(pos)

I got it working as well as it was before, however it still extends past the dedent in some cases
what exactly am I doing wrong??

it still extends past the dedent in some cases what exactly am I doing wrong??

It is difficult to say without an sufficiently detailed example.

Another issue that you may have is that code in the editor may be invalid python code (with unterminated strings or bad unicode characters, ...). Can your code handle this ?

I havn't quite gotten into error testing yet
I'm loading a working script from my other program to test with.

here's a screen of my issue:
http://lh3.ggpht.com/-OADjYNgm5u0/VdsSL_ZINZI/AAAAAAAAJpQ/vmuqH34KQgE/s1600-Ic42/SIDE_indents_issue.png

so the code in my last post is what builds the indents...
here's what parses/draws them:

    for ln,line in enumerate(lines[TextStartY:TextEndY]):
        ln+=TextStartY

        ...

        try:
            painter.setPen( this.indentColor )
            x1 = xpos*this.font_width
            #print this.indents
            for i in range(this.indents[ln]):
                x = x1+(i*4*this.font_width)
                painter.drawLine(x,y1,x,y1+y2)
        except: pass # index error with this.indents[ln], not sure why

Perhaps an error in the computations of y1 an y2 ? Also find the reason of the index error.

fixed the index error :)
not sure what I did, but I'm sure it has something to do with the fixups to the intellisesne database.
(note: when I say "the intellisense database", don't expect too much out of that, it's still being designed)

also y1 is just the start of the line (the text displayed uses this as well)
and y2 should really be labeled h
but no, these work correctly or the text would be messed up too ;)

the indent line is only drawn to the text-height
the painter only displays what's in it's view area ;)

also, I'm a dumb...
I should've posted my full code as well:
https://copy.com/0dasKAPIaSNjsmg9

I've updated the colorize function a bit, so the code above no longer uses erow
but the 2 vars were the same anyways

well, I know what to do, since tokenize can't do this correctly.

python's ast module should be able to return a much more accurate representation for what I need.

do note, the code editor is currently borked due to the implementation of a new regex engine that can handle recursive expressions for a better syntax highlighter (QRegExp() and the multi-expressive implementations I did are faaaar too slow)

EDIT:
I hate this new layout (can't post and answer at the same time)
would've been nice if there was an answer button at the bottom of the page.
stupid modernization >_<

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.