--- a/RefactoringRope/CodeAssistServer.py Sun Jul 25 12:16:21 2021 +0200 +++ b/RefactoringRope/CodeAssistServer.py Fri Jul 30 16:38:13 2021 +0200 @@ -7,9 +7,10 @@ Module implementing the autocompletion interface to rope. """ +import contextlib import os import sys -import contextlib +import uuid from PyQt6.QtCore import ( pyqtSlot, QCoreApplication, QTimer @@ -81,6 +82,7 @@ "CallTipsResult": self.__processCallTipsResult, "DocumentationResult": self.__processDocumentationResult, "GotoDefinitionResult": self.__gotoDefinitionResult, + "GotoReferencesResult": self.__processGotoReferencesResult, "ClientException": self.__processClientException, } @@ -97,6 +99,9 @@ "<unknown>": self.tr("not known"), } + # temporary store for editor references indexed by Uuid + self.__editors = {} + # Python 3 self.__ensureActive("Python3") @@ -460,11 +465,16 @@ offset = len("".join(source.splitlines(True)[:line])) + index self.__ensureActive(idString) + + euuid = str(uuid.uuid4()) + self.__editors[euuid] = editor + self.sendJson("gotoDefinition", { "FileName": filename, "Offset": offset, - "Source": editor.text(), + "Source": source, "SysPath": sys.path, + "Uuid": euuid, }, idString=idString) def __gotoDefinitionResult(self, result): @@ -477,14 +487,80 @@ """ if "Error" not in result: # ignore errors silently - if "Location" in result: - location = result["Location"] + location = result["Location"] + euuid = result["Uuid"] + if location: + try: + editor = self.__editors[euuid] + except KeyError: + editor = None + + if ( + editor is not None and + editor.getFileName() == location["ModulePath"] and + editor.getCursorPosition()[0] + 1 == location["Line"] + ): + # this was a click onto the definition line + # -> get references + idString = self.__idString(editor) + filename = editor.getFileName() + source = editor.text() + line, index = editor.getCursorPosition() + offset = ( + len("".join(source.splitlines(True)[:line])) + + index + ) + self.sendJson("gotoReferences", { + "FileName": filename, + "Offset": offset, + "Line": line + 1, + "SysPath": sys.path, + "Uuid": euuid, + }, idString=idString) + return + self.__vm.openSourceFile(location["ModulePath"], location["Line"], addNext=True) else: ericApp().getObject("UserInterface").statusBar().showMessage( self.tr('Code Assist: No definition found'), 5000) + + with contextlib.suppress(KeyError): + del self.__editors[euuid] + + def __processGotoReferencesResult(self, result): + """ + Private method callback for the goto references result. + + @param result dictionary containing the result data + @type dict + """ + with contextlib.suppress(ImportError): + from QScintilla.Editor import ReferenceItem + + if "Error" not in result: + # ignore errors silently + references = result["GotoReferencesList"] + euuid = result["Uuid"] + if references: + try: + editor = self.__editors[euuid] + except KeyError: + editor = None + if editor is not None: + referenceItemsList = [ + ReferenceItem( + modulePath=ref["ModulePath"], + codeLine=ref["Code"], + line=ref["Line"], + column=0, + ) for ref in references + ] + editor.gotoReferenceHandler(referenceItemsList) + + with contextlib.suppress(KeyError): + del self.__editors[euuid] ####################################################################### ## Methods below handle the network connection