From 7ac7d20bec4bce34ced7e783b14de62471c6a07e Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Thu, 9 Feb 2012 17:57:32 +0400 Subject: [PATCH] IDEA-81100 Groovy: "Remove unnecessary semicolon" intention shouldn't be shown on code selection if there are no semicolons at all --- .../RemoveUnnecessarySemicolonsIntention.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/RemoveUnnecessarySemicolonsIntention.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/RemoveUnnecessarySemicolonsIntention.java index 31b873ce5780..a05b771d6d2b 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/RemoveUnnecessarySemicolonsIntention.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/RemoveUnnecessarySemicolonsIntention.java @@ -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);