Permalink
Browse files

Added a Scala preference for Mark Occurrences

Added a toolbar button for quick access.
Fixes #1001466
  • Loading branch information...
1 parent a5ab7c5 commit 2ec99cd10e96bf62e1e00fa0123141ce5ebfd6b1 @skyluc skyluc committed Feb 7, 2013
View
BIN org.scala-ide.sdt.core/icons/full/etool16/mark_occurrences.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
16 org.scala-ide.sdt.core/plugin.xml
@@ -869,6 +869,12 @@
description="Debug Scala JUnit Test"
id="org.eclipse.shortcut.scala.junit.debug"
name="Debug Scala JUnit Test" />
+ <command
+ categoryId="scala.tools.eclipse.category"
+ description="Toggle Mark Occurences"
+ id="org.scala-ide.sdt.core.toggleMarkOccurences"
+ name="Toggle Mark Occurrences">
+ </command>
</extension>
<extension point="org.eclipse.ui.bindings">
@@ -1315,6 +1321,10 @@
class="scala.tools.eclipse.actions.OpenImplicitCommand"
commandId="scala.tools.eclipse.editor.OpenImplicit">
</handler>
+ <handler
+ class="scala.tools.eclipse.markoccurrences.ToggleMarkOccurrencesHandler"
+ commandId="org.scala-ide.sdt.core.toggleMarkOccurences">
+ </handler>
</extension>
<extension
point="org.eclipse.ui.menus">
@@ -1328,6 +1338,12 @@
id="org.scala-ide.sdt.core.toolbar.ToggleImplicitsDisplay"
style="toggle">
</command>
+ <command
+ commandId="org.scala-ide.sdt.core.toggleMarkOccurences"
+ icon="icons/full/etool16/mark_occurrences.png"
+ id="org.scala-ide.sdt.core.toolbar.ToggleMarkOccurences"
+ style="toggle">
+ </command>
</toolbar>
</menuContribution>
<menuContribution
View
48 org.scala-ide.sdt.core/src/scala/tools/eclipse/ScalaSourceFileEditor.scala
@@ -65,13 +65,16 @@ import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport
import org.eclipse.ui.texteditor.TextOperationAction
import scala.tools.eclipse.util.EclipseUtils
+import org.eclipse.jdt.ui.PreferenceConstants
+import scala.tools.eclipse.properties.EditorPreferencePage
class ScalaSourceFileEditor extends CompilationUnitEditor with ScalaEditor { self =>
import ScalaSourceFileEditor._
private var occurrenceAnnotations: Set[Annotation] = Set()
private var occurrencesFinder: ScalaOccurrencesFinder = _
+ private var occurencesFinderInstalled = false
private val preferenceListener: IPropertyChangeListener = handlePreferenceStoreChanged _
private lazy val selectionListener = new ISelectionListener() {
def selectionChanged(part: IWorkbenchPart, selection: ISelection) {
@@ -160,15 +163,17 @@ class ScalaSourceFileEditor extends CompilationUnitEditor with ScalaEditor { sel
override def updateOccurrenceAnnotations(selection: ITextSelection, astRoot: CompilationUnit): Unit = {
askForOccurrencesUpdate(selection)
}
+
+ /** Returns the annotation model of the current document provider.
+ */
+ private def getAnnotationModelOpt: Option[IAnnotationModel] = {
+ for {
+ documentProvider <- Option(getDocumentProvider)
+ annotationModel <- Option(documentProvider.getAnnotationModel(getEditorInput))
+ } yield annotationModel
+ }
private def performOccurrencesUpdate(selection: ITextSelection, documentLastModified: Long) {
- def getAnnotationModelOpt: Option[IAnnotationModel] = {
- for {
- documentProvider <- Option(getDocumentProvider)
- annotationModel <- Option(documentProvider.getAnnotationModel(getEditorInput))
- } yield annotationModel
- }
-
val annotations = getAnnotations(selection, getInteractiveCompilationUnit, documentLastModified)
for(annotationModel <- getAnnotationModelOpt) annotationModel.withLock {
annotationModel.replaceAnnotations(occurrenceAnnotations, annotations)
@@ -221,13 +226,27 @@ class ScalaSourceFileEditor extends CompilationUnitEditor with ScalaEditor { sel
}
override def installOccurrencesFinder(forceUpdate: Boolean) {
- super.installOccurrencesFinder(forceUpdate)
- getEditorSite.getPage.addPostSelectionListener(selectionListener)
+ if (!occurencesFinderInstalled) {
+ super.installOccurrencesFinder(forceUpdate)
+ getEditorSite.getPage.addPostSelectionListener(selectionListener)
+ occurencesFinderInstalled = true
+ }
}
override def uninstallOccurrencesFinder() {
+ occurencesFinderInstalled = false
getEditorSite.getPage.removePostSelectionListener(selectionListener)
super.uninstallOccurrencesFinder
+ removeScalaOccurrenceAnnotations()
+ }
+
+ /** Clear the existing Mark Occurrences annotations.
+ */
+ def removeScalaOccurrenceAnnotations() {
+ for (annotationModel <- getAnnotationModelOpt) annotationModel.withLock {
+ annotationModel.replaceAnnotations(occurrenceAnnotations, Map())
+ occurrenceAnnotations = Set()
+ }
}
override def dispose() {
@@ -303,6 +322,14 @@ class ScalaSourceFileEditor extends CompilationUnitEditor with ScalaEditor { sel
event.getProperty match {
case ShowInferredSemicolonsAction.PREFERENCE_KEY =>
getAction(ShowInferredSemicolonsAction.ACTION_ID).asInstanceOf[IUpdate].update()
+ case PreferenceConstants.EDITOR_MARK_OCCURRENCES =>
+ // swallow the event. We don't want 'mark occurrences' to be linked to the Java editor preference
+ case EditorPreferencePage.P_ENABLE_MARK_OCCURRENCES =>
+ if (event.getNewValue().equals("true")) {
+ installOccurrencesFinder(true)
+ } else {
+ uninstallOccurrencesFinder()
+ }
case _ =>
if (affectsTextPresentation(event)) {
// those events will trigger a UI change
@@ -312,6 +339,9 @@ class ScalaSourceFileEditor extends CompilationUnitEditor with ScalaEditor { sel
}
}
+ override def isMarkingOccurrences =
+ scalaPrefStore.getBoolean(EditorPreferencePage.P_ENABLE_MARK_OCCURRENCES)
+
override def configureSourceViewerDecorationSupport(support: SourceViewerDecorationSupport) {
super.configureSourceViewerDecorationSupport(support)
SemanticHighlightingAnnotations.addAnnotationPreferences(support)
View
13 ...a-ide.sdt.core/src/scala/tools/eclipse/markoccurrences/ToggleMarkOccurrencesHandler.scala
@@ -0,0 +1,13 @@
+package scala.tools.eclipse.markoccurrences
+
+import scala.tools.eclipse.properties.ImplicitsPreferencePage
+import scala.tools.eclipse.ui.AbstractToggleHandler
+import scala.tools.eclipse.properties.EditorPreferencePage
+
+/**
+ * Handler to toggle the Occurrences (shortcut to avoid open Preferences,...)
+ *
+ * @see scala.tools.eclipse.ui.AbstractToggleHandler
+ */
+
+class ToggleMarkOccurrencesHandler extends AbstractToggleHandler("org.scala-ide.sdt.core.toggleMarkOccurences", EditorPreferencePage.P_ENABLE_MARK_OCCURRENCES)
View
15 org.scala-ide.sdt.core/src/scala/tools/eclipse/properties/EditorPreferencePage.scala
@@ -5,8 +5,11 @@ import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer
import org.eclipse.jface.preference.{ FieldEditorPreferencePage, BooleanFieldEditor }
import org.eclipse.ui.{ IWorkbenchPreferencePage, IWorkbench }
import EditorPreferencePage._
+import org.eclipse.swt.SWT
+import org.eclipse.swt.widgets.Label
+import org.eclipse.swt.layout.GridData
-class EditorPreferencePage extends FieldEditorPreferencePage with IWorkbenchPreferencePage {
+class EditorPreferencePage extends FieldEditorPreferencePage(FieldEditorPreferencePage.GRID) with IWorkbenchPreferencePage {
setPreferenceStore(ScalaPlugin.plugin.getPreferenceStore)
@@ -15,8 +18,12 @@ class EditorPreferencePage extends FieldEditorPreferencePage with IWorkbenchPref
addField(new BooleanFieldEditor(P_ENABLE_SMART_BRACES, "Automatically surround selection with {braces}", getFieldEditorParent))
addField(new BooleanFieldEditor(P_ENABLE_SMART_PARENS, "Automatically surround selection with (parenthesis)", getFieldEditorParent))
addField(new BooleanFieldEditor(P_ENABLE_SMART_QUOTES, "Automatically surround selection with \"quotes\"", getFieldEditorParent))
-
+
+ new Label(getFieldEditorParent, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL))
addField(new BooleanFieldEditor(P_ENABLE_AUTO_CLOSING_BRACES, "Enable auto closing braces when editing an existing line", getFieldEditorParent))
+
+ new Label(getFieldEditorParent, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL))
+ addField(new BooleanFieldEditor(P_ENABLE_MARK_OCCURRENCES, "Mark Occurences of the selected element in the current file", getFieldEditorParent))
}
def init(workbench: IWorkbench) {}
@@ -31,6 +38,8 @@ object EditorPreferencePage {
final val P_ENABLE_SMART_QUOTES = BASE + "smartQuotes"
final val P_ENABLE_AUTO_CLOSING_BRACES = BASE + "autoClosingBrace"
+
+ final val P_ENABLE_MARK_OCCURRENCES = BASE + "markOccurences"
}
class DebugPreferenceInitializer extends AbstractPreferenceInitializer {
@@ -43,5 +52,7 @@ class DebugPreferenceInitializer extends AbstractPreferenceInitializer {
store.setDefault(P_ENABLE_SMART_QUOTES, false)
store.setDefault(P_ENABLE_AUTO_CLOSING_BRACES, true)
+
+ store.setDefault(P_ENABLE_MARK_OCCURRENCES, false)
}
}
View
67 ...rc/scala/tools/eclipse/semantichighlighting/implicits/ToggleImplicitsDisplayHandler.scala
@@ -1,73 +1,12 @@
package scala.tools.eclipse.semantichighlighting.implicits
import scala.tools.eclipse.properties.ImplicitsPreferencePage
-import org.eclipse.core.commands.AbstractHandler
-import org.eclipse.core.commands.ExecutionEvent
-import org.eclipse.jface.preference.IPreferenceStore
-import org.eclipse.jface.util.IPropertyChangeListener
-import org.eclipse.jface.util.PropertyChangeEvent
-import org.eclipse.ui.commands.ICommandService
-import org.eclipse.ui.commands.IElementUpdater
-import org.eclipse.ui.menus.UIElement
-import org.eclipse.ui.PlatformUI
-import scala.tools.eclipse.ScalaPlugin
+import scala.tools.eclipse.ui.AbstractToggleHandler
/**
* Handler to toggle the Implicits Display (shortcut to avoid open Preferences,...)
*
- * @see org.eclipse.core.commands.IHandler
- * @see org.eclipse.core.commands.AbstractHandler
+ * @see scala.tools.eclipse.ui.AbstractToggleHandler
*/
-class ToggleImplicitsDisplayHandler extends AbstractHandler with IElementUpdater {
-
- private val _commandId = "org.scala-ide.sdt.core.commands.ToggleImplicitsDisplay"
-
- /**
- * @throws ExecutionException
- */
- def execute(event : ExecutionEvent) : Object = {
- // see http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/
- // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=154130#c27
- // see http://wiki.eclipse.org/Menu_Contributions
- toggle()
- // refresh() // refresh will be triggered property update
- null
- }
-
- /**
- * refresh widget linked to this handlers (button, menu items,...)
- */
- def updateElement(element : UIElement, parameters : java.util.Map[_,_]) {
- element.setChecked(isChecked)
- }
-
- private def isChecked : Boolean = {
- pluginStore.getBoolean(ImplicitsPreferencePage.P_ACTIVE)
- }
-
- private def toggle() : Boolean = {
- val newValue = !pluginStore.getBoolean(ImplicitsPreferencePage.P_ACTIVE)
- pluginStore.setValue(ImplicitsPreferencePage.P_ACTIVE, newValue.toString)
- newValue
- }
-
- private def refresh() {
- val service = PlatformUI.getWorkbench().getService(classOf[ICommandService]).asInstanceOf[ICommandService]
- service.refreshElements(_commandId, null)
-
- }
-
- // listen change on the property regardless the source of the change (preferences page, widget linked to the handler)
- private val _listener = new IPropertyChangeListener {
- def propertyChange(event: PropertyChangeEvent) {
- if (event.getProperty() == ImplicitsPreferencePage.P_ACTIVE) {
- refresh()
- }
- }
- }
-
- protected def pluginStore : IPreferenceStore = ScalaPlugin.plugin.getPreferenceStore
-
- PropertyChangeListenerProxy(_listener, pluginStore).autoRegister()
-}
+class ToggleImplicitsDisplayHandler extends AbstractToggleHandler("org.scala-ide.sdt.core.commands.ToggleImplicitsDisplay", ImplicitsPreferencePage.P_ACTIVE)
View
64 org.scala-ide.sdt.core/src/scala/tools/eclipse/ui/AbstractToggleHandler.scala
@@ -0,0 +1,64 @@
+package scala.tools.eclipse.ui
+
+import org.eclipse.core.commands.ExecutionEvent
+import org.eclipse.jface.preference.IPreferenceStore
+import org.eclipse.ui.commands.ICommandService
+import org.eclipse.ui.commands.IElementUpdater
+import org.eclipse.ui.PlatformUI
+import org.eclipse.jface.util.IPropertyChangeListener
+import scala.tools.eclipse.ScalaPlugin
+import org.eclipse.ui.menus.UIElement
+import org.eclipse.jface.util.PropertyChangeEvent
+import scala.tools.eclipse.semantichighlighting.implicits.PropertyChangeListenerProxy
+import org.eclipse.core.commands.AbstractHandler
+
+/** Base handler for a toggle command linked to a platform preference.
+ *
+ * The preference is updated when button is pushed. The UI element is updated when the value of the preference is modified.
+ */
+abstract class AbstractToggleHandler(commandId: String, preferenceId: String) extends AbstractHandler with IElementUpdater {
+
+ private def pluginStore: IPreferenceStore = ScalaPlugin.plugin.getPreferenceStore
+
+ /** Call when the button is push.
+ */
+ def execute(event: ExecutionEvent): Object = {
+ // see http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/
+ // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=154130#c27
+ // see http://wiki.eclipse.org/Menu_Contributions
+ toggle()
+ null
+ }
+
+ /** Update the UI element state according to the preference.
+ */
+ def updateElement(element: UIElement, parameters: java.util.Map[_, _]) {
+ element.setChecked(isChecked)
+ }
+
+ private def isChecked: Boolean = {
+ pluginStore.getBoolean(preferenceId)
+ }
+
+ private def toggle(): Boolean = {
+ val newValue = !pluginStore.getBoolean(preferenceId)
+ pluginStore.setValue(preferenceId, newValue.toString)
+ newValue
+ }
+
+ PropertyChangeListenerProxy(_listener, pluginStore).autoRegister()
+
+ // listen change on the property regardless the source of the change (preferences page, widget linked to the handler)
+ private val _listener = new IPropertyChangeListener {
+ def propertyChange(event: PropertyChangeEvent) {
+ if (event.getProperty() == preferenceId) {
+ refresh()
+ }
+ }
+ }
+
+ private def refresh() {
+ val service = PlatformUI.getWorkbench().getService(classOf[ICommandService]).asInstanceOf[ICommandService]
+ service.refreshElements(commandId, null)
+ }
+}

0 comments on commit 2ec99cd

Please sign in to comment.