Beispiel #1
0
    def autoComplete(self, userText):
        """
        If userText begins with this factory's prefix, and the
        remainder of userText begins a word of one of this factory's
        postfixes, then returns an Autocompletion object for the
        match.  Otherwise, returns None.
        """

        if self.PREFIX.startswith(userText):
            # If seed text is all or part of the prefix, then
            # autocomplete with help text.
            return AutoCompletion(userText, self.PREFIX, self.HELP_TEXT)

        elif not userText.startswith(self.PREFIX):
            return None

        pattern = userText[len(self.PREFIX):]
        pattern = _equivalizeChars(pattern)
        matches = self.__findMatches(pattern)
        if len(self.PREFIX) > 0 and len(matches) == 0:
            # We have a real prefix; look for beginings of words.
            matches = self.__findMatches(".*\\b" + pattern)
        if len(matches) < 1:
            return None
        match = matches[0]
        matchLocation = re.search(pattern, match).start()

        newUserText = self.PREFIX
        start = matchLocation
        end = matchLocation + len(userText) - len(self.PREFIX)
        newUserText += match[start:end]
        completion = AutoCompletion(newUserText, self.PREFIX + match)
        return completion
Beispiel #2
0
    def autoComplete(self, seedText):
        """
        Returns an autocompletion for seedText if seedText begins
        with all or part of the prefix for this command factory.
        If the seedText did not have a postfix, then the returned
        suggestion object will have help text.

        Returns None otherwise.
        """

        if hasattr(self, "NAME") and str(
                self.NAME) in config.DISABLED_COMMANDS:
            return None

        if self.PREFIX.startswith(seedText):
            # If seed text is all or part of the prefix, then
            # autocomplete with help text.
            return AutoCompletion(seedText, self.PREFIX, self.HELP_TEXT)
        elif seedText.startswith(self.PREFIX):
            # If the seed text begins with the prefix, but has
            # more than just the seed text (i.e., made it beyond
            # the previous condition), then there is a postfix;
            # autocomplete without trailing help text.
            return AutoCompletion(seedText, seedText)
        else:
            return None
Beispiel #3
0
    def autoComplete(self, userText):
        """
        If userText begins with this factory's prefix, and the
        remainder of userText begins a word of one of this factory's
        postfixes, then returns an Autocompletion object for the
        match.  Otherwise, returns None.
        """

        if self.PREFIX.startswith(userText):
            # If seed text is all or part of the prefix, then
            # autocomplete with help text.
            return AutoCompletion(userText, self.PREFIX, self.HELP_TEXT)
        elif not userText.startswith(self.PREFIX):
            return None

        pattern = userText[len(self.PREFIX):]
        pattern = _equivalizeChars(pattern)
        matches = self.__findMatches(pattern)
        if len(self.PREFIX) > 0 and len(matches) == 0:
            # We have a real prefix; look for beginings of words.
            # Instead of \b for word boundary, use [^a-zA-Z0-9] so the underscore
            # character is also considered as a word boundary
            matches = self.__findMatches(r".*?(?:^|[^a-zA-Z0-9])(%s)" %
                                         re.escape(pattern))
        if len(matches) < 1:
            return None

        # Take first hit by default...
        match, match_location = matches[0]
        """
        #FIXME: Is this really needed here? It does not work properly (try to type 'open o', it will result into 'open open'
        # ..but prefer 'open' command if it's in the list
        if (userText.startswith("o") and config.PRIORITIZE_OPEN_COMMAND and
            any(m[0].startswith("open ") for m in matches)):
            # Special handling for 'open' command:
            # Put it before other commands starting with "o".
            match = "open"
        """
        # TODO: This is incorrect. It matches the first one, not the correct one
        #matchLocation = re.search( pattern, match, re.I ).start()

        newUserText = self.PREFIX
        start = match_location
        end = match_location + len(userText) - len(self.PREFIX)
        newUserText += match[start:end]
        completion = AutoCompletion(newUserText, self.PREFIX + match, None,
                                    len(self.PREFIX), start, end)
        return completion
Beispiel #4
0
    def clearState(self):
        """
        Clears all of the variables relating to the state of the
        quasimode's generated information.
        """

        # The "user text".  Together with the active index, constitutes
        # the "source" information, i.e., the information from which
        # all the rest is calculated.
        self.__userText = ""
        self.__userTextPrefix = ""

        # An index of the above suggestion list indicating which
        # command name the user has indicated.
        self.__activeIndex = 0

        # The current auto-completion object.
        self.__autoCompletion = AutoCompletion(originalText="",
                                               suggestedText="")

        # The current list of suggestions. The 0th element is the
        # auto-completion.
        self.__suggestions = [self.__autoCompletion]

        # Did-you-mean hint
        self.__didyoumean_hint = None

        self.__activeCommand = None

        # A boolean telling whether the suggestion list and
        # auto-completion attributes above need to be updated.
        self.__isDirty = False
Beispiel #5
0
    def __autoComplete(self, userText):
        """
        Uses the CommandManager to determine if userText auto-completes
        to a command name, and what that command name is.

        Returns an AutoCompletion object; if the AutoCompletion object
        is empty (i.e., the text representation has 0 length), then there
        was no valid auto-completed command name.
        """

        if len(userText) < config.QUASIMODE_MIN_AUTOCOMPLETE_CHARS:
            autoCompletion = AutoCompletion(userText, "")
        else:
            autoCompletion = self.__cmdManager.autoComplete(userText)
            if autoCompletion is None:
                autoCompletion = AutoCompletion(userText, "")

        return autoCompletion