25 from PyQt5.QtPrintSupport import QPrinter, QPrintDialog, QAbstractPrintDialog |
25 from PyQt5.QtPrintSupport import QPrinter, QPrintDialog, QAbstractPrintDialog |
26 from PyQt5.Qsci import QsciScintilla, QsciMacro, QsciStyledText |
26 from PyQt5.Qsci import QsciScintilla, QsciMacro, QsciStyledText |
27 |
27 |
28 from E5Gui.E5Application import e5App |
28 from E5Gui.E5Application import e5App |
29 from E5Gui import E5FileDialog, E5MessageBox |
29 from E5Gui import E5FileDialog, E5MessageBox |
|
30 from E5Utilities.E5Cache import E5Cache |
30 |
31 |
31 from .QsciScintillaCompat import QsciScintillaCompat, QSCINTILLA_VERSION |
32 from .QsciScintillaCompat import QsciScintillaCompat, QSCINTILLA_VERSION |
32 from .EditorMarkerMap import EditorMarkerMap |
33 from .EditorMarkerMap import EditorMarkerMap |
33 |
34 |
34 import Preferences |
35 import Preferences |
382 |
383 |
383 # set the auto-completion function |
384 # set the auto-completion function |
384 self.__acContext = True |
385 self.__acContext = True |
385 self.__acText = "" |
386 self.__acText = "" |
386 self.__acCompletions = set() |
387 self.__acCompletions = set() |
|
388 self.__acCache = E5Cache( |
|
389 size=Preferences.getEditor("AutoCompletionCacheSize")) |
387 self.__acTimer = QTimer(self) |
390 self.__acTimer = QTimer(self) |
388 self.__acTimer.setSingleShot(True) |
391 self.__acTimer.setSingleShot(True) |
389 self.__acTimer.setInterval( |
392 self.__acTimer.setInterval( |
390 Preferences.getEditor("AutoCompletionTimeout")) |
393 Preferences.getEditor("AutoCompletionTimeout")) |
391 self.__acTimer.timeout.connect(self.__autoComplete) |
394 self.__acTimer.timeout.connect(self.__autoComplete) |
829 menu = QMenu(self.tr('Complete')) |
832 menu = QMenu(self.tr('Complete')) |
830 |
833 |
831 self.menuActs["acDynamic"] = menu.addAction( |
834 self.menuActs["acDynamic"] = menu.addAction( |
832 self.tr('Complete'), self.autoComplete) |
835 self.tr('Complete'), self.autoComplete) |
833 menu.addSeparator() |
836 menu.addSeparator() |
|
837 self.menuActs["acClearCache"] = menu.addAction( |
|
838 self.tr("Clear Completions Cache"), self.__clearCompletionsCache) |
|
839 menu.addSeparator() |
834 menu.addAction( |
840 menu.addAction( |
835 self.tr('Complete from Document'), self.autoCompleteFromDocument) |
841 self.tr('Complete from Document'), self.autoCompleteFromDocument) |
836 self.menuActs["acAPI"] = menu.addAction( |
842 self.menuActs["acAPI"] = menu.addAction( |
837 self.tr('Complete from APIs'), self.autoCompleteFromAPIs) |
843 self.tr('Complete from APIs'), self.autoCompleteFromAPIs) |
838 self.menuActs["acAPIDocument"] = menu.addAction( |
844 self.menuActs["acAPIDocument"] = menu.addAction( |
4041 self.__setTextDisplay() |
4047 self.__setTextDisplay() |
4042 |
4048 |
4043 # set margin 0 and 2 configuration |
4049 # set margin 0 and 2 configuration |
4044 self.__setMarginsDisplay() |
4050 self.__setMarginsDisplay() |
4045 |
4051 |
4046 # set the autocompletion and calltips function |
4052 # set the auto-completion function |
|
4053 self.__acCache.setSize( |
|
4054 Preferences.getEditor("AutoCompletionCacheSize")) |
|
4055 acTimeout = Preferences.getEditor("AutoCompletionTimeout") |
|
4056 if acTimeout != self.__acTimer.interval: |
|
4057 self.__acTimer.setInterval(acTimeout) |
4047 self.__setAutoCompletion() |
4058 self.__setAutoCompletion() |
|
4059 |
|
4060 # set the calltips function |
4048 self.__setCallTips() |
4061 self.__setCallTips() |
4049 |
4062 |
4050 # set the autosave flags |
4063 # set the autosave flags |
4051 self.autosaveEnabled = Preferences.getEditor("AutosaveInterval") > 0 |
4064 self.autosaveEnabled = Preferences.getEditor("AutosaveInterval") > 0 |
4052 |
4065 |
4632 Private method to start auto-completion via plug-ins. |
4645 Private method to start auto-completion via plug-ins. |
4633 |
4646 |
4634 @keyparam context flag indicating to complete a context |
4647 @keyparam context flag indicating to complete a context |
4635 @type bool or None |
4648 @type bool or None |
4636 """ |
4649 """ |
4637 # TODO: add a cache for recent completions |
|
4638 if context is None: |
|
4639 context = self.__acContext |
|
4640 |
|
4641 line, col = self.getCursorPosition() |
4650 line, col = self.getCursorPosition() |
4642 self.__acText = self.getWordLeft(line, col) |
4651 text = self.text(line) |
|
4652 if self.__isStartChar(text[col - 1]): |
|
4653 self.__acText = self.getWordLeft(line, col - 1) + text[col - 1] |
|
4654 else: |
|
4655 self.__acText = self.getWordLeft(line, col) |
4643 self.__acCompletions.clear() |
4656 self.__acCompletions.clear() |
4644 |
4657 |
4645 for key in self.__completionListAsyncHookFunctions: |
4658 completions = self.__acCache.get(self.__acText) |
4646 self.__completionListAsyncHookFunctions[key]( |
4659 if completions is not None: |
4647 self, context, self.__acText) |
4660 # show list with cached entries |
4648 |
4661 if self.isListActive(): |
4649 for key in self.__completionListHookFunctions: |
4662 self.cancelList() |
4650 completions = self.__completionListHookFunctions[key]( |
4663 |
4651 self, context) |
4664 self.showUserList( |
4652 self.completionsListReady(completions, self.__acText) |
4665 EditorAutoCompletionListID, |
|
4666 sorted(list(completions), |
|
4667 reverse=Preferences.getEditor( |
|
4668 "AutoCompletionReversedList"))) |
|
4669 else: |
|
4670 if context is None: |
|
4671 context = self.__acContext |
|
4672 |
|
4673 for key in self.__completionListAsyncHookFunctions: |
|
4674 self.__completionListAsyncHookFunctions[key]( |
|
4675 self, context, self.__acText) |
|
4676 |
|
4677 for key in self.__completionListHookFunctions: |
|
4678 completions = self.__completionListHookFunctions[key]( |
|
4679 self, context) |
|
4680 self.completionsListReady(completions, self.__acText) |
4653 |
4681 |
4654 def completionsListReady(self, completions, acText): |
4682 def completionsListReady(self, completions, acText): |
4655 """ |
4683 """ |
4656 Public method to show the completions determined by a completions |
4684 Public method to show the completions determined by a completions |
4657 provider. |
4685 provider. |
4658 |
4686 |
4659 @param completions list of possible completions |
4687 @param completions list of possible completions |
4660 @type list of str |
4688 @type list of str or set of str |
4661 @param acText text to be completed |
4689 @param acText text to be completed |
4662 @type str |
4690 @type str |
4663 """ |
4691 """ |
4664 if acText == self.__acText: |
4692 if acText == self.__acText and bool(completions): |
4665 # process the list only, if not already obsolete |
4693 # process the list only, if not already obsolete or completions |
|
4694 # are not empty |
4666 if self.isListActive(): |
4695 if self.isListActive(): |
4667 self.cancelList() |
4696 self.cancelList() |
4668 |
4697 |
4669 self.__acCompletions.update(set(completions)) |
4698 self.__acCompletions.update(set(completions)) |
4670 if self.__acCompletions: |
4699 if self.__acCompletions: |
|
4700 self.__acCache.add(acText, set(self.__acCompletions)) |
4671 self.showUserList( |
4701 self.showUserList( |
4672 EditorAutoCompletionListID, |
4702 EditorAutoCompletionListID, |
4673 sorted(list(self.__acCompletions), |
4703 sorted(list(self.__acCompletions), |
4674 reverse=Preferences.getEditor( |
4704 reverse=Preferences.getEditor( |
4675 "AutoCompletionReversedList"))) |
4705 "AutoCompletionReversedList"))) |
|
4706 |
|
4707 def __clearCompletionsCache(self): |
|
4708 """ |
|
4709 Private method to clear the auto-completions cache. |
|
4710 """ |
|
4711 self.__acCache.clear() |
4676 |
4712 |
4677 def __completionListSelected(self, listId, txt): |
4713 def __completionListSelected(self, listId, txt): |
4678 """ |
4714 """ |
4679 Private slot to handle the selection from the completion list. |
4715 Private slot to handle the selection from the completion list. |
4680 |
4716 |
5030 def __showContextMenuAutocompletion(self): |
5066 def __showContextMenuAutocompletion(self): |
5031 """ |
5067 """ |
5032 Private slot called before the autocompletion menu is shown. |
5068 Private slot called before the autocompletion menu is shown. |
5033 """ |
5069 """ |
5034 self.menuActs["acDynamic"].setEnabled( |
5070 self.menuActs["acDynamic"].setEnabled( |
|
5071 self.canProvideDynamicAutoCompletion()) |
|
5072 self.menuActs["acClearCache"].setEnabled( |
5035 self.canProvideDynamicAutoCompletion()) |
5073 self.canProvideDynamicAutoCompletion()) |
5036 self.menuActs["acAPI"].setEnabled(self.acAPI) |
5074 self.menuActs["acAPI"].setEnabled(self.acAPI) |
5037 self.menuActs["acAPIDocument"].setEnabled(self.acAPI) |
5075 self.menuActs["acAPIDocument"].setEnabled(self.acAPI) |
5038 self.menuActs["calltip"].setEnabled(self.canProvideCallTipps()) |
5076 self.menuActs["calltip"].setEnabled(self.canProvideCallTipps()) |
5039 |
5077 |