AssistantEric/Assistant.py

branch
eric7
changeset 180
89ff060ef0d9
parent 177
25cb41783971
child 182
0df33a07a6c8
equal deleted inserted replaced
179:62d9dc4eedd5 180:89ff060ef0d9
9 """ 9 """
10 10
11 import re 11 import re
12 import imp 12 import imp
13 13
14 from PyQt5.QtCore import QRegExp, QObject 14 from PyQt6.QtCore import QObject
15 15
16 from E5Gui.E5Application import e5App 16 from EricWidgets.EricApplication import ericApp
17 17
18 from .APIsManager import APIsManager, ApisNameProject 18 from .APIsManager import APIsManager, ApisNameProject
19 19
20 AcsAPIs = 0x0001 20 AcsAPIs = 0x0001
21 AcsDocument = 0x0002 21 AcsDocument = 0x0002
29 def __init__(self, plugin, parent=None): 29 def __init__(self, plugin, parent=None):
30 """ 30 """
31 Constructor 31 Constructor
32 32
33 @param plugin reference to the plugin object 33 @param plugin reference to the plugin object
34 @param parent parent (QObject) 34 @type AssistantEricPlugin
35 @param parent parent
36 @type QObject
35 """ 37 """
36 QObject.__init__(self, parent) 38 QObject.__init__(self, parent)
37 39
38 self.__plugin = plugin 40 self.__plugin = plugin
39 self.__ui = parent 41 self.__ui = parent
40 self.__project = e5App().getObject("Project") 42 self.__project = ericApp().getObject("Project")
41 self.__viewmanager = e5App().getObject("ViewManager") 43 self.__viewmanager = ericApp().getObject("ViewManager")
42 self.__pluginManager = e5App().getObject("PluginManager") 44 self.__pluginManager = ericApp().getObject("PluginManager")
43 45
44 self.__apisManager = APIsManager(self.__ui, self) 46 self.__apisManager = APIsManager(self.__ui, self)
45 47
46 self.__editors = [] 48 self.__editors = []
47 self.__lastContext = None 49 self.__lastContext = None
91 93
92 def setEnabled(self, key, enabled): 94 def setEnabled(self, key, enabled):
93 """ 95 """
94 Public method to enable or disable a feature. 96 Public method to enable or disable a feature.
95 97
96 @param key feature to set (string) 98 @param key feature to set
97 @param enabled flag indicating the status (boolean) 99 @type str
100 @param enabled flag indicating the status
101 @type bool
98 """ 102 """
99 for editor in self.__editors[:]: 103 for editor in self.__editors[:]:
100 self.__editorClosed(editor) 104 self.__editorClosed(editor)
101 for editor in self.__viewmanager.getOpenEditors(): 105 for editor in self.__viewmanager.getOpenEditors():
102 self.__editorOpened(editor) 106 self.__editorOpened(editor)
103 107
104 def __editorOpened(self, editor): 108 def __editorOpened(self, editor):
105 """ 109 """
106 Private slot called, when a new editor was opened. 110 Private slot called, when a new editor was opened.
107 111
108 @param editor reference to the new editor (QScintilla.Editor) 112 @param editor reference to the new editor
113 @type Editor
109 """ 114 """
110 if self.__plugin.getPreferences("AutoCompletionEnabled"): 115 if self.__plugin.getPreferences("AutoCompletionEnabled"):
111 self.__setAutoCompletionHook(editor) 116 self.__setAutoCompletionHook(editor)
112 if self.__plugin.getPreferences("CalltipsEnabled"): 117 if self.__plugin.getPreferences("CalltipsEnabled"):
113 self.__setCalltipsHook(editor) 118 self.__setCalltipsHook(editor)
123 128
124 def __editorClosed(self, editor): 129 def __editorClosed(self, editor):
125 """ 130 """
126 Private slot called, when an editor was closed. 131 Private slot called, when an editor was closed.
127 132
128 @param editor reference to the editor (QScintilla.Editor) 133 @param editor reference to the editor
134 @type Editor
129 """ 135 """
130 if editor in self.__editors: 136 if editor in self.__editors:
131 editor.editorSaved.disconnect( 137 editor.editorSaved.disconnect(
132 self.__apisManager.getAPIs(ApisNameProject).editorSaved) 138 self.__apisManager.getAPIs(ApisNameProject).editorSaved)
133 self.__editors.remove(editor) 139 self.__editors.remove(editor)
169 def __recordSelectedContext(self, userListId, txt): 175 def __recordSelectedContext(self, userListId, txt):
170 """ 176 """
171 Private slot to handle the selection from the completion list to 177 Private slot to handle the selection from the completion list to
172 record the selected completion context. 178 record the selected completion context.
173 179
174 @param userListId the ID of the user list (should be 1) (integer) 180 @param userListId the ID of the user list (should be 1)
175 @param txt the selected text (string) 181 @type int
182 @param txt the selected text
183 @type str
176 """ 184 """
177 from QScintilla.Editor import EditorAutoCompletionListID 185 from QScintilla.Editor import EditorAutoCompletionListID
178 186
179 if userListId == EditorAutoCompletionListID: 187 if userListId == EditorAutoCompletionListID:
180 lst = txt.split() 188 lst = txt.split()
185 193
186 def __setAutoCompletionHook(self, editor): 194 def __setAutoCompletionHook(self, editor):
187 """ 195 """
188 Private method to set the autocompletion hook. 196 Private method to set the autocompletion hook.
189 197
190 @param editor reference to the editor (QScintilla.Editor) 198 @param editor reference to the editor
199 @type Editor
191 """ 200 """
192 editor.userListActivated.connect(self.__recordSelectedContext) 201 editor.userListActivated.connect(self.__recordSelectedContext)
193 editor.addCompletionListHook("Assistant", self.getCompletionsList) 202 editor.addCompletionListHook("Assistant", self.getCompletionsList)
194 203
195 def __unsetAutoCompletionHook(self, editor): 204 def __unsetAutoCompletionHook(self, editor):
196 """ 205 """
197 Private method to unset the autocompletion hook. 206 Private method to unset the autocompletion hook.
198 207
199 @param editor reference to the editor (QScintilla.Editor) 208 @param editor reference to the editor
209 @type Editor
200 """ 210 """
201 editor.userListActivated.disconnect(self.__recordSelectedContext) 211 editor.userListActivated.disconnect(self.__recordSelectedContext)
202 editor.removeCompletionListHook("Assistant") 212 editor.removeCompletionListHook("Assistant")
203 213
204 def getCompletionsList(self, editor, context): 214 def getCompletionsList(self, editor, context):
205 """ 215 """
206 Public method to get a list of possible completions. 216 Public method to get a list of possible completions.
207 217
208 @param editor reference to the editor object, that called this method 218 @param editor reference to the editor object, that called this method
209 (QScintilla.Editor) 219 @type Editor
210 @param context flag indicating to autocomplete a context (boolean) 220 @param context flag indicating to autocomplete a context
211 @return list of possible completions (list of strings) 221 @type bool
222 @return list of possible completions
223 @rtype list of str
212 """ 224 """
213 language = editor.getApiLanguage() 225 language = editor.getApiLanguage()
214 completeFromDocumentOnly = False 226 completeFromDocumentOnly = False
215 if language in ["", "Guessed"] or language.startswith("Pygments|"): 227 if language in ["", "Guessed"] or language.startswith("Pygments|"):
216 if ( 228 if (
340 def __getCompletions(self, word, context, prefix, language, projectType, 352 def __getCompletions(self, word, context, prefix, language, projectType,
341 module, editor, importCompletion, documentOnly, sep): 353 module, editor, importCompletion, documentOnly, sep):
342 """ 354 """
343 Private method to get the list of possible completions. 355 Private method to get the list of possible completions.
344 356
345 @param word word (or wordpart) to complete (string) 357 @param word word (or wordpart) to complete
346 @param context flag indicating to autocomplete a context (boolean) 358 @type str
347 @param prefix prefix of the word to be completed (string) 359 @param context flag indicating to autocomplete a context
348 @param language programming language of the source (string) 360 @type bool
349 @param projectType type of the project (string) 361 @param prefix prefix of the word to be completed
350 @param module reference to the scanned module info (Module) 362 @type str
351 @param editor reference to the editor object (QScintilla.Editor.Editor) 363 @param language programming language of the source
352 @param importCompletion flag indicating an import completion (boolean) 364 @type str
365 @param projectType type of the project
366 @type str
367 @param module reference to the scanned module info
368 @type Module
369 @param editor reference to the editor object
370 @type Editor
371 @param importCompletion flag indicating an import completion
372 @type bool
353 @param documentOnly flag indicating to complete from the document only 373 @param documentOnly flag indicating to complete from the document only
354 (boolean) 374 @type bool
355 @param sep separator string (string) 375 @param sep separator string
356 @return list of possible completions (list of strings) 376 @type str
377 @return list of possible completions
378 @rtype list of str
357 """ 379 """
358 apiCompletionsList = [] 380 apiCompletionsList = []
359 docCompletionsList = [] 381 docCompletionsList = []
360 projectCompletionList = [] 382 projectCompletionList = []
361 383
391 413
392 def __getApiCompletions(self, api, word, context, prefix, module, editor): 414 def __getApiCompletions(self, api, word, context, prefix, module, editor):
393 """ 415 """
394 Private method to determine a list of completions from an API object. 416 Private method to determine a list of completions from an API object.
395 417
396 @param api reference to the API object to be used (APIsManager.DbAPIs) 418 @param api reference to the API object to be used
397 @param word word (or wordpart) to complete (string) 419 @type APIsManager.DbAPIs
398 @param context flag indicating to autocomplete a context (boolean) 420 @param word word (or wordpart) to complete
399 @param prefix prefix of the word to be completed (string) 421 @type str
400 @param module reference to the scanned module info (Module) 422 @param context flag indicating to autocomplete a context
401 @param editor reference to the editor object (QScintilla.Editor.Editor) 423 @type bool
402 @return list of possible completions (list of strings) 424 @param prefix prefix of the word to be completed
425 @type str
426 @param module reference to the scanned module info
427 @type Module
428 @param editor reference to the editor object
429 @type Editor
430 @return list of possible completions
431 @rtype list of str
403 """ 432 """
404 completionsList = [] 433 completionsList = []
405 if api is not None: 434 if api is not None:
406 if prefix and module and prefix == "self": 435 if prefix and module and prefix == "self":
407 line, col = editor.getCursorPosition() 436 line, col = editor.getCursorPosition()
444 completionsList.remove(entry) 473 completionsList.remove(entry)
445 if completion["pictureId"]: 474 if completion["pictureId"]:
446 entry += "?{0}".format(completion["pictureId"]) 475 entry += "?{0}".format(completion["pictureId"])
447 else: 476 else:
448 cont = False 477 cont = False
449 re = QRegExp( 478 regexp = re.compile(
450 QRegExp.escape(entry) + "\?\d{,2}") 479 re.escape(entry) + r"\?\d{,2}")
451 for comp in completionsList: 480 for comp in completionsList:
452 if re.exactMatch(comp): 481 if regexp.fullmatchS(comp):
453 cont = True 482 cont = True
454 break 483 break
455 if cont: 484 if cont:
456 continue 485 continue
457 if entry not in completionsList: 486 if entry not in completionsList:
485 completionsList.remove(entry) 514 completionsList.remove(entry)
486 if completion["pictureId"]: 515 if completion["pictureId"]:
487 entry += "?{0}".format(completion["pictureId"]) 516 entry += "?{0}".format(completion["pictureId"])
488 else: 517 else:
489 cont = False 518 cont = False
490 re = QRegExp(QRegExp.escape(entry) + "\?\d{,2}") 519 regexp = re.compile(re.escape(entry) + "\?\d{,2}")
491 for comp in completionsList: 520 for comp in completionsList:
492 if re.exactMatch(comp): 521 if regexp.fullmatch(comp):
493 cont = True 522 cont = True
494 break 523 break
495 if cont: 524 if cont:
496 continue 525 continue
497 if entry not in completionsList: 526 if entry not in completionsList:
501 def __getDocumentCompletions(self, editor, word, context, sep, prefix, 530 def __getDocumentCompletions(self, editor, word, context, sep, prefix,
502 module, doHierarchy=False): 531 module, doHierarchy=False):
503 """ 532 """
504 Private method to determine autocompletion proposals from the document. 533 Private method to determine autocompletion proposals from the document.
505 534
506 @param editor reference to the editor object (QScintilla.Editor.Editor) 535 @param editor reference to the editor object
507 @param word string to be completed (string) 536 @type Editor
508 @param context flag indicating to autocomplete a context (boolean) 537 @param word string to be completed
509 @param sep separator string (string) 538 @type str
510 @param prefix prefix of the word to be completed (string) 539 @param context flag indicating to autocomplete a context
511 @param module reference to the scanned module info (Module) 540 @type bool
512 @keyparam doHierarchy flag indicating a hierarchical search (boolean) 541 @param sep separator string
513 @return list of possible completions (list of strings) 542 @type str
543 @param prefix prefix of the word to be completed
544 @type str
545 @param module reference to the scanned module info
546 @type Module
547 @param doHierarchy flag indicating a hierarchical search
548 @type bool
549 @return list of possible completions
550 @rtype list of str
514 """ 551 """
515 completionsList = [] 552 completionsList = []
516 553
517 prefixFound = False 554 prefixFound = False
518 if prefix and module: 555 if prefix and module:
680 717
681 def __setCalltipsHook(self, editor): 718 def __setCalltipsHook(self, editor):
682 """ 719 """
683 Private method to set the calltip hook. 720 Private method to set the calltip hook.
684 721
685 @param editor reference to the editor (QScintilla.Editor) 722 @param editor reference to the editor
723 @type Editor
686 """ 724 """
687 editor.addCallTipHook("Assistant", self.calltips) 725 editor.addCallTipHook("Assistant", self.calltips)
688 726
689 def __unsetCalltipsHook(self, editor): 727 def __unsetCalltipsHook(self, editor):
690 """ 728 """
691 Private method to unset the calltip hook. 729 Private method to unset the calltip hook.
692 730
693 @param editor reference to the editor (QScintilla.Editor) 731 @param editor reference to the editor
732 @type Editor
694 """ 733 """
695 editor.removeCallTipHook("Assistant") 734 editor.removeCallTipHook("Assistant")
696 735
697 def calltips(self, editor, pos, commas): 736 def calltips(self, editor, pos, commas):
698 """ 737 """
699 Public method to return a list of calltips. 738 Public method to return a list of calltips.
700 739
701 @param editor reference to the editor (QScintilla.Editor) 740 @param editor reference to the editor
702 @param pos position in the text for the calltip (integer) 741 @type Editor
742 @param pos position in the text for the calltip
743 @type int
703 @param commas minimum number of commas contained in the calltip 744 @param commas minimum number of commas contained in the calltip
704 (integer) 745 @type int
705 @return list of possible calltips (list of strings) 746 @return list of possible calltips
747 @rtype list of str
706 """ 748 """
707 language = editor.getApiLanguage() 749 language = editor.getApiLanguage()
708 completeFromDocumentOnly = False 750 completeFromDocumentOnly = False
709 if language in ["", "Guessed"] or language.startswith("Pygments|"): 751 if language in ["", "Guessed"] or language.startswith("Pygments|"):
710 if ( 752 if (
784 826
785 def __getApiCalltips(self, api, word, commas, prefix, module, editor): 827 def __getApiCalltips(self, api, word, commas, prefix, module, editor):
786 """ 828 """
787 Private method to determine calltips from APIs. 829 Private method to determine calltips from APIs.
788 830
789 @param api reference to the API object to be used (APIsManager.DbAPIs) 831 @param api reference to the API object to be used
790 @param word function to get calltips for (string) 832 @type APIsManager.DbAPIs
833 @param word function to get calltips for
834 @type str
791 @param commas minimum number of commas contained in the calltip 835 @param commas minimum number of commas contained in the calltip
792 (integer) 836 @type int
793 @param prefix prefix of the word to be completed (string) 837 @param prefix prefix of the word to be completed
794 @param module reference to the scanned module info (Module) 838 @type str
795 @param editor reference to the editor object (QScintilla.Editor) 839 @param module reference to the scanned module info
796 @return list of calltips (list of string) 840 @type Module
841 @param editor reference to the editor object
842 @type Editor
843 @return list of calltips
844 @rtype list of str
797 """ 845 """
798 calltips = [] 846 calltips = []
799 if prefix and module and prefix == "self": 847 if prefix and module and prefix == "self":
800 line, col = editor.getCursorPosition() 848 line, col = editor.getCursorPosition()
801 for cl in module.classes.values(): 849 for cl in module.classes.values():
821 def __getDocumentCalltips(self, word, prefix, module, editor, 869 def __getDocumentCalltips(self, word, prefix, module, editor,
822 doHierarchy=False): 870 doHierarchy=False):
823 """ 871 """
824 Private method to determine calltips from the document. 872 Private method to determine calltips from the document.
825 873
826 @param word function to get calltips for (string) 874 @param word function to get calltips for
827 @param prefix prefix of the word to be completed (string) 875 @type str
828 @param module reference to the scanned module info (Module) 876 @param prefix prefix of the word to be completed
829 @param editor reference to the editor object (QScintilla.Editor) 877 @type str
830 @keyparam doHierarchy flag indicating a hierarchical search (boolean) 878 @param module reference to the scanned module info
831 @return list of calltips (list of string) 879 @type Module
880 @param editor reference to the editor object
881 @type Editor
882 @param doHierarchy flag indicating a hierarchical search
883 @type bool
884 @return list of calltips
885 @rtype list of str
832 """ 886 """
833 calltips = [] 887 calltips = []
834 if module and bool(editor.getLexer()): 888 if module and bool(editor.getLexer()):
835 if prefix: 889 if prefix:
836 # prefix can be 'self', 'cls' or a class name 890 # prefix can be 'self', 'cls' or a class name

eric ide

mercurial