JetPsiPrecedences rewritten in Kotlin

This commit is contained in:
Andrey Breslav
2013-09-23 02:59:53 +04:00
parent fc70a65f99
commit e829ef8304
5 changed files with 71 additions and 75 deletions
@@ -0,0 +1,5 @@
<root>
<item name='com.intellij.openapi.diagnostic.Logger com.intellij.openapi.diagnostic.Logger getInstance(java.lang.Class)'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
@@ -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;
}
@@ -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<IElementType, Integer> precedence;
static {
Map<IElementType, Integer> builder = new HashMap<IElementType, Integer>();
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
}
}
@@ -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)
@@ -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<JetPsiPrecedences>())
private val precedence: Map<IElementType, Int>
{
val builder = HashMap<IElementType, Int>()
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
}
}
}