From 3e188e2f0b8e5da3852b08a2cebd9b5993a7257e Mon Sep 17 00:00:00 2001 From: Sergey Lukjanov Date: Mon, 18 Jun 2012 18:46:43 +0400 Subject: [PATCH] Quickfix for unnecessary non-null assertion added. --- .../jet/plugin/quickfix/QuickFixes.java | 10 ++- .../UnnecessaryNotNullAssertionFix.java | 73 +++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/UnnecessaryNotNullAssertionFix.java diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index ee3e4874220..9b9eb6922c7 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -133,8 +133,12 @@ public class QuickFixes { actions.put(VAL_WITH_SETTER, changeVariableMutabilityFix); actions.put(VAL_REASSIGNMENT, changeVariableMutabilityFix); - actions.put(UNNECESSARY_SAFE_CALL, new ReplaceCallFix(false)); - actions.put(UNSAFE_CALL, new ReplaceCallFix(true)); + actions.put(UNNECESSARY_SAFE_CALL, new ReplaceCallFix(true, false)); + actions.put(UNSAFE_CALL, new ReplaceCallFix(true, true)); + actions.put(UNSAFE_CALL, new ReplaceCallFix(false, true)); + + actions.put(UNNECESSARY_NOT_NULL_ASSERTION, new UnnecessaryNotNullAssertionFix()); + actions.put(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE, new SpecifyTypeExplicitlyFix()); } -} \ No newline at end of file +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/UnnecessaryNotNullAssertionFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/UnnecessaryNotNullAssertionFix.java new file mode 100644 index 00000000000..ef49339aaa8 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/UnnecessaryNotNullAssertionFix.java @@ -0,0 +1,73 @@ +/* + * Copyright 2010-2012 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.quickfix; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.impl.source.tree.LeafPsiElement; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lexer.JetTokens; +import org.jetbrains.jet.plugin.JetBundle; + +/** + * @author slukjanov aka Frostman + */ +public class UnnecessaryNotNullAssertionFix implements IntentionAction { + @NotNull + @Override + public String getText() { + return JetBundle.message("replace.with.dot.call"); + } + + @NotNull + @Override + public String getFamilyName() { + return getText(); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + return file instanceof JetFile && getExclExclElement(editor, file) != null; + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + final PsiElement exclExcl = getExclExclElement(editor, file); + assert exclExcl != null; + + exclExcl.delete(); + } + + @Override + public boolean startInWriteAction() { + return true; + } + + private static PsiElement getExclExclElement(Editor editor, PsiFile file) { + final PsiElement elementAtCaret = file.findElementAt(editor.getCaretModel().getOffset()); + if (elementAtCaret instanceof LeafPsiElement && ((LeafPsiElement) elementAtCaret).getElementType() == JetTokens.EXCLEXCL) { + return elementAtCaret; + } + + return null; + } +}