QScintilla/Editor.py

changeset 5886
ba6d27371e25
parent 5881
13381dbbb81e
child 5887
9a6ce5faed7a
equal deleted inserted replaced
5884:7a6f30e7f79c 5886:ba6d27371e25
378 self.gotoLine(1) 378 self.gotoLine(1)
379 379
380 # set the text display again 380 # set the text display again
381 self.__setTextDisplay() 381 self.__setTextDisplay()
382 382
383 # set the autocompletion and calltips function 383 # set the auto-completion function
384 self.__acContext = True
385 self.__acText = ""
386 self.__acCompletions = set()
387 self.__acTimer = QTimer(self)
388 self.__acTimer.setSingleShot(True)
389 self.__acTimer.setInterval(
390 Preferences.getEditor("AutoCompletionTimeout"))
391 self.__acTimer.timeout.connect(self.__autoComplete)
392
384 self.__completionListHookFunctions = {} 393 self.__completionListHookFunctions = {}
394 self.__completionListAsyncHookFunctions = {}
385 self.__setAutoCompletion() 395 self.__setAutoCompletion()
386 396
397 # set the call-tips function
387 self.__ctHookFunctions = {} 398 self.__ctHookFunctions = {}
388 self.__setCallTips() 399 self.__setCallTips()
389 400
390 # set the mouse click handlers (fired on mouse release) 401 # set the mouse click handlers (fired on mouse release)
391 self.__mouseClickHandlers = {} 402 self.__mouseClickHandlers = {}
4519 4530
4520 ################################################################# 4531 #################################################################
4521 ## auto-completion hook interfaces 4532 ## auto-completion hook interfaces
4522 ################################################################# 4533 #################################################################
4523 4534
4524 def addCompletionListHook(self, key, func): 4535 # TODO: add support for asynchroneous auto-completion lists
4536 def addCompletionListHook(self, key, func, async=False):
4525 """ 4537 """
4526 Public method to set an auto-completion list provider. 4538 Public method to set an auto-completion list provider.
4527 4539
4528 @param key name of the provider 4540 @param key name of the provider
4529 @type str 4541 @type str
4530 @param func function providing completion list. func 4542 @param func function providing completion list. func
4531 should be a function taking a reference to the editor and 4543 should be a function taking a reference to the editor and
4532 a boolean indicating to complete a context. It should return 4544 a boolean indicating to complete a context. It should return
4533 the possible completions as a list of strings. 4545 the possible completions as a list of strings.
4534 @type function(editor, bool) -> list of str 4546 @type function(editor, bool) -> list of str in case async is False
4535 """ 4547 and function(editor, bool, str) returning nothing in case async
4536 if key in self.__completionListHookFunctions: 4548 is True
4549 @param async flag indicating an asynchroneous function
4550 @type bool
4551 """
4552 if key in self.__completionListHookFunctions or \
4553 key in self.__completionListAsyncHookFunctions:
4537 # it was already registered 4554 # it was already registered
4538 E5MessageBox.warning( 4555 E5MessageBox.warning(
4539 self, 4556 self,
4540 self.tr("Auto-Completion Provider"), 4557 self.tr("Auto-Completion Provider"),
4541 self.tr("""The completion list provider '{0}' was already""" 4558 self.tr("""The completion list provider '{0}' was already"""
4542 """ registered. Ignoring duplicate request.""") 4559 """ registered. Ignoring duplicate request.""")
4543 .format(key)) 4560 .format(key))
4544 return 4561 return
4545 4562
4546 if not self.__completionListHookFunctions: 4563 if not self.__completionListHookFunctions and \
4564 not self.__completionListAsyncHookFunctions:
4547 if self.autoCompletionThreshold() > 0: 4565 if self.autoCompletionThreshold() > 0:
4548 self.setAutoCompletionThreshold(0) 4566 self.setAutoCompletionThreshold(0)
4549 self.SCN_CHARADDED.connect(self.__charAdded) 4567 self.SCN_CHARADDED.connect(self.__charAdded)
4550 self.__completionListHookFunctions[key] = func 4568
4569 if async:
4570 self.__completionListAsyncHookFunctions[key] = func
4571 else:
4572 self.__completionListHookFunctions[key] = func
4551 4573
4552 def removeCompletionListHook(self, key): 4574 def removeCompletionListHook(self, key):
4553 """ 4575 """
4554 Public method to remove a previously registered completion list 4576 Public method to remove a previously registered completion list
4555 provider. 4577 provider.
4557 @param key name of the provider 4579 @param key name of the provider
4558 @type str 4580 @type str
4559 """ 4581 """
4560 if key in self.__completionListHookFunctions: 4582 if key in self.__completionListHookFunctions:
4561 del self.__completionListHookFunctions[key] 4583 del self.__completionListHookFunctions[key]
4562 4584 elif key in self.__completionListAsyncHookFunctions:
4563 if not self.__completionListHookFunctions: 4585 del self.__completionListAsyncHookFunctions[key]
4586
4587 if not self.__completionListHookFunctions and \
4588 not self.__completionListAsyncHookFunctions:
4564 self.SCN_CHARADDED.disconnect(self.__charAdded) 4589 self.SCN_CHARADDED.disconnect(self.__charAdded)
4565 if self.autoCompletionThreshold() == 0: 4590 if self.autoCompletionThreshold() == 0:
4566 self.setAutoCompletionThreshold( 4591 self.setAutoCompletionThreshold(
4567 Preferences.getEditor("AutoCompletionThreshold")) 4592 Preferences.getEditor("AutoCompletionThreshold"))
4568 4593
4573 @param key name of the provider 4598 @param key name of the provider
4574 @type str 4599 @type str
4575 @return function providing completion list 4600 @return function providing completion list
4576 @rtype function or None 4601 @rtype function or None
4577 """ 4602 """
4578 if key in self.__completionListHookFunctions: 4603 return (self.__completionListHookFunctions.get(key) or
4579 return self.__completionListHookFunctions[key] 4604 self.__completionListAsyncHookFunctions.get(key))
4580 else:
4581 return None
4582 4605
4583 def autoComplete(self, auto=False, context=True): 4606 def autoComplete(self, auto=False, context=True):
4584 """ 4607 """
4585 Public method to start autocompletion. 4608 Public method to start auto-completion.
4586 4609
4587 @keyparam auto flag indicating a call from the __charAdded method 4610 @keyparam auto flag indicating a call from the __charAdded method
4588 (boolean) 4611 (boolean)
4589 @keyparam context flag indicating to complete a context (boolean) 4612 @keyparam context flag indicating to complete a context (boolean)
4590 """ 4613 """
4591 if auto and self.autoCompletionThreshold() == -1: 4614 if auto and self.autoCompletionThreshold() == -1:
4592 # autocompletion is disabled 4615 # auto-completion is disabled
4593 return 4616 return
4594 4617
4595 if self.__completionListHookFunctions: 4618 if self.__completionListHookFunctions or \
4596 if self.isListActive(): 4619 self.__completionListAsyncHookFunctions:
4597 self.cancelList() 4620 if Preferences.getEditor("AutoCompletionTimeout"):
4598 completionsList = [] 4621 self.__acTimer.stop()
4599 for key in self.__completionListHookFunctions: 4622 self.__acContext = context
4600 completionsList.extend( 4623 self.__acTimer.start()
4601 self.__completionListHookFunctions[key](self, context))
4602 completionsList = list(set(completionsList))
4603 if len(completionsList) == 0:
4604 if Preferences.getEditor("AutoCompletionScintillaOnFail") and \
4605 (self.autoCompletionSource() != QsciScintilla.AcsNone or
4606 not auto):
4607 self.autoCompleteQScintilla()
4608 else: 4624 else:
4609 completionsList.sort() 4625 self.__autoComplete(context)
4610 self.showUserList(EditorAutoCompletionListID,
4611 completionsList)
4612 elif not auto: 4626 elif not auto:
4613 self.autoCompleteQScintilla() 4627 self.autoCompleteQScintilla()
4614 elif self.autoCompletionSource() != QsciScintilla.AcsNone: 4628 elif self.autoCompletionSource() != QsciScintilla.AcsNone:
4615 self.autoCompleteQScintilla() 4629 self.autoCompleteQScintilla()
4630
4631 def __autoComplete(self, context=None):
4632 """
4633 Private method to start auto-completion via plug-ins.
4634
4635 @keyparam context flag indicating to complete a context
4636 @type bool or None
4637 """
4638 # TODO: add a cache for recent completions
4639 if context is None:
4640 context = self.__acContext
4641
4642 line, col = self.getCursorPosition()
4643 self.__acText = self.getWordLeft(line, col)
4644 self.__acCompletions.clear()
4645
4646 for key in self.__completionListAsyncHookFunctions:
4647 self.__completionListAsyncHookFunctions[key](
4648 self, context, self.__acText)
4649
4650 for key in self.__completionListHookFunctions:
4651 completions = self.__completionListHookFunctions[key](
4652 self, context)
4653 self.completionsListReady(completions, self.__acText)
4654
4655 def completionsListReady(self, completions, acText):
4656 """
4657 Public method to show the completions determined by a completions
4658 provider.
4659
4660 @param completions list of possible completions
4661 @type list of str
4662 @param acText text to be completed
4663 @type str
4664 """
4665 if acText == self.__acText:
4666 # process the list only, if not already obsolete
4667 if self.isListActive():
4668 self.cancelList()
4669
4670 self.__acCompletions.update(set(completions))
4671 if self.__acCompletions:
4672 self.showUserList(
4673 EditorAutoCompletionListID,
4674 sorted(list(self.__acCompletions),
4675 reverse=Preferences.getEditor(
4676 "AutoCompletionReversedList")))
4616 4677
4617 def __completionListSelected(self, listId, txt): 4678 def __completionListSelected(self, listId, txt):
4618 """ 4679 """
4619 Private slot to handle the selection from the completion list. 4680 Private slot to handle the selection from the completion list.
4620 4681

eric ide

mercurial