Source

codeaide / codeaide / modes / python / __init__.py

Full commit
from codeaide.base import *
from codeaide.modes.completer import CompleterBase
from codeaide.modes.indenter import IndenterBase

from ._simple_highlighter import PythonHighlighter

import sys, os; sys.path.insert(0, os.path.join(__path__[-1], "_rope.zip"))
from .rope.base.project import get_no_project
from .rope.contrib.codeassist import code_assist
from ._ropeide_indenter import PythonCodeIndenter as RopePythonIndenter



class RopeEditorWrapper(object):


    def __init__(self, textedit):
        self.textedit = textedit


    def length(self):
        return self.textedit.toPlainText().length()


    def line_editor(self):
        return self


    def get_line(self, line_no=None):
        block = self.textedit.document().findBlockByNumber(line_no)
        return unicode(block.text())


    def insert(self, pos, s):
        cursor = QTextCursor(block)
        cursor.setPosition(pos)
        cursor.insertText(s)


    def indent_line(self, line_no, indent_length):
        block = self.textedit.document().findBlockByNumber(line_no)
        cursor = QTextCursor(block)
        cursor.joinPreviousEditBlock()
        cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
        if indent_length <  0:
            for i in range(-indent_length):
                cursor.deleteChar()
        else:
            cursor.insertText(" " * indent_length)
        if indent_length:
            cursor.movePosition(
                QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
            line = unicode(cursor.block().text())
            if len(line) and line[0] == " ":
                cursor.movePosition(
                    QTextCursor.NextWord, QTextCursor.MoveAnchor)
            self.textedit.setTextCursor(cursor)
        cursor.endEditBlock()




class PythonCompleter(CompleterBase):


    def init_completer(self):
        self.prj = get_no_project()
        self.prj.root = None

    
    def find_completitions(self, pos):
        try:
            items = code_assist(self.prj, unicode(self.textedit.toPlainText()), pos)
        except Exception,  e:
            items = []
        print "completitions at %s:" % pos
        print items
        return items



class PythonHighlighting(ModeBase):
    
    def init(self):
        self.highlighter = PythonHighlighter(self.textedit)
    

        
class PythonIndenting(IndenterBase):
    
    
    def init_indenter(self):
        self.wrapper = RopeEditorWrapper(self.textedit)
        self.rope_indenter = RopePythonIndenter(self.wrapper)

        
    def indent_line(self, num):
        self.rope_indenter.indent(num)
        return True


    def dedent_line(self, num):
        self.rope_indenter.deindent(num)
        return True


    def on_tab(self, *args):
        self.rope_indenter.correct_indentation(self.current_line_number())
        return True


    def on_newline(self, *args):
        self.textedit.insertPlainText("\n")
        self.rope_indenter.entering_new_line(self.current_line_number())
        return True



class Python(ModeBase):
    
    requires = [
        PythonHighlighting, 
        PythonIndenting,
        PythonCompleter
      ]

    def init(self):
        self.textedit.setTabStopWidth(4)
        self.textedit.setLineWrapMode(QPlainTextEdit.NoWrap)