From 941867855fb894a63ffe7ad79131fc53e2c02eea Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 25 Sep 2011 13:30:25 +0400 Subject: [PATCH] syntax highlighting --- .../calculator/CalculatorEditText.java | 94 ++++++++++++++----- .../android/calculator/CalculatorView.java | 4 + 2 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java b/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java index 3cac4bdc..c060a77a 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java @@ -7,9 +7,13 @@ package org.solovyev.android.calculator; import android.content.Context; import android.content.res.ColorStateList; +import android.graphics.Color; +import android.text.Html; import android.util.AttributeSet; +import android.util.Log; import android.widget.EditText; -import org.solovyev.common.exceptions.SersoException; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.math.MathEntityType; /** * User: serso @@ -36,36 +40,76 @@ public class CalculatorEditText extends EditText { super.setTextColor(colors); //To change body of overridden methods use File | Settings | File Templates. } - @Override - public void setText(CharSequence text, BufferType type) { - /*try { - final List> groupSymbols = new ArrayList>(); + public void redraw() { + String text = getText().toString(); + int selectionStart = getSelectionStart(); + int selectionEnd = getSelectionEnd(); - final Stack> groupSymbolsStack = new Stack>(); - for (int i = 0; i < text.length(); i++) { - char ch = text.charAt(i); - if (MathEntityType.openGroupSymbols.contains(ch)) { - groupSymbolsStack.push(new Pair(i, null)); - } else if (MathEntityType.closeGroupSymbols.contains(ch)) { - final Pair pair = groupSymbolsStack.pop(); - if (pair != null) { - pair.setSecond(i); - groupSymbols.add(0, pair); - } else { - throw new NoPairGroupSymbolException(); - } - } + int maxNumberOfOpenGroupSymbols = 0; + int numberOfOpenGroupSymbols = 0; + for (int i = 0; i < text.length(); i++) { + char ch = text.charAt(i); + if (MathEntityType.openGroupSymbols.contains(ch)) { + numberOfOpenGroupSymbols++; + maxNumberOfOpenGroupSymbols = Math.max(maxNumberOfOpenGroupSymbols, numberOfOpenGroupSymbols); + } else if (MathEntityType.closeGroupSymbols.contains(ch)) { + numberOfOpenGroupSymbols--; } + } - text = insertHtml(text, groupSymbols); - } catch (NoPairGroupSymbolException e) { - // do nothing - }*/ + if (maxNumberOfOpenGroupSymbols > 0) { - super.setText(text, type); + final StringBuilder sb = new StringBuilder(); + + processGroup(sb, text, 0, 0, maxNumberOfOpenGroupSymbols); + + Log.d(CalculatorEditText.class.getName(), sb.toString()); + + super.setText(Html.fromHtml(sb.toString()), BufferType.EDITABLE); + } else { + super.setText(text, BufferType.EDITABLE); + } + + setSelection(selectionStart, selectionEnd); } - private class NoPairGroupSymbolException extends SersoException { + private int processGroup(@NotNull StringBuilder result, @NotNull String s, int i, int numberOfOpenings, int maxNumberOfGroups) { + result.append(""); + + for (; i < s.length(); i++) { + char ch = s.charAt(i); + + if (MathEntityType.openGroupSymbols.contains(ch)) { + result.append(ch); + result.append(""); + i = processGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups); + result.append(""); + if (i < s.length() && MathEntityType.closeGroupSymbols.contains(s.charAt(i))) { + result.append(s.charAt(i)); + } + } else if (MathEntityType.closeGroupSymbols.contains(ch)) { + break; + } else { + result.append(ch); + } + } + + result.append(""); + + + return i; + } + + private String getColor(int numberOfOpenGroupSymbols, int numberOfOpenings) { + final int baseColor = Color.WHITE; + + double c = 1; + + int i = ((int)(255 * c)) * numberOfOpenings / (numberOfOpenGroupSymbols + 1); + + int result = Color.rgb( Color.red(baseColor) - i, Color.green(baseColor) - i, Color.blue(baseColor) - i); + + return "#" + Integer.toHexString(result).substring(2); } } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorView.java b/src/main/java/org/solovyev/android/calculator/CalculatorView.java index 0def0da2..54d288e1 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorView.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorView.java @@ -118,6 +118,8 @@ public class CalculatorView implements CursorControl, HistoryControl