RefactoringRope/RefactoringServer.py

branch
server_client_variant
changeset 212
f05681349336
parent 208
df77c3a4976d
child 217
874115c79ca7
equal deleted inserted replaced
211:891b660bcbda 212:f05681349336
48 super(RefactoringServer, self).__init__( 48 super(RefactoringServer, self).__init__(
49 "RefactoringServer", parent=parent) 49 "RefactoringServer", parent=parent)
50 50
51 self.__plugin = plugin 51 self.__plugin = plugin
52 self.__ui = parent 52 self.__ui = parent
53 self.__vm = e5App().getObject("ViewManager")
53 self.__e5project = e5App().getObject("Project") 54 self.__e5project = e5App().getObject("Project")
54 self.__projectpath = '' 55 self.__projectpath = ''
55 self.__projectLanguage = "" 56 self.__projectLanguage = ""
56 self.__projectopen = False 57 self.__projectopen = False
57 self.__ropeConfig = {} 58 self.__ropeConfig = {}
828 829
829 def __showRefactoringHistoryMenu(self): 830 def __showRefactoringHistoryMenu(self):
830 """ 831 """
831 Private slot called before the refactoring history menu is shown. 832 Private slot called before the refactoring history menu is shown.
832 """ 833 """
833 aw = e5App().getObject("ViewManager").activeWindow() 834 aw = self.__vm.activeWindow()
834 enable = aw is not None and bool(aw.getFileName()) 835 enable = aw is not None and bool(aw.getFileName())
835 836
836 self.refactoringFileHistoryAct.setEnabled(enable) 837 self.refactoringFileHistoryAct.setEnabled(enable)
837 838
838 def handleRopeError(self, result): 839 def handleRopeError(self, result):
857 self.__ui, title, 858 self.__ui, title,
858 self.tr("Rope error: {0}").format( 859 self.tr("Rope error: {0}").format(
859 result["ErrorString"]), 860 result["ErrorString"]),
860 E5MessageBox.Ok | E5MessageBox.Open) 861 E5MessageBox.Ok | E5MessageBox.Open)
861 if res == E5MessageBox.Open: 862 if res == E5MessageBox.Open:
862 e5App().getObject("ViewManager").openSourceFile( 863 self.__vm.openSourceFile(
863 os.path.join(self.__e5project.getProjectPath(), 864 os.path.join(self.__e5project.getProjectPath(),
864 result["ErrorFile"]), 865 result["ErrorFile"]),
865 result["ErrorLine"]) 866 result["ErrorLine"])
866 elif result["Error"] == "InterruptedTaskError": 867 elif result["Error"] == "InterruptedTaskError":
867 return True 868 return True
964 the local file 965 the local file
965 @type bool 966 @type bool
966 @param renameModule flag indicating a module rename refactoring 967 @param renameModule flag indicating a module rename refactoring
967 @type bool 968 @type bool
968 """ 969 """
969 aw = e5App().getObject("ViewManager").activeWindow() 970 aw = self.__vm.activeWindow()
970 971
971 if aw is None: 972 if aw is None:
972 return 973 return
973 974
974 if not renameModule and not aw.hasSelectedText(): 975 if not renameModule and not aw.hasSelectedText():
1015 1016
1016 def __changeOccurrences(self): 1017 def __changeOccurrences(self):
1017 """ 1018 """
1018 Private slot to perform the Change Occurrences refactoring. 1019 Private slot to perform the Change Occurrences refactoring.
1019 """ 1020 """
1020 aw = e5App().getObject("ViewManager").activeWindow() 1021 aw = self.__vm.activeWindow()
1021 1022
1022 if aw is None: 1023 if aw is None:
1023 return 1024 return
1024 1025
1025 title = self.tr("Change Occurrences") 1026 title = self.tr("Change Occurrences")
1072 @param kind kind of extraction to be done 1073 @param kind kind of extraction to be done
1073 @type str ("method" or "variable") 1074 @type str ("method" or "variable")
1074 """ 1075 """
1075 assert kind in ["variable", "method"] 1076 assert kind in ["variable", "method"]
1076 1077
1077 aw = e5App().getObject("ViewManager").activeWindow() 1078 aw = self.__vm.activeWindow()
1078 1079
1079 if aw is None: 1080 if aw is None:
1080 return 1081 return
1081 1082
1082 if not aw.hasSelectedText(): 1083 if not aw.hasSelectedText():
1110 1111
1111 def __inline(self): 1112 def __inline(self):
1112 """ 1113 """
1113 Private slot to handle the Inline Local Variable action. 1114 Private slot to handle the Inline Local Variable action.
1114 """ 1115 """
1115 aw = e5App().getObject("ViewManager").activeWindow() 1116 aw = self.__vm.activeWindow()
1116 1117
1117 if aw is None: 1118 if aw is None:
1118 return 1119 return
1119 1120
1120 title = self.tr("Inline") 1121 title = self.tr("Inline")
1150 Private slot to handle the Move Method action. 1151 Private slot to handle the Move Method action.
1151 1152
1152 @param moveKind kind of move to be performed 1153 @param moveKind kind of move to be performed
1153 @type str (one of 'move_method' or 'move_module') 1154 @type str (one of 'move_method' or 'move_module')
1154 """ 1155 """
1155 aw = e5App().getObject("ViewManager").activeWindow() 1156 aw = self.__vm.activeWindow()
1156 1157
1157 if aw is None: 1158 if aw is None:
1158 return 1159 return
1159 1160
1160 if moveKind == "move_method": 1161 if moveKind == "move_method":
1193 1194
1194 def __useFunction(self): 1195 def __useFunction(self):
1195 """ 1196 """
1196 Private slot to use a function wherever possible. 1197 Private slot to use a function wherever possible.
1197 """ 1198 """
1198 aw = e5App().getObject("ViewManager").activeWindow() 1199 aw = self.__vm.activeWindow()
1199 1200
1200 if aw is None: 1201 if aw is None:
1201 return 1202 return
1202 1203
1203 title = self.tr("Use Function") 1204 title = self.tr("Use Function")
1230 1231
1231 def __introduceFactoryMethod(self): 1232 def __introduceFactoryMethod(self):
1232 """ 1233 """
1233 Private slot to introduce a factory method or global function. 1234 Private slot to introduce a factory method or global function.
1234 """ 1235 """
1235 aw = e5App().getObject("ViewManager").activeWindow() 1236 aw = self.__vm.activeWindow()
1236 1237
1237 if aw is None: 1238 if aw is None:
1238 return 1239 return
1239 1240
1240 title = self.tr("Introduce Factory Method") 1241 title = self.tr("Introduce Factory Method")
1264 1265
1265 def __introduceParameter(self): 1266 def __introduceParameter(self):
1266 """ 1267 """
1267 Private slot to introduce a parameter in a function. 1268 Private slot to introduce a parameter in a function.
1268 """ 1269 """
1269 aw = e5App().getObject("ViewManager").activeWindow() 1270 aw = self.__vm.activeWindow()
1270 1271
1271 if aw is None: 1272 if aw is None:
1272 return 1273 return
1273 1274
1274 title = self.tr("Introduce Parameter") 1275 title = self.tr("Introduce Parameter")
1347 @param title title to be used for the import refactoring 1348 @param title title to be used for the import refactoring
1348 @type str 1349 @type str
1349 @param methodName name of the method performing the import refactoring 1350 @param methodName name of the method performing the import refactoring
1350 @type str 1351 @type str
1351 """ 1352 """
1352 aw = e5App().getObject("ViewManager").activeWindow() 1353 aw = self.__vm.activeWindow()
1353 1354
1354 if aw is None: 1355 if aw is None:
1355 return 1356 return
1356 1357
1357 if not self.confirmBufferIsSaved(aw): 1358 if not self.confirmBufferIsSaved(aw):
1397 1398
1398 def __changeSignature(self): 1399 def __changeSignature(self):
1399 """ 1400 """
1400 Private slot to change the signature of a method or function. 1401 Private slot to change the signature of a method or function.
1401 """ 1402 """
1402 aw = e5App().getObject("ViewManager").activeWindow() 1403 aw = self.__vm.activeWindow()
1403 1404
1404 if aw is None: 1405 if aw is None:
1405 return 1406 return
1406 1407
1407 title = self.tr("Change Method Signature") 1408 title = self.tr("Change Method Signature")
1432 def __inlineArgumentDefault(self): 1433 def __inlineArgumentDefault(self):
1433 """ 1434 """
1434 Private slot to inline the default value of a parameter of a 1435 Private slot to inline the default value of a parameter of a
1435 method or function. 1436 method or function.
1436 """ 1437 """
1437 aw = e5App().getObject("ViewManager").activeWindow() 1438 aw = self.__vm.activeWindow()
1438 1439
1439 if aw is None: 1440 if aw is None:
1440 return 1441 return
1441 1442
1442 title = self.tr("Inline Argument Default") 1443 title = self.tr("Inline Argument Default")
1466 1467
1467 def __transformModuleToPackage(self): 1468 def __transformModuleToPackage(self):
1468 """ 1469 """
1469 Private slot to transform a module to a package. 1470 Private slot to transform a module to a package.
1470 """ 1471 """
1471 aw = e5App().getObject("ViewManager").activeWindow() 1472 aw = self.__vm.activeWindow()
1472 1473
1473 if aw is None: 1474 if aw is None:
1474 return 1475 return
1475 1476
1476 title = self.tr("Transform Module to Package") 1477 title = self.tr("Transform Module to Package")
1495 1496
1496 def __encapsulateAttribute(self): 1497 def __encapsulateAttribute(self):
1497 """ 1498 """
1498 Private slot to encapsulate an attribute. 1499 Private slot to encapsulate an attribute.
1499 """ 1500 """
1500 aw = e5App().getObject("ViewManager").activeWindow() 1501 aw = self.__vm.activeWindow()
1501 1502
1502 if aw is None: 1503 if aw is None:
1503 return 1504 return
1504 1505
1505 title = self.tr("Encapsulate Attribute") 1506 title = self.tr("Encapsulate Attribute")
1529 1530
1530 def __convertLocalToAttribute(self): 1531 def __convertLocalToAttribute(self):
1531 """ 1532 """
1532 Private slot to convert a local variable to an attribute. 1533 Private slot to convert a local variable to an attribute.
1533 """ 1534 """
1534 aw = e5App().getObject("ViewManager").activeWindow() 1535 aw = self.__vm.activeWindow()
1535 1536
1536 if aw is None: 1537 if aw is None:
1537 return 1538 return
1538 1539
1539 title = self.tr("Local Variable to Attribute") 1540 title = self.tr("Local Variable to Attribute")
1568 1569
1569 def __methodToMethodObject(self): 1570 def __methodToMethodObject(self):
1570 """ 1571 """
1571 Private slot to change the signature of a method or function. 1572 Private slot to change the signature of a method or function.
1572 """ 1573 """
1573 aw = e5App().getObject("ViewManager").activeWindow() 1574 aw = self.__vm.activeWindow()
1574 1575
1575 if aw is None: 1576 if aw is None:
1576 return 1577 return
1577 1578
1578 title = self.tr("Replace Method With Method Object") 1579 title = self.tr("Replace Method With Method Object")
1618 1619
1619 def __showFileHistory(self): 1620 def __showFileHistory(self):
1620 """ 1621 """
1621 Private method to show the refactoring history of the current file. 1622 Private method to show the refactoring history of the current file.
1622 """ 1623 """
1623 aw = e5App().getObject("ViewManager").activeWindow() 1624 aw = self.__vm.activeWindow()
1624 1625
1625 if aw is None: 1626 if aw is None:
1626 return 1627 return
1627 1628
1628 if self.__historyDialog is not None: 1629 if self.__historyDialog is not None:
1674 1675
1675 def __queryReferences(self): 1676 def __queryReferences(self):
1676 """ 1677 """
1677 Private slot to handle the Find References action. 1678 Private slot to handle the Find References action.
1678 """ 1679 """
1679 aw = e5App().getObject("ViewManager").activeWindow() 1680 aw = self.__vm.activeWindow()
1680 1681
1681 if aw is None: 1682 if aw is None:
1682 return 1683 return
1683 1684
1684 title = self.tr("Find Occurrences") 1685 title = self.tr("Find Occurrences")
1721 1722
1722 def __queryDefinition(self): 1723 def __queryDefinition(self):
1723 """ 1724 """
1724 Private slot to handle the Find Definition action. 1725 Private slot to handle the Find Definition action.
1725 """ 1726 """
1726 aw = e5App().getObject("ViewManager").activeWindow() 1727 aw = self.__vm.activeWindow()
1727 1728
1728 if aw is None: 1729 if aw is None:
1729 return 1730 return
1730 1731
1731 title = self.tr("Find Definition") 1732 title = self.tr("Find Definition")
1753 Note: This is executed upon a mouse click sequence. 1754 Note: This is executed upon a mouse click sequence.
1754 1755
1755 @param editor reference to the calling editor 1756 @param editor reference to the calling editor
1756 @type QScintilla.Editor 1757 @type QScintilla.Editor
1757 """ 1758 """
1758 filename = editor.getFileName() 1759 if self.__projectopen:
1759 line, index = editor.getCursorPosition() 1760 filename = editor.getFileName()
1760 offset = self.__getOffset(editor, line, index) 1761 line, index = editor.getCursorPosition()
1761 1762 offset = self.__getOffset(editor, line, index)
1762 self.sendJson("QueryDefinition", { 1763
1763 "Title": "", 1764 self.sendJson("QueryDefinition", {
1764 "FileName": filename, 1765 "Title": "",
1765 "Offset": offset, 1766 "FileName": filename,
1766 "Source": editor.text(), 1767 "Offset": offset,
1767 "Subcommand": "Goto", 1768 "Source": editor.text(),
1768 }) 1769 "Subcommand": "Goto",
1770 })
1769 1771
1770 def __queryDefinitionResult(self, result): 1772 def __queryDefinitionResult(self, result):
1771 """ 1773 """
1772 Private method to handle the "Query Definition" result sent by 1774 Private method to handle the "Query Definition" result sent by
1773 the client. 1775 the client.
1794 if "Error" not in result: 1796 if "Error" not in result:
1795 # ignore errors silently 1797 # ignore errors silently
1796 if "Location" in result: 1798 if "Location" in result:
1797 location = result["Location"] 1799 location = result["Location"]
1798 try: 1800 try:
1799 e5App().getObject("ViewManager").openSourceFile( 1801 self.__vm.openSourceFile(
1800 location[0], location[1], addNext=True) 1802 location[0], location[1], addNext=True)
1801 except TypeError: 1803 except TypeError:
1802 # backward compatibility; <= 17.03 1804 # backward compatibility; <= 17.03
1803 e5App().getObject("ViewManager").openSourceFile( 1805 self.__vm.openSourceFile(
1804 location[0], location[1], next=True) 1806 location[0], location[1], next=True)
1805 else: 1807 else:
1806 e5App().getObject("UserInterface").statusBar().showMessage( 1808 e5App().getObject("UserInterface").statusBar().showMessage(
1807 self.tr('No definition found'), 5000) 1809 self.tr('No definition found'), 5000)
1808 1810
1809 def __queryImplementations(self): 1811 def __queryImplementations(self):
1810 """ 1812 """
1811 Private slot to handle the Find Implementations action. 1813 Private slot to handle the Find Implementations action.
1812 """ 1814 """
1813 aw = e5App().getObject("ViewManager").activeWindow() 1815 aw = self.__vm.activeWindow()
1814 1816
1815 if aw is None: 1817 if aw is None:
1816 return 1818 return
1817 1819
1818 title = self.tr("Find Implementations") 1820 title = self.tr("Find Implementations")
2137 Public method to check, if any editor has unsaved changes. 2139 Public method to check, if any editor has unsaved changes.
2138 2140
2139 @return flag indicating, that no editor contains unsaved edits 2141 @return flag indicating, that no editor contains unsaved edits
2140 @rtype bool 2142 @rtype bool
2141 """ 2143 """
2142 res = e5App().getObject("ViewManager").checkAllDirty() 2144 res = self.__vm.checkAllDirty()
2143 self.sendJson("Validate", {}) 2145 self.sendJson("Validate", {})
2144 return res 2146 return res
2145 2147
2146 def refreshEditors(self, changedFiles): 2148 def refreshEditors(self, changedFiles):
2147 """ 2149 """
2148 Public method to refresh modified editors. 2150 Public method to refresh modified editors.
2149 2151
2150 @param changedFiles list of changed files 2152 @param changedFiles list of changed files
2151 @type list of str 2153 @type list of str
2152 """ 2154 """
2153 vm = e5App().getObject("ViewManager") 2155 openFiles = [Utilities.normcasepath(f)
2154 openFiles = [Utilities.normcasepath(f) for f in vm.getOpenFilenames()] 2156 for f in self.__vm.getOpenFilenames()]
2155 2157
2156 for fileName in changedFiles: 2158 for fileName in changedFiles:
2157 normfile = Utilities.normcasepath(fileName) 2159 normfile = Utilities.normcasepath(fileName)
2158 if normfile in openFiles: 2160 if normfile in openFiles:
2159 editor = vm.getEditor(normfile)[1] 2161 editor = self.__vm.getEditor(normfile)[1]
2160 editor.refresh() 2162 editor.refresh()
2161 2163
2162 aw = vm.activeWindow() 2164 aw = self.__vm.activeWindow()
2163 if aw is not None: 2165 if aw is not None:
2164 filename = aw.getFileName() 2166 filename = aw.getFileName()
2165 if filename is not None: 2167 if filename is not None:
2166 vm.openSourceFile(filename, aw.getCursorPosition()[0] + 1) 2168 self.__vm.openSourceFile(filename,
2169 aw.getCursorPosition()[0] + 1)
2167 2170
2168 def reportChanged(self, filename, oldSource): 2171 def reportChanged(self, filename, oldSource):
2169 """ 2172 """
2170 Public slot to report some changed sources. 2173 Public slot to report some changed sources.
2171 2174
2174 @param oldSource source code before the change 2177 @param oldSource source code before the change
2175 @type str 2178 @type str
2176 """ 2179 """
2177 if self.__e5project.isOpen() and \ 2180 if self.__e5project.isOpen() and \
2178 self.__e5project.isProjectFile(filename): 2181 self.__e5project.isProjectFile(filename):
2179 self.sendJson("ReportChanged", { 2182 editor = self.__vm.getOpenEditor(filename)
2180 "FileName": filename, 2183 if editor is not None and \
2181 "OldSource": oldSource, 2184 editor.getLanguage() == self.__ropeConfig["PythonVersion"]:
2182 }) 2185 self.sendJson("ReportChanged", {
2186 "FileName": filename,
2187 "OldSource": oldSource,
2188 })
2183 2189
2184 ####################################################################### 2190 #######################################################################
2185 ## Methods below handle the network connection 2191 ## Methods below handle the network connection
2186 ####################################################################### 2192 #######################################################################
2187 2193
2246 """ 2252 """
2247 client = os.path.join(os.path.dirname(__file__), 2253 client = os.path.join(os.path.dirname(__file__),
2248 "RefactoringClient.py") 2254 "RefactoringClient.py")
2249 ok = self.startClient(interpreter, client, [self.__projectpath]) 2255 ok = self.startClient(interpreter, client, [self.__projectpath])
2250 return ok 2256 return ok
2257
2258 #########################################################################
2259 ## Methods below handle setting/unsetting the mouse click handler methods
2260 #########################################################################
2261
2262 def connectEditor(self, editor):
2263 """
2264 Public method to connect an editor.
2265
2266 @param editor reference to the editor
2267 @type QScintilla.Editor
2268 """
2269
2270 if self.__plugin.getPreferences("MouseClickEnabled"):
2271 self.__disconnectMouseClickHandler(editor)
2272 self.__connectMouseClickHandler(editor)
2273
2274 def disconnectEditor(self, editor):
2275 """
2276 Public method to disconnect an editor.
2277
2278 @param editor reference to the editor
2279 @type QScintilla.Editor
2280 """
2281 self.__disconnectMouseClickHandler(editor)
2282
2283 def __connectMouseClickHandler(self, editor):
2284 """
2285 Private method to connect the mouse click handler to an editor.
2286
2287 @param editor reference to the editor
2288 @type QScintilla.Editor
2289 """
2290 if self.__plugin.getPreferences("MouseClickGotoButton"):
2291 editor.setMouseClickHandler(
2292 "rope",
2293 self.__plugin.getPreferences("MouseClickGotoModifiers"),
2294 self.__plugin.getPreferences("MouseClickGotoButton"),
2295 self.gotoDefinition
2296 )
2297
2298 def __disconnectMouseClickHandler(self, editor):
2299 """
2300 Private method to disconnect the mouse click handler from an editor.
2301
2302 @param editor reference to the editor
2303 @type QScintilla.Editor
2304 """
2305 editor.removeMouseClickHandlers("rope")

eric ide

mercurial