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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*******************************************************************************
* Copyright (c) 2009-2011 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Alexander Kurtakov - initial API and implementation
* Mat Booth
*******************************************************************************/
package org.eclipse.dltk.sh.ui.text;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.dltk.sh.ui.Activator;
import org.eclipse.dltk.sh.ui.IShellColorConstants;
import org.eclipse.dltk.ui.text.AbstractScriptScanner;
import org.eclipse.dltk.ui.text.IColorManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordRule;
public class ShellCodeScanner extends AbstractScriptScanner {
public class ShellWordDetector implements IWordDetector {
@Override
public boolean isWordPart(char character) {
return (Character.isJavaIdentifierPart(character) && (character != '$')) || (character == '-')
|| (character == '.');
}
@Override
public boolean isWordStart(char character) {
return Character.isJavaIdentifierStart(character) && (character != 36);
}
}
public static String[] KEYWORDS = { "do", "done", "if", "fi", "then", "else", "elif", "case", "esac", "while",
"for", "in", "select", "time", "until", "function", "[", "[[", "]", "]]", "set", "unset", "declare" };
private static List<String> fgCommands = getCommands();
private static String fgTokenProperties[] = new String[] { IShellColorConstants.SHELL_DEFAULT,
IShellColorConstants.SHELL_KEYWORD, IShellColorConstants.SHELL_VARIABLE,
IShellColorConstants.SHELL_COMMAND };
public ShellCodeScanner(IColorManager manager, IPreferenceStore store) {
super(manager, store);
this.initialize();
}
public static List<String> getCommands() {
String path = System.getenv("PATH");
List<String> commands = new ArrayList<>();
String[] pathEntries = path.split(System.getProperty("path.separator"));
for (String pathEntry : pathEntries) {
File dir = new File(pathEntry);
if (dir.exists() && dir.isDirectory()) {
File[] files = dir.listFiles();
// GRO: Prevent NullPointerException
if (files == null) {
// @formatter:off
Activator.getDefault().getLog().log(// +
new Status(IStatus.ERROR, Activator.PLUGIN_ID, "listFiles() returned null: " + dir));
// @formatter:on
files = new File[] {};
}
for (File file : files) {
if (file.canExecute()) {
if (file.getName().endsWith(".exe"))
commands.add(file.getName().substring(0, file.getName().length() - 4));
else
commands.add(file.getName());
}
}
}
}
return commands;
}
@Override
protected List<IRule> createRules() {
List<IRule> rules = new ArrayList<>();
IToken keyword = this.getToken(IShellColorConstants.SHELL_KEYWORD);
IToken commandToken = this.getToken(IShellColorConstants.SHELL_COMMAND);
IToken other = this.getToken(IShellColorConstants.SHELL_DEFAULT);
IToken variable = this.getToken(IShellColorConstants.SHELL_VARIABLE);
rules.add(new WhitespaceRule(new WhitespaceDetector()));
rules.add(new AssignmentRule(new AssignmentDetector(), Token.UNDEFINED, variable));
rules.add(new DollarRule(new DollarDetector(), Token.UNDEFINED, variable));
WordRule wordRule = new WordRule(new ShellWordDetector(), other);
for (String element : KEYWORDS) {
wordRule.addWord(element, keyword);
}
for (String command : fgCommands) {
wordRule.addWord(command, commandToken);
}
rules.add(wordRule);
return rules;
}
@Override
protected String[] getTokenProperties() {
return fgTokenProperties;
}
}
|