redcar-javamateview 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/LICENSE +34 -0
  2. data/README +58 -0
  3. data/Rakefile +94 -0
  4. data/lib/javamateview.rb +41 -0
  5. data/lib/javamateview/example.rb +334 -0
  6. data/lib/javamateview/jar/java-mateview.jar +0 -0
  7. data/lib/javamateview/jcodings.jar +0 -0
  8. data/lib/javamateview/jdom.jar +0 -0
  9. data/lib/javamateview/joni.jar +0 -0
  10. data/spec/onig/match_spec.rb +50 -0
  11. data/spec/parsing/dynamic_parsing_spec.rb +172 -0
  12. data/spec/parsing/static_parsing_spec.rb +476 -0
  13. data/spec/spec_helper.rb +33 -0
  14. data/src/com/redcareditor/mate/Bundle.java +81 -0
  15. data/src/com/redcareditor/mate/DoublePattern.java +89 -0
  16. data/src/com/redcareditor/mate/Grammar.java +129 -0
  17. data/src/com/redcareditor/mate/IAnnotationAreaListener.java +7 -0
  18. data/src/com/redcareditor/mate/IGrammarListener.java +5 -0
  19. data/src/com/redcareditor/mate/IncludePattern.java +10 -0
  20. data/src/com/redcareditor/mate/LineNumberRulerColumn.java +922 -0
  21. data/src/com/redcareditor/mate/Marker.java +22 -0
  22. data/src/com/redcareditor/mate/MateText.java +697 -0
  23. data/src/com/redcareditor/mate/ParseThunk.java +71 -0
  24. data/src/com/redcareditor/mate/Parser.java +627 -0
  25. data/src/com/redcareditor/mate/ParserScheduler.java +237 -0
  26. data/src/com/redcareditor/mate/Pattern.java +152 -0
  27. data/src/com/redcareditor/mate/RangeSet.java +91 -0
  28. data/src/com/redcareditor/mate/Scanner.java +178 -0
  29. data/src/com/redcareditor/mate/Scope.java +534 -0
  30. data/src/com/redcareditor/mate/ScopeMatcher.java +162 -0
  31. data/src/com/redcareditor/mate/SharedTextColors.java +110 -0
  32. data/src/com/redcareditor/mate/SinglePattern.java +20 -0
  33. data/src/com/redcareditor/mate/WhitespaceCharacterPainter.java +395 -0
  34. data/src/com/redcareditor/mate/colouring/Colourer.java +16 -0
  35. data/src/com/redcareditor/mate/colouring/swt/MarginPaintListener.java +62 -0
  36. data/src/com/redcareditor/mate/colouring/swt/SwtColourer.java +501 -0
  37. data/src/com/redcareditor/mate/document/MateDocument.java +15 -0
  38. data/src/com/redcareditor/mate/document/MateTextFactory.java +9 -0
  39. data/src/com/redcareditor/mate/document/MateTextLocation.java +8 -0
  40. data/src/com/redcareditor/mate/document/MateTextLocationComparator.java +17 -0
  41. data/src/com/redcareditor/mate/document/MateTextRange.java +18 -0
  42. data/src/com/redcareditor/mate/document/swt/SwtMateDocument.java +143 -0
  43. data/src/com/redcareditor/mate/document/swt/SwtMateTextLocation.java +88 -0
  44. data/src/com/redcareditor/mate/document/swt/SwtMateTextRange.java +92 -0
  45. data/src/com/redcareditor/mate/document/swt/SwtScopePositionUpdater.java +90 -0
  46. data/src/com/redcareditor/mate/undo/MateTextUndoManager.java +11 -0
  47. data/src/com/redcareditor/mate/undo/swt/SwtMateTextUndoManager.java +166 -0
  48. data/src/com/redcareditor/onig/Match.java +212 -0
  49. data/src/com/redcareditor/onig/NullMatch.java +57 -0
  50. data/src/com/redcareditor/onig/NullRx.java +29 -0
  51. data/src/com/redcareditor/onig/Range.java +45 -0
  52. data/src/com/redcareditor/onig/Rx.java +167 -0
  53. data/src/com/redcareditor/plist/Dict.java +119 -0
  54. data/src/com/redcareditor/plist/PlistNode.java +52 -0
  55. data/src/com/redcareditor/plist/PlistPropertyLoader.java +44 -0
  56. data/src/com/redcareditor/theme/ScopeSelector.java +39 -0
  57. data/src/com/redcareditor/theme/Theme.java +122 -0
  58. data/src/com/redcareditor/theme/ThemeManager.java +41 -0
  59. data/src/com/redcareditor/theme/ThemeSetting.java +78 -0
  60. data/src/com/redcareditor/util/FileUtility.java +64 -0
  61. data/src/com/redcareditor/util/SingleLineFormatter.java +11 -0
  62. data/src/com/redcareditor/util/swt/ColourUtil.java +56 -0
  63. data/src/ruby/java-mateview.rb +68 -0
  64. data/test/com/redcareditor/mate/BundleTest.java +33 -0
  65. data/test/com/redcareditor/mate/EmptyRangeSetTest.java +27 -0
  66. data/test/com/redcareditor/mate/FilledRangeSetTest.java +82 -0
  67. data/test/com/redcareditor/mate/GrammarTest.java +158 -0
  68. data/test/com/redcareditor/mate/MateTextTest.java +35 -0
  69. data/test/com/redcareditor/mate/ScopeMatcherMatchingTest.java +55 -0
  70. data/test/com/redcareditor/mate/ScopeMatcherRankingTest.java +40 -0
  71. data/test/com/redcareditor/onig/RxTest.java +54 -0
  72. data/test/com/redcareditor/plist/DictTest.java +33 -0
  73. data/test/com/redcareditor/theme/RailsCastThemeTest.java +37 -0
  74. data/test/com/redcareditor/theme/ScopeSelectorTest.java +38 -0
  75. data/test/com/redcareditor/theme/ThemeManagerTest.java +29 -0
  76. data/test/com/redcareditor/util/swt/ColourUtilTest.java +17 -0
  77. metadata +134 -0
@@ -0,0 +1,11 @@
1
+ package com.redcareditor.mate.undo;
2
+
3
+ public interface MateTextUndoManager {
4
+
5
+ public abstract void undo();
6
+
7
+ public abstract void redo();
8
+
9
+ public abstract boolean isDirty();
10
+
11
+ }
@@ -0,0 +1,166 @@
1
+ package com.redcareditor.mate.undo.swt;
2
+
3
+ import java.util.Stack;
4
+
5
+ import org.eclipse.jface.text.source.SourceViewer;
6
+ import org.eclipse.swt.custom.ExtendedModifyEvent;
7
+ import org.eclipse.swt.custom.ExtendedModifyListener;
8
+ import org.eclipse.swt.custom.StyledText;
9
+
10
+ import com.redcareditor.mate.MateText;
11
+ import com.redcareditor.mate.undo.MateTextUndoManager;
12
+
13
+ /**
14
+ * this class can be attached to {@link MateText} widgets to provide undo/redo.<br>
15
+ * It will plug into the event handling of the text editing widget of
16
+ * {@link MateText} which is currently a {@link StyledText} inside a
17
+ * {@link SourceViewer}.
18
+ */
19
+ public class SwtMateTextUndoManager implements ExtendedModifyListener, MateTextUndoManager {
20
+ // TODO: maybe these stacks needs limits, if we want unlimited undo/redo,
21
+ // there you go...
22
+ private Stack<UndoRedoStep> undoStack;
23
+ private Stack<UndoRedoStep> redoStack;
24
+ private StyledText styledText;
25
+
26
+ public SwtMateTextUndoManager(MateText matetext) {
27
+ styledText = matetext.getTextWidget();
28
+ undoStack = new Stack<UndoRedoStep>();
29
+ redoStack = new Stack<UndoRedoStep>();
30
+ styledText.addExtendedModifyListener(this);
31
+ }
32
+
33
+ /**
34
+ * this method will get called once the text widget is modified.
35
+ */
36
+ public void modifyText(ExtendedModifyEvent e) {
37
+ String currentText = styledText.getText();
38
+ String newText = currentText.substring(e.start, e.start + e.length);
39
+
40
+ if (isTextReplaceEvent(e)) {
41
+ undoStack.push(new ReplaceStep(e.start, e.replacedText));
42
+ }
43
+ if (isTextEntryEvent(newText)) {
44
+ undoStack.push(new EntryStep(e.start, newText));
45
+ }
46
+ }
47
+
48
+ /* (non-Javadoc)
49
+ * @see com.redcareditor.mate.MateTexUndoManager#undo()
50
+ */
51
+ public void undo() {
52
+ if (!undoStack.isEmpty()) {
53
+ undoStack.pop().undo();
54
+ }
55
+ }
56
+
57
+ /* (non-Javadoc)
58
+ * @see com.redcareditor.mate.MateTexUndoManager#redo()
59
+ */
60
+ public void redo() {
61
+ if (!redoStack.isEmpty()) {
62
+ redoStack.pop().redo();
63
+ }
64
+ }
65
+
66
+ /* (non-Javadoc)
67
+ * @see com.redcareditor.mate.MateTexUndoManager#isDirty()
68
+ */
69
+ public boolean isDirty() {
70
+ return !undoStack.empty();
71
+ }
72
+
73
+ private boolean isTextEntryEvent(String newText) {
74
+ return newText != null && newText.length() > 0;
75
+ }
76
+
77
+ private boolean isTextReplaceEvent(ExtendedModifyEvent e) {
78
+ return e.replacedText != null && e.replacedText.length() > 0;
79
+ }
80
+
81
+ private void attachListener() {
82
+ styledText.addExtendedModifyListener(this);
83
+ }
84
+
85
+ private void disattachListener() {
86
+ styledText.removeExtendedModifyListener(this);
87
+ }
88
+
89
+ /*
90
+ * ---------------------------------------------------------- these are
91
+ * private classes, because the outside world doesn't need or understand
92
+ * them. Plus we can easily access and juggle around the instance variables
93
+ * from here. ----------------------------------------------------------
94
+ */
95
+ private abstract class UndoRedoStep {
96
+ public int location;
97
+ public String text;
98
+
99
+ public UndoRedoStep(int location, String text) {
100
+ super();
101
+ this.location = location;
102
+ this.text = text;
103
+ }
104
+
105
+ @Override
106
+ public String toString() {
107
+ return getClass().getName() + String.format("{%d : %s}", location, text);
108
+ }
109
+
110
+ public abstract void undo();
111
+
112
+ public abstract void redo();
113
+ }
114
+
115
+ /**
116
+ * When text is replaced or deleted. Deleting is considered replacing it
117
+ * with ''
118
+ */
119
+ private class ReplaceStep extends UndoRedoStep {
120
+ public ReplaceStep(int location, String text) {
121
+ super(location, text);
122
+ }
123
+
124
+ public void redo() {
125
+ disattachListener();
126
+ undoStack.push(new EntryStep(location, text));
127
+ styledText.replaceTextRange(location, 0, text);
128
+ styledText.setCaretOffset(location + text.length());
129
+ attachListener();
130
+ }
131
+
132
+ public void undo() {
133
+ disattachListener();
134
+ redoStack.push(new EntryStep(location, text));
135
+ styledText.replaceTextRange(location, 0, text);
136
+ styledText.setCaretOffset(location + text.length());
137
+ attachListener();
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Represents text that has been entered. Also deleted text, that is undone.
143
+ */
144
+ private class EntryStep extends UndoRedoStep {
145
+ public EntryStep(int location, String text) {
146
+ super(location, text);
147
+ }
148
+
149
+ public void redo() {
150
+ disattachListener();
151
+ undoStack.push(new ReplaceStep(location, text));
152
+ styledText.replaceTextRange(location, text.length(), "");
153
+ styledText.setCaretOffset(location + text.length());
154
+ attachListener();
155
+ }
156
+
157
+ public void undo() {
158
+ disattachListener();
159
+ String changedText = styledText.getText().substring(location, location + text.length());
160
+ redoStack.push(new ReplaceStep(location, changedText));
161
+ styledText.replaceTextRange(location, text.length(), "");
162
+ styledText.setCaretOffset(location + text.length());
163
+ attachListener();
164
+ }
165
+ }
166
+ }
@@ -0,0 +1,212 @@
1
+ package com.redcareditor.onig;
2
+
3
+ import java.io.UnsupportedEncodingException;
4
+
5
+ import java.util.Arrays;
6
+ import java.util.ArrayList;
7
+ import java.util.Iterator;
8
+ import java.util.List;
9
+
10
+ import org.jcodings.Encoding;
11
+ import org.jcodings.specific.UTF8Encoding;
12
+
13
+ import org.joni.Regex;
14
+ import org.joni.Region;
15
+
16
+ public class Match implements Iterable<Range> {
17
+ private Regex regex;
18
+ private Region region;
19
+ private String text;
20
+
21
+ private boolean charOffsetUpdated;
22
+ private Region charOffsets;
23
+
24
+ public Match() {}
25
+
26
+ public Match(Regex regex, Region region, String text) {
27
+ super();
28
+ this.regex = regex;
29
+ this.region = region;
30
+ this.text = text;
31
+ }
32
+
33
+ public int numCaptures() {
34
+ return region.numRegs;
35
+ }
36
+
37
+ public Range getCapture(int capture) {
38
+ // checkBounds(capture);
39
+ updateCharOffset();
40
+ return new Range(
41
+ charOffsets.beg[capture],
42
+ charOffsets.end[capture]
43
+ );
44
+ }
45
+
46
+ public Range getByteCapture(int capture) {
47
+ // checkBounds(capture);
48
+ updateCharOffset();
49
+ return new Range(
50
+ region.beg[capture],
51
+ region.end[capture]
52
+ );
53
+ }
54
+
55
+ private void checkBounds(int capture) {
56
+ // System.out.printf("checkBounds(%d) (out of %d)\n", capture, numCaptures()-1);
57
+ if (capture > numCaptures()-1 || capture < 0) {
58
+ throw new IllegalArgumentException("Capture Index out of bounds!");
59
+ }
60
+ }
61
+
62
+ public List<Range> ranges() {
63
+ List<Range> result = new ArrayList<Range>();
64
+ for (Range r : this) {
65
+ result.add(r);
66
+ }
67
+ return result;
68
+ }
69
+
70
+
71
+ private static final class Pair implements Comparable {
72
+ int bytePos, charPos;
73
+ public int compareTo(Object o) {
74
+ return bytePos - ((Pair)o).bytePos;
75
+ }
76
+ }
77
+
78
+ private void updateCharOffset() {
79
+ if (charOffsetUpdated) return;
80
+
81
+ int numRegs = region == null ? 1 : region.numRegs;
82
+ if (charOffsets == null || charOffsets.numRegs < numRegs)
83
+ charOffsets = new Region(numRegs);
84
+
85
+ Pair[] pairs = new Pair[numRegs * 2];
86
+ for (int i = 0; i < pairs.length; i++) pairs[i] = new Pair();
87
+
88
+ int numPos = 0;
89
+ // System.out.printf("regions (numRegs:%d):\n", numRegs);
90
+ for (int i = 0; i < numRegs; i++) {
91
+ // System.out.printf(" [%d, %d]\n", region.beg[i], region.end[i]);
92
+ if (region.beg[i] < 0) {
93
+ numPos++; numPos++;
94
+ continue;
95
+ }
96
+ pairs[numPos++].bytePos = region.beg[i];
97
+ pairs[numPos++].bytePos = region.end[i];
98
+ }
99
+
100
+ // for (Pair pair : pairs) {
101
+ // System.out.printf(" * %d\n", pair.bytePos);
102
+ // }
103
+
104
+ Arrays.sort(pairs);
105
+
106
+ Encoding enc = UTF8Encoding.INSTANCE;
107
+ byte[] bytes;
108
+ try {
109
+ bytes = text.getBytes("UTF-8");
110
+ } catch (UnsupportedEncodingException e) {
111
+ e.printStackTrace();
112
+ bytes = text.getBytes();
113
+ }
114
+ int p = 0;
115
+ int s = p;
116
+
117
+ int c = 0;
118
+ for (int i = 0; i < numPos; i++) {
119
+ int q = s + pairs[i].bytePos;
120
+ c += Match.strLength(enc, bytes, p, q);
121
+ // System.out.printf("p:%d s:%d c:%d q:%d bytePos:%d\n", p, s, c, q, pairs[i].bytePos);
122
+ // System.out.printf("'%s' %d %d %d\n", new String(bytes), Match.strLength(enc, bytes, p, q), p, q);
123
+ pairs[i].charPos = c;
124
+ p = q;
125
+ }
126
+
127
+ Pair key = new Pair();
128
+ for (int i = 0; i < numRegs; i++) {
129
+ if (region.beg[i] < 0) {
130
+ charOffsets.beg[i] = charOffsets.end[i] = -1;
131
+ continue;
132
+ }
133
+ key.bytePos = region.beg[i];
134
+ charOffsets.beg[i] = pairs[Arrays.binarySearch(pairs, key)].charPos;
135
+ key.bytePos = region.end[i];
136
+ charOffsets.end[i] = pairs[Arrays.binarySearch(pairs, key)].charPos;
137
+ // System.out.printf(" * bytes %d, %d chars %d, %d\n", region.beg[i], region.end[i], charOffsets.beg[i], charOffsets.end[i]);
138
+ }
139
+
140
+ charOffsetUpdated = true;
141
+ }
142
+
143
+
144
+ @Override
145
+ public String toString() {
146
+ StringBuilder bui = new StringBuilder();
147
+ bui.append(text);
148
+ bui.append(region);
149
+ return bui.toString();
150
+ }
151
+
152
+ public Iterator<Range> iterator() {
153
+ return new Iterator<Range>() {
154
+ int i = 0;
155
+
156
+ public boolean hasNext() {
157
+ return i < numCaptures();
158
+ }
159
+
160
+ public Range next() {
161
+ Range r = new Range(region.beg[i], region.end[i]);
162
+ i++;
163
+ return r;
164
+ }
165
+
166
+ public void remove() {
167
+ throw new UnsupportedOperationException("no removing!");
168
+ }
169
+ };
170
+ }
171
+
172
+ public static int searchNonAscii(byte[]bytes, int p, int end) {
173
+ while (p < end) {
174
+ if (!Encoding.isAscii(bytes[p])) return p;
175
+ p++;
176
+ }
177
+ return -1;
178
+ }
179
+
180
+ public static int length(Encoding enc, byte[]bytes, int p, int end) {
181
+ int n = enc.length(bytes, p, end);
182
+ if (n > 0 && end - p >= n) return n;
183
+ return end - p >= enc.minLength() ? enc.minLength() : end - p;
184
+ }
185
+
186
+ public static int strLength(Encoding enc, byte[] bytes, int p, int end) {
187
+ if (enc.isFixedWidth()) {
188
+ return (end - p + enc.minLength() - 1) / enc.minLength();
189
+ } else if (enc.isAsciiCompatible()) {
190
+ int c = 0;
191
+ while (p < end) {
192
+ if (Encoding.isAscii(bytes[p])) {
193
+ int q = searchNonAscii(bytes, p, end);
194
+ if (q == -1) return c + (end - p);
195
+ c += q - p;
196
+ p = q;
197
+ }
198
+ p += length(enc, bytes, p, end);
199
+ c++;
200
+ }
201
+ return c;
202
+ }
203
+
204
+ int c;
205
+ for (c = 0; end > p; c++) p += length(enc, bytes, p, end);
206
+ return c;
207
+ }
208
+
209
+ public static int strLength(byte[] bytes) {
210
+ return strLength(UTF8Encoding.INSTANCE, bytes, 0, bytes.length);
211
+ }
212
+ }
@@ -0,0 +1,57 @@
1
+ package com.redcareditor.onig;
2
+
3
+ import java.util.Iterator;
4
+ import java.util.LinkedList;
5
+ import java.util.List;
6
+
7
+ public class NullMatch extends Match {
8
+ private static NullMatch instance;
9
+ private List<Range> emptyList = new LinkedList<Range>();
10
+
11
+ public static NullMatch instance() {
12
+ if (instance == null) {
13
+ instance = new NullMatch();
14
+ }
15
+ return instance;
16
+ }
17
+
18
+ private NullMatch() {
19
+ super(null, null, null);
20
+ }
21
+
22
+ @Override
23
+ public Range getCapture(int capture) {
24
+ return new Range(0,0);
25
+ }
26
+
27
+ @Override
28
+ public List<Range> ranges() {
29
+ return emptyList;
30
+ }
31
+
32
+ @Override
33
+ public int numCaptures() {
34
+ return 0;
35
+ }
36
+
37
+ @Override
38
+ public String toString() {
39
+ return "NullMatch";
40
+ }
41
+
42
+ @Override
43
+ public Iterator<Range> iterator() {
44
+ return new Iterator<Range>() {
45
+ public boolean hasNext() {
46
+ return false;
47
+ }
48
+
49
+ public Range next() {
50
+ return null;
51
+ }
52
+
53
+ public void remove() {
54
+ }
55
+ };
56
+ }
57
+ }
@@ -0,0 +1,29 @@
1
+ package com.redcareditor.onig;
2
+
3
+ public class NullRx extends Rx{
4
+ private static NullRx instance;
5
+
6
+ public static NullRx instance(){
7
+ if(instance == null){
8
+ instance = new NullRx();
9
+ }
10
+ return instance;
11
+ }
12
+
13
+ private NullRx(){}
14
+
15
+ @Override
16
+ public String toString() {
17
+ return "NullRx";
18
+ }
19
+
20
+ @Override
21
+ public Match search(String line) {
22
+ return NullMatch.instance();
23
+ }
24
+
25
+ @Override
26
+ public Match search(String target, int start, int end) {
27
+ return search(target);
28
+ }
29
+ }