Esempio n. 1
0
    def get_word_before_cursor_with_kind(self, document: Document) -> WordToken:
        """
        Get the word before the cursor and clasify it into one of the kinds
        of tokens: NamedCharacter, AsciiOperator, Symbol, etc.


        If we have whitespace before the cursor this returns an empty string.
        """

        text_before_cursor = document.text_before_cursor

        if self._is_space_before_cursor(
            document=document, text_before_cursor=text_before_cursor
        ):
            return WordToken("", TokenKind.Null)

        start = (
            document.find_start_of_previous_word(
                WORD=False, pattern=FIND_MATHICS_WORD_RE
            )
            or 0
        )

        word_before_cursor = text_before_cursor[len(text_before_cursor) + start :]
        text_before_word = text_before_cursor[: len(word_before_cursor)]
        if word_before_cursor.startswith(r"\["):
            return WordToken(word_before_cursor[2:], TokenKind.NamedCharacter)
        elif text_before_word.endswith(r"\["):
            return WordToken(word_before_cursor, TokenKind.NamedCharacter)
        elif text_before_word[-1:] in CHARGROUP_START:
            word_before_cursor = word_before_cursor
        elif word_before_cursor[1:] in CHARGROUP_START:
            word_before_cursor = word_before_cursor[1:]

        if word_before_cursor.isnumeric():
            return WordToken(word_before_cursor, TokenKind.Null)
        elif word_before_cursor in self.ascii_operators:
            return WordToken(word_before_cursor, TokenKind.ASCII_Operator)
        elif word_before_cursor.startswith("\1xb"):
            return WordToken(word_before_cursor, TokenKind.EscapeSequence)

        return word_before_cursor, TokenKind.Symbol