forked from redcar/java-mateview
/
Bundle.java
87 lines (75 loc) · 2.04 KB
/
Bundle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.redcareditor.mate;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Bundle {
private String name;
private List<Grammar> grammars;
private static List<Bundle> bundles;
public Bundle(String name) {
this.name = name;
grammars = new ArrayList<Grammar>();
}
public static Bundle getBundleByName(String findName) {
for (Bundle b : getBundles()) {
if (b.getName().equals(findName))
return b;
}
return null;
}
public String getName() {
return name;
}
public List<Grammar> getGrammars() {
return grammars;
}
public static List<Bundle> getBundles() {
return bundles;
}
/**
* Return a list of bundle names like "Ruby.tmbundle"
*/
public static List<String> bundleDirs(String textmateDir) {
List<String> names = new ArrayList<String>();
File dir = new File(textmateDir + "/Bundles");
if (dir.exists()) {
for (String name : dir.list()) {
if (name.endsWith(".tmbundle")) {
names.add(name);
}
}
}
return names;
}
public static void loadBundles(String textmateDir) {
if (getBundles() != null) {
return;
}
bundles = new ArrayList<Bundle>();
for (String bundleDir : bundleDirs(textmateDir)) {
Bundle bundle = new Bundle(bundleDir.split("\\.")[0]);
getBundles().add(bundle);
File grammarDirectory = new File(textmateDir + "/Bundles/" + bundleDir + "/Syntaxes");
if (grammarDirectory.exists()) {
loadGrammar(bundle, grammarDirectory);
}
}
for (Bundle b : getBundles()) {
for (Grammar g : b.getGrammars()) {
g.initForReference();
}
}
}
private static void loadGrammar(Bundle bundle, File syntaxDir) {
for (String grammarFileName : syntaxDir.list()) {
if (isTmBundlefile(grammarFileName)) {
String grammarFilePath = syntaxDir.getPath() + "/" + grammarFileName;
Grammar grammar = new Grammar(grammarFilePath);
bundle.getGrammars().add(grammar);
}
}
}
private static boolean isTmBundlefile(String syntaxFileName) {
return (syntaxFileName.endsWith(".tmLanguage") || syntaxFileName.endsWith(".plist"));
}
}