IDEA-81100 Groovy: "Remove unnecessary semicolon" intention shouldn't be shown on code selection if there are no semicolons at all

This commit is contained in:
Maxim.Medvedev
2012-02-10 13:02:20 +04:00
parent 6d024064c5
commit 7ac7d20bec
@@ -19,6 +19,8 @@ import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
@@ -59,13 +61,21 @@ public class RemoveUnnecessarySemicolonsIntention implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof GroovyFileBase) || editor.getSelectionModel().hasBlockSelection()) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (!(file instanceof GroovyFileBase)) return false;
if (selectionModel.hasBlockSelection()) return false;
if (selectionModel.hasSelection()) {
final HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(selectionModel.getSelectionStart());
final int end = selectionModel.getSelectionEnd();
while (!iterator.atEnd()) {
if (iterator.getTokenType() == GroovyTokenTypes.mSEMI) return true;
if (iterator.getStart() > end) return false;
iterator.advance();
}
return false;
}
if (editor.getSelectionModel().hasSelection()) return true;
int offset = editor.getCaretModel().getOffset();
if (offset >= editor.getDocument().getTextLength()) offset = editor.getDocument().getTextLength() - 1;
final PsiElement element = file.findElementAt(offset);