Esempio n. 1
0
        def search_once(working_index, document):
            """
            Do search one time.
            Return (working_index, document) or `None`
            """
            if direction == SearchDirection.FORWARD:
                # Try find at the current input.
                new_index = document.find(
                    text,
                    include_current_position=include_current_position,
                    ignore_case=ignore_case)

                if new_index is not None:
                    return (working_index,
                            Document(document.text,
                                     document.cursor_position + new_index))
                else:
                    # No match, go forward in the history. (Include len+1 to wrap around.)
                    # (Here we should always include all cursor positions, because
                    # it's a different line.)
                    for i in range(working_index + 1,
                                   len(self._working_lines) + 1):
                        i %= len(self._working_lines)

                        # modified by rtichoke
                        if not no_duplicates or self._working_lines[
                                i] not in self.search_history:
                            document = Document(self._working_lines[i], 0)
                            new_index = document.find(
                                text,
                                include_current_position=True,
                                ignore_case=ignore_case)
                            if new_index is not None:
                                return (i, Document(document.text, new_index))
            else:
                # Try find at the current input.
                new_index = document.find_backwards(text,
                                                    ignore_case=ignore_case)

                if new_index is not None:
                    return (working_index,
                            Document(document.text,
                                     document.cursor_position + new_index))
                else:
                    # No match, go back in the history. (Include -1 to wrap around.)
                    for i in range(working_index - 1, -2, -1):
                        i %= len(self._working_lines)

                        # modified by rtichoke
                        if not no_duplicates or self._working_lines[
                                i] not in self.search_history:
                            document = Document(self._working_lines[i],
                                                len(self._working_lines[i]))
                            new_index = document.find_backwards(
                                text, ignore_case=ignore_case)
                            if new_index is not None:
                                return (i,
                                        Document(
                                            document.text,
                                            len(document.text) + new_index))
Esempio n. 2
0
def last_incomplete_token(document: Document,
                          unparsed_text: str) -> IncompleteToken:
    if document.char_before_cursor in ' ]}':
        last_token = ''
    else:
        last_space = document.find_backwards(' ', in_current_line=True)
        if last_space is None:
            last_space = -1

        last_token = document.text[last_space + 1:]

    # The longer of the last_token and unparsed_text is taken in the event that the
    # unparsed_text is an open literal, which could itself contain spaces.
    if len(unparsed_text) > len(last_token):
        last_token = unparsed_text

    return IncompleteToken(last_token)