Skip to content

Commit

Permalink
remove occurrence highlightings after cancellation of refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
niktrop committed Apr 24, 2015
1 parent 77647b4 commit 71bdb29
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
Expand Up @@ -9,8 +9,8 @@ import com.intellij.lang.refactoring.InlineHandler
import com.intellij.lang.refactoring.InlineHandler.Settings
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.wm.WindowManager
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.impl.source.codeStyle.CodeEditUtil
import com.intellij.psi.search.searches.ReferencesSearch
Expand Down Expand Up @@ -41,6 +41,9 @@ import scala.collection.mutable.ArrayBuffer
*/

class ScalaInlineHandler extends InlineHandler {

private var occurrenceHighlighters: Seq[RangeHighlighter] = Seq.empty

def removeDefinition(element: PsiElement, settings: InlineHandler.Settings) {
element match {
case rp: ScBindingPattern =>
Expand Down Expand Up @@ -95,8 +98,7 @@ class ScalaInlineHandler extends InlineHandler {
val project = newExpr.getProject
val manager = FileEditorManager.getInstance(project)
val editor = manager.getSelectedTextEditor
ScalaRefactoringUtil.highlightOccurrences(project, Array[PsiElement](newExpr), editor)
WindowManager.getInstance().getStatusBar(project).setInfo(ScalaBundle.message("press.escape.to.remove.the.highlighting"))
occurrenceHighlighters = ScalaRefactoringUtil.highlightOccurrences(project, Array[PsiElement](newExpr), editor)
CodeStyleManager.getInstance(project).reformatRange(newExpr.getContainingFile, newExpr.getTextRange.getStartOffset - 1,
newExpr.getTextRange.getEndOffset + 1) //to prevent situations like this 2 ++2 (+2 was inlined)
}
Expand All @@ -120,7 +122,7 @@ class ScalaInlineHandler extends InlineHandler {
val bind = v.declaredElements.apply(0)
val refs = ReferencesSearch.search(bind, bind.getUseScope).findAll.asScala
val inlineTitle = title(inlineTitleSuffix)
ScalaRefactoringUtil.highlightOccurrences(element.getProject, refs.map(_.getElement).toArray, editor)
occurrenceHighlighters = ScalaRefactoringUtil.highlightOccurrences(element.getProject, refs.map(_.getElement).toArray, editor)
val settings = new InlineHandler.Settings {def isOnlyOneReferenceToInline: Boolean = false}
if (refs.size == 0)
showErrorHint(ScalaBundle.message("cannot.inline.never.used"), inlineTitleSuffix)
Expand All @@ -142,8 +144,9 @@ class ScalaInlineHandler extends InlineHandler {
element.getProject)
dialog.show()
if (!dialog.isOK) {
WindowManager.getInstance().getStatusBar(element.getProject).setInfo(ScalaBundle.message("press.escape.to.remove.the.highlighting"))
null
occurrenceHighlighters.foreach(_.dispose())
occurrenceHighlighters = Seq.empty
InlineHandler.Settings.CANNOT_INLINE_SETTINGS
} else settings
} else settings
}
Expand Down
Expand Up @@ -3,10 +3,10 @@ package lang.refactoring.introduceField

import com.intellij.internal.statistic.UsageTrigger
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.editor.{Document, Editor}
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.wm.WindowManager
import com.intellij.psi.{PsiDocumentManager, PsiElement, PsiFile}
import org.jetbrains.plugins.scala.extensions.childOf
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
Expand All @@ -27,6 +27,9 @@ import org.jetbrains.plugins.scala.util.ScalaUtils
* 6/27/13
*/
class ScalaIntroduceFieldFromExpressionHandler extends ScalaIntroduceFieldHandlerBase {

private var occurrenceHighlighters = Seq.empty[RangeHighlighter]

def invoke(project: Project, editor: Editor, file: PsiFile, startOffset: Int, endOffset: Int) {
try {
UsageTrigger.trigger(ScalaBundle.message("introduce.field.id"))
Expand Down Expand Up @@ -152,14 +155,14 @@ class ScalaIntroduceFieldFromExpressionHandler extends ScalaIntroduceFieldHandle
val occCount = ifc.occurrences.length
// Add occurrences highlighting
if (occCount > 1)
ScalaRefactoringUtil.highlightOccurrences(ifc.project, ifc.occurrences, ifc.editor)
occurrenceHighlighters = ScalaRefactoringUtil.highlightOccurrences(ifc.project, ifc.occurrences, ifc.editor)

val dialog = new ScalaIntroduceFieldDialog(ifc, settings)
dialog.show()
if (!dialog.isOK) {
if (occCount > 1) {
WindowManager.getInstance.getStatusBar(ifc.project).
setInfo(ScalaBundle.message("press.escape.to.remove.the.highlighting"))
occurrenceHighlighters.foreach(_.dispose())
occurrenceHighlighters = Seq.empty
}
}
dialog
Expand Down
Expand Up @@ -8,6 +8,7 @@ import com.intellij.ide.util.SuperMethodWarningUtil
import com.intellij.internal.statistic.UsageTrigger
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.editor.{Editor, SelectionModel}
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
Expand Down Expand Up @@ -41,6 +42,8 @@ import scala.collection.mutable.ArrayBuffer
*/
class ScalaIntroduceParameterHandler extends RefactoringActionHandler with DialogConflictsReporter {

private var occurrenceHighlighters = Seq.empty[RangeHighlighter]

def invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext) {
if (!file.isInstanceOf[ScalaFile]) return
if (!ScalaRefactoringUtil.ensureFileWritable(project, file)) {
Expand Down Expand Up @@ -103,13 +106,15 @@ class ScalaIntroduceParameterHandler extends RefactoringActionHandler with Dialo
if (editor != null && !editor.isDisposed)
editor.getSelectionModel.removeSelection()
}
} else {
occurrenceHighlighters.foreach(_.dispose())
occurrenceHighlighters = Seq.empty
}
}
}
}

private type ExprWithTypes = Option[(ScExpression, Array[ScType])]

def selectedElements(file: PsiFile, project: Project, editor: Editor): Option[(ExprWithTypes, Seq[PsiElement])] = {
try {
val selModel: SelectionModel = editor.getSelectionModel
Expand Down Expand Up @@ -137,6 +142,7 @@ class ScalaIntroduceParameterHandler extends RefactoringActionHandler with Dialo
}
}


def collectData(exprWithTypes: ExprWithTypes, elems: Seq[PsiElement], methodLike: ScMethodLike, editor: Editor): Option[ScalaIntroduceParameterData] = {
val project = methodLike.getProject

Expand Down Expand Up @@ -184,7 +190,7 @@ class ScalaIntroduceParameterHandler extends RefactoringActionHandler with Dialo

val occurrences = ScalaRefactoringUtil.getOccurrenceRanges(ScalaRefactoringUtil.unparExpr(expr), occurrencesScope)
if (occurrences.length > 1)
ScalaRefactoringUtil.highlightOccurrences(project, occurrences, editor)
occurrenceHighlighters = ScalaRefactoringUtil.highlightOccurrences(project, occurrences, editor)

(occurrences, expr.getTextRange)
case _ => (Array.empty[TextRange], elems.head.getTextRange.union(elems.last.getTextRange))
Expand Down
Expand Up @@ -11,6 +11,7 @@ import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.project.Project
import com.intellij.openapi.util._
import com.intellij.openapi.wm.WindowManager
Expand Down Expand Up @@ -44,7 +45,9 @@ import scala.collection.JavaConverters._
*/

class ScalaIntroduceVariableHandler extends RefactoringActionHandler with DialogConflictsReporter {

val REFACTORING_NAME = ScalaBundle.message("introduce.variable.title")
private var occurrenceHighlighters = Seq.empty[RangeHighlighter]

def invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext) {
val canBeIntroduced: ScExpression => Boolean = ScalaRefactoringUtil.checkCanBeIntroduced(_)
Expand Down Expand Up @@ -73,7 +76,11 @@ class ScalaIntroduceVariableHandler extends RefactoringActionHandler with Dialog

def runWithDialog() {
val dialog = getDialog(project, editor, expr, types, occurrences, declareVariable = false, validator)
if (!dialog.isOK) return
if (!dialog.isOK) {
occurrenceHighlighters.foreach(_.dispose())
occurrenceHighlighters = Seq.empty
return
}
val varName: String = dialog.getEnteredName
val varType: ScType = dialog.getSelectedType
val isVariable: Boolean = dialog.isDeclareVariable
Expand Down Expand Up @@ -281,7 +288,7 @@ class ScalaIntroduceVariableHandler extends RefactoringActionHandler with Dialog
}
result
}

def createVariableDefinition(): PsiElement = {
val created = ScalaPsiElementFactory.createDeclaration(varName, typeName, isVariable,
ScalaRefactoringUtil.unparExpr(expression), file.getManager)
Expand Down Expand Up @@ -362,7 +369,7 @@ class ScalaIntroduceVariableHandler extends RefactoringActionHandler with Dialog
validator: ScalaVariableValidator): ScalaIntroduceVariableDialog = {
// Add occurrences highlighting
if (occurrences.length > 1)
ScalaRefactoringUtil.highlightOccurrences(project, occurrences, editor)
occurrenceHighlighters = ScalaRefactoringUtil.highlightOccurrences(project, occurrences, editor)

val possibleNames = NameSuggester.suggestNames(expr, validator)
val dialog = new ScalaIntroduceVariableDialog(project, typez, occurrences.length, validator, possibleNames)
Expand Down
Expand Up @@ -45,6 +45,7 @@ import org.jetbrains.plugins.scala.lang.resolve.ScalaResolveResult
import org.jetbrains.plugins.scala.util.JListCompatibility

import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

Expand Down Expand Up @@ -333,7 +334,7 @@ object ScalaRefactoringUtil {
map
}

def highlightOccurrences(project: Project, occurrences: Array[TextRange], editor: Editor) {
def highlightOccurrences(project: Project, occurrences: Array[TextRange], editor: Editor): Seq[RangeHighlighter] = {
val highlighters = new java.util.ArrayList[RangeHighlighter]
var highlightManager: HighlightManager = null
if (editor != null) {
Expand All @@ -343,9 +344,10 @@ object ScalaRefactoringUtil {
for (occurence <- occurrences)
highlightManager.addRangeHighlight(editor, occurence.getStartOffset, occurence.getEndOffset, attributes, true, highlighters)
}
highlighters.asScala
}

def highlightOccurrences(project: Project, occurrences: Array[PsiElement], editor: Editor) {
def highlightOccurrences(project: Project, occurrences: Array[PsiElement], editor: Editor): Seq[RangeHighlighter] = {
highlightOccurrences(project, occurrences.map({
el: PsiElement => el.getTextRange
}), editor)
Expand Down

0 comments on commit 71bdb29

Please sign in to comment.