code suggestions
This commit is contained in:
1 parent
238257f057
commit
e6158c1aab
5 files changed
+85
-17
No files matched your search
@@ -1,6 +1,13 @@
|
||||
package carpet.script;
|
||||
|
||||
import carpet.CarpetServer;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* sole purpose of this package is to provide public access to package-private methods of Expression and CarpetExpression
|
||||
@@ -17,4 +24,29 @@ public class ExpressionInspector
|
||||
{
|
||||
return e.getName();
|
||||
}
|
||||
|
||||
private static TreeSet<String> scarpetFunctions;
|
||||
private static TreeSet<String> APIFunctions;
|
||||
static
|
||||
{
|
||||
Set<String> allFunctions = (new CarpetExpression("null", null, null)).getExpr().getFunctionNames();
|
||||
scarpetFunctions = new TreeSet<>(new Expression("null").getFunctionNames());
|
||||
APIFunctions = new TreeSet<>(allFunctions.stream().filter(s -> !scarpetFunctions.contains(s)).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
public static List<String> suggestFunctions(String previous, String prefix)
|
||||
{
|
||||
previous = previous.replace("\\'", "");
|
||||
int quoteCount = StringUtils.countMatches(previous,'\'');
|
||||
if (quoteCount % 2 == 1)
|
||||
return Collections.emptyList();
|
||||
int maxLen = prefix.length()<3 ? (prefix.length()*2+1) : 1234;
|
||||
List<String> scarpetMatches = scarpetFunctions.stream().
|
||||
filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> s+"(").collect(Collectors.toList());
|
||||
scarpetMatches.addAll(APIFunctions.stream().
|
||||
filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> s+"(").collect(Collectors.toList()));
|
||||
scarpetMatches.addAll(CarpetServer.scriptServer.globalHost.globalVariables.keySet().stream().
|
||||
filter(s -> s.startsWith(prefix)).collect(Collectors.toList()));
|
||||
return scarpetMatches;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user