Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(462)

Unified Diff: editor/tools/plugins/com.xored.glance.ui/src/com/xored/glance/ui/utils/TextUtils.java

Issue 17431004: New UI for Find command: find-as-you-type. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: editor/tools/plugins/com.xored.glance.ui/src/com/xored/glance/ui/utils/TextUtils.java
===================================================================
--- editor/tools/plugins/com.xored.glance.ui/src/com/xored/glance/ui/utils/TextUtils.java (revision 0)
+++ editor/tools/plugins/com.xored.glance.ui/src/com/xored/glance/ui/utils/TextUtils.java (revision 0)
@@ -0,0 +1,100 @@
+/**
+ *
+ */
+package com.xored.glance.ui.utils;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.TextPresentation;
+import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.graphics.Color;
+
+import com.xored.glance.ui.sources.ColorManager;
+import com.xored.glance.ui.sources.Match;
+
+/**
+ * @author Yuri Strot
+ */
+public class TextUtils {
+
+ public static StyleRange[] copy(final StyleRange[] ranges) {
+ final StyleRange[] result = new StyleRange[ranges.length];
+ for (int i = 0; i < result.length; i++) {
+ result[i] = copy(ranges[i]);
+ }
+ return result;
+ }
+
+ public static StyleRange copy(final StyleRange range) {
+ final StyleRange result = new StyleRange(range);
+ result.start = range.start;
+ result.length = range.length;
+ result.fontStyle = range.fontStyle;
+ return result;
+ }
+
+ public static StyleRange[] getStyles(final TextPresentation presentation) {
+ final StyleRange[] ranges = new StyleRange[presentation.getDenumerableRanges()];
+ final Iterator<?> e = presentation.getAllStyleRangeIterator();
+ for (int i = 0; e.hasNext(); i++) {
+ ranges[i] = (StyleRange) e.next();
+ }
+ return ranges;
+ }
+
+ public static void applyStyles(final TextPresentation presentation, final Match[] matches,
+ final Match selected) {
+
+ final StyleRange[] ranges = createStyleRanges(presentation.getExtent(), matches,
+ ColorManager.getInstance().getBackgroundColor());
+
+ presentation.mergeStyleRanges(ranges);
+
+ if (selected != null) {
+ final StyleRange[] selectedRanges = createStyleRanges(presentation.getExtent(),
+ new Match[] {selected}, ColorManager.getInstance().getSelectedBackgroundColor());
+ presentation.mergeStyleRanges(selectedRanges);
+
+ }
+ }
+
+ private static StyleRange[] createStyleRanges(final IRegion region, final Match[] matches,
+ final Color color) {
+ final Match[] regionMatches = getRangeMatches(region.getOffset(), region.getLength(), matches);
+ final StyleRange[] ranges = new StyleRange[regionMatches.length];
+ for (int i = 0; i < regionMatches.length; i++) {
+ final StyleRange range = new StyleRange(regionMatches[i].getOffset(),
+ regionMatches[i].getLength(), null, color);
+ ranges[i] = range;
+ }
+ return ranges;
+ }
+
+ private static Match[] getRangeMatches(final int start, final int length, final Match[] matches) {
+ int from = getPosition(start, matches);
+ if (from >= matches.length)
+ return Match.EMPTY;
+ if (from > 0) {
+ final Match border = matches[from - 1];
+ if (border.getLength() + border.getOffset() > start)
+ from--;
+ }
+ final int to = getPosition(start + length, matches) - 1;
+ if (from <= to) {
+ final Match[] result = new Match[to - from + 1];
+ System.arraycopy(matches, from, result, 0, result.length);
+ return result;
+ }
+ return Match.EMPTY;
+ }
+
+ private static int getPosition(final int offset, final Match[] matches) {
+ final int index = Arrays.binarySearch(matches, new Match(null, offset, 0));
+ if (index >= 0)
+ return index;
+ return -index - 1;
+ }
+
+}

Powered by Google App Engine
This is Rietveld 408576698