RefactoringRope/CodeAssist.py

Sat, 07 Mar 2015 19:52:36 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 07 Mar 2015 19:52:36 +0100
changeset 104
f6049d39f83d
parent 103
2d77b3afc98f
child 105
45872a13d197
permissions
-rw-r--r--

Made the code completion work in case no project is open.

# -*- 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

import Globals

class CodeAssist(QObject):
    """
    Class implementing the autocompletion interface to rope.
    """
    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 for proposal in proposals]
            return names
        except Exception:
            return []
    
    def getCallTips(self, pos, editor):
        """
        Public method to calculate calltips.
        
        @param editor reference to the editor object, that called this method
            QScintilla.Editor)
        @param pos position in the text for the calltip (integer)
        @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:
            if pos >= len(source) or source[pos] != "(":
                offset = source.rindex("(", 0, pos)
            else:
                offset = pos
            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)
        """
        import rope.base.libutils
        
        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