RefactoringRope/CodeAssist.py

Sat, 28 Feb 2015 15:09:53 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 28 Feb 2015 15:09:53 +0100
changeset 100
2bfe9e3fad8d
child 101
5098ad8960ed
permissions
-rw-r--r--

Ported the code completion and calltips support of the eric4 variant of this plug-in.

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

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

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

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 PyQt5.QtGui import *

class CodeAssist(QObject):
    """
    Class implementing the autocompletion interface to rope.
    """
    def __init__(self, refactoring, editor, plugin, parent = None):
        """
        Constructor
        
        @param refactoring reference to the rope refactoring object
            (Refactoring)
        @param editor reference to the editor object, that called this method
            QScintilla.Editor)
        @param plugin reference to the plugin object
        @param parent parent (QObject)
        """
        QObject.__init__(self, parent)
        
        self.__refactoring = refactoring
        self.__editor = editor
        self.__plugin = plugin
    
    def getCompletions(self):
        """
        Public method to calculate the possible completions.
        
        @return list of proposals (QStringList)
        """
        project = self.__refactoring.getProject()
        project.validate(project.root)
        filename = self.__editor.getFileName()
        if filename:
            resource = rope.base.libutils.path_to_resource(project, filename)
        else:
            resource = None
        line, index = self.__editor.getCursorPosition()
        offset = self.__editor.positionFromLineIndex(line, index)
        source = self.__editor.text()
        maxfixes = self.__plugin.getPreferences("MaxFixes")
        try:
            proposals = rope.contrib.codeassist.code_assist(
                project, source, offset, resource, maxfixes = maxfixes)
            proposals = rope.contrib.codeassist.sorted_proposals(proposals)
            names = [proposal.name for proposal in proposals]
            return names
        except Exception:
            return []
    
    def getCallTips(self, pos):
        """
        Public method to calculate calltips.
        
        @param pos position in the text for the calltip (integer)
        @return list of possible calltips (list of strings)
        """
        project = self.__refactoring.getProject()
        project.validate(project.root)
        filename = self.__editor.getFileName()
        if filename:
            resource = rope.base.libutils.path_to_resource(project, filename)
        else:
            resource = None
        source = self.__editor.text()
        maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes")
        try:
            cts = rope.contrib.codeassist.get_calltip(
                project, source, pos, resource, maxfixes = maxfixes,
                remove_self = True)
            if cts is not None:
                cts = [cts]
            else:
                cts = []
        except Exception:
            cts = []
        return cts

eric ide

mercurial