From e829ef8304d91308b944bbc1020a57e177beb71f Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Mon, 23 Sep 2013 02:59:53 +0400 Subject: [PATCH] JetPsiPrecedences rewritten in Kotlin --- .../openapi/diagnostic/annotations.xml | 5 ++ .../lang/parsing/JetExpressionParsing.java | 2 + .../jet/lang/psi/JetPsiPrecedences.java | 73 ------------------- .../quickfix/KotlinSuppressIntentionAction.kt | 4 +- .../jet/plugin/util/JetPsiPrecedences.kt | 62 ++++++++++++++++ 5 files changed, 71 insertions(+), 75 deletions(-) create mode 100644 annotations/com/intellij/openapi/diagnostic/annotations.xml delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiPrecedences.java create mode 100644 idea/src/org/jetbrains/jet/plugin/util/JetPsiPrecedences.kt diff --git a/annotations/com/intellij/openapi/diagnostic/annotations.xml b/annotations/com/intellij/openapi/diagnostic/annotations.xml new file mode 100644 index 00000000000..d9806ca45bc --- /dev/null +++ b/annotations/com/intellij/openapi/diagnostic/annotations.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java index 435fcab80fc..c9811b17ce2 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableMap; import com.intellij.lang.PsiBuilder; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; +import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetNodeType; import org.jetbrains.jet.lexer.JetToken; import org.jetbrains.jet.lexer.JetTokens; @@ -208,6 +209,7 @@ public class JetExpressionParsing extends AbstractJetParsing { return BINARY_EXPRESSION; } + @NotNull public final TokenSet getOperations() { return operations; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiPrecedences.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiPrecedences.java deleted file mode 100644 index c1943333695..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiPrecedences.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2010-2013 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 org.jetbrains.jet.lang.psi; - -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.psi.tree.IElementType; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.parsing.JetExpressionParsing; - -import java.util.HashMap; -import java.util.Map; - -import static org.jetbrains.jet.lang.parsing.JetExpressionParsing.Precedence.*; - -public class JetPsiPrecedences { - private static final Logger LOG = Logger.getInstance(JetPsiPrecedences.class); - - private static final Map precedence; - static { - Map builder = new HashMap(); - - JetExpressionParsing.Precedence[] records = values(); - for (int i = 0; i < records.length; i++) { - for (IElementType elementType : records[i].getOperations().getTypes()) { - builder.put(elementType, i); - } - } - - precedence = builder; - } - - public static final int PRECEDENCE_OF_ATOMIC_EXPRESSION = -1; - - public static final int PRECEDENCE_OF_PREFIX_EXPRESSION = PREFIX.ordinal(); - - public static final int PRECEDENCE_OF_POSTFIX_EXPRESSION = POSTFIX.ordinal(); - - public static int getPrecedence(@NotNull JetExpression expression) { - if (expression instanceof JetAnnotatedExpression || expression instanceof JetPrefixExpression) { - return PRECEDENCE_OF_PREFIX_EXPRESSION; - } - if (expression instanceof JetPostfixExpression) { - return PRECEDENCE_OF_POSTFIX_EXPRESSION; - } - if (expression instanceof JetOperationExpression) { - JetOperationExpression operationExpression = (JetOperationExpression) expression; - - IElementType operation = operationExpression.getOperationReference().getReferencedNameElementType(); - - Integer precedenceNumber = precedence.get(operation); - if (precedenceNumber == null) { - LOG.error("No precedence for operation: " + operation); - return precedence.size(); // lowest - } - return precedenceNumber; - } - return PRECEDENCE_OF_ATOMIC_EXPRESSION; // Atomic expression - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/KotlinSuppressIntentionAction.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/KotlinSuppressIntentionAction.kt index 59b01dbece6..4c5ebf3c935 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/KotlinSuppressIntentionAction.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/KotlinSuppressIntentionAction.kt @@ -26,7 +26,7 @@ import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns import com.intellij.codeInspection.SuppressIntentionAction -import org.jetbrains.jet.lang.psi.JetPsiPrecedences.* +import org.jetbrains.jet.plugin.util.JetPsiPrecedences public class KotlinSuppressIntentionAction( private val suppressAt: JetExpression, @@ -95,7 +95,7 @@ public class KotlinSuppressIntentionAction( val project = suppressAt.getProject() - val parentheses = getPrecedence(suppressAt) > PRECEDENCE_OF_PREFIX_EXPRESSION + val parentheses = JetPsiPrecedences.getPrecedence(suppressAt) > JetPsiPrecedences.PRECEDENCE_OF_PREFIX_EXPRESSION val placeholderText = "PLACEHOLDER_ID" val inner = if (parentheses) "($placeholderText)" else placeholderText val annotatedExpression = JetPsiFactory.createExpression(project, suppressAnnotationText(id) + "\n" + inner) diff --git a/idea/src/org/jetbrains/jet/plugin/util/JetPsiPrecedences.kt b/idea/src/org/jetbrains/jet/plugin/util/JetPsiPrecedences.kt new file mode 100644 index 00000000000..bfe018c2ebd --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/util/JetPsiPrecedences.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2010-2013 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 org.jetbrains.jet.plugin.util + +import com.intellij.openapi.diagnostic.Logger +import com.intellij.psi.tree.IElementType +import org.jetbrains.jet.lang.parsing.JetExpressionParsing +import java.util.HashMap +import org.jetbrains.jet.lang.parsing.JetExpressionParsing.Precedence.* +import org.jetbrains.jet.lang.psi.* + +public object JetPsiPrecedences { + + private val LOG = Logger.getInstance(javaClass()) + + private val precedence: Map + { + val builder = HashMap() + for ((i, record) in JetExpressionParsing.Precedence.values().withIndices()) { + for (elementType in record.getOperations().getTypes()) { + builder[elementType] = i + } + } + precedence = builder + } + + public val PRECEDENCE_OF_ATOMIC_EXPRESSION: Int = -1 + public val PRECEDENCE_OF_PREFIX_EXPRESSION: Int = PREFIX.ordinal() + public val PRECEDENCE_OF_POSTFIX_EXPRESSION: Int = POSTFIX.ordinal() + + public fun getPrecedence(expression: JetExpression): Int { + return when (expression) { + is JetAnnotatedExpression, + is JetPrefixExpression -> PRECEDENCE_OF_PREFIX_EXPRESSION + is JetPostfixExpression -> PRECEDENCE_OF_POSTFIX_EXPRESSION + is JetOperationExpression -> { + val operation = expression.getOperationReference().getReferencedNameElementType() + val precedenceNumber = precedence[operation] + if (precedenceNumber == null) { + LOG.error("No precedence for operation: " + operation) + precedence.size() + } + else precedenceNumber + } + else -> PRECEDENCE_OF_ATOMIC_EXPRESSION + } + } +}