RefactoringRope/CodeAssist.py

Sat, 07 Mar 2015 15:57:24 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 07 Mar 2015 15:57:24 +0100
changeset 103
2d77b3afc98f
parent 101
5098ad8960ed
child 104
f6049d39f83d
permissions
-rw-r--r--

Changed the way the offset into the text gets calculated because rope handles unicode characters as one character (i.e. string) and QScintilla as multiple (i.e. bytes).

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

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()
        source = self.__editor.text()
        offset = len("".join(source.splitlines(True)[:line])) + index
        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