From b3eeba17f63e8d9889800bd4b75370c58ae00b4a Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Thu, 12 Jan 2017 18:49:12 +0300 Subject: [PATCH] False, True, None made keywords for Python 2 due to typeshed's __builtins__.pyi --- .../PyKeywordCompletionContributor.java | 4 ++-- .../controlflow/PyControlFlowBuilder.java | 16 ++++++++++++++++ .../python/lexer/PythonHighlightingLexer.java | 7 ++++--- .../python/parsing/StatementParsing.java | 18 +++++++++--------- python/testData/highlighting/declarations.py | 2 +- .../highlighting/yieldInNestedFunction.py | 2 +- .../keywordCompletion/elseInCondExpr.py | 2 +- python/testData/psi/BlockWithoutColon.txt | 4 ++-- python/testData/psi/ErrorInParameterList.txt | 4 ++-- python/testData/psi/LambdaComprehension.txt | 8 ++++---- python/testData/psi/NotClosedSlice.txt | 4 ++-- python/testData/psi/ResetAfterSemicolon.txt | 4 ++-- 12 files changed, 46 insertions(+), 29 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java b/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java index ff500a61e5b4..65c385684ee0 100644 --- a/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java +++ b/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java @@ -248,7 +248,8 @@ public class PyKeywordCompletionContributor extends CompletionContributor { psiElement().inFile(psiFile(PyDocstringFile.class))); private static final ElementPattern IN_FUNCTION_HEADER = - or(psiElement().inside(PyFunction.class).andNot(psiElement().inside(false, psiElement(PyStatementList.class), psiElement(PyFunction.class))), + or(psiElement().inside(PyFunction.class).andNot(or(psiElement().inside(false, psiElement(PyStatementList.class), psiElement(PyFunction.class)), + psiElement().inside(false, psiElement(PyParameterList.class), psiElement(PyFunction.class)))), psiElement().inside(PyClass.class).andNot(psiElement().inside(false, psiElement(PyStatementList.class), psiElement(PyClass.class)))); public static final PsiElementPattern.Capture AFTER_QUALIFIER = @@ -604,7 +605,6 @@ public class PyKeywordCompletionContributor extends CompletionContributor { extend( CompletionType.BASIC, psiElement() .withLanguage(PythonLanguage.getInstance()) - .and(PY3K) .andNot(IN_COMMENT) .andNot(IN_IMPORT_STMT) .and(NOT_PARAMETER_OR_DEFAULT_VALUE) diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index 805000903f33..eaa0ee388b79 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -164,6 +164,22 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { myBuilder.checkPending(readWriteInstruction); } + @Override + public void visitPyBoolLiteralExpression(PyBoolLiteralExpression node) { + final ReadWriteInstruction readWriteInstruction = ReadWriteInstruction.newInstruction(myBuilder, node, node.getText(), + ReadWriteInstruction.ACCESS.READ); + myBuilder.addNode(readWriteInstruction); + myBuilder.checkPending(readWriteInstruction); + } + + @Override + public void visitPyNoneLiteralExpression(PyNoneLiteralExpression node) { + final ReadWriteInstruction readWriteInstruction = ReadWriteInstruction.newInstruction(myBuilder, node, node.getText(), + ReadWriteInstruction.ACCESS.READ); + myBuilder.addNode(readWriteInstruction); + myBuilder.checkPending(readWriteInstruction); + } + @Override public void visitPyTypeDeclarationStatement(PyTypeDeclarationStatement node) { myBuilder.startNode(node); diff --git a/python/src/com/jetbrains/python/lexer/PythonHighlightingLexer.java b/python/src/com/jetbrains/python/lexer/PythonHighlightingLexer.java index 22ace3b1aa66..a785a0ed5813 100644 --- a/python/src/com/jetbrains/python/lexer/PythonHighlightingLexer.java +++ b/python/src/com/jetbrains/python/lexer/PythonHighlightingLexer.java @@ -69,6 +69,10 @@ public class PythonHighlightingLexer extends PythonLexer { if (tokenType == PyTokenTypes.IDENTIFIER) { final String tokenText = getTokenText(); + if (tokenText.equals("None")) return PyTokenTypes.NONE_KEYWORD; + if (tokenText.equals("True")) return PyTokenTypes.TRUE_KEYWORD; + if (tokenText.equals("False")) return PyTokenTypes.FALSE_KEYWORD; + if (myLanguageLevel.hasWithStatement()) { if (tokenText.equals("with")) return PyTokenTypes.WITH_KEYWORD; if (tokenText.equals("as")) return PyTokenTypes.AS_KEYWORD; @@ -79,9 +83,6 @@ public class PythonHighlightingLexer extends PythonLexer { } if (myLanguageLevel.isPy3K()) { - if (tokenText.equals("None")) return PyTokenTypes.NONE_KEYWORD; - if (tokenText.equals("True")) return PyTokenTypes.TRUE_KEYWORD; - if (tokenText.equals("False")) return PyTokenTypes.FALSE_KEYWORD; if (tokenText.equals("nonlocal")) return PyTokenTypes.NONLOCAL_KEYWORD; if (tokenText.equals("__debug__")) return PyTokenTypes.DEBUG_KEYWORD; } diff --git a/python/src/com/jetbrains/python/parsing/StatementParsing.java b/python/src/com/jetbrains/python/parsing/StatementParsing.java index ec54d4755b5e..bde85589af69 100644 --- a/python/src/com/jetbrains/python/parsing/StatementParsing.java +++ b/python/src/com/jetbrains/python/parsing/StatementParsing.java @@ -927,16 +927,16 @@ public class StatementParsing extends Parsing implements ITokenTypeRemapper { isWordAtPosition(text, start, end, TOK_PRINT)) { return PyTokenTypes.PRINT_KEYWORD; } + else if (source == PyTokenTypes.IDENTIFIER && isWordAtPosition(text, start, end, TOK_NONE)) { + return PyTokenTypes.NONE_KEYWORD; + } + else if (source == PyTokenTypes.IDENTIFIER && isWordAtPosition(text, start, end, TOK_TRUE)) { + return PyTokenTypes.TRUE_KEYWORD; + } + else if (source == PyTokenTypes.IDENTIFIER && isWordAtPosition(text, start, end, TOK_FALSE)) { + return PyTokenTypes.FALSE_KEYWORD; + } else if (myContext.getLanguageLevel().isPy3K() && source == PyTokenTypes.IDENTIFIER) { - if (isWordAtPosition(text, start, end, TOK_NONE)) { - return PyTokenTypes.NONE_KEYWORD; - } - if (isWordAtPosition(text, start, end, TOK_TRUE)) { - return PyTokenTypes.TRUE_KEYWORD; - } - if (isWordAtPosition(text, start, end, TOK_FALSE)) { - return PyTokenTypes.FALSE_KEYWORD; - } if (isWordAtPosition(text, start, end, TOK_DEBUG)) { return PyTokenTypes.DEBUG_KEYWORD; } diff --git a/python/testData/highlighting/declarations.py b/python/testData/highlighting/declarations.py index 9f1fcb0455de..fee843c0f7dc 100644 --- a/python/testData/highlighting/declarations.py +++ b/python/testData/highlighting/declarations.py @@ -14,4 +14,4 @@ class __made_up__(self): - return None + return None diff --git a/python/testData/highlighting/yieldInNestedFunction.py b/python/testData/highlighting/yieldInNestedFunction.py index 67628a4e57fb..d366af9e3a25 100644 --- a/python/testData/highlighting/yieldInNestedFunction.py +++ b/python/testData/highlighting/yieldInNestedFunction.py @@ -2,4 +2,4 @@ def foo(): def a(): yield 1 - return False + return False diff --git a/python/testData/keywordCompletion/elseInCondExpr.py b/python/testData/keywordCompletion/elseInCondExpr.py index 647fb0fbd5bd..4a73af1eb5e0 100644 --- a/python/testData/keywordCompletion/elseInCondExpr.py +++ b/python/testData/keywordCompletion/elseInCondExpr.py @@ -1 +1 @@ -a = 1 if True el \ No newline at end of file +a = 1 if True els \ No newline at end of file diff --git a/python/testData/psi/BlockWithoutColon.txt b/python/testData/psi/BlockWithoutColon.txt index abf54f4785da..fd3fed765935 100644 --- a/python/testData/psi/BlockWithoutColon.txt +++ b/python/testData/psi/BlockWithoutColon.txt @@ -13,8 +13,8 @@ PyFile:BlockWithoutColon.py PyWhilePart PsiElement(Py:WHILE_KEYWORD)('while') PsiWhiteSpace(' ') - PyReferenceExpression: True - PsiElement(Py:IDENTIFIER)('True') + PyBoolLiteralExpression + PsiElement(Py:TRUE_KEYWORD)('True') PsiErrorElement:Colon expected PsiWhiteSpace('\n ') diff --git a/python/testData/psi/ErrorInParameterList.txt b/python/testData/psi/ErrorInParameterList.txt index 38b8ad524b53..51ede5e81269 100644 --- a/python/testData/psi/ErrorInParameterList.txt +++ b/python/testData/psi/ErrorInParameterList.txt @@ -12,8 +12,8 @@ PyFile:ErrorInParameterList.py PyNamedParameter('filds') PsiElement(Py:IDENTIFIER)('filds') PsiElement(Py:EQ)('=') - PyReferenceExpression: None - PsiElement(Py:IDENTIFIER)('None') + PyNoneLiteralExpression + PsiElement(Py:NONE_KEYWORD)('None') PsiElement(Py:COMMA)(',') PsiWhiteSpace(' ') PsiErrorElement:formal parameter name expected diff --git a/python/testData/psi/LambdaComprehension.txt b/python/testData/psi/LambdaComprehension.txt index ebd6e914655e..c79d5ba97b38 100644 --- a/python/testData/psi/LambdaComprehension.txt +++ b/python/testData/psi/LambdaComprehension.txt @@ -19,8 +19,8 @@ PyFile:LambdaComprehension.py PsiElement(Py:COLON)(':') PsiWhiteSpace(' ') - PyReferenceExpression: True - PsiElement(Py:IDENTIFIER)('True') + PyBoolLiteralExpression + PsiElement(Py:TRUE_KEYWORD)('True') PsiElement(Py:COMMA)(',') PsiWhiteSpace(' ') PyLambdaExpression @@ -29,8 +29,8 @@ PyFile:LambdaComprehension.py PsiElement(Py:COLON)(':') PsiWhiteSpace(' ') - PyReferenceExpression: False - PsiElement(Py:IDENTIFIER)('False') + PyBoolLiteralExpression + PsiElement(Py:FALSE_KEYWORD)('False') PsiWhiteSpace(' ') PsiElement(Py:IF_KEYWORD)('if') PsiWhiteSpace(' ') diff --git a/python/testData/psi/NotClosedSlice.txt b/python/testData/psi/NotClosedSlice.txt index 4ed3fbbce45f..2012b4959002 100644 --- a/python/testData/psi/NotClosedSlice.txt +++ b/python/testData/psi/NotClosedSlice.txt @@ -14,8 +14,8 @@ PyFile:NotClosedSlice.py PyIfPartIf PsiElement(Py:IF_KEYWORD)('if') PsiWhiteSpace(' ') - PyReferenceExpression: True - PsiElement(Py:IDENTIFIER)('True') + PyBoolLiteralExpression + PsiElement(Py:TRUE_KEYWORD)('True') PsiElement(Py:COLON)(':') PsiWhiteSpace('\n ') PyStatementList diff --git a/python/testData/psi/ResetAfterSemicolon.txt b/python/testData/psi/ResetAfterSemicolon.txt index 6ad856879290..17aea367c18a 100644 --- a/python/testData/psi/ResetAfterSemicolon.txt +++ b/python/testData/psi/ResetAfterSemicolon.txt @@ -3,8 +3,8 @@ PyFile:ResetAfterSemicolon.py PyIfPartIf PsiElement(Py:IF_KEYWORD)('if') PsiWhiteSpace(' ') - PyReferenceExpression: True - PsiElement(Py:IDENTIFIER)('True') + PyBoolLiteralExpression + PsiElement(Py:TRUE_KEYWORD)('True') PsiElement(Py:COLON)(':') PsiWhiteSpace('\n ') PyStatementList