--- /dev/null
+/*
+ * Copyright 2000-2014 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python.codeInsight;
+
+/**
+ * @author traff
+ */
+public class PyKeywords {
+ public static final String DEF = "def";
+ public static final String CLASS = "class";
+
+ public static final String IF = "if";
+ public static final String ELSE = "else";
+ public static final String ELIF = "elif";
+
+ public static final String TRY = "try";
+ public static final String EXCEPT = "except";
+ public static final String FINALLY = "finally";
+
+ public static final String WHILE = "while";
+
+ public static final String FOR = "for";
+ public static final String WITH = "with";
+ public static final String AS = "as";
+
+ public static final String ASSERT = "assert";
+ public static final String DEL = "del";
+ public static final String EXEC = "exec";
+ public static final String FROM = "from";
+ public static final String IMPORT = "import";
+ public static final String RAISE = "raise";
+ public static final String PRINT = "print";
+ public static final String BREAK = "break";
+ public static final String CONTINUE = "continue";
+ public static final String GLOBAL = "global";
+ public static final String RETURN = "return";
+ public static final String YIELD = "yield";
+ public static final String NONLOCAL = "nonlocal";
+
+ public static final String AND = "and";
+ public static final String OR = "or";
+ public static final String IS = "is";
+ public static final String IN = "in";
+ public static final String NOT = "not";
+
+ public static final String LAMBDA = "lambda";
+
+ public static final String TRUE = "True";
+ public static final String FALSE = "False";
+ public static final String NONE = "None";
+}
* User: dcheryasov
* Date: Mar 2, 2010 6:48:40 PM
*/
-public class UnindentingInsertHandler implements InsertHandler<PythonLookupElement> {
- public final static UnindentingInsertHandler INSTANCE = new UnindentingInsertHandler();
+public class PyUnindentingInsertHandler implements InsertHandler<PythonLookupElement> {
+ public final static PyUnindentingInsertHandler INSTANCE = new PyUnindentingInsertHandler();
- private UnindentingInsertHandler() {
+ private PyUnindentingInsertHandler() {
}
public void handleInsert(InsertionContext context, PythonLookupElement item) {
final Document document = editor.getDocument();
int offset = editor.getCaretModel().getOffset();
CharSequence text = document.getCharsSequence();
- if (offset >= text.length()) offset = text.length() - 1;
+ if (offset >= text.length()) {
+ offset = text.length() - 1;
+ }
int line_start_offset = document.getLineStartOffset(document.getLineNumber(offset));
int nonspace_offset = findBeginning(line_start_offset, text);
Class<? extends PsiElement> parentClass = null;
- int last_offset = nonspace_offset + "finally".length(); // the longest of all
+ int last_offset = nonspace_offset + PyKeywords.FINALLY.length(); // the longest of all
if (last_offset > offset) last_offset = offset;
int local_length = last_offset - nonspace_offset + 1;
if (local_length > 0) {
String piece = text.subSequence(nonspace_offset, last_offset+1).toString();
- final int else_len = "else".length();
+ final int else_len = PyKeywords.ELSE.length();
if (local_length >= else_len) {
- if ((piece.startsWith("else") || piece.startsWith("elif")) && (else_len == piece.length() || piece.charAt(else_len) < 'a' || piece.charAt(else_len) > 'z')) {
+ if ((piece.startsWith(PyKeywords.ELSE) || piece.startsWith(PyKeywords.ELIF)) && (else_len == piece.length() || piece.charAt(else_len) < 'a' || piece.charAt(else_len) > 'z')) {
parentClass = PyStatementWithElse.class;
}
}
- final int except_len = "except".length();
+ final int except_len = PyKeywords.EXCEPT.length();
if (local_length >= except_len) {
- if (piece.startsWith("except") && (except_len == piece.length() || piece.charAt(except_len) < 'a' || piece.charAt(except_len) > 'z')) {
+ if (piece.startsWith(PyKeywords.EXCEPT) && (except_len == piece.length() || piece.charAt(except_len) < 'a' || piece.charAt(except_len) > 'z')) {
parentClass = PyTryExceptStatement.class;
}
}
- final int finally_len = "finally".length();
+ final int finally_len = PyKeywords.FINALLY.length();
if (local_length >= finally_len) {
- if (piece.startsWith("finally") && (finally_len == piece.length() || piece.charAt(finally_len) < 'a' || piece.charAt(finally_len) > 'z')) {
+ if (piece.startsWith(PyKeywords.FINALLY) && (finally_len == piece.length() || piece.charAt(finally_len) < 'a' || piece.charAt(finally_len) > 'z')) {
parentClass = PyTryExceptStatement.class;
}
}
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.PythonLanguage;
-import com.jetbrains.python.codeInsight.UnindentingInsertHandler;
+import com.jetbrains.python.codeInsight.PyKeywords;
+import com.jetbrains.python.codeInsight.PyUnindentingInsertHandler;
import com.jetbrains.python.documentation.doctest.PyDocstringFile;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NonNls;
psiElement()
.inside(PyConditionalStatementPart.class)
.andOr(
- psiElement().afterLeaf(psiElement().withText("if")),
- psiElement().afterLeaf(psiElement().withText("elif")),
- psiElement().afterLeaf(psiElement().withText("while"))
+ psiElement().afterLeaf(psiElement().withText(PyKeywords.IF)),
+ psiElement().afterLeaf(psiElement().withText(PyKeywords.ELIF)),
+ psiElement().afterLeaf(psiElement().withText(PyKeywords.WHILE))
);
private static final PsiElementPattern.Capture<PsiElement> IN_IMPORT_STMT =
protected void addCompletions(
@NotNull final CompletionParameters parameters, final ProcessingContext context, @NotNull final CompletionResultSet result
) {
- putKeywords(result, TailType.NONE, "def", "class", "for", "if", "while", "with");
- putKeywords(result, TailType.CASE_COLON, "try");
+ putKeywords(result, TailType.NONE, PyKeywords.DEF, PyKeywords.CLASS, PyKeywords.FOR, PyKeywords.IF, PyKeywords.WHILE, PyKeywords.WITH);
+ putKeywords(result, TailType.CASE_COLON, PyKeywords.TRY);
}
}
);
protected void addCompletions(
@NotNull final CompletionParameters parameters, final ProcessingContext context, @NotNull final CompletionResultSet result
) {
- putKeywords(result, TailType.SPACE, "assert", "del", "exec", "from", "import", "raise");
+ putKeywords(result, TailType.SPACE, PyKeywords.ASSERT, PyKeywords.DEL, PyKeywords.EXEC, PyKeywords.FROM, PyKeywords.IMPORT, PyKeywords.RAISE);
putKeywords(result, TailType.NONE, PyNames.PASS);
}
}
);
- extend(CompletionType.BASIC, inStatement.andNot(PY3K), new PyKeywordCompletionProvider(TailType.SPACE, "print"));
+ extend(CompletionType.BASIC, inStatement.andNot(PY3K), new PyKeywordCompletionProvider(TailType.SPACE, PyKeywords.PRINT));
}
private void addBreak() {
.andNot(IN_ARG_LIST)
.and(IN_LOOP)
,
- new PyKeywordCompletionProvider(TailType.NONE, "break")
+ new PyKeywordCompletionProvider(TailType.NONE, PyKeywords.BREAK)
);
}
.andNot(IN_FINALLY_NO_LOOP)
.and(IN_LOOP)
,
- new PyKeywordCompletionProvider(TailType.NONE, "continue")
+ new PyKeywordCompletionProvider(TailType.NONE, PyKeywords.CONTINUE)
);
}
.and(IN_BEGIN_STMT)
.andNot(AFTER_QUALIFIER)
,
- new PyKeywordCompletionProvider("global", "return", "yield")
+ new PyKeywordCompletionProvider(PyKeywords.GLOBAL, PyKeywords.RETURN, PyKeywords.YIELD)
);
extend(
.and(PY3K)
.andNot(AFTER_QUALIFIER)
,
- new PyKeywordCompletionProvider("nonlocal")
+ new PyKeywordCompletionProvider(PyKeywords.NONLOCAL)
);
}
.andOr(IN_IF_BODY, AFTER_IF)
.andNot(AFTER_QUALIFIER).andNot(IN_STRING_LITERAL)
,
- new PyKeywordCompletionProvider(TailType.NONE, UnindentingInsertHandler.INSTANCE, "elif"));
+ new PyKeywordCompletionProvider(TailType.NONE, PyUnindentingInsertHandler.INSTANCE, PyKeywords.ELIF));
}
private void addWithinTry() {
protected void addCompletions(
@NotNull final CompletionParameters parameters, final ProcessingContext context, @NotNull final CompletionResultSet result
) {
- putKeyword("except", UnindentingInsertHandler.INSTANCE, TailType.NONE, result);
- putKeyword("finally", UnindentingInsertHandler.INSTANCE, TailType.CASE_COLON, result);
+ putKeyword(PyKeywords.EXCEPT, PyUnindentingInsertHandler.INSTANCE, TailType.NONE, result);
+ putKeyword(PyKeywords.FINALLY, PyUnindentingInsertHandler.INSTANCE, TailType.CASE_COLON, result);
}
}
);
.andOr(IN_COND_STMT, IN_EXCEPT_BODY, AFTER_COND_STMT_NO_ELSE, AFTER_LOOP_NO_ELSE, AFTER_EXCEPT)
.andNot(AFTER_QUALIFIER).andNot(IN_STRING_LITERAL)
,
- new PyKeywordCompletionProvider(TailType.CASE_COLON, UnindentingInsertHandler.INSTANCE, "else"));
+ new PyKeywordCompletionProvider(TailType.CASE_COLON, PyUnindentingInsertHandler.INSTANCE, PyKeywords.ELSE));
}
private void addInfixOperators() {
.andNot(AFTER_QUALIFIER).
andNot(IN_STRING_LITERAL).and(IN_BEGIN_STMT)
,
- new PyKeywordCompletionProvider("and", "or", "is", "in")
+ new PyKeywordCompletionProvider(PyKeywords.AND, PyKeywords.OR, PyKeywords.IS, PyKeywords.IN)
);
}
.andNot(IN_FUNCTION_HEADER)
.andNot(AFTER_QUALIFIER).andNot(IN_STRING_LITERAL)
,
- new PyKeywordCompletionProvider("not", "lambda")
+ new PyKeywordCompletionProvider(PyKeywords.NOT, PyKeywords.LAMBDA)
);
}
.andNot(AFTER_QUALIFIER)
.andNot(IN_FUNCTION_HEADER)
,
- new PyKeywordCompletionProvider(TailType.NONE, "True", "False", "None")
+ new PyKeywordCompletionProvider(TailType.NONE, PyKeywords.TRUE, PyKeywords.FALSE, PyKeywords.NONE)
);
}
.andOr(IN_IMPORT_AFTER_REF, IN_WITH_AFTER_REF, IN_EXCEPT_AFTER_REF)
.andNot(AFTER_QUALIFIER)
,
- new PyKeywordCompletionProvider("as")
+ new PyKeywordCompletionProvider(PyKeywords.AS)
);
}
.and(IN_FROM_IMPORT_AFTER_REF)
.andNot(AFTER_QUALIFIER)
,
- new PyKeywordCompletionProvider("import")
+ new PyKeywordCompletionProvider(PyKeywords.IMPORT)
);
}
.withLanguage(PythonLanguage.getInstance())
.afterLeafSkipping(psiElement().whitespace(),
psiElement().inside(psiElement(PyConditionalExpression.class))
- .and(psiElement().afterLeaf("if")))
+ .and(psiElement().afterLeaf(PyKeywords.IF)))
,
- new PyKeywordCompletionProvider(TailType.SPACE, "else"));
+ new PyKeywordCompletionProvider(TailType.SPACE, PyKeywords.ELSE));
}
private void addRaiseFrom() {
.withLanguage(PythonLanguage.getInstance())
.and(PY3K)
.afterLeaf(psiElement().inside(PyRaiseStatement.class)),
- new PyKeywordCompletionProvider("from"));
+ new PyKeywordCompletionProvider(PyKeywords.FROM));
}
private void addYieldFrom() {
.withLanguage(PythonLanguage.getInstance())
.and(PY3K)
.afterLeaf(psiElement().withElementType(PyTokenTypes.YIELD_KEYWORD)),
- new PyKeywordCompletionProvider("from"));
+ new PyKeywordCompletionProvider(PyKeywords.FROM));
}
public PyKeywordCompletionContributor() {
.withLanguage(PythonLanguage.getInstance())
.inside(psiElement(PySequenceExpression.class))
.andNot(psiElement().afterLeaf(or(psiElement(PyTokenTypes.LBRACE), psiElement(PyTokenTypes.LBRACKET), psiElement(PyTokenTypes.LPAR)))),
- new PyKeywordCompletionProvider("for"));
+ new PyKeywordCompletionProvider(PyKeywords.FOR));
}
private void addInToFor() {
extend(CompletionType.BASIC,
psiElement()
.withLanguage(PythonLanguage.getInstance())
- .and(psiElement()).afterLeaf(psiElement().afterLeaf("for")),
+ .and(psiElement()).afterLeaf(psiElement().afterLeaf(PyKeywords.FOR)),
new PyKeywordCompletionProvider("in"));
}