PluginRefactoringRope.py

branch
eric7
changeset 365
f740b50380df
parent 362
792c2651fbd7
child 368
c206d08c28e7
equal deleted inserted replaced
364:a92b3272f4c1 365:f740b50380df
8 """ 8 """
9 9
10 import os 10 import os
11 import contextlib 11 import contextlib
12 12
13 from PyQt5.QtCore import Qt, QObject, QTranslator, QCoreApplication 13 from PyQt6.QtCore import Qt, QObject, QTranslator, QCoreApplication
14 14
15 from E5Gui.E5Application import e5App 15 from EricWidgets.EricApplication import ericApp
16 16
17 import Preferences 17 import Preferences
18 import Utilities 18 import Utilities
19 19
20 # Start-Of-Header 20 # Start-Of-Header
21 name = "Refactoring Rope Plugin" 21 name = "Refactoring Rope Plugin"
22 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 22 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
23 autoactivate = True 23 autoactivate = True
24 deactivateable = True 24 deactivateable = True
25 version = "8.0.1" 25 version = "1.0.0"
26 className = "RefactoringRopePlugin" 26 className = "RefactoringRopePlugin"
27 packageName = "RefactoringRope" 27 packageName = "RefactoringRope"
28 internalPackages = "rope" 28 internalPackages = "rope"
29 shortDescription = "Refactoring using the Rope library." 29 shortDescription = "Refactoring using the Rope library."
30 longDescription = ( 30 longDescription = (
31 """This plug-in implements refactoring functionality""" 31 """This plug-in implements refactoring functionality"""
32 """ using the Rope refactoring library. Additionally it implements an""" 32 """ using the Rope refactoring library. Additionally it implements an"""
33 """ alternative auto-completion and call-tips provider. It is a""" 33 """ auto-completion, call-tips and code documentation provider as well"""
34 """ provider for code documentation as well.""" 34 """ as a mouse click handler."""
35 ) 35 )
36 pyqtApi = 2 36 pyqtApi = 2
37 doNotCompile = True 37 doNotCompile = True
38 # End-Of-Header 38 # End-Of-Header
39 39
45 def createAutoCompletionPage(configDlg): 45 def createAutoCompletionPage(configDlg):
46 """ 46 """
47 Module function to create the autocompletion configuration page. 47 Module function to create the autocompletion configuration page.
48 48
49 @param configDlg reference to the configuration dialog 49 @param configDlg reference to the configuration dialog
50 @type ConfigurationWidget
50 @return reference to the configuration page 51 @return reference to the configuration page
52 @rtype AutoCompletionRopePage
51 """ 53 """
52 global refactoringRopePluginObject 54 global refactoringRopePluginObject
53 from RefactoringRope.ConfigurationPage.AutoCompletionRopePage import ( 55 from RefactoringRope.ConfigurationPage.AutoCompletionRopePage import (
54 AutoCompletionRopePage 56 AutoCompletionRopePage
55 ) 57 )
60 def createCallTipsPage(configDlg): 62 def createCallTipsPage(configDlg):
61 """ 63 """
62 Module function to create the calltips configuration page. 64 Module function to create the calltips configuration page.
63 65
64 @param configDlg reference to the configuration dialog 66 @param configDlg reference to the configuration dialog
67 @type ConfigurationWidget
65 @return reference to the configuration page 68 @return reference to the configuration page
69 @rtype CallTipsRopePage
66 """ 70 """
67 global refactoringRopePluginObject 71 global refactoringRopePluginObject
68 from RefactoringRope.ConfigurationPage.CallTipsRopePage import ( 72 from RefactoringRope.ConfigurationPage.CallTipsRopePage import (
69 CallTipsRopePage 73 CallTipsRopePage
70 ) 74 )
75 def createMouseClickHandlerPage(configDlg): 79 def createMouseClickHandlerPage(configDlg):
76 """ 80 """
77 Module function to create the mouse click handler configuration page. 81 Module function to create the mouse click handler configuration page.
78 82
79 @param configDlg reference to the configuration dialog 83 @param configDlg reference to the configuration dialog
84 @type ConfigurationWidget
80 @return reference to the configuration page 85 @return reference to the configuration page
86 @rtype MouseClickHandlerRopePage
81 """ 87 """
82 global refactoringRopePluginObject 88 global refactoringRopePluginObject
83 from RefactoringRope.ConfigurationPage.MouseClickHandlerRopePage import ( 89 from RefactoringRope.ConfigurationPage.MouseClickHandlerRopePage import (
84 MouseClickHandlerRopePage 90 MouseClickHandlerRopePage
85 ) 91 )
94 100
95 @return dictionary containing the relevant data 101 @return dictionary containing the relevant data
96 @rtype dict 102 @rtype dict
97 """ 103 """
98 try: 104 try:
99 usesDarkPalette = e5App().usesDarkPalette() 105 usesDarkPalette = ericApp().usesDarkPalette()
100 except AttributeError: 106 except AttributeError:
101 from PyQt5.QtGui import QPalette 107 from PyQt6.QtGui import QPalette
102 palette = e5App().palette() 108 palette = ericApp().palette()
103 lightness = palette.color(QPalette.Window).lightness() 109 lightness = palette.color(QPalette.Window).lightness()
104 usesDarkPalette = lightness <= 128 110 usesDarkPalette = lightness <= 128
105 iconSuffix = "dark" if usesDarkPalette else "light" 111 iconSuffix = "dark" if usesDarkPalette else "light"
106 112
107 data = { 113 data = {
141 def __init__(self, ui): 147 def __init__(self, ui):
142 """ 148 """
143 Constructor 149 Constructor
144 150
145 @param ui reference to the user interface object 151 @param ui reference to the user interface object
146 @type UI.UserInterface 152 @type UserInterface
147 """ 153 """
148 QObject.__init__(self, ui) 154 QObject.__init__(self, ui)
149 self.__ui = ui 155 self.__ui = ui
150 self.__initialize() 156 self.__initialize()
151 157
155 161
156 "CodeAssistCalltipsEnabled": False, 162 "CodeAssistCalltipsEnabled": False,
157 "CalltipsMaxFixes": 10, 163 "CalltipsMaxFixes": 10,
158 164
159 "MouseClickEnabled": True, 165 "MouseClickEnabled": True,
160 "MouseClickGotoModifiers": int(Qt.ControlModifier), 166 "MouseClickGotoModifiers": (
161 "MouseClickGotoButton": int(Qt.LeftButton), 167 Qt.KeyboardModifier.ControlModifier |
168 Qt.KeyboardModifier.AltModifier
169 ),
170 "MouseClickGotoButton": Qt.MouseButton.LeftButton,
162 } 171 }
163 172
164 self.__translator = None 173 self.__translator = None
165 self.__loadTranslator() 174 self.__loadTranslator()
166 175
185 @rtype tuple of (None, bool) 194 @rtype tuple of (None, bool)
186 """ 195 """
187 global refactoringRopePluginObject 196 global refactoringRopePluginObject
188 refactoringRopePluginObject = self 197 refactoringRopePluginObject = self
189 198
190 e5App().getObject("PluginManager").shutdown.connect( 199 ericApp().getObject("PluginManager").shutdown.connect(
191 self.__shutdown) 200 self.__shutdown)
192 201
193 from RefactoringRope.CodeAssistServer import CodeAssistServer 202 from RefactoringRope.CodeAssistServer import CodeAssistServer
194 self.__codeAssistServer = CodeAssistServer(self, self.__ui) 203 self.__codeAssistServer = CodeAssistServer(self, self.__ui)
195 self.__codeAssistServer.activate() 204 self.__codeAssistServer.activate()
196 205
197 from RefactoringRope.RefactoringServer import RefactoringServer 206 from RefactoringRope.RefactoringServer import RefactoringServer
198 self.__refactoringServer = RefactoringServer(self, self.__ui) 207 self.__refactoringServer = RefactoringServer(self, self.__ui)
199 self.__refactoringServer.activate() 208 self.__refactoringServer.activate()
200 209
201 e5App().getObject("PluginManager").shutdown.connect( 210 ericApp().getObject("PluginManager").shutdown.connect(
202 self.__shutdown) 211 self.__shutdown)
203 e5App().getObject("ViewManager").editorOpenedEd.connect( 212 ericApp().getObject("ViewManager").editorOpenedEd.connect(
204 self.__editorOpened) 213 self.__editorOpened)
205 e5App().getObject("ViewManager").editorClosedEd.connect( 214 ericApp().getObject("ViewManager").editorClosedEd.connect(
206 self.__editorClosed) 215 self.__editorClosed)
207 216
208 for editor in e5App().getObject("ViewManager").getOpenEditors(): 217 for editor in ericApp().getObject("ViewManager").getOpenEditors():
209 self.__editorOpened(editor) 218 self.__editorOpened(editor)
210 219
211 return None, True 220 return None, True
212 221
213 def deactivate(self): 222 def deactivate(self):
218 self.__refactoringServer.deactivate() 227 self.__refactoringServer.deactivate()
219 228
220 if self.__codeAssistServer: 229 if self.__codeAssistServer:
221 self.__codeAssistServer.deactivate() 230 self.__codeAssistServer.deactivate()
222 231
223 e5App().getObject("PluginManager").shutdown.disconnect( 232 ericApp().getObject("PluginManager").shutdown.disconnect(
224 self.__shutdown) 233 self.__shutdown)
225 e5App().getObject("ViewManager").editorOpenedEd.disconnect( 234 ericApp().getObject("ViewManager").editorOpenedEd.disconnect(
226 self.__editorOpened) 235 self.__editorOpened)
227 e5App().getObject("ViewManager").editorClosedEd.disconnect( 236 ericApp().getObject("ViewManager").editorClosedEd.disconnect(
228 self.__editorClosed) 237 self.__editorClosed)
229 238
230 for editor in self.__editors[:]: 239 for editor in self.__editors[:]:
231 self.__editorClosed(editor) 240 self.__editorClosed(editor)
232 241
251 translation = "rope_{0}".format(loc) 260 translation = "rope_{0}".format(loc)
252 translator = QTranslator(None) 261 translator = QTranslator(None)
253 loaded = translator.load(translation, locale_dir) 262 loaded = translator.load(translation, locale_dir)
254 if loaded: 263 if loaded:
255 self.__translator = translator 264 self.__translator = translator
256 e5App().installTranslator(self.__translator) 265 ericApp().installTranslator(self.__translator)
257 else: 266 else:
258 print("Warning: translation file '{0}' could not" 267 print("Warning: translation file '{0}' could not"
259 " be loaded.".format(translation)) 268 " be loaded.".format(translation))
260 print("Using default.") 269 print("Using default.")
261 270
264 Public method to retrieve the various refactoring settings. 273 Public method to retrieve the various refactoring settings.
265 274
266 @param key the key of the value to get 275 @param key the key of the value to get
267 @type str 276 @type str
268 @return the requested refactoring setting 277 @return the requested refactoring setting
278 @rtype Any
269 """ 279 """
270 if key in ["CodeAssistEnabled", "CodeAssistCalltipsEnabled", 280 if key in ["CodeAssistEnabled", "CodeAssistCalltipsEnabled",
271 "MouseClickEnabled"]: 281 "MouseClickEnabled"]:
272 return Preferences.toBool(Preferences.Prefs.settings.value( 282 return Preferences.toBool(Preferences.Prefs.settings.value(
273 self.PreferencesKey + "/" + key, self.__defaults[key])) 283 self.PreferencesKey + "/" + key, self.__defaults[key]))
274 else: 284 elif key in ["CalltipsMaxFixes", "MaxFixes"]:
275 return int(Preferences.Prefs.settings.value( 285 return int(Preferences.Prefs.settings.value(
276 self.PreferencesKey + "/" + key, self.__defaults[key])) 286 self.PreferencesKey + "/" + key, self.__defaults[key]))
287 else:
288 return Preferences.Prefs.settings.value(
289 self.PreferencesKey + "/" + key, self.__defaults[key])
277 290
278 def setPreferences(self, key, value): 291 def setPreferences(self, key, value):
279 """ 292 """
280 Public method to store the various refactoring settings. 293 Public method to store the various refactoring settings.
281 294
282 @param key the key of the setting to be set 295 @param key the key of the setting to be set
283 @type str 296 @type str
284 @param value the value to be set 297 @param value the value to be set
298 @type Any
285 """ 299 """
286 Preferences.Prefs.settings.setValue( 300 Preferences.Prefs.settings.setValue(
287 self.PreferencesKey + "/" + key, value) 301 self.PreferencesKey + "/" + key, value)
288 302
289 if key in ["MouseClickGotoModifiers", "MouseClickGotoButton"]: 303 if key in ["MouseClickGotoModifiers", "MouseClickGotoButton"]:
297 311
298 @return list of valid language strings 312 @return list of valid language strings
299 @rtype list of str 313 @rtype list of str
300 """ 314 """
301 return ["Python3", "MicroPython", 315 return ["Python3", "MicroPython",
302 "Pygments|Python", "Pygments|Python 2.x"] 316 "Pygments|Python", "Pygments|Python 2.x",
317 "Cython"]
303 318
304 def __editorOpened(self, editor): 319 def __editorOpened(self, editor):
305 """ 320 """
306 Private slot called, when a new editor was opened. 321 Private slot called, when a new editor was opened.
307 322
308 @param editor reference to the new editor 323 @param editor reference to the new editor
309 @type QScintilla.Editor.Editor 324 @type Editor
310 """ 325 """
311 languages = self.__determineLanguage() 326 languages = self.__determineLanguage()
312 327
313 if editor.getLanguage() in languages: 328 if editor.getLanguage() in languages:
314 self.__connectEditor(editor) 329 self.__connectEditor(editor)
319 def __editorClosed(self, editor): 334 def __editorClosed(self, editor):
320 """ 335 """
321 Private slot called, when an editor was closed. 336 Private slot called, when an editor was closed.
322 337
323 @param editor reference to the editor 338 @param editor reference to the editor
324 @type QScintilla.Editor.Editor 339 @type Editor
325 """ 340 """
326 if editor in self.__editors: 341 if editor in self.__editors:
327 editor.languageChanged.disconnect(self.__editorLanguageChanged) 342 editor.languageChanged.disconnect(self.__editorLanguageChanged)
328 self.__disconnectEditor(editor) 343 self.__disconnectEditor(editor)
329 self.__editors.remove(editor) 344 self.__editors.remove(editor)
345 def __connectEditor(self, editor): 360 def __connectEditor(self, editor):
346 """ 361 """
347 Private method to connect an editor. 362 Private method to connect an editor.
348 363
349 @param editor reference to the editor 364 @param editor reference to the editor
350 @type QScintilla.Editor.Editor 365 @type Editor
351 """ 366 """
352 editor.editorAboutToBeSaved.connect(self.__editorAboutToBeSaved) 367 editor.editorAboutToBeSaved.connect(self.__editorAboutToBeSaved)
353 editor.editorSaved.connect(self.__editorSaved) 368 editor.editorSaved.connect(self.__editorSaved)
354 369
355 self.__setAutoCompletionHook(editor) 370 self.__setAutoCompletionHook(editor)
360 def __disconnectEditor(self, editor): 375 def __disconnectEditor(self, editor):
361 """ 376 """
362 Private method to disconnect an editor. 377 Private method to disconnect an editor.
363 378
364 @param editor reference to the editor 379 @param editor reference to the editor
365 @type QScintilla.Editor.Editor 380 @type Editor
366 """ 381 """
367 with contextlib.suppress(TypeError): 382 with contextlib.suppress(TypeError):
368 editor.editorAboutToBeSaved.disconnect(self.__editorAboutToBeSaved) 383 editor.editorAboutToBeSaved.disconnect(self.__editorAboutToBeSaved)
369 editor.editorSaved.disconnect(self.__editorSaved) 384 editor.editorSaved.disconnect(self.__editorSaved)
370 385
376 def __connectMouseClickHandler(self, editor): 391 def __connectMouseClickHandler(self, editor):
377 """ 392 """
378 Private method to connect the mouse click handler to an editor. 393 Private method to connect the mouse click handler to an editor.
379 394
380 @param editor reference to the editor 395 @param editor reference to the editor
381 @type QScintilla.Editor 396 @type Editor
382 """ 397 """
383 if self.getPreferences("MouseClickGotoButton"): 398 if self.getPreferences("MouseClickGotoButton"):
384 editor.setMouseClickHandler( 399 editor.setMouseClickHandler(
385 "rope", 400 "rope",
386 self.getPreferences("MouseClickGotoModifiers"), 401 self.getPreferences("MouseClickGotoModifiers"),
391 def __disconnectMouseClickHandler(self, editor): 406 def __disconnectMouseClickHandler(self, editor):
392 """ 407 """
393 Private method to disconnect the mouse click handler from an editor. 408 Private method to disconnect the mouse click handler from an editor.
394 409
395 @param editor reference to the editor 410 @param editor reference to the editor
396 @type QScintilla.Editor 411 @type Editor
397 """ 412 """
398 editor.removeMouseClickHandlers("rope") 413 editor.removeMouseClickHandlers("rope")
399 414
400 def __setAutoCompletionHook(self, editor): 415 def __setAutoCompletionHook(self, editor):
401 """ 416 """
402 Private method to set the autocompletion hook. 417 Private method to set the autocompletion hook.
403 418
404 @param editor reference to the editor 419 @param editor reference to the editor
405 @type QScintilla.Editor 420 @type Editor
406 """ 421 """
407 try: 422 editor.addCompletionListHook(
408 editor.addCompletionListHook( 423 "rope", self.__codeAssistServer.requestCompletions, True)
409 "rope", self.__codeAssistServer.requestCompletions, True)
410 self.__codeAssistServer.setAsyncCompletions(True)
411 except TypeError:
412 # interface before 17.11
413 editor.addCompletionListHook(
414 "rope", self.__codeAssistServer.getCompletions)
415 self.__codeAssistServer.setAsyncCompletions(False)
416 424
417 def __unsetAutoCompletionHook(self, editor): 425 def __unsetAutoCompletionHook(self, editor):
418 """ 426 """
419 Private method to unset the autocompletion hook. 427 Private method to unset the autocompletion hook.
420 428
421 @param editor reference to the editor 429 @param editor reference to the editor
422 @type QScintilla.Editor 430 @type Editor
423 """ 431 """
424 editor.removeCompletionListHook("rope") 432 editor.removeCompletionListHook("rope")
425 433
426 def __setCalltipsHook(self, editor): 434 def __setCalltipsHook(self, editor):
427 """ 435 """
428 Private method to set the calltip hook. 436 Private method to set the calltip hook.
429 437
430 @param editor reference to the editor 438 @param editor reference to the editor
431 @type QScintilla.Editor 439 @type Editor
432 """ 440 """
433 editor.addCallTipHook("rope", self.__codeAssistServer.getCallTips) 441 editor.addCallTipHook("rope", self.__codeAssistServer.getCallTips)
434 442
435 def __unsetCalltipsHook(self, editor): 443 def __unsetCalltipsHook(self, editor):
436 """ 444 """
437 Private method to unset the calltip hook. 445 Private method to unset the calltip hook.
438 446
439 @param editor reference to the editor 447 @param editor reference to the editor
440 @type QScintilla.Editor 448 @type Editor
441 """ 449 """
442 editor.removeCallTipHook("rope") 450 editor.removeCallTipHook("rope")
443 451
444 def __editorAboutToBeSaved(self, filename): 452 def __editorAboutToBeSaved(self, filename):
445 """ 453 """

eric ide

mercurial