Tue, 26 Sep 2017 19:05:18 +0200
Continued implementing the distributed Code Assist.
# -*- coding: utf-8 -*- # Copyright (c) 2010 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the Rope refactoring plugin. """ from __future__ import unicode_literals import os from PyQt5.QtCore import Qt, QObject, QTranslator, QCoreApplication from E5Gui.E5Application import e5App import Preferences import Utilities # Start-Of-Header name = "Refactoring Rope Plugin" author = "Detlev Offenbach <detlev@die-offenbachs.de>" autoactivate = True deactivateable = True version = "5.0.0" className = "RefactoringRopePlugin" packageName = "RefactoringRope" internalPackages = "rope" shortDescription = "Refactoring using the Rope library." longDescription = """This plug-in implements refactoring functionality""" \ """ using the Rope refactoring library. Additonally it implements an """ \ """ alternative auto-completion and calltips provider. Only""" \ """ refactoring, completions and calltips in the same Python variant""" \ """ as Eric is running is allowed.""" pyqtApi = 2 doNotCompile = True python2Compatible = True # End-Of-Header error = "" refactoringRopePluginObject = None def createAutoCompletionPage(configDlg): """ Module function to create the autocompletion configuration page. @param configDlg reference to the configuration dialog @return reference to the configuration page """ global refactoringRopePluginObject from RefactoringRope.ConfigurationPage.AutoCompletionRopePage \ import AutoCompletionRopePage page = AutoCompletionRopePage(refactoringRopePluginObject) return page def createCallTipsPage(configDlg): """ Module function to create the calltips configuration page. @param configDlg reference to the configuration dialog @return reference to the configuration page """ global refactoringRopePluginObject from RefactoringRope.ConfigurationPage.CallTipsRopePage \ import CallTipsRopePage page = CallTipsRopePage(refactoringRopePluginObject) return page def createMouseClickHandlerPage(configDlg): """ Module function to create the mouse click handler configuration page. @param configDlg reference to the configuration dialog @return reference to the configuration page """ global refactoringRopePluginObject from RefactoringRope.ConfigurationPage.MouseClickHandlerRopePage \ import MouseClickHandlerRopePage page = MouseClickHandlerRopePage(refactoringRopePluginObject) return page def getConfigData(): """ Module function returning data as required by the configuration dialog. @return dictionary containing the relevant data """ data = { "ropeAutoCompletionPage": [ QCoreApplication.translate("RefactoringRopePlugin", "Rope"), os.path.join("RefactoringRope", "ConfigurationPage", "preferences-refactoring.png"), createAutoCompletionPage, "editorAutocompletionPage", None], "ropeCallTipsPage": [ QCoreApplication.translate("RefactoringRopePlugin", "Rope"), os.path.join("RefactoringRope", "ConfigurationPage", "preferences-refactoring.png"), createCallTipsPage, "editorCalltipsPage", None], "ropeMouseClickHandlerPage": [ QCoreApplication.translate("RefactoringRopePlugin", "Rope"), os.path.join("RefactoringRope", "ConfigurationPage", "preferences-refactoring.png"), createMouseClickHandlerPage, "1editorMouseClickHandlers", None], } return data def prepareUninstall(): """ Module function to prepare for an uninstallation. """ Preferences.Prefs.settings.remove(RefactoringRopePlugin.PreferencesKey) class RefactoringRopePlugin(QObject): """ Class implementing the Rope refactoring plugin. """ PreferencesKey = "RefactoringRope" def __init__(self, ui): """ Constructor @param ui reference to the user interface object (UI.UserInterface) """ QObject.__init__(self, ui) self.__ui = ui self.__initialize() self.__defaults = { "CodeAssistEnabled": False, "MaxFixes": 10, "CodeAssistCalltipsEnabled": False, "CalltipsMaxFixes": 10, "MouseClickEnabled": True, "MouseClickGotoModifiers": int(Qt.ControlModifier), "MouseClickGotoButton": int(Qt.LeftButton), } self.__translator = None self.__loadTranslator() def __initialize(self): """ Private slot to (re)initialize the plugin. """ self.__refactoringServer = None self.__codeAssistServer = None self.__editors = [] self.__currentEditor = None self.__savedEditorName = None self.__oldEditorText = "" def activate(self): """ Public method to activate this plugin. @return tuple of None and activation status (boolean) """ global refactoringRopePluginObject refactoringRopePluginObject = self e5App().getObject("PluginManager").shutdown.connect( self.__shutdown) from RefactoringRope.CodeAssistServer import CodeAssistServer self.__codeAssistServer = CodeAssistServer(self, self.__ui) from RefactoringRope.RefactoringServer import RefactoringServer self.__refactoringServer = RefactoringServer(self, self.__ui) self.__refactoringServer.activate() e5App().getObject("PluginManager").shutdown.connect( self.__shutdown) e5App().getObject("ViewManager").editorOpenedEd.connect( self.__editorOpened) e5App().getObject("ViewManager").editorClosedEd.connect( self.__editorClosed) if e5App().getObject("Project").isOpen(): for editor in e5App().getObject("ViewManager").getOpenEditors(): self.__editorOpened(editor) return None, True def deactivate(self): """ Public method to deactivate this plugin. """ self.__refactoringServer.deactivate() self.__codeAssistServer.deactivate() e5App().getObject("ViewManager").editorOpenedEd.disconnect( self.__editorOpened) e5App().getObject("ViewManager").editorClosedEd.disconnect( self.__editorClosed) for editor in self.__editors[:]: self.__editorClosed(editor) self.__initialize() def __shutdown(self): """ Private slot handling the shutdown signal of the plug-in manager. """ if self.__codeAssistServer: self.__codeAssistServer.deactivate() ## if self.__refactoringServer: ## self.__refactoringServer.deactivate() def __loadTranslator(self): """ Private method to load the translation file. """ if self.__ui is not None: loc = self.__ui.getLocale() if loc and loc != "C": locale_dir = \ os.path.join(os.path.dirname(__file__), "RefactoringRope", "i18n") translation = "rope_{0}".format(loc) translator = QTranslator(None) loaded = translator.load(translation, locale_dir) if loaded: self.__translator = translator e5App().installTranslator(self.__translator) else: print("Warning: translation file '{0}' could not" " be loaded.".format(translation)) print("Using default.") def getPreferences(self, key): """ Public method to retrieve the various refactoring settings. @param key the key of the value to get @return the requested refactoring setting """ if key in ["CodeAssistEnabled", "CodeAssistCalltipsEnabled", "MouseClickEnabled"]: return Preferences.toBool(Preferences.Prefs.settings.value( self.PreferencesKey + "/" + key, self.__defaults[key])) else: return int(Preferences.Prefs.settings.value( self.PreferencesKey + "/" + key, self.__defaults[key])) def setPreferences(self, key, value): """ Public method to store the various refactoring settings. @param key the key of the setting to be set (string) @param value the value to be set """ Preferences.Prefs.settings.setValue( self.PreferencesKey + "/" + key, value) if key in ["CodeAssistEnabled", "CodeAssistCalltipsEnabled", "MouseClickEnabled"]: if value: if e5App().getObject("Project").isOpen(): for editor in e5App().getObject("ViewManager")\ .getOpenEditors(): if editor not in self.__editors: self.__editorOpened(editor) else: for editor in self.__editors[:]: self.__editorClosed(editor) elif key in ["MouseClickGotoModifiers", "MouseClickGotoButton"]: for editor in self.__editors: self.__disconnectMouseClickHandler(editor) self.__connectMouseClickHandler(editor) # TODO: get this info from CodeAssistClient or Server def __determineLanguages(self): """ Private method to determine the valid language strings. @return list of valid language strings (list of string) """ langs = [] interpreter = Preferences.getDebugger("PythonInterpreter") if interpreter and Utilities.isinpath(interpreter): langs.extend(["Python", "Python2", "Pygments|Python"]) interpreter = Preferences.getDebugger("Python3Interpreter") if interpreter and Utilities.isinpath(interpreter): langs.extend(["Python3", "Pygments|Python 3"]) return langs # TODO: move this to CodeAssistServer def __editorOpened(self, editor): """ Private slot called, when a new editor was opened. @param editor reference to the new editor (QScintilla.Editor) """ langs = self.__determineLanguages() if editor.getLanguage() in langs: self.__connectEditor(editor) editor.languageChanged.connect(self.__editorLanguageChanged) self.__editors.append(editor) # TODO: move this to CodeAssistServer def __editorClosed(self, editor): """ Private slot called, when an editor was closed. @param editor reference to the editor (QScintilla.Editor) """ if editor in self.__editors: editor.languageChanged.disconnect(self.__editorLanguageChanged) self.__disconnectEditor(editor) self.__editors.remove(editor) # TODO: move this to CodeAssistServer def __editorLanguageChanged(self, language): """ Private slot to handle the language change of an editor. @param language programming language of the editor (string) """ editor = self.sender() langs = self.__determineLanguages() if language in langs: if editor.getCompletionListHook("rope") is None or \ editor.getCallTipHook("rope") is None: self.__connectEditor(editor) else: self.__disconnectEditor(editor) def __connectEditor(self, editor): """ Private method to connect an editor. @param editor reference to the editor (QScintilla.Editor) """ editor.editorAboutToBeSaved.connect(self.__editorAboutToBeSaved) editor.editorSaved.connect(self.__editorSaved) # TODO: move this to CodeAssistServer if self.getPreferences("CodeAssistEnabled"): self.__setAutoCompletionHook(editor) if self.getPreferences("CodeAssistCalltipsEnabled"): self.__setCalltipsHook(editor) if self.getPreferences("MouseClickEnabled"): self.__disconnectMouseClickHandler(editor) self.__connectMouseClickHandler(editor) def __connectMouseClickHandler(self, editor): """ Private method to connect the mouse click handler to an editor. @param editor reference to the editor (QScintilla.Editor) """ if self.getPreferences("MouseClickGotoButton"): editor.setMouseClickHandler( "rope", self.getPreferences("MouseClickGotoModifiers"), self.getPreferences("MouseClickGotoButton"), self.__refactoringServer.gotoDefinition ) def __disconnectEditor(self, editor): """ Private method to disconnect an editor. @param editor reference to the editor (QScintilla.Editor) """ try: editor.editorAboutToBeSaved.disconnect(self.__editorAboutToBeSaved) editor.editorSaved.disconnect(self.__editorSaved) except TypeError: # just ignore it pass # TODO: move this to CodeAssistServer if editor.getCompletionListHook("rope"): self.__unsetAutoCompletionHook(editor) if editor.getCallTipHook("rope"): self.__unsetCalltipsHook(editor) self.__disconnectMouseClickHandler(editor) # TODO: move this to CodeAssistServer def __disconnectMouseClickHandler(self, editor): """ Private method to disconnect the mouse click handler from an editor. @param editor reference to the editor (QScintilla.Editor) """ editor.removeMouseClickHandlers("rope") # TODO: move this to CodeAssistServer def __setAutoCompletionHook(self, editor): """ Private method to set the autocompletion hook. @param editor reference to the editor (QScintilla.Editor) """ editor.addCompletionListHook("rope", self.getCompletionsList) # TODO: move this to CodeAssistServer def __unsetAutoCompletionHook(self, editor): """ Private method to unset the autocompletion hook. @param editor reference to the editor (QScintilla.Editor) """ editor.removeCompletionListHook("rope") # TODO: move this to CodeAssistServer def getCompletionsList(self, editor, context): """ Public method to get a list of possible completions. @param editor reference to the editor object, that called this method (QScintilla.Editor) @param context flag indicating to autocomplete a context (boolean) @return list of possible completions (list of strings) """ completions = self.__codeAssistServer.getCompletions(editor) return completions def __editorAboutToBeSaved(self, filename): """ Private slot to get the old contents of the named file. @param filename name of the file about to be saved (string) """ if filename and os.path.exists(filename): try: self.__oldEditorText = Utilities.readEncodedFile(filename)[0] except IOError: self.__oldEditorText = "" self.__savedEditorName = filename else: self.__savedEditorName = "" self.__oldEditorText = "" def __editorSaved(self, filename): """ Private slot to activate SOA. @param filename name of the file that was saved (string) """ if filename == self.__savedEditorName and self.__oldEditorText: self.__refactoringServer.reportChanged(self.__savedEditorName, self.__oldEditorText) self.__codeAssistServer.reportChanged(self.__savedEditorName, self.__oldEditorText) else: self.__refactoringServer.reportChanged(filename, "") self.__codeAssistServer.reportChanged(filename, "") # TODO: move this to CodeAssistServer def __setCalltipsHook(self, editor): """ Private method to set the calltip hook. @param editor reference to the editor (QScintilla.Editor) """ editor.addCallTipHook("rope", self.codeAssistCallTip) # TODO: move this to CodeAssistServer def __unsetCalltipsHook(self, editor): """ Private method to unset the calltip hook. @param editor reference to the editor (QScintilla.Editor) """ editor.removeCallTipHook("rope") # TODO: move this to CodeAssistServer def codeAssistCallTip(self, editor, pos, commas): """ Public method to return a list of calltips. @param editor reference to the editor (QScintilla.Editor) @param pos position in the text for the calltip (integer) @param commas minimum number of commas contained in the calltip (integer) @return list of possible calltips (list of strings) """ cts = self.__codeAssistServer.getCallTips(pos, editor) return cts # # eflag: noqa = M801