Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Pazderski2019-05-18 20:45:17 +0000
committerPaul Pazderski2019-05-18 21:47:36 +0000
commit9e7f66919496659795478f8cd80804da71498fff (patch)
treeced476eaaaa4e1cb0d65f56c3b7852a908eee1f8 /org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor
parent51ef424212f4bf315ddbedade152ae861d836fd8 (diff)
downloadeclipse.platform.text-9e7f66919496659795478f8cd80804da71498fff.tar.gz
eclipse.platform.text-9e7f66919496659795478f8cd80804da71498fff.tar.xz
eclipse.platform.text-9e7f66919496659795478f8cd80804da71498fff.zip
Bug 547304 - [cleanup] Fix wrong line delimiters
This updates all Java files with wrong or mixed line delimiters to use Unix style delimiters. The change includes only whitespace formatting and no code changes. Change-Id: I970d212a3e4edb8a85c58901336551190dfd2164
Diffstat (limited to 'org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor')
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CharacterPairMatcherRegistry.java200
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/folding/IndentFoldingStrategy.java934
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPluginPreferenceInitializer.java178
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPreferenceConstants.java212
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/IGenericEditorThemeConstants.java62
5 files changed, 793 insertions, 793 deletions
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CharacterPairMatcherRegistry.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CharacterPairMatcherRegistry.java
index 52f03824f42..c18e2173f67 100644
--- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CharacterPairMatcherRegistry.java
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CharacterPairMatcherRegistry.java
@@ -1,100 +1,100 @@
-/**
- * Copyright (c) 2018 Angelo ZERR.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v2.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v20.html
- *
- * Contributors:
- * Angelo Zerr <angelo.zerr@gmail.com> - Bug 538111 - [generic editor] Extension point for ICharacterPairMatcher
- */
-package org.eclipse.ui.internal.genericeditor;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
-
-import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.IRegistryChangeEvent;
-import org.eclipse.core.runtime.IRegistryChangeListener;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.content.IContentType;
-import org.eclipse.jface.text.source.ICharacterPairMatcher;
-import org.eclipse.jface.text.source.ISourceViewer;
-import org.eclipse.ui.texteditor.ITextEditor;
-
-/**
- * A registry of character pair matchers provided by extension
- * <code>org.eclipse.ui.genericeditor.characterPairMatchers</code>. Those
- * extensions are specific to a given {@link IContentType}.
- *
- * @since 1.2
- */
-public class CharacterPairMatcherRegistry {
-
- private static final String EXTENSION_POINT_ID = GenericEditorPlugin.BUNDLE_ID + ".characterPairMatchers"; //$NON-NLS-1$
-
- private Map<IConfigurationElement, GenericContentTypeRelatedExtension<ICharacterPairMatcher>> extensions = new HashMap<>();
- private boolean outOfSync = true;
-
- /**
- * Creates the registry and binds it to the extension point.
- */
- public CharacterPairMatcherRegistry() {
- Platform.getExtensionRegistry().addRegistryChangeListener(new IRegistryChangeListener() {
- @Override
- public void registryChanged(IRegistryChangeEvent event) {
- outOfSync = true;
- }
- }, EXTENSION_POINT_ID);
- }
-
- /**
- * Get the contributed {@link IPresentationReconciliers}s that are relevant to
- * hook on source viewer according to document content types.
- *
- * @param sourceViewer the source viewer we're hooking completion to.
- * @param editor the text editor
- * @param contentTypes the content types of the document we're editing.
- * @return the list of {@link ICharacterPairMatcher} contributed for at least
- * one of the content types.
- */
- public List<ICharacterPairMatcher> getCharacterPairMatchers(ISourceViewer sourceViewer, ITextEditor editor,
- Set<IContentType> contentTypes) {
- if (this.outOfSync) {
- sync();
- }
- return this.extensions.values().stream().filter(ext -> contentTypes.contains(ext.targetContentType))
- .filter(ext -> ext.matches(sourceViewer, editor))
- .sorted(new ContentTypeSpecializationComparator<ICharacterPairMatcher>())
- .map(GenericContentTypeRelatedExtension<ICharacterPairMatcher>::createDelegate)
- .collect(Collectors.toList());
- }
-
- private void sync() {
- Set<IConfigurationElement> toRemoveExtensions = new HashSet<>(this.extensions.keySet());
- for (IConfigurationElement extension : Platform.getExtensionRegistry()
- .getConfigurationElementsFor(EXTENSION_POINT_ID)) {
- toRemoveExtensions.remove(extension);
- if (!this.extensions.containsKey(extension)) {
- try {
- this.extensions.put(extension,
- new GenericContentTypeRelatedExtension<ICharacterPairMatcher>(extension));
- } catch (Exception ex) {
- GenericEditorPlugin.getDefault().getLog()
- .log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex));
- }
- }
- }
- for (IConfigurationElement toRemove : toRemoveExtensions) {
- this.extensions.remove(toRemove);
- }
- this.outOfSync = false;
- }
-
-}
+/**
+ * Copyright (c) 2018 Angelo ZERR.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v20.html
+ *
+ * Contributors:
+ * Angelo Zerr <angelo.zerr@gmail.com> - Bug 538111 - [generic editor] Extension point for ICharacterPairMatcher
+ */
+package org.eclipse.ui.internal.genericeditor;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IRegistryChangeEvent;
+import org.eclipse.core.runtime.IRegistryChangeListener;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.content.IContentType;
+import org.eclipse.jface.text.source.ICharacterPairMatcher;
+import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.ui.texteditor.ITextEditor;
+
+/**
+ * A registry of character pair matchers provided by extension
+ * <code>org.eclipse.ui.genericeditor.characterPairMatchers</code>. Those
+ * extensions are specific to a given {@link IContentType}.
+ *
+ * @since 1.2
+ */
+public class CharacterPairMatcherRegistry {
+
+ private static final String EXTENSION_POINT_ID = GenericEditorPlugin.BUNDLE_ID + ".characterPairMatchers"; //$NON-NLS-1$
+
+ private Map<IConfigurationElement, GenericContentTypeRelatedExtension<ICharacterPairMatcher>> extensions = new HashMap<>();
+ private boolean outOfSync = true;
+
+ /**
+ * Creates the registry and binds it to the extension point.
+ */
+ public CharacterPairMatcherRegistry() {
+ Platform.getExtensionRegistry().addRegistryChangeListener(new IRegistryChangeListener() {
+ @Override
+ public void registryChanged(IRegistryChangeEvent event) {
+ outOfSync = true;
+ }
+ }, EXTENSION_POINT_ID);
+ }
+
+ /**
+ * Get the contributed {@link IPresentationReconciliers}s that are relevant to
+ * hook on source viewer according to document content types.
+ *
+ * @param sourceViewer the source viewer we're hooking completion to.
+ * @param editor the text editor
+ * @param contentTypes the content types of the document we're editing.
+ * @return the list of {@link ICharacterPairMatcher} contributed for at least
+ * one of the content types.
+ */
+ public List<ICharacterPairMatcher> getCharacterPairMatchers(ISourceViewer sourceViewer, ITextEditor editor,
+ Set<IContentType> contentTypes) {
+ if (this.outOfSync) {
+ sync();
+ }
+ return this.extensions.values().stream().filter(ext -> contentTypes.contains(ext.targetContentType))
+ .filter(ext -> ext.matches(sourceViewer, editor))
+ .sorted(new ContentTypeSpecializationComparator<ICharacterPairMatcher>())
+ .map(GenericContentTypeRelatedExtension<ICharacterPairMatcher>::createDelegate)
+ .collect(Collectors.toList());
+ }
+
+ private void sync() {
+ Set<IConfigurationElement> toRemoveExtensions = new HashSet<>(this.extensions.keySet());
+ for (IConfigurationElement extension : Platform.getExtensionRegistry()
+ .getConfigurationElementsFor(EXTENSION_POINT_ID)) {
+ toRemoveExtensions.remove(extension);
+ if (!this.extensions.containsKey(extension)) {
+ try {
+ this.extensions.put(extension,
+ new GenericContentTypeRelatedExtension<ICharacterPairMatcher>(extension));
+ } catch (Exception ex) {
+ GenericEditorPlugin.getDefault().getLog()
+ .log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex));
+ }
+ }
+ }
+ for (IConfigurationElement toRemove : toRemoveExtensions) {
+ this.extensions.remove(toRemove);
+ }
+ this.outOfSync = false;
+ }
+
+}
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/folding/IndentFoldingStrategy.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/folding/IndentFoldingStrategy.java
index 7a05cb91521..19a56b9e6c7 100644
--- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/folding/IndentFoldingStrategy.java
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/folding/IndentFoldingStrategy.java
@@ -1,468 +1,468 @@
-/*******************************************************************************
- * Copyright (c) 2009, 2018 IBM Corporation and others.
- *
- * This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License 2.0
- * which accompanies this distribution, and is available at
- * https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- * Angelo Zerr <angelo.zerr@gmail.com> - adapt code org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingStrategy to support generic indent folding strategy.
- * [generic editor] Default Code folding for generic editor should use IndentFoldingStrategy - Bug 520659
- */
-package org.eclipse.ui.internal.genericeditor.folding;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.IDocument;
-import org.eclipse.jface.text.IRegion;
-import org.eclipse.jface.text.Position;
-import org.eclipse.jface.text.reconciler.DirtyRegion;
-import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
-import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
-import org.eclipse.jface.text.source.Annotation;
-import org.eclipse.jface.text.source.projection.IProjectionListener;
-import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
-import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
-import org.eclipse.jface.text.source.projection.ProjectionViewer;
-import org.eclipse.swt.graphics.FontMetrics;
-import org.eclipse.swt.graphics.GC;
-import org.eclipse.swt.graphics.Rectangle;
-import org.eclipse.swt.widgets.Canvas;
-
-/**
- * Indent folding strategy to fold code by using indentation. The folding
- * strategy must be associated with a viewer for it to function.
- */
-public class IndentFoldingStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension, IProjectionListener {
-
- private IDocument document;
- private ProjectionViewer viewer;
- private ProjectionAnnotationModel projectionAnnotationModel;
- private final String lineStartsWithKeyword;
-
- public IndentFoldingStrategy() {
- this(null);
- }
-
- public IndentFoldingStrategy(String lineStartsWithKeyword) {
- this.lineStartsWithKeyword = lineStartsWithKeyword;
- }
-
- /**
- * A FoldingAnnotation is a {@link ProjectionAnnotation} it is folding and
- * overriding the paint method (in a hacky type way) to prevent one line folding
- * annotations to be drawn.
- */
- protected class FoldingAnnotation extends ProjectionAnnotation {
- private boolean visible; /* workaround for BUG85874 */
-
- /**
- * Creates a new FoldingAnnotation.
- *
- * @param isCollapsed true if this annotation should be collapsed, false
- * otherwise
- */
- public FoldingAnnotation(boolean isCollapsed) {
- super(isCollapsed);
- visible = false;
- }
-
- /**
- * Does not paint hidden annotations. Annotations are hidden when they only span
- * one line.
- *
- * @see ProjectionAnnotation#paint(org.eclipse.swt.graphics.GC,
- * org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
- */
- @Override
- public void paint(GC gc, Canvas canvas, Rectangle rectangle) {
- /* workaround for BUG85874 */
- /*
- * only need to check annotations that are expanded because hidden annotations
- * should never have been given the chance to collapse.
- */
- if (!isCollapsed()) {
- // working with rectangle, so line height
- FontMetrics metrics = gc.getFontMetrics();
- if (metrics != null) {
- // do not draw annotations that only span one line and
- // mark them as not visible
- if ((rectangle.height / metrics.getHeight()) <= 1) {
- visible = false;
- return;
- }
- }
- }
- visible = true;
- super.paint(gc, canvas, rectangle);
- }
-
- @Override
- public void markCollapsed() {
- /* workaround for BUG85874 */
- // do not mark collapsed if annotation is not visible
- if (visible)
- super.markCollapsed();
- }
- }
-
- /**
- * The folding strategy must be associated with a viewer for it to function
- *
- * @param viewer the viewer to associate this folding strategy with
- */
- public void setViewer(ProjectionViewer viewer) {
- if (this.viewer != null) {
- this.viewer.removeProjectionListener(this);
- }
- this.viewer = viewer;
- this.viewer.addProjectionListener(this);
- this.projectionAnnotationModel = this.viewer.getProjectionAnnotationModel();
- }
-
- public void uninstall() {
- setDocument(null);
-
- if (viewer != null) {
- viewer.removeProjectionListener(this);
- viewer = null;
- }
-
- projectionDisabled();
- }
-
- @Override
- public void setDocument(IDocument document) {
- this.document = document;
- }
-
- @Override
- public void projectionDisabled() {
- projectionAnnotationModel = null;
- }
-
- @Override
- public void projectionEnabled() {
- if (viewer != null) {
- projectionAnnotationModel = viewer.getProjectionAnnotationModel();
- }
- }
-
- private class LineIndent {
- public int line;
- public final int indent;
-
- public LineIndent(int line, int indent) {
- this.line = line;
- this.indent = indent;
- }
- }
-
- @Override
- public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
- if (projectionAnnotationModel != null) {
-
- // these are what are passed off to the annotation model to
- // actually create and maintain the annotations
- List<Annotation> modifications = new ArrayList<Annotation>();
- List<FoldingAnnotation> deletions = new ArrayList<FoldingAnnotation>();
- List<FoldingAnnotation> existing = new ArrayList<FoldingAnnotation>();
- Map<Annotation, Position> additions = new HashMap<Annotation, Position>();
-
- // find and mark all folding annotations with length 0 for deletion
- markInvalidAnnotationsForDeletion(dirtyRegion, deletions, existing);
-
- List<LineIndent> previousRegions = new ArrayList<LineIndent>();
-
- int tabSize = 1;
- int minimumRangeSize = 1;
- try {
-
- // Today we recompute annotation from the whole document each
- // time.
- // performance s good even with large document, but it should be
- // better to loop for only DirtyRegion (and before/after)
- // int offset = dirtyRegion.getOffset();
- // int length = dirtyRegion.getLength();
- // int startLine = 0; //document.getLineOfOffset(offset);
- int endLine = document.getNumberOfLines() - 1; // startLine +
- // document.getNumberOfLines(offset,
- // length) - 1;
-
- // sentinel, to make sure there's at least one entry
- previousRegions.add(new LineIndent(endLine + 1, -1));
-
- int lastLineWhichIsNotEmpty = 0;
- int lineEmptyCount = 0;
- Integer lastLineForKeyword = null;
- int line = endLine;
- for (line = endLine; line >= 0; line--) {
- int lineOffset = document.getLineOffset(line);
- String delim = document.getLineDelimiter(line);
- int lineLength = document.getLineLength(line) - (delim != null ? delim.length() : 0);
- String lineContent = document.get(lineOffset, lineLength);
-
- LineState state = getLineState(lineContent, lastLineForKeyword);
- switch (state) {
- case StartWithKeyWord:
- lineEmptyCount = 0;
- lastLineWhichIsNotEmpty = line;
- if (lastLineForKeyword == null) {
- lastLineForKeyword = line;
- }
- break;
- case EmptyLine:
- lineEmptyCount++;
- break;
- default:
- addAnnotationForKeyword(modifications, deletions, existing, additions,
- line + 1 + lineEmptyCount, lastLineForKeyword);
- lastLineForKeyword = null;
- lineEmptyCount = 0;
- lastLineWhichIsNotEmpty = line;
- int indent = computeIndentLevel(lineContent, tabSize);
- if (indent == -1) {
- continue; // only whitespace
- }
-
- LineIndent previous = previousRegions.get(previousRegions.size() - 1);
- if (previous.indent > indent) {
- // discard all regions with larger indent
- do {
- previousRegions.remove(previousRegions.size() - 1);
- previous = previousRegions.get(previousRegions.size() - 1);
- } while (previous.indent > indent);
-
- // new folding range
- int endLineNumber = previous.line - 1;
- if (endLineNumber - line >= minimumRangeSize) {
- updateAnnotation(modifications, deletions, existing, additions, line, endLineNumber);
- }
- }
- if (previous.indent == indent) {
- previous.line = line;
- } else { // previous.indent < indent
- // new region with a bigger indent
- previousRegions.add(new LineIndent(line, indent));
- }
- }
- }
- addAnnotationForKeyword(modifications, deletions, existing, additions, lastLineWhichIsNotEmpty,
- lastLineForKeyword);
- } catch (BadLocationException e) {
- // should never done
- e.printStackTrace();
- }
-
- // be sure projection has not been disabled
- if (projectionAnnotationModel != null) {
- if (existing.size() > 0) {
- deletions.addAll(existing);
- }
- // send the calculated updates to the annotations to the
- // annotation model
- projectionAnnotationModel.modifyAnnotations(deletions.toArray(new Annotation[1]), additions,
- modifications.toArray(new Annotation[0]));
- }
- }
- }
-
- private void addAnnotationForKeyword(List<Annotation> modifications, List<FoldingAnnotation> deletions,
- List<FoldingAnnotation> existing, Map<Annotation, Position> additions, int startLine,
- Integer lastLineForKeyword) throws BadLocationException {
- if (lastLineForKeyword != null) {
- updateAnnotation(modifications, deletions, existing, additions, startLine, lastLineForKeyword);
- }
- }
-
- private enum LineState {
- StartWithKeyWord, DontStartWithKeyWord, EmptyLine
- }
-
- /**
- * Returns the line state for line which starts with a given keyword.
- *
- * @param lineContent line content.
- * @param lastLineForKeyword last line for the given keyword.
- * @return
- */
- private LineState getLineState(String lineContent, Integer lastLineForKeyword) {
- if (lineStartsWithKeyword == null) {
- // none keyword defined.
- return LineState.DontStartWithKeyWord;
- }
- if (lineContent != null && lineContent.trim().startsWith(lineStartsWithKeyword)) {
- // The line starts with the given keyword (ex: starts with "import")
- return LineState.StartWithKeyWord;
- }
- if (lastLineForKeyword != null && (lineContent == null || lineContent.trim().length() == 0)) {
- // a last line for keyword was defined, line is empty
- return LineState.EmptyLine;
- }
- return LineState.DontStartWithKeyWord;
- }
-
- /**
- * Compute indentation level of the given line by using the given tab size.
- *
- * @param line the line text.
- * @param tabSize the tab size.
- * @return the indentation level of the given line by using the given tab size.
- */
- private static int computeIndentLevel(String line, int tabSize) {
- int i = 0;
- int indent = 0;
- while (i < line.length()) {
- char ch = line.charAt(i);
- if (ch == ' ') {
- indent++;
- } else if (ch == '\t') {
- indent = indent - indent % tabSize + tabSize;
- } else {
- break;
- }
- i++;
- }
- if (i == line.length()) {
- return -1; // line only consists of whitespace
- }
- return indent;
- }
-
- /**
- * Given a {@link DirtyRegion} returns an {@link Iterator} of the already
- * existing annotations in that region.
- *
- * @param dirtyRegion the {@link DirtyRegion} to check for existing annotations
- * in
- *
- * @return an {@link Iterator} over the annotations in the given
- * {@link DirtyRegion}. The iterator could have no annotations in it. Or
- * <code>null</code> if projection has been disabled.
- */
- private Iterator<Annotation> getAnnotationIterator(DirtyRegion dirtyRegion) {
- Iterator<Annotation> annoIter = null;
- // be sure project has not been disabled
- if (projectionAnnotationModel != null) {
- // workaround for Platform Bug 299416
- annoIter = projectionAnnotationModel.getAnnotationIterator(0, document.getLength(), false, false);
- }
- return annoIter;
- }
-
- /**
- * Update annotations.
- *
- * @param modifications the folding annotations to update.
- * @param deletions the folding annotations to delete.
- * @param existing the existing folding annotations.
- * @param additions annoation to add
- * @param line the line index
- * @param endLineNumber the end line number
- * @throws BadLocationException
- */
- private void updateAnnotation(List<Annotation> modifications, List<FoldingAnnotation> deletions,
- List<FoldingAnnotation> existing, Map<Annotation, Position> additions, int line, Integer endLineNumber)
- throws BadLocationException {
- int startOffset = document.getLineOffset(line);
- int endOffset = document.getLineOffset(endLineNumber) + document.getLineLength(endLineNumber);
- Position newPos = new Position(startOffset, endOffset - startOffset);
- if (existing.size() > 0) {
- FoldingAnnotation existingAnnotation = existing.remove(existing.size() - 1);
- updateAnnotations(existingAnnotation, newPos, modifications, deletions);
- } else {
- additions.put(new FoldingAnnotation(false), newPos);
- }
- }
-
- /**
- * Update annotations.
- *
- * @param existingAnnotation the existing annotations that need to be updated
- * based on the given dirtied IndexRegion
- * @param newPos the new position that caused the annotations need
- * for updating and null otherwise.
- * @param modifications the list of annotations to be modified
- * @param deletions the list of annotations to be deleted
- */
- protected void updateAnnotations(Annotation existingAnnotation, Position newPos, List<Annotation> modifications,
- List<FoldingAnnotation> deletions) {
- if (existingAnnotation instanceof FoldingAnnotation) {
- FoldingAnnotation foldingAnnotation = (FoldingAnnotation) existingAnnotation;
-
- // if a new position can be calculated then update the position of
- // the annotation,
- // else the annotation needs to be deleted
- if (newPos != null && newPos.length > 0 && projectionAnnotationModel != null) {
- Position oldPos = projectionAnnotationModel.getPosition(foldingAnnotation);
- // only update the position if we have to
- if (!newPos.equals(oldPos)) {
- oldPos.setOffset(newPos.offset);
- oldPos.setLength(newPos.length);
- modifications.add(foldingAnnotation);
- }
- } else {
- deletions.add(foldingAnnotation);
- }
- }
- }
-
- /**
- * <p>
- * Searches the given {@link DirtyRegion} for annotations that now have a length
- * of 0. This is caused when something that was being folded has been deleted.
- * These {@link FoldingAnnotation}s are then added to the {@link List} of
- * {@link FoldingAnnotation}s to be deleted
- * </p>
- *
- * @param dirtyRegion find the now invalid {@link FoldingAnnotation}s in this
- * {@link DirtyRegion}
- * @param deletions the current list of {@link FoldingAnnotation}s marked for
- * deletion that the newly found invalid
- * {@link FoldingAnnotation}s will be added to
- */
- protected void markInvalidAnnotationsForDeletion(DirtyRegion dirtyRegion, List<FoldingAnnotation> deletions,
- List<FoldingAnnotation> existing) {
- Iterator<Annotation> iter = getAnnotationIterator(dirtyRegion);
- if (iter != null) {
- while (iter.hasNext()) {
- Annotation anno = iter.next();
- if (anno instanceof FoldingAnnotation) {
- FoldingAnnotation folding = (FoldingAnnotation) anno;
- Position pos = projectionAnnotationModel.getPosition(anno);
- if (pos.length == 0) {
- deletions.add(folding);
- } else {
- existing.add(folding);
- }
- }
- }
- }
- }
-
- @Override
- public void reconcile(IRegion partition) {
- // not used, we use:
- // reconcile(DirtyRegion dirtyRegion, IRegion subRegion)
- }
-
- @Override
- public void setProgressMonitor(IProgressMonitor monitor) {
- // Do nothing
- }
-
- @Override
- public void initialReconcile() {
- reconcile(new DirtyRegion(0, document.getLength(), DirtyRegion.INSERT, document.get()), null);
- }
+/*******************************************************************************
+ * Copyright (c) 2009, 2018 IBM Corporation and others.
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ * Angelo Zerr <angelo.zerr@gmail.com> - adapt code org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingStrategy to support generic indent folding strategy.
+ * [generic editor] Default Code folding for generic editor should use IndentFoldingStrategy - Bug 520659
+ */
+package org.eclipse.ui.internal.genericeditor.folding;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.text.reconciler.DirtyRegion;
+import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
+import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
+import org.eclipse.jface.text.source.Annotation;
+import org.eclipse.jface.text.source.projection.IProjectionListener;
+import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
+import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
+import org.eclipse.jface.text.source.projection.ProjectionViewer;
+import org.eclipse.swt.graphics.FontMetrics;
+import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.widgets.Canvas;
+
+/**
+ * Indent folding strategy to fold code by using indentation. The folding
+ * strategy must be associated with a viewer for it to function.
+ */
+public class IndentFoldingStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension, IProjectionListener {
+
+ private IDocument document;
+ private ProjectionViewer viewer;
+ private ProjectionAnnotationModel projectionAnnotationModel;
+ private final String lineStartsWithKeyword;
+
+ public IndentFoldingStrategy() {
+ this(null);
+ }
+
+ public IndentFoldingStrategy(String lineStartsWithKeyword) {
+ this.lineStartsWithKeyword = lineStartsWithKeyword;
+ }
+
+ /**
+ * A FoldingAnnotation is a {@link ProjectionAnnotation} it is folding and
+ * overriding the paint method (in a hacky type way) to prevent one line folding
+ * annotations to be drawn.
+ */
+ protected class FoldingAnnotation extends ProjectionAnnotation {
+ private boolean visible; /* workaround for BUG85874 */
+
+ /**
+ * Creates a new FoldingAnnotation.
+ *
+ * @param isCollapsed true if this annotation should be collapsed, false
+ * otherwise
+ */
+ public FoldingAnnotation(boolean isCollapsed) {
+ super(isCollapsed);
+ visible = false;
+ }
+
+ /**
+ * Does not paint hidden annotations. Annotations are hidden when they only span
+ * one line.
+ *
+ * @see ProjectionAnnotation#paint(org.eclipse.swt.graphics.GC,
+ * org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
+ */
+ @Override
+ public void paint(GC gc, Canvas canvas, Rectangle rectangle) {
+ /* workaround for BUG85874 */
+ /*
+ * only need to check annotations that are expanded because hidden annotations
+ * should never have been given the chance to collapse.
+ */
+ if (!isCollapsed()) {
+ // working with rectangle, so line height
+ FontMetrics metrics = gc.getFontMetrics();
+ if (metrics != null) {
+ // do not draw annotations that only span one line and
+ // mark them as not visible
+ if ((rectangle.height / metrics.getHeight()) <= 1) {
+ visible = false;
+ return;
+ }
+ }
+ }
+ visible = true;
+ super.paint(gc, canvas, rectangle);
+ }
+
+ @Override
+ public void markCollapsed() {
+ /* workaround for BUG85874 */
+ // do not mark collapsed if annotation is not visible
+ if (visible)
+ super.markCollapsed();
+ }
+ }
+
+ /**
+ * The folding strategy must be associated with a viewer for it to function
+ *
+ * @param viewer the viewer to associate this folding strategy with
+ */
+ public void setViewer(ProjectionViewer viewer) {
+ if (this.viewer != null) {
+ this.viewer.removeProjectionListener(this);
+ }
+ this.viewer = viewer;
+ this.viewer.addProjectionListener(this);
+ this.projectionAnnotationModel = this.viewer.getProjectionAnnotationModel();
+ }
+
+ public void uninstall() {
+ setDocument(null);
+
+ if (viewer != null) {
+ viewer.removeProjectionListener(this);
+ viewer = null;
+ }
+
+ projectionDisabled();
+ }
+
+ @Override
+ public void setDocument(IDocument document) {
+ this.document = document;
+ }
+
+ @Override
+ public void projectionDisabled() {
+ projectionAnnotationModel = null;
+ }
+
+ @Override
+ public void projectionEnabled() {
+ if (viewer != null) {
+ projectionAnnotationModel = viewer.getProjectionAnnotationModel();
+ }
+ }
+
+ private class LineIndent {
+ public int line;
+ public final int indent;
+
+ public LineIndent(int line, int indent) {
+ this.line = line;
+ this.indent = indent;
+ }
+ }
+
+ @Override
+ public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
+ if (projectionAnnotationModel != null) {
+
+ // these are what are passed off to the annotation model to
+ // actually create and maintain the annotations
+ List<Annotation> modifications = new ArrayList<Annotation>();
+ List<FoldingAnnotation> deletions = new ArrayList<FoldingAnnotation>();
+ List<FoldingAnnotation> existing = new ArrayList<FoldingAnnotation>();
+ Map<Annotation, Position> additions = new HashMap<Annotation, Position>();
+
+ // find and mark all folding annotations with length 0 for deletion
+ markInvalidAnnotationsForDeletion(dirtyRegion, deletions, existing);
+
+ List<LineIndent> previousRegions = new ArrayList<LineIndent>();
+
+ int tabSize = 1;
+ int minimumRangeSize = 1;
+ try {
+
+ // Today we recompute annotation from the whole document each
+ // time.
+ // performance s good even with large document, but it should be
+ // better to loop for only DirtyRegion (and before/after)
+ // int offset = dirtyRegion.getOffset();
+ // int length = dirtyRegion.getLength();
+ // int startLine = 0; //document.getLineOfOffset(offset);
+ int endLine = document.getNumberOfLines() - 1; // startLine +
+ // document.getNumberOfLines(offset,
+ // length) - 1;
+
+ // sentinel, to make sure there's at least one entry
+ previousRegions.add(new LineIndent(endLine + 1, -1));
+
+ int lastLineWhichIsNotEmpty = 0;
+ int lineEmptyCount = 0;
+ Integer lastLineForKeyword = null;
+ int line = endLine;
+ for (line = endLine; line >= 0; line--) {
+ int lineOffset = document.getLineOffset(line);
+ String delim = document.getLineDelimiter(line);
+ int lineLength = document.getLineLength(line) - (delim != null ? delim.length() : 0);
+ String lineContent = document.get(lineOffset, lineLength);
+
+ LineState state = getLineState(lineContent, lastLineForKeyword);
+ switch (state) {
+ case StartWithKeyWord:
+ lineEmptyCount = 0;
+ lastLineWhichIsNotEmpty = line;
+ if (lastLineForKeyword == null) {
+ lastLineForKeyword = line;
+ }
+ break;
+ case EmptyLine:
+ lineEmptyCount++;
+ break;
+ default:
+ addAnnotationForKeyword(modifications, deletions, existing, additions,
+ line + 1 + lineEmptyCount, lastLineForKeyword);
+ lastLineForKeyword = null;
+ lineEmptyCount = 0;
+ lastLineWhichIsNotEmpty = line;
+ int indent = computeIndentLevel(lineContent, tabSize);
+ if (indent == -1) {
+ continue; // only whitespace
+ }
+
+ LineIndent previous = previousRegions.get(previousRegions.size() - 1);
+ if (previous.indent > indent) {
+ // discard all regions with larger indent
+ do {
+ previousRegions.remove(previousRegions.size() - 1);
+ previous = previousRegions.get(previousRegions.size() - 1);
+ } while (previous.indent > indent);
+
+ // new folding range
+ int endLineNumber = previous.line - 1;
+ if (endLineNumber - line >= minimumRangeSize) {
+ updateAnnotation(modifications, deletions, existing, additions, line, endLineNumber);
+ }
+ }
+ if (previous.indent == indent) {
+ previous.line = line;
+ } else { // previous.indent < indent
+ // new region with a bigger indent
+ previousRegions.add(new LineIndent(line, indent));
+ }
+ }
+ }
+ addAnnotationForKeyword(modifications, deletions, existing, additions, lastLineWhichIsNotEmpty,
+ lastLineForKeyword);
+ } catch (BadLocationException e) {
+ // should never done
+ e.printStackTrace();
+ }
+
+ // be sure projection has not been disabled
+ if (projectionAnnotationModel != null) {
+ if (existing.size() > 0) {
+ deletions.addAll(existing);
+ }
+ // send the calculated updates to the annotations to the
+ // annotation model
+ projectionAnnotationModel.modifyAnnotations(deletions.toArray(new Annotation[1]), additions,
+ modifications.toArray(new Annotation[0]));
+ }
+ }
+ }
+
+ private void addAnnotationForKeyword(List<Annotation> modifications, List<FoldingAnnotation> deletions,
+ List<FoldingAnnotation> existing, Map<Annotation, Position> additions, int startLine,
+ Integer lastLineForKeyword) throws BadLocationException {
+ if (lastLineForKeyword != null) {
+ updateAnnotation(modifications, deletions, existing, additions, startLine, lastLineForKeyword);
+ }
+ }
+
+ private enum LineState {
+ StartWithKeyWord, DontStartWithKeyWord, EmptyLine
+ }
+
+ /**
+ * Returns the line state for line which starts with a given keyword.
+ *
+ * @param lineContent line content.
+ * @param lastLineForKeyword last line for the given keyword.
+ * @return
+ */
+ private LineState getLineState(String lineContent, Integer lastLineForKeyword) {
+ if (lineStartsWithKeyword == null) {
+ // none keyword defined.
+ return LineState.DontStartWithKeyWord;
+ }
+ if (lineContent != null && lineContent.trim().startsWith(lineStartsWithKeyword)) {
+ // The line starts with the given keyword (ex: starts with "import")
+ return LineState.StartWithKeyWord;
+ }
+ if (lastLineForKeyword != null && (lineContent == null || lineContent.trim().length() == 0)) {
+ // a last line for keyword was defined, line is empty
+ return LineState.EmptyLine;
+ }
+ return LineState.DontStartWithKeyWord;
+ }
+
+ /**
+ * Compute indentation level of the given line by using the given tab size.
+ *
+ * @param line the line text.
+ * @param tabSize the tab size.
+ * @return the indentation level of the given line by using the given tab size.
+ */
+ private static int computeIndentLevel(String line, int tabSize) {
+ int i = 0;
+ int indent = 0;
+ while (i < line.length()) {
+ char ch = line.charAt(i);
+ if (ch == ' ') {
+ indent++;
+ } else if (ch == '\t') {
+ indent = indent - indent % tabSize + tabSize;
+ } else {
+ break;
+ }
+ i++;
+ }
+ if (i == line.length()) {
+ return -1; // line only consists of whitespace
+ }
+ return indent;
+ }
+
+ /**
+ * Given a {@link DirtyRegion} returns an {@link Iterator} of the already
+ * existing annotations in that region.
+ *
+ * @param dirtyRegion the {@link DirtyRegion} to check for existing annotations
+ * in
+ *
+ * @return an {@link Iterator} over the annotations in the given
+ * {@link DirtyRegion}. The iterator could have no annotations in it. Or
+ * <code>null</code> if projection has been disabled.
+ */
+ private Iterator<Annotation> getAnnotationIterator(DirtyRegion dirtyRegion) {
+ Iterator<Annotation> annoIter = null;
+ // be sure project has not been disabled
+ if (projectionAnnotationModel != null) {
+ // workaround for Platform Bug 299416
+ annoIter = projectionAnnotationModel.getAnnotationIterator(0, document.getLength(), false, false);
+ }
+ return annoIter;
+ }
+
+ /**
+ * Update annotations.
+ *
+ * @param modifications the folding annotations to update.
+ * @param deletions the folding annotations to delete.
+ * @param existing the existing folding annotations.
+ * @param additions annoation to add
+ * @param line the line index
+ * @param endLineNumber the end line number
+ * @throws BadLocationException
+ */
+ private void updateAnnotation(List<Annotation> modifications, List<FoldingAnnotation> deletions,
+ List<FoldingAnnotation> existing, Map<Annotation, Position> additions, int line, Integer endLineNumber)
+ throws BadLocationException {
+ int startOffset = document.getLineOffset(line);
+ int endOffset = document.getLineOffset(endLineNumber) + document.getLineLength(endLineNumber);
+ Position newPos = new Position(startOffset, endOffset - startOffset);
+ if (existing.size() > 0) {
+ FoldingAnnotation existingAnnotation = existing.remove(existing.size() - 1);
+ updateAnnotations(existingAnnotation, newPos, modifications, deletions);
+ } else {
+ additions.put(new FoldingAnnotation(false), newPos);
+ }
+ }
+
+ /**
+ * Update annotations.
+ *
+ * @param existingAnnotation the existing annotations that need to be updated
+ * based on the given dirtied IndexRegion
+ * @param newPos the new position that caused the annotations need
+ * for updating and null otherwise.
+ * @param modifications the list of annotations to be modified
+ * @param deletions the list of annotations to be deleted
+ */
+ protected void updateAnnotations(Annotation existingAnnotation, Position newPos, List<Annotation> modifications,
+ List<FoldingAnnotation> deletions) {
+ if (existingAnnotation instanceof FoldingAnnotation) {
+ FoldingAnnotation foldingAnnotation = (FoldingAnnotation) existingAnnotation;
+
+ // if a new position can be calculated then update the position of
+ // the annotation,
+ // else the annotation needs to be deleted
+ if (newPos != null && newPos.length > 0 && projectionAnnotationModel != null) {
+ Position oldPos = projectionAnnotationModel.getPosition(foldingAnnotation);
+ // only update the position if we have to
+ if (!newPos.equals(oldPos)) {
+ oldPos.setOffset(newPos.offset);
+ oldPos.setLength(newPos.length);
+ modifications.add(foldingAnnotation);
+ }
+ } else {
+ deletions.add(foldingAnnotation);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Searches the given {@link DirtyRegion} for annotations that now have a length
+ * of 0. This is caused when something that was being folded has been deleted.
+ * These {@link FoldingAnnotation}s are then added to the {@link List} of
+ * {@link FoldingAnnotation}s to be deleted
+ * </p>
+ *
+ * @param dirtyRegion find the now invalid {@link FoldingAnnotation}s in this
+ * {@link DirtyRegion}
+ * @param deletions the current list of {@link FoldingAnnotation}s marked for
+ * deletion that the newly found invalid
+ * {@link FoldingAnnotation}s will be added to
+ */
+ protected void markInvalidAnnotationsForDeletion(DirtyRegion dirtyRegion, List<FoldingAnnotation> deletions,
+ List<FoldingAnnotation> existing) {
+ Iterator<Annotation> iter = getAnnotationIterator(dirtyRegion);
+ if (iter != null) {
+ while (iter.hasNext()) {
+ Annotation anno = iter.next();
+ if (anno instanceof FoldingAnnotation) {
+ FoldingAnnotation folding = (FoldingAnnotation) anno;
+ Position pos = projectionAnnotationModel.getPosition(anno);
+ if (pos.length == 0) {
+ deletions.add(folding);
+ } else {
+ existing.add(folding);
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void reconcile(IRegion partition) {
+ // not used, we use:
+ // reconcile(DirtyRegion dirtyRegion, IRegion subRegion)
+ }
+
+ @Override
+ public void setProgressMonitor(IProgressMonitor monitor) {
+ // Do nothing
+ }
+
+ @Override
+ public void initialReconcile() {
+ reconcile(new DirtyRegion(0, document.getLength(), DirtyRegion.INSERT, document.get()), null);
+ }
} \ No newline at end of file
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPluginPreferenceInitializer.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPluginPreferenceInitializer.java
index 40ed2349648..a93f180da98 100644
--- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPluginPreferenceInitializer.java
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPluginPreferenceInitializer.java
@@ -1,89 +1,89 @@
-/**
- * Copyright (c) 2018 Angelo ZERR.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v2.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v20.html
- *
- * Contributors:
- * Angelo Zerr <angelo.zerr@gmail.com> - Bug 538111 - [generic editor] Extension point for ICharacterPairMatcher
- */
-package org.eclipse.ui.internal.genericeditor.preferences;
-
-import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
-import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.jface.preference.PreferenceConverter;
-import org.eclipse.jface.resource.ColorRegistry;
-import org.eclipse.swt.graphics.RGB;
-import org.eclipse.ui.PlatformUI;
-
-/**
- * Preference initializer for Generic Editor plug-in.
- *
- * @since 1.2
- */
-public class GenericEditorPluginPreferenceInitializer extends AbstractPreferenceInitializer {
-
- @Override
- public void initializeDefaultPreferences() {
- IPreferenceStore store = GenericEditorPreferenceConstants.getPreferenceStore();
- GenericEditorPreferenceConstants.initializeDefaultValues(store);
- }
-
- public static void setThemeBasedPreferences(IPreferenceStore store, boolean fireEvent) {
- ColorRegistry registry = null;
- if (PlatformUI.isWorkbenchRunning())
- registry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
-
- setDefault(store, GenericEditorPreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR,
- findRGB(registry, IGenericEditorThemeConstants.EDITOR_MATCHING_BRACKETS_COLOR, new RGB(127, 0, 85)),
- fireEvent);
-
- }
-
- /**
- * Sets the default value and fires a property change event if necessary.
- *
- * @param store the preference store
- * @param key the preference key
- * @param newValue the new value
- * @param fireEvent <code>false</code> if no event should be fired
- * @since 1.2
- */
- private static void setDefault(IPreferenceStore store, String key, RGB newValue, boolean fireEvent) {
- if (!fireEvent) {
- PreferenceConverter.setDefault(store, key, newValue);
- return;
- }
-
- RGB oldValue = null;
- if (store.isDefault(key))
- oldValue = PreferenceConverter.getDefaultColor(store, key);
-
- PreferenceConverter.setDefault(store, key, newValue);
-
- if (oldValue != null && !oldValue.equals(newValue))
- store.firePropertyChangeEvent(key, oldValue, newValue);
- }
-
- /**
- * Returns the RGB for the given key in the given color registry.
- *
- * @param registry the color registry
- * @param key the key for the constant in the registry
- * @param defaultRGB the default RGB if no entry is found
- * @return RGB the RGB
- * @since 1.2
- */
- private static RGB findRGB(ColorRegistry registry, String key, RGB defaultRGB) {
- if (registry == null)
- return defaultRGB;
-
- RGB rgb = registry.getRGB(key);
- if (rgb != null)
- return rgb;
-
- return defaultRGB;
- }
-
-}
+/**
+ * Copyright (c) 2018 Angelo ZERR.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v20.html
+ *
+ * Contributors:
+ * Angelo Zerr <angelo.zerr@gmail.com> - Bug 538111 - [generic editor] Extension point for ICharacterPairMatcher
+ */
+package org.eclipse.ui.internal.genericeditor.preferences;
+
+import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.preference.PreferenceConverter;
+import org.eclipse.jface.resource.ColorRegistry;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * Preference initializer for Generic Editor plug-in.
+ *
+ * @since 1.2
+ */
+public class GenericEditorPluginPreferenceInitializer extends AbstractPreferenceInitializer {
+
+ @Override
+ public void initializeDefaultPreferences() {
+ IPreferenceStore store = GenericEditorPreferenceConstants.getPreferenceStore();
+ GenericEditorPreferenceConstants.initializeDefaultValues(store);
+ }
+
+ public static void setThemeBasedPreferences(IPreferenceStore store, boolean fireEvent) {
+ ColorRegistry registry = null;
+ if (PlatformUI.isWorkbenchRunning())
+ registry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
+
+ setDefault(store, GenericEditorPreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR,
+ findRGB(registry, IGenericEditorThemeConstants.EDITOR_MATCHING_BRACKETS_COLOR, new RGB(127, 0, 85)),
+ fireEvent);
+
+ }
+
+ /**
+ * Sets the default value and fires a property change event if necessary.
+ *
+ * @param store the preference store
+ * @param key the preference key
+ * @param newValue the new value
+ * @param fireEvent <code>false</code> if no event should be fired
+ * @since 1.2
+ */
+ private static void setDefault(IPreferenceStore store, String key, RGB newValue, boolean fireEvent) {
+ if (!fireEvent) {
+ PreferenceConverter.setDefault(store, key, newValue);
+ return;
+ }
+
+ RGB oldValue = null;
+ if (store.isDefault(key))
+ oldValue = PreferenceConverter.getDefaultColor(store, key);
+
+ PreferenceConverter.setDefault(store, key, newValue);
+
+ if (oldValue != null && !oldValue.equals(newValue))
+ store.firePropertyChangeEvent(key, oldValue, newValue);
+ }
+
+ /**
+ * Returns the RGB for the given key in the given color registry.
+ *
+ * @param registry the color registry
+ * @param key the key for the constant in the registry
+ * @param defaultRGB the default RGB if no entry is found
+ * @return RGB the RGB
+ * @since 1.2
+ */
+ private static RGB findRGB(ColorRegistry registry, String key, RGB defaultRGB) {
+ if (registry == null)
+ return defaultRGB;
+
+ RGB rgb = registry.getRGB(key);
+ if (rgb != null)
+ return rgb;
+
+ return defaultRGB;
+ }
+
+}
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPreferenceConstants.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPreferenceConstants.java
index 75dda6478d0..13660961fad 100644
--- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPreferenceConstants.java
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/GenericEditorPreferenceConstants.java
@@ -1,106 +1,106 @@
-/**
- * Copyright (c) 2018 Angelo ZERR.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v2.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v20.html
- *
- * Contributors:
- * Angelo Zerr <angelo.zerr@gmail.com> - Bug 538111 - [generic editor] Extension point for ICharacterPairMatcher
- */
-package org.eclipse.ui.internal.genericeditor.preferences;
-
-import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.ui.internal.genericeditor.GenericEditorPlugin;
-
-/**
- * Preference constants used in the Generic Editor preference store. Clients
- * should only read the Generic Editor preference store using these values.
- * Clients are not allowed to modify the preference store programmatically.
- * <p>
- * This class it is not intended to be instantiated or subclassed by clients.
- * </p>
- *
- * @since 1.2
- *
- * @noinstantiate This class is not intended to be instantiated by clients.
- * @noextend This class is not intended to be subclassed by clients.
- */
-public class GenericEditorPreferenceConstants {
-
- private GenericEditorPreferenceConstants() {
-
- }
-
- /**
- * A named preference that controls whether bracket matching highlighting is
- * turned on or off.
- * <p>
- * Value is of type <code>Boolean</code>.
- * </p>
- *
- * @since 1.2
- */
- public final static String EDITOR_MATCHING_BRACKETS = "matchingBrackets"; //$NON-NLS-1$
-
- /**
- * A named preference that holds the color used to highlight matching brackets.
- * <p>
- * Value is of type <code>String</code>. A RGB color value encoded as a string
- * using class <code>PreferenceConverter</code>
- * </p>
- *
- * @see org.eclipse.jface.resource.StringConverter
- * @see org.eclipse.jface.preference.PreferenceConverter
- *
- * @since 1.2
- */
- public final static String EDITOR_MATCHING_BRACKETS_COLOR = "matchingBracketsColor"; //$NON-NLS-1$
-
- /**
- * A named preference that controls whether bracket at caret location is
- * highlighted or not.
- * <p>
- * Value is of type <code>Boolean</code>.
- * </p>
- *
- * @since 1.2
- */
- public final static String EDITOR_HIGHLIGHT_BRACKET_AT_CARET_LOCATION = "highlightBracketAtCaretLocation"; //$NON-NLS-1$
-
- /**
- * A named preference that controls whether enclosing bracket matching
- * highlighting is turned on or off.
- * <p>
- * Value is of type <code>Boolean</code>.
- * </p>
- *
- * @since 1.2
- */
- public final static String EDITOR_ENCLOSING_BRACKETS = "enclosingBrackets"; //$NON-NLS-1$
-
- /**
- * Returns the Generic Editor preference store.
- *
- * @return the Generic Editor preference store
- */
- public static IPreferenceStore getPreferenceStore() {
- return GenericEditorPlugin.getDefault().getPreferenceStore();
- }
-
- /**
- * Initializes the given preference store with the default values.
- *
- * @param store the preference store to be initialized
- *
- * @since 1.2
- */
- public static void initializeDefaultValues(IPreferenceStore store) {
- store.setDefault(GenericEditorPreferenceConstants.EDITOR_MATCHING_BRACKETS, true);
- store.setDefault(GenericEditorPreferenceConstants.EDITOR_HIGHLIGHT_BRACKET_AT_CARET_LOCATION, false);
- store.setDefault(GenericEditorPreferenceConstants.EDITOR_ENCLOSING_BRACKETS, false);
- // Colors that are set by the current theme
- GenericEditorPluginPreferenceInitializer.setThemeBasedPreferences(store, false);
- }
-
-}
+/**
+ * Copyright (c) 2018 Angelo ZERR.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v20.html
+ *
+ * Contributors:
+ * Angelo Zerr <angelo.zerr@gmail.com> - Bug 538111 - [generic editor] Extension point for ICharacterPairMatcher
+ */
+package org.eclipse.ui.internal.genericeditor.preferences;
+
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.ui.internal.genericeditor.GenericEditorPlugin;
+
+/**
+ * Preference constants used in the Generic Editor preference store. Clients
+ * should only read the Generic Editor preference store using these values.
+ * Clients are not allowed to modify the preference store programmatically.
+ * <p>
+ * This class it is not intended to be instantiated or subclassed by clients.
+ * </p>
+ *
+ * @since 1.2
+ *
+ * @noinstantiate This class is not intended to be instantiated by clients.
+ * @noextend This class is not intended to be subclassed by clients.
+ */
+public class GenericEditorPreferenceConstants {
+
+ private GenericEditorPreferenceConstants() {
+
+ }
+
+ /**
+ * A named preference that controls whether bracket matching highlighting is
+ * turned on or off.
+ * <p>
+ * Value is of type <code>Boolean</code>.
+ * </p>
+ *
+ * @since 1.2
+ */
+ public final static String EDITOR_MATCHING_BRACKETS = "matchingBrackets"; //$NON-NLS-1$
+
+ /**
+ * A named preference that holds the color used to highlight matching brackets.
+ * <p>
+ * Value is of type <code>String</code>. A RGB color value encoded as a string
+ * using class <code>PreferenceConverter</code>
+ * </p>
+ *
+ * @see org.eclipse.jface.resource.StringConverter
+ * @see org.eclipse.jface.preference.PreferenceConverter
+ *
+ * @since 1.2
+ */
+ public final static String EDITOR_MATCHING_BRACKETS_COLOR = "matchingBracketsColor"; //$NON-NLS-1$
+
+ /**
+ * A named preference that controls whether bracket at caret location is
+ * highlighted or not.
+ * <p>
+ * Value is of type <code>Boolean</code>.
+ * </p>
+ *
+ * @since 1.2
+ */
+ public final static String EDITOR_HIGHLIGHT_BRACKET_AT_CARET_LOCATION = "highlightBracketAtCaretLocation"; //$NON-NLS-1$
+
+ /**
+ * A named preference that controls whether enclosing bracket matching
+ * highlighting is turned on or off.
+ * <p>
+ * Value is of type <code>Boolean</code>.
+ * </p>
+ *
+ * @since 1.2
+ */
+ public final static String EDITOR_ENCLOSING_BRACKETS = "enclosingBrackets"; //$NON-NLS-1$
+
+ /**
+ * Returns the Generic Editor preference store.
+ *
+ * @return the Generic Editor preference store
+ */
+ public static IPreferenceStore getPreferenceStore() {
+ return GenericEditorPlugin.getDefault().getPreferenceStore();
+ }
+
+ /**
+ * Initializes the given preference store with the default values.
+ *
+ * @param store the preference store to be initialized
+ *
+ * @since 1.2
+ */
+ public static void initializeDefaultValues(IPreferenceStore store) {
+ store.setDefault(GenericEditorPreferenceConstants.EDITOR_MATCHING_BRACKETS, true);
+ store.setDefault(GenericEditorPreferenceConstants.EDITOR_HIGHLIGHT_BRACKET_AT_CARET_LOCATION, false);
+ store.setDefault(GenericEditorPreferenceConstants.EDITOR_ENCLOSING_BRACKETS, false);
+ // Colors that are set by the current theme
+ GenericEditorPluginPreferenceInitializer.setThemeBasedPreferences(store, false);
+ }
+
+}
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/IGenericEditorThemeConstants.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/IGenericEditorThemeConstants.java
index aad591abfd8..e65baf9aeac 100644
--- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/IGenericEditorThemeConstants.java
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/preferences/IGenericEditorThemeConstants.java
@@ -1,31 +1,31 @@
-/**
- * Copyright (c) 2018 Angelo ZERR.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v2.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v20.html
- *
- * Contributors:
- * Angelo Zerr <angelo.zerr@gmail.com> - Bug 538111 - [generic editor] Extension point for ICharacterPairMatcher
- */
-package org.eclipse.ui.internal.genericeditor.preferences;
-
-import org.eclipse.ui.internal.genericeditor.GenericEditorPlugin;
-
-/**
- * Defines the constants used in the <code>org.eclipse.ui.themes</code>
- * extension contributed by this plug-in.
- *
- * @since 1.2
- */
-public interface IGenericEditorThemeConstants {
-
- String ID_PREFIX = GenericEditorPlugin.BUNDLE_ID + "."; //$NON-NLS-1$
-
- /**
- * Theme constant for the color used to highlight matching brackets.
- */
- public final String EDITOR_MATCHING_BRACKETS_COLOR = ID_PREFIX
- + GenericEditorPreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
-
-}
+/**
+ * Copyright (c) 2018 Angelo ZERR.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v20.html
+ *
+ * Contributors:
+ * Angelo Zerr <angelo.zerr@gmail.com> - Bug 538111 - [generic editor] Extension point for ICharacterPairMatcher
+ */
+package org.eclipse.ui.internal.genericeditor.preferences;
+
+import org.eclipse.ui.internal.genericeditor.GenericEditorPlugin;
+
+/**
+ * Defines the constants used in the <code>org.eclipse.ui.themes</code>
+ * extension contributed by this plug-in.
+ *
+ * @since 1.2
+ */
+public interface IGenericEditorThemeConstants {
+
+ String ID_PREFIX = GenericEditorPlugin.BUNDLE_ID + "."; //$NON-NLS-1$
+
+ /**
+ * Theme constant for the color used to highlight matching brackets.
+ */
+ public final String EDITOR_MATCHING_BRACKETS_COLOR = ID_PREFIX
+ + GenericEditorPreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
+
+}

Back to the top