Search in sources :

Example 1 with PyStringLiteralLexer

use of com.jetbrains.python.lexer.PyStringLiteralLexer in project intellij-community by JetBrains.

the class PyHighlighter method getHighlightingLexer.

@NotNull
public Lexer getHighlightingLexer() {
    LayeredLexer ret = new LayeredLexer(createHighlightingLexer(myLanguageLevel));
    ret.registerLayer(new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_STRING), PyTokenTypes.SINGLE_QUOTED_STRING);
    ret.registerLayer(new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), PyTokenTypes.SINGLE_QUOTED_UNICODE);
    ret.registerLayer(new PyStringLiteralLexer(PyTokenTypes.TRIPLE_QUOTED_STRING), PyTokenTypes.TRIPLE_QUOTED_STRING);
    ret.registerLayer(new PyStringLiteralLexer(PyTokenTypes.TRIPLE_QUOTED_UNICODE), PyTokenTypes.TRIPLE_QUOTED_UNICODE);
    return ret;
}
Also used : PyStringLiteralLexer(com.jetbrains.python.lexer.PyStringLiteralLexer) LayeredLexer(com.intellij.lexer.LayeredLexer) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PyStringLiteralLexer

use of com.jetbrains.python.lexer.PyStringLiteralLexer in project intellij-community by JetBrains.

the class PyLiteralSelectionHandler method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    final PyStringLiteralExpression literal = PsiTreeUtil.getParentOfType(e, PyStringLiteralExpression.class);
    if (literal != null) {
        List<TextRange> ranges = literal.getStringValueTextRanges();
        List<ASTNode> nodes = literal.getStringNodes();
        for (int i = 0; i < ranges.size(); i++) {
            TextRange stringRange = ranges.get(i);
            TextRange offsetRange = stringRange.shiftRight(literal.getTextRange().getStartOffset());
            if (offsetRange.contains(cursorOffset) && offsetRange.getLength() > 1) {
                List<TextRange> result = new ArrayList<>();
                SelectWordUtil.addWordHonoringEscapeSequences(editorText, nodes.get(i).getTextRange(), cursorOffset, new PyStringLiteralLexer(nodes.get(i).getElementType()), result);
                result.add(offsetRange);
                return result;
            }
        }
    }
    return Collections.emptyList();
}
Also used : PyStringLiteralLexer(com.jetbrains.python.lexer.PyStringLiteralLexer) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange)

Example 3 with PyStringLiteralLexer

use of com.jetbrains.python.lexer.PyStringLiteralLexer in project intellij-community by JetBrains.

the class PyStringLiteralLexerTest method testIllegalPrefixes.

// PY-20921
public void testIllegalPrefixes() {
    PyLexerTestCase.doLexerTest("ff'foo'", new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), "Py:SINGLE_QUOTED_UNICODE");
    PyLexerTestCase.doLexerTest("fff'foo'", new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), "Py:SINGLE_QUOTED_UNICODE");
    PyLexerTestCase.doLexerTest("rrr'''foo'''", new PyStringLiteralLexer(PyTokenTypes.TRIPLE_QUOTED_UNICODE), "Py:TRIPLE_QUOTED_UNICODE");
}
Also used : PyStringLiteralLexer(com.jetbrains.python.lexer.PyStringLiteralLexer)

Example 4 with PyStringLiteralLexer

use of com.jetbrains.python.lexer.PyStringLiteralLexer in project intellij-community by JetBrains.

the class PyStringLiteralLexerTest method testBackslashBreaksAnyEscapeSequence.

// PY-21399
public void testBackslashBreaksAnyEscapeSequence() {
    PyLexerTestCase.doLexerTest("'\\u\\')", new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), true, "'", "\\u", "\\'", ")");
    PyLexerTestCase.doLexerTest("'\\u\\')", new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), "Py:SINGLE_QUOTED_UNICODE", "INVALID_UNICODE_ESCAPE_TOKEN", "VALID_STRING_ESCAPE_TOKEN", "Py:SINGLE_QUOTED_UNICODE");
    PyLexerTestCase.doLexerTest("'\\x\\')", new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), true, "'", "\\x", "\\'", ")");
    PyLexerTestCase.doLexerTest("'\\x\\')", new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), "Py:SINGLE_QUOTED_UNICODE", "INVALID_UNICODE_ESCAPE_TOKEN", "VALID_STRING_ESCAPE_TOKEN", "Py:SINGLE_QUOTED_UNICODE");
    PyLexerTestCase.doLexerTest("'\\N{F\\')", new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), true, "'", "\\N{F", "\\'", ")");
    PyLexerTestCase.doLexerTest("'\\N{F\\')", new PyStringLiteralLexer(PyTokenTypes.SINGLE_QUOTED_UNICODE), "Py:SINGLE_QUOTED_UNICODE", "INVALID_UNICODE_ESCAPE_TOKEN", "VALID_STRING_ESCAPE_TOKEN", "Py:SINGLE_QUOTED_UNICODE");
}
Also used : PyStringLiteralLexer(com.jetbrains.python.lexer.PyStringLiteralLexer)

Aggregations

PyStringLiteralLexer (com.jetbrains.python.lexer.PyStringLiteralLexer)4 ASTNode (com.intellij.lang.ASTNode)1 LayeredLexer (com.intellij.lexer.LayeredLexer)1 TextRange (com.intellij.openapi.util.TextRange)1 PyStringLiteralExpression (com.jetbrains.python.psi.PyStringLiteralExpression)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1