Mon, 25 Sep 2017 20:08:59 +0200
Continued implementing the distributed Code Assist.
# -*- coding: utf-8 -*- # Copyright (c) 2008 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the autocompletion interface to rope. """ from __future__ import unicode_literals import os import sys from PyQt5.QtCore import pyqtSlot 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 QScintilla.Editor import Editor from JsonServer import JsonServer import Globals import Preferences class CodeAssistServer(JsonServer): """ 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 @type RefactoringRopePlugin @param parent parent @type QObject """ super(CodeAssistServer, self).__init__(multiplex=True, parent=parent) self.__plugin = plugin self.__ui = parent self.__editorLanguageMapping = {} # Python 2 interpreter = Preferences.getDebugger("PythonInterpreter") self.__startCodeAssistClient(interpreter, "Python2") # Python 3 interpreter = Preferences.getDebugger("Python3Interpreter") self.__startCodeAssistClient(interpreter, "Python3") ## self.__project = rope.base.project.Project(Globals.getConfigDir()) ## self.__project.validate(self.__project.root) def __updateEditorLanguageMapping(self): """ Private method to update the editor language to connection mapping. """ self.__editorLanguageMapping = {} for name in self.connectionNames(): if name == "Python2": self.__editorLanguageMapping.update({ "Python": "Python2", "Python2": "Python2", "Pygments|Python": "Python2", }) elif name == "Python3": self.__editorLanguageMapping.update({ "Python3": "Python3", "Pygments|Python 3": "Python3", }) # TODO: port this to the distributed variant 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) """ return [] ## 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 [] # TODO: port this to the distributed variant 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) """ return [] ## 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 # TODO: port this to the distributed variant 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 ####################################################################### ## Methods below handle the network connection ####################################################################### def __startCodeAssistClient(self, interpreter, idString): """ Private method to start the code assist client with the given interpreter. @param interpreter interpreter to be used for the code assist client @type str @param idString id of the client to be started @type str @return flag indicating a successful client start @rtype bool """ if interpreter: client = os.path.join(os.path.dirname(__file__), "CodeAssistClient.py") ok = self.startClient(interpreter, client, [Globals.getConfigDir()], idString=idString) if not ok: self.__ui.appendToStderr(self.tr( "'{0}' is not supported because the configured interpreter" " could not be started." ).format(idString)) else: self.__ui.appendToStderr(self.tr( "'{0}' is not supported because no suitable interpreter is" " configured." ).format(idString)) @pyqtSlot() def handleNewConnection(self): """ Public slot for new incoming connections from a client. """ super(CodeAssistServer, self).handleNewConnection() self.__updateEditorLanguageMapping()