syntax highlighting

This commit is contained in:
serso 2011-09-25 13:30:25 +04:00
parent f930f3e495
commit 941867855f
2 changed files with 73 additions and 25 deletions

View File

@ -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<Pair<Integer, Integer>> groupSymbols = new ArrayList<Pair<Integer, Integer>>();
public void redraw() {
String text = getText().toString();
int selectionStart = getSelectionStart();
int selectionEnd = getSelectionEnd();
final Stack<Pair<Integer, Integer>> groupSymbolsStack = new Stack<Pair<Integer, Integer>>();
int maxNumberOfOpenGroupSymbols = 0;
int numberOfOpenGroupSymbols = 0;
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (MathEntityType.openGroupSymbols.contains(ch)) {
groupSymbolsStack.push(new Pair<Integer, Integer>(i, null));
numberOfOpenGroupSymbols++;
maxNumberOfOpenGroupSymbols = Math.max(maxNumberOfOpenGroupSymbols, numberOfOpenGroupSymbols);
} else if (MathEntityType.closeGroupSymbols.contains(ch)) {
final Pair<Integer, Integer> pair = groupSymbolsStack.pop();
if (pair != null) {
pair.setSecond(i);
groupSymbols.add(0, pair);
numberOfOpenGroupSymbols--;
}
}
if (maxNumberOfOpenGroupSymbols > 0) {
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 {
throw new NoPairGroupSymbolException();
super.setText(text, BufferType.EDITABLE);
}
setSelection(selectionStart, selectionEnd);
}
private int processGroup(@NotNull StringBuilder result, @NotNull String s, int i, int numberOfOpenings, int maxNumberOfGroups) {
result.append("<font color=\"").append(getColor(maxNumberOfGroups, numberOfOpenings)).append("\">");
for (; i < s.length(); i++) {
char ch = s.charAt(i);
if (MathEntityType.openGroupSymbols.contains(ch)) {
result.append(ch);
result.append("</font>");
i = processGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups);
result.append("<font color=\"").append(getColor(maxNumberOfGroups, numberOfOpenings)).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);
}
}
text = insertHtml(text, groupSymbols);
} catch (NoPairGroupSymbolException e) {
// do nothing
}*/
result.append("</font>");
super.setText(text, type);
return i;
}
private class NoPairGroupSymbolException extends SersoException {
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);
}
}

View File

@ -118,6 +118,8 @@ public class CalculatorView implements CursorControl, HistoryControl<CalculatorH
final String editorStateAfter = this.editor.getText().toString();
if (!editorStateBefore.equals(editorStateAfter)) {
editor.redraw();
currentRunner.setObject(new Runnable() {
@Override
public void run() {
@ -240,6 +242,8 @@ public class CalculatorView implements CursorControl, HistoryControl<CalculatorH
synchronized (history) {
setValuesFromHistory(this.editor, editorHistoryState.getEditorState());
setValuesFromHistory(this.display, editorHistoryState.getDisplayState());
editor.redraw();
}
}