RefactoringRope/CodeAssist.py

Sun, 22 Mar 2015 18:58:31 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 22 Mar 2015 18:58:31 +0100
changeset 119
a03f2be1997b
parent 118
d242ba11a04c
child 144
bfc6a3b38330
permissions
-rw-r--r--

Added some eye-candy and made the method to get a list of completions publicly available.

# -*- coding: utf-8 -*-

# Copyright (c) 2008 - 2015 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the autocompletion interface to rope.
"""

from __future__ import unicode_literals

import os
import sys

sys.path.insert(0, os.path.dirname(__file__))
if sys.version_info[0] >= 3:
    path = os.path.join(os.path.dirname(__file__), 'rope_py3')
else:
    path = os.path.join(os.path.dirname(__file__), 'rope_py2')
    str = unicode   # __IGNORE_WARNING__
sys.path.insert(0, path)

import rope.base.libutils
import rope.contrib.codeassist

from PyQt5.QtCore import QObject

from QScintilla.Editor import Editor

import Globals


class CodeAssist(QObject):
    """
    Class implementing the autocompletion interface to rope.
    """
    PictureIDs = {
        "class": "?{0}".format(Editor.ClassID),
        "instance": "?{0}".format(Editor.ClassID),
        "function": "?{0}".format(Editor.MethodID),
        "module": "?{0}".format(Editor.AttributeID),
        "None": "",
    }
    
    def __init__(self, plugin, parent=None):
        """
        Constructor
        
        @param plugin reference to the plugin object
        @param parent parent (QObject)
        """
        QObject.__init__(self, parent)
        
        self.__plugin = plugin
        
        self.__project = rope.base.project.Project(Globals.getConfigDir())
        self.__project.validate(self.__project.root)
    
    def getCompletions(self, editor):
        """
        Public method to calculate the possible completions.
        
        @param editor reference to the editor object, that called this method
            QScintilla.Editor)
        @return list of proposals (QStringList)
        """
        filename = editor.getFileName()
        if filename:
            resource = rope.base.libutils.path_to_resource(
                self.__project, filename)
        else:
            resource = None
        line, index = editor.getCursorPosition()
        source = editor.text()
        offset = len("".join(source.splitlines(True)[:line])) + index
        maxfixes = self.__plugin.getPreferences("MaxFixes")
        try:
            proposals = rope.contrib.codeassist.code_assist(
                self.__project, source, offset, resource, maxfixes=maxfixes)
            proposals = rope.contrib.codeassist.sorted_proposals(proposals)
            names = [proposal.name + self.PictureIDs[proposal.type]
                     for proposal in proposals]
            return names
        except Exception:
            return []
    
    def getCallTips(self, pos, editor):
        """
        Public method to calculate calltips.
        
        @param pos position in the text for the calltip (integer)
        @param editor reference to the editor object, that called this method
            QScintilla.Editor)
        @return list of possible calltips (list of strings)
        """
        filename = editor.getFileName()
        if filename:
            resource = rope.base.libutils.path_to_resource(
                self.__project, filename)
        else:
            resource = None
        source = editor.text()
        maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes")
        try:
            line, index = editor.lineIndexFromPosition(pos)
            offset = len("".join(source.splitlines(True)[:line])) + index
            cts = rope.contrib.codeassist.get_calltip(
                self.__project, source, offset, resource, maxfixes=maxfixes,
                remove_self=True)
            if cts is not None:
                cts = [cts]
            else:
                cts = []
        except Exception:
            cts = []
        return cts
    
    def reportChanged(self, filename, oldSource):
        """
        Public slot to report some changed sources.
        
        @param filename file name of the changed source (string)
        @param oldSource source code before the change (string)
        """
        try:
            rope.base.libutils.report_change(
                self.__project, filename, oldSource)
        except RuntimeError:
            # this could come from trying to do PyQt4/PyQt5 mixed stuff
            # simply ignore it
            pass

eric ide

mercurial