redcar-javamateview 0.1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +142 -0
@@ -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
+ }
@@ -0,0 +1,45 @@
1
+ package com.redcareditor.onig;
2
+
3
+ public class Range implements Comparable<Range> {
4
+ public int start;
5
+ public int end;
6
+
7
+ public Range(int start, int end) {
8
+ super();
9
+ this.start = start;
10
+ this.end = end;
11
+ }
12
+
13
+ @Override
14
+ public String toString() {
15
+ if (start == end) {
16
+ return Integer.toString(start);
17
+ }
18
+ return String.format("%d..%d", start, end);
19
+ }
20
+
21
+ public boolean isTouching(Range other) {
22
+ if (start < other.start) {
23
+ return other.start <= end + 1;
24
+ } else {
25
+ return start <= other.end + 1;
26
+ }
27
+ }
28
+
29
+ public int compareTo(Range o) {
30
+ int compareStart = start - o.start;
31
+ if (compareStart == 0) {
32
+ return end - o.end;
33
+ } else {
34
+ return compareStart;
35
+ }
36
+ }
37
+
38
+ @Override
39
+ public boolean equals(Object obj) {
40
+ if (obj instanceof Range) {
41
+ return 0 == compareTo((Range) obj);
42
+ }
43
+ return false;
44
+ }
45
+ }
@@ -0,0 +1,167 @@
1
+ package com.redcareditor.onig;
2
+
3
+ import java.io.UnsupportedEncodingException;
4
+
5
+ import org.jcodings.Encoding;
6
+ import org.jcodings.specific.UTF8Encoding;
7
+ import org.jcodings.specific.UTF16BEEncoding;
8
+ import org.joni.Matcher;
9
+ import org.joni.Option;
10
+ import org.joni.Regex;
11
+ import org.joni.Region;
12
+ import org.joni.Syntax;
13
+ import org.joni.WarnCallback;
14
+
15
+ import java.text.CharacterIterator;
16
+ import java.text.StringCharacterIterator;
17
+
18
+ /**
19
+ * wrapper class around the Joni Regex library which is a optimized port of
20
+ * Onigurama
21
+ *
22
+ * @author kungfoo
23
+ *
24
+ */
25
+ public class Rx {
26
+ public String pattern;
27
+ public Regex regex;
28
+ public boolean matchesStartOfLine = false;
29
+
30
+ public static Rx createRx(String pattern){
31
+ // System.out.printf("createRx(%s)\n", pattern);
32
+ if(pattern == null){
33
+ return NullRx.instance();
34
+ } else {
35
+ return new Rx(pattern);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * this is used implicitly by the null object class.
41
+ */
42
+ protected Rx(){}
43
+
44
+ private Rx(String pattern) {
45
+ this.pattern = pattern;
46
+ regex = compileRegex(pattern);
47
+ if (!pattern.equals("")) {
48
+ matchesStartOfLine = pattern.charAt(0) == '^';
49
+ }
50
+ }
51
+
52
+ public boolean usable() {
53
+ return (regex != null);
54
+ }
55
+
56
+ public Match search(String target, int start, int end) {
57
+ byte[] bytes;
58
+ try {
59
+ bytes = target.getBytes("UTF-8");
60
+ Matcher matcher = regex.matcher(bytes, 0, bytes.length);
61
+ int a = matcher.search(start, end, Option.NONE);
62
+
63
+ if(a == -1){
64
+ return null;
65
+ }
66
+
67
+ Region region = matcher.getEagerRegion();
68
+ return new Match(regex, region, target);
69
+
70
+ } catch (UnsupportedEncodingException e) {
71
+ e.printStackTrace();
72
+ }
73
+ return null;
74
+ }
75
+
76
+ public Match search(String line) {
77
+ return search(line, 0, line.length());
78
+ }
79
+
80
+ class Warnings implements WarnCallback {
81
+ public void warn(String message) {
82
+ // System.out.printf("got warning from regex: %s\n", message);
83
+ }
84
+ }
85
+
86
+ private Regex compileRegex(String pattern) {
87
+ byte[] bytes;
88
+ try {
89
+ bytes = pattern.getBytes("UTF-8");
90
+ return new Regex(bytes, 0, bytes.length, Option.DEFAULT,
91
+ UTF8Encoding.INSTANCE, Syntax.DEFAULT, new Warnings());
92
+
93
+ } catch (UnsupportedEncodingException e) {
94
+ e.printStackTrace();
95
+ } catch (org.joni.exception.SyntaxException e) {
96
+ //System.out.printf("** WARNING: SyntaxException when compiling '%s': %s\n", pattern, e.getMessage());
97
+ //e.printStackTrace();
98
+ }
99
+ return null;
100
+ }
101
+
102
+ @Override
103
+ public String toString() {
104
+ return pattern;
105
+ }
106
+
107
+
108
+ public static String escape(String aRegexFragment){
109
+ final StringBuilder result = new StringBuilder();
110
+
111
+ final StringCharacterIterator iterator = new StringCharacterIterator(aRegexFragment);
112
+ char character = iterator.current();
113
+ while (character != CharacterIterator.DONE ){
114
+ if (character == '.') {
115
+ result.append("\\.");
116
+ }
117
+ else if (character == '\\') {
118
+ result.append("\\\\");
119
+ }
120
+ else if (character == '?') {
121
+ result.append("\\?");
122
+ }
123
+ else if (character == '*') {
124
+ result.append("\\*");
125
+ }
126
+ else if (character == '+') {
127
+ result.append("\\+");
128
+ }
129
+ else if (character == '&') {
130
+ result.append("\\&");
131
+ }
132
+ else if (character == ':') {
133
+ result.append("\\:");
134
+ }
135
+ else if (character == '{') {
136
+ result.append("\\{");
137
+ }
138
+ else if (character == '}') {
139
+ result.append("\\}");
140
+ }
141
+ else if (character == '[') {
142
+ result.append("\\[");
143
+ }
144
+ else if (character == ']') {
145
+ result.append("\\]");
146
+ }
147
+ else if (character == '(') {
148
+ result.append("\\(");
149
+ }
150
+ else if (character == ')') {
151
+ result.append("\\)");
152
+ }
153
+ else if (character == '^') {
154
+ result.append("\\^");
155
+ }
156
+ else if (character == '$') {
157
+ result.append("\\$");
158
+ }
159
+ else {
160
+ result.append(character);
161
+ }
162
+ character = iterator.next();
163
+ }
164
+ return result.toString();
165
+ }
166
+
167
+ }
@@ -0,0 +1,119 @@
1
+ package com.redcareditor.plist;
2
+
3
+ import java.io.File;
4
+ import java.io.IOException;
5
+ import java.util.*;
6
+ import java.util.concurrent.locks.Lock;
7
+ import java.util.concurrent.locks.ReentrantLock;
8
+
9
+ import org.jdom.Document;
10
+ import org.jdom.Element;
11
+ import org.jdom.JDOMException;
12
+ import org.jdom.input.SAXBuilder;
13
+
14
+ import com.redcareditor.onig.Rx;
15
+
16
+ /**
17
+ * class to load plist files.
18
+ *
19
+ * @author kungfoo
20
+ *
21
+ */
22
+ public class Dict extends PlistNode<Map<String, PlistNode<?>>> {
23
+
24
+ private static SAXBuilder builder;
25
+
26
+ // NOTE: this method is not thread safe, if we ever go there, this needs to
27
+ // contain a mutex!
28
+ private static SAXBuilder getSAXBuilderInstance() {
29
+ if (builder == null) {
30
+ builder = new SAXBuilder(false);
31
+ builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
32
+
33
+ }
34
+ return builder;
35
+ }
36
+
37
+ public static Dict parseFile(String filename) {
38
+ try {
39
+ Document document = getSAXBuilderInstance().build(new File(filename));
40
+ return new Dict(document.getRootElement().getChild("dict"));
41
+
42
+ } catch (JDOMException e) {
43
+ e.printStackTrace();
44
+ } catch (IOException e) {
45
+ System.out.println("The file " + filename + " was not found!");
46
+ e.printStackTrace();
47
+ }
48
+ return null;
49
+ }
50
+
51
+ @SuppressWarnings("unchecked")
52
+ protected Dict(Element element) {
53
+ value = new HashMap<String, PlistNode<?>>();
54
+
55
+ List<Element> children = element.getChildren();
56
+ String key = null;
57
+ for (Element c : children) {
58
+ if (c.getName().equals("key")) {
59
+ key = c.getValue();
60
+ } else {
61
+ value.put(key, PlistNode.parseElement(c));
62
+ }
63
+ }
64
+ }
65
+
66
+ public String getString(String key) {
67
+ return tryGettingValue(this, key);
68
+ }
69
+
70
+ public Integer getInt(String key) {
71
+ return tryGettingValue(this, key);
72
+ }
73
+
74
+ @SuppressWarnings("unchecked")
75
+ public String[] getStrings(String key) {
76
+ List<PlistNode<String>> strings = (List<PlistNode<String>>) value.get(key).value;
77
+ String[] result = new String[strings.size()];
78
+ int i = 0;
79
+ for (PlistNode<String> str : strings) {
80
+ result[i++] = str.value;
81
+ }
82
+ return result;
83
+ }
84
+
85
+ @SuppressWarnings("unchecked")
86
+ public Dict[] getDictionaries(String key) {
87
+ List<Dict> dictionaries = (List<Dict>) value.get(key).value;
88
+ return dictionaries.toArray(new Dict[0]);
89
+ }
90
+
91
+ public List<PlistNode<?>> getArray(String key) {
92
+ return tryGettingValue(this, key);
93
+ }
94
+
95
+ public Dict getDictionary(String key) {
96
+ return (Dict) value.get(key);
97
+ }
98
+
99
+ public Rx getRegExp(String key) {
100
+ return Rx.createRx(getString(key));
101
+ }
102
+
103
+ public boolean containsElement(String key) {
104
+ return value.containsKey(key);
105
+ }
106
+
107
+ public Set<String> keys() {
108
+ return value.keySet();
109
+ }
110
+
111
+ @SuppressWarnings("unchecked")
112
+ private static <T> T tryGettingValue(Dict dict, String key) {
113
+ if (dict.value.containsKey(key)) {
114
+ return (T) dict.value.get(key).value;
115
+ } else {
116
+ return null;
117
+ }
118
+ }
119
+ }
@@ -0,0 +1,52 @@
1
+ package com.redcareditor.plist;
2
+
3
+ import java.util.ArrayList;
4
+ import java.util.List;
5
+
6
+ import org.jdom.Element;
7
+
8
+ /**
9
+ * generic class that holds the various elements of the property tree in a plist
10
+ * file.
11
+ *
12
+ * @author kungfoo
13
+ *
14
+ * @param <T>
15
+ */
16
+ public class PlistNode<T> {
17
+ public T value;
18
+
19
+ public PlistNode(T value) {
20
+ this.value = value;
21
+ }
22
+
23
+ public PlistNode() {
24
+ }
25
+
26
+ @SuppressWarnings("unchecked")
27
+ public static PlistNode<?> parseElement(Element element) {
28
+ if (elementNameIs(element, "string")) {
29
+ return new PlistNode<String>(element.getValue());
30
+ }
31
+ if (elementNameIs(element, "integer")) {
32
+ return new PlistNode<Integer>(Integer.parseInt(element.getValue()));
33
+ }
34
+ if (elementNameIs(element, "array")) {
35
+ List<PlistNode<?>> array = new ArrayList<PlistNode<?>>();
36
+ List<Element> children = element.getChildren();
37
+ for (Element e : children) {
38
+ array.add(PlistNode.parseElement(e));
39
+ }
40
+ return new PlistNode<List<PlistNode<?>>>(array);
41
+ }
42
+ if (elementNameIs(element, "dict")) {
43
+ return new Dict(element);
44
+ }
45
+
46
+ return null;
47
+ }
48
+
49
+ private static boolean elementNameIs(Element element, String name) {
50
+ return element.getName().equals(name);
51
+ }
52
+ }
@@ -0,0 +1,44 @@
1
+ package com.redcareditor.plist;
2
+
3
+ import java.lang.reflect.Field;
4
+
5
+ import com.redcareditor.onig.Rx;
6
+
7
+ /**
8
+ * This class will load properties from the given {@link Dict} into the object.
9
+ * It uses reflection, so beware of them typos. :D
10
+ *
11
+ * @author kungfoo
12
+ */
13
+ public class PlistPropertyLoader {
14
+ private Dict dict;
15
+ private Object object;
16
+
17
+ public PlistPropertyLoader(Dict dict, Object obj) {
18
+ this.dict = dict;
19
+ this.object = obj;
20
+ }
21
+
22
+ public void loadStringProperty(String propertyName) {
23
+ String value = dict.getString(propertyName);
24
+ trySettingProperty(propertyName, value);
25
+ }
26
+
27
+ public void loadRegexProperty(String propertyName) {
28
+ String value = dict.getString(propertyName);
29
+ Rx regex = Rx.createRx(value);
30
+ trySettingProperty(propertyName, regex);
31
+ }
32
+
33
+ private void trySettingProperty(String propertyName, Object value) {
34
+ try {
35
+ Field prop = object.getClass().getDeclaredField(propertyName);
36
+ if (value != null) {
37
+ prop.set(object, value);
38
+ }
39
+ } catch (Exception e) {
40
+ System.out.println(String.format("Can't set %s = %s on object %s", propertyName, value, object.toString()));
41
+ e.printStackTrace();
42
+ }
43
+ }
44
+ }