You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2013/04/29 16:51:06 UTC
svn commit: r1477113 [3/18] - in
/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide:
./ core/ core/builder/ core/codeassist/ core/extensions/ core/packages/
core/parser/ core/search/ debug/ debug/ui/ debug/ui/handlers/ debug/ui...
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaSimpleBuilder.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaSimpleBuilder.java?rev=1477113&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaSimpleBuilder.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaSimpleBuilder.java Mon Apr 29 14:50:56 2013
@@ -0,0 +1,433 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.builder;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.uima.ResourceSpecifierFactory;
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.resource.impl.ResourceManager_impl;
+import org.apache.uima.resource.metadata.Capability;
+import org.apache.uima.resource.metadata.ConfigurationParameterSettings;
+import org.apache.uima.resource.metadata.FsIndexDescription;
+import org.apache.uima.resource.metadata.Import;
+import org.apache.uima.resource.metadata.TypeDescription;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.resource.metadata.impl.Import_impl;
+import org.apache.uima.ruta.UIMAConstants;
+import org.apache.uima.ruta.engine.RutaEngine;
+import org.apache.uima.util.CasCreationUtils;
+import org.apache.uima.util.InvalidXMLException;
+import org.apache.uima.util.XMLInputSource;
+import org.apache.uima.util.XMLSerializer;
+import org.apache.uima.util.XMLizable;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+public class RutaSimpleBuilder {
+
+ private static ResourceSpecifierFactory uimaFactory = UIMAFramework.getResourceSpecifierFactory();
+
+ private TypeSystemDescription typeSystemDescription;
+
+ private AnalysisEngineDescription analysisEngineDescription;
+
+ private final String defaultTypeSystem;
+
+ private ResourceManager rm;
+
+ public RutaSimpleBuilder(String defaultTypeSystem, String defaultEngine)
+ throws InvalidXMLException, IOException {
+ super();
+ this.defaultTypeSystem = defaultTypeSystem;
+ initialize(defaultEngine);
+ }
+
+ private void initialize(String defaultEngine) throws InvalidXMLException, IOException {
+ typeSystemDescription = uimaFactory.createTypeSystemDescription();
+ analysisEngineDescription = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
+ new XMLInputSource(new File(defaultEngine)));
+
+ }
+
+ public void build(DescriptorManager desc, String typeSystemOutput, String engineOutput,
+ RutaBuildOptions option, String mainScript, String[] scriptPaths,
+ String[] enginePaths) throws SAXException, RutaBuildException, InvalidXMLException,
+ IOException, ResourceInitializationException {
+
+ rm = new ResourceManager_impl();
+ String dataPath ="";
+ for (String string : enginePaths) {
+ dataPath += string + File.pathSeparator;
+ }
+ rm.setDataPath(dataPath);
+
+ Map<String, String> typeNameMap = new HashMap<String, String>();
+ Capability capability = uimaFactory.createCapability();
+ // String defaultTypeSystem2 = defaultTypeSystem.replaceAll("/", "\\\\");
+ File defaultTypeSystemFile = new File(defaultTypeSystem);
+ if (!defaultTypeSystemFile.exists()) {
+ System.out.println("Does not exist: " + defaultTypeSystemFile.getName());
+ }
+ TypeSystemDescription initialTypeSystem = UIMAFramework.getXMLParser()
+ .parseTypeSystemDescription(new XMLInputSource(defaultTypeSystemFile));
+ CAS cas = CasCreationUtils.createCas(initialTypeSystem, null, new FsIndexDescription[0]);
+ fillTypeNameMap(typeNameMap, cas.getTypeSystem());
+ cas.release();
+ List<Import> importList = new ArrayList<Import>();
+ Import_impl import_impl = new Import_impl();
+ if (option.isImportByName()) {
+ String name = initialTypeSystem.getName();
+ import_impl.setName(name);
+ } else if (option.isResolveImports()) {
+ String absoluteLocation = initialTypeSystem.getSourceUrlString();
+ import_impl.setLocation(absoluteLocation);
+ } else {
+ String relativeLocation = getRelativeLocation(defaultTypeSystemFile.getAbsolutePath(),
+ typeSystemOutput);
+ import_impl.setLocation(relativeLocation);
+ }
+ importList.add(import_impl);
+ for (String eachName : desc.getImportedTypeSystems()) {
+ String locate = RutaEngine.locate(eachName, enginePaths, ".xml");
+ if(locate == null) {
+ throw new FileNotFoundException("Build process can't find " + eachName + " in "
+ + mainScript);
+ }
+ File file = new File(locate);
+ TypeSystemDescription each = getTypeSystemDescriptor(file, option);
+ if (each != null) {
+ fillTypeNameMap(typeNameMap, each);
+ import_impl = new Import_impl();
+ if (option.isImportByName()) {
+ import_impl.setName(eachName);
+ } else if (option.isResolveImports()) {
+ String absoluteLocation = each.getSourceUrlString();
+ import_impl.setLocation(absoluteLocation);
+ } else {
+ String relativeLocation = getRelativeLocation(file.getAbsolutePath(), typeSystemOutput);
+ import_impl.setLocation(relativeLocation);
+ }
+ importList.add(import_impl);
+ } else {
+ throw new FileNotFoundException("Build process can't find " + eachName + " in "
+ + mainScript);
+ }
+ }
+ for (String eachName : desc.getImportedScripts()) {
+ String locate = RutaEngine.locate(eachName, enginePaths, "TypeSystem.xml");
+ File file = new File(locate);
+ TypeSystemDescription each = getTypeSystemDescriptor(file, option);
+ if (each != null) {
+ fillTypeNameMap(typeNameMap, each);
+ import_impl = new Import_impl();
+ if (option.isImportByName()) {
+ import_impl.setName(eachName + "TypeSystem");
+ } else if (option.isResolveImports()) {
+ String absoluteLocation = each.getSourceUrlString();
+ import_impl.setLocation(absoluteLocation);
+ } else {
+ String relativeLocation = getRelativeLocation(file.getAbsolutePath(), typeSystemOutput);
+ import_impl.setLocation(relativeLocation);
+ }
+ importList.add(import_impl);
+ } else {
+ throw new FileNotFoundException("Build process can't find " + eachName + " in "
+ + mainScript);
+ }
+ }
+
+ Import[] newImports = importList.toArray(new Import[0]);
+ typeSystemDescription.setImports(newImports);
+ if (option.isResolveImports()) {
+ try {
+ typeSystemDescription.resolveImports(rm);
+ } catch (InvalidXMLException e) {
+ throw new RutaBuildException("Failed to resolve imported Type Systems", e);
+ }
+ }
+
+ // TODO hotfixes: where do I get the final types??
+ Set<String> finalTypes = new HashSet<String>();
+ finalTypes.addAll(Arrays.asList(new String[] { "uima.cas.Boolean", "uima.cas.Byte",
+ "uima.cas.Short", "uima.cas.Integer", "uima.cas.Long", "uima.cas.Float", "uima.cas.Double",
+ "uima.cas.BooleanArray", "uima.cas.ByteArray", "uima.cas.ShortArray",
+ "uima.cas.IntegerArray", "uima.cas.LongArray", "uima.cas.FloatArray",
+ "uima.cas.DoubleArray", "uima.cas.StringArray", "uima.cas.FSArray" }));
+
+ int typeIndex = 0;
+ for (String eachType : desc.getTypeShortNames()) {
+ StringTriple typeTriple = desc.getTypeTriples().get(typeIndex);
+ typeTriple = resolveType(typeTriple, typeNameMap, mainScript);
+ if (typeSystemDescription.getType(typeTriple.getName()) != null) {
+ continue;
+ }
+ if (!finalTypes.contains(typeTriple.getParent())) {
+ TypeDescription newType = typeSystemDescription.addType(typeTriple.getName(),
+ typeTriple.getDescription(), typeTriple.getParent());
+ capability.addInputType(typeTriple.getName(), false);
+ capability.addOutputType(typeTriple.getName(), false);
+ Collection<StringTriple> collection = desc.getFeatures().get(eachType);
+ if (collection != null) {
+ for (StringTriple eachFeature : collection) {
+ eachFeature = resolveFeature(eachFeature, typeNameMap);
+ newType.addFeature(eachFeature.getName(), eachFeature.getDescription(),
+ eachFeature.getParent());
+ // capability.addInputFeature(eachFeature.getName());
+ // capability.addOutputFeature(eachFeature.getName());
+ }
+ }
+ }
+ typeIndex++;
+ }
+
+ Set<String> names = new HashSet<String>();
+ List<TypeDescription> types = new ArrayList<TypeDescription>();
+ for (TypeDescription each : typeSystemDescription.getTypes()) {
+ String name = each.getName();
+ if (!names.contains(name)) {
+ names.add(name);
+ types.add(each);
+ }
+ }
+
+ typeSystemDescription.setTypes(types.toArray(new TypeDescription[0]));
+ File typeSystemFile = getFile(typeSystemOutput);
+ typeSystemDescription.setName(mainScript + "TypeSystem");
+ typeSystemDescription.setSourceUrl(typeSystemFile.toURI().toURL());
+ TypeSystemDescription aets = uimaFactory.createTypeSystemDescription();
+ import_impl = new Import_impl();
+ if (option.isImportByName()) {
+ import_impl.setName(typeSystemDescription.getName());
+ } else {
+ String relativeLocation = getRelativeLocation(engineOutput, typeSystemOutput);
+ import_impl.setLocation(relativeLocation);
+ }
+
+ File engineFile = configureEngine(desc, engineOutput, option, mainScript, scriptPaths,
+ enginePaths, capability, import_impl, aets);
+
+ toFile(typeSystemDescription, typeSystemFile);
+ toFile(analysisEngineDescription, engineFile);
+ }
+
+ private void fillTypeNameMap(Map<String, String> typeNameMap, TypeSystem typeSystem) {
+ Iterator<Type> typeIterator = typeSystem.getTypeIterator();
+ while (typeIterator.hasNext()) {
+ Type type = (Type) typeIterator.next();
+ String shortName = type.getShortName();
+ String name = type.getName();
+ typeNameMap.put(shortName, name);
+ }
+ }
+
+ private boolean fillTypeNameMap(Map<String, String> typeNameMap, TypeSystemDescription desc) {
+ boolean contained = false;
+ for (TypeDescription each : desc.getTypes()) {
+ String name = each.getName();
+ int lastIndexOf = name.lastIndexOf(".");
+ String shortName = name.substring(lastIndexOf + 1, name.length());
+ typeNameMap.put(shortName, name);
+ }
+ return contained;
+ }
+
+ private StringTriple resolveFeature(StringTriple eachFeature, Map<String, String> types) {
+ String parent = eachFeature.getParent();
+ String name = eachFeature.getName();
+ parent = translate(parent);
+ if (parent.indexOf(".") == -1 && types.containsKey(parent)) {
+ parent = types.get(parent);
+ }
+ return new StringTriple(name, eachFeature.getDescription(), parent);
+ }
+
+ private StringTriple resolveType(StringTriple typeTriple, Map<String, String> types,
+ String packageName) {
+ String parent = typeTriple.getParent();
+ String name = typeTriple.getName();
+ if (parent == null) {
+ parent = "uima.tcas.Annotation";
+ }
+ parent = translate(parent);
+ name = translate(name);
+ if (parent.indexOf(".") == -1 && types.containsKey(parent)) {
+ parent = types.get(parent);
+ }
+ if (name.indexOf(".") == -1) {
+ if (types.containsKey(name)) {
+ name = types.get(name);
+ } else {
+ name = packageName + "." + name;
+ }
+ }
+ int lastIndexOf = name.lastIndexOf(".");
+ String shortName = name.substring(lastIndexOf + 1, name.length());
+ types.put(shortName, name);
+ return new StringTriple(name, typeTriple.getDescription(), parent);
+ }
+
+ private String translate(String name) {
+ if (name == null) {
+ return null;
+ }
+ if (name.equals("Annotation")) {
+ return "uima.tcas.Annotation";
+ } else if (name.equals("STRING")) {
+ return UIMAConstants.TYPE_STRING;
+ } else if (name.equals("INT")) {
+ return UIMAConstants.TYPE_INTEGER;
+ } else if (name.equals("DOUBLE")) {
+ return UIMAConstants.TYPE_DOUBLE;
+ } else if (name.equals("FLOAT")) {
+ return UIMAConstants.TYPE_FLOAT;
+ } else if (name.equals("BOOLEAN")) {
+ return UIMAConstants.TYPE_BOOLEAN;
+ } else if (name.equals("TYPE")) {
+ return UIMAConstants.TYPE_STRING;
+ }
+ return name;
+ }
+
+ private File configureEngine(DescriptorManager desc, String engineOutput,
+ RutaBuildOptions option, String mainScript, String[] scriptPaths,
+ String[] descriptorPaths, Capability capability, Import_impl import_impl,
+ TypeSystemDescription aets) throws MalformedURLException {
+ aets.setImports(new Import[] { import_impl });
+ analysisEngineDescription.getAnalysisEngineMetaData().setTypeSystem(aets);
+ File file = getFile(engineOutput);
+ analysisEngineDescription.setSourceUrl(file.toURI().toURL());
+
+ if (!desc.getTypeShortNames().isEmpty()) {
+ Capability[] capabilities = analysisEngineDescription.getAnalysisEngineMetaData()
+ .getCapabilities();
+ Capability[] newArray = new Capability[capabilities.length + 1];
+ System.arraycopy(capabilities, 0, newArray, 0, capabilities.length);
+ newArray[capabilities.length] = capability;
+ analysisEngineDescription.getAnalysisEngineMetaData().setCapabilities(newArray);
+ }
+
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.MAIN_SCRIPT, mainScript);
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.SCRIPT_PATHS, scriptPaths);
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.DESCRIPTOR_PATHS, descriptorPaths);
+ String[] parameterValue = (String[]) analysisEngineDescription.getAnalysisEngineMetaData()
+ .getConfigurationParameterSettings().getParameterValue(RutaEngine.RESOURCE_PATHS);
+ Set<String> resourceLocations = new HashSet<String>();
+
+ if (parameterValue != null && parameterValue.length != 0) {
+ resourceLocations.addAll(Arrays.asList(parameterValue));
+ }
+ for (String string : descriptorPaths) {
+ File descDir = new File(string);
+ File defaultResourceDir = new File(descDir.getParent(),
+ RutaProjectUtils.getDefaultResourcesLocation());
+ resourceLocations.add(defaultResourceDir.getAbsolutePath());
+ }
+ analysisEngineDescription
+ .getAnalysisEngineMetaData()
+ .getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.RESOURCE_PATHS,
+ resourceLocations.toArray(new String[0]));
+
+ String[] additionalScriptsArray = desc.getImportedScripts().toArray(new String[] {});
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.ADDITIONAL_SCRIPTS, additionalScriptsArray);
+
+ String[] additionalEnginesArray = desc.getImportedEngines().toArray(new String[] {});
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.ADDITIONAL_ENGINES, additionalEnginesArray);
+
+ analysisEngineDescription.getAnalysisEngineMetaData().setTypeSystem(typeSystemDescription);
+
+ configureExtensions(option);
+ return file;
+ }
+
+ private String getRelativeLocation(String target, String base) {
+ IPath targetPath = Path.fromOSString(target);
+ IPath basePath = Path.fromPortableString(base);
+ IPath result = targetPath.makeRelativeTo(basePath);
+ // TODO remove the first part! Should be correct in first place!
+ result = result.removeFirstSegments(1);
+ return result.toPortableString();
+ }
+
+ private void configureExtensions(RutaBuildOptions options) {
+ ConfigurationParameterSettings configurationParameterSettings = analysisEngineDescription
+ .getAnalysisEngineMetaData().getConfigurationParameterSettings();
+
+ List<String> languageExtensions = options.getLanguage();
+
+ configurationParameterSettings.setParameterValue(RutaEngine.ADDITIONAL_EXTENSIONS,
+ languageExtensions.toArray(new String[0]));
+ configurationParameterSettings.setParameterValue(RutaEngine.ADDITIONAL_ENGINE_LOADERS,
+ options.getEngines().toArray(new String[0]));
+ }
+
+ private TypeSystemDescription getTypeSystemDescriptor(File file, RutaBuildOptions option)
+ throws InvalidXMLException, IOException {
+ TypeSystemDescription tsdesc = UIMAFramework.getXMLParser().parseTypeSystemDescription(
+ new XMLInputSource(file));
+ if (option.isResolveImports()) {
+ tsdesc.resolveImports(rm);
+ }
+ return tsdesc;
+ }
+
+ private File getFile(String location) {
+ return new File(location);
+ }
+
+ private void toFile(XMLizable desc, File destination) throws SAXException, FileNotFoundException {
+ destination.getParentFile().mkdirs();
+ OutputStream out = new FileOutputStream(destination);
+ XMLSerializer sax = new XMLSerializer(out);
+ ContentHandler ch = sax.getContentHandler();
+ ch.startDocument();
+ desc.toXML(ch);
+ ch.endDocument();
+ }
+
+}
Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/StringTriple.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/StringTriple.java?rev=1477113&r1=1477112&r2=1477113&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/StringTriple.java (original)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/StringTriple.java Mon Apr 29 14:50:56 2013
@@ -17,7 +17,7 @@
* under the License.
*/
-package org.apache.uima.textmarker.ide.core.builder;
+package org.apache.uima.ruta.ide.core.builder;
public class StringTriple {
Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordArgumentOrFunctionArgument.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordArgumentOrFunctionArgument.java?rev=1477113&r1=1477112&r2=1477113&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordArgumentOrFunctionArgument.java (original)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordArgumentOrFunctionArgument.java Mon Apr 29 14:50:56 2013
@@ -17,9 +17,9 @@
* under the License.
*/
-package org.apache.uima.textmarker.ide.core.codeassist;
+package org.apache.uima.ruta.ide.core.codeassist;
-import org.apache.uima.textmarker.ide.parser.ast.TextMarkerStatement;
+import org.apache.uima.ruta.ide.parser.ast.RutaStatement;
import org.eclipse.dltk.ast.ASTNode;
import org.eclipse.dltk.ast.ASTVisitor;
import org.eclipse.dltk.ast.references.SimpleReference;
@@ -31,19 +31,19 @@ public class CompletionOnKeywordArgument
private String[] possibleKeywords;
- private TextMarkerStatement statement;
+ private RutaStatement statement;
private ASTNode completionNode;
public CompletionOnKeywordArgumentOrFunctionArgument(String token, ASTNode completionNode,
- TextMarkerStatement node, String[] KeywordspossibleKeywords) {
+ RutaStatement node, String[] KeywordspossibleKeywords) {
super(completionNode.sourceStart(), completionNode.sourceEnd(), token);
this.possibleKeywords = KeywordspossibleKeywords;
this.statement = node;
this.completionNode = completionNode;
}
- public CompletionOnKeywordArgumentOrFunctionArgument(String token, TextMarkerStatement node,
+ public CompletionOnKeywordArgumentOrFunctionArgument(String token, RutaStatement node,
String[] possibleKeywords, int position) {
super(position, position, token);
this.possibleKeywords = possibleKeywords;
@@ -74,7 +74,7 @@ public class CompletionOnKeywordArgument
return true;
}
- public TextMarkerStatement getStatement() {
+ public RutaStatement getStatement() {
return this.statement;
}
Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordOrFunction.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordOrFunction.java?rev=1477113&r1=1477112&r2=1477113&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordOrFunction.java (original)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordOrFunction.java Mon Apr 29 14:50:56 2013
@@ -17,7 +17,7 @@
* under the License.
*/
-package org.apache.uima.textmarker.ide.core.codeassist;
+package org.apache.uima.ruta.ide.core.codeassist;
import org.eclipse.dltk.ast.ASTNode;
import org.eclipse.dltk.ast.ASTVisitor;
Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnVariable.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnVariable.java?rev=1477113&r1=1477112&r2=1477113&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnVariable.java (original)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnVariable.java Mon Apr 29 14:50:56 2013
@@ -17,7 +17,7 @@
* under the License.
*/
-package org.apache.uima.textmarker.ide.core.codeassist;
+package org.apache.uima.ruta.ide.core.codeassist;
import org.eclipse.dltk.ast.ASTNode;
import org.eclipse.dltk.ast.references.SimpleReference;
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaASTUtil.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaASTUtil.java?rev=1477113&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaASTUtil.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaASTUtil.java Mon Apr 29 14:50:56 2013
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.ruta.ide.core.parser.RutaParseUtils;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.ASTVisitor;
+import org.eclipse.dltk.ast.declarations.MethodDeclaration;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.declarations.TypeDeclaration;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.ast.statements.Block;
+import org.eclipse.dltk.core.DLTKCore;
+
+
+public class RutaASTUtil {
+ public static List getStatements(ASTNode node) {
+ if (node instanceof ModuleDeclaration) {
+ return ((ModuleDeclaration) node).getStatements();
+ } else if (node instanceof TypeDeclaration) {
+ return ((TypeDeclaration) node).getStatements();
+ } else if (node instanceof MethodDeclaration) {
+ return ((MethodDeclaration) node).getStatements();
+ } else if (node instanceof Block) {
+ return ((Block) node).getStatements();
+ } else {
+ final List innerBlockStatements = new ArrayList();
+ // Lets traverse to see inner blocks.
+ ASTVisitor visitor = new ASTVisitor() {
+ @Override
+ public boolean visit(Expression s) throws Exception {
+ if (s instanceof Block) {
+ List tStatements = ((Block) s).getStatements();
+ innerBlockStatements.addAll(tStatements);
+ }
+ return false;
+ }
+
+ @Override
+ public boolean visit(MethodDeclaration s) throws Exception {
+ return false;
+ }
+
+ @Override
+ public boolean visit(TypeDeclaration s) throws Exception {
+ return false;
+ }
+ };
+ try {
+ node.traverse(visitor);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return innerBlockStatements;
+ }
+ // return null;
+ }
+
+ /**
+ * We need to extend all statements to end of lines or to begining of next statement. This is
+ * needed to for correct completion in statements. Such as variable completion and so on.
+ */
+ public static void extendStatements(ASTNode node, String content) {
+ List statements = getStatements(node);
+
+ if (statements != null) {
+ int len = statements.size();
+ for (int i = 0; i < len; ++i) {
+ ASTNode nde = (ASTNode) statements.get(i);
+
+ extendStatement(nde, content);
+ extendStatements(nde, content);
+ }
+ }
+ }
+
+ public static void extendStatement(ASTNode node, String content) {
+ int newValueStart = startLineOrSymbol(node, content);
+ int newValueEnd = endLineOrSymbol(node, content);
+ if (DLTKCore.DEBUG_COMPLETION) {
+ if (node.sourceEnd() != newValueEnd || node.sourceStart() != newValueStart) {
+ // TODO
+ }
+ }
+ node.setStart(newValueStart);
+ node.setEnd(newValueEnd);
+ }
+
+ public static int endLineOrSymbol(ASTNode node, String content) {
+ return RutaParseUtils.endLineOrSymbol(node.sourceEnd(), content);
+ }
+
+ public static int startLineOrSymbol(ASTNode node, String content) {
+ return RutaParseUtils.startLineOrSymbol(node.sourceStart(), content);
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaAssistParser.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaAssistParser.java?rev=1477113&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaAssistParser.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaAssistParser.java Mon Apr 29 14:50:56 2013
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.uima.ruta.ide.core.RutaNature;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.parser.ISourceParser;
+import org.eclipse.dltk.codeassist.IAssistParser;
+import org.eclipse.dltk.core.DLTKLanguageManager;
+
+
+public abstract class RutaAssistParser implements IAssistParser {
+ public static final int MODULE = 0;// IRutaKeywords.MODULE;
+
+ public static final int NAMESPACE = 1;// IRutaKeywords.NAMESPACE;
+
+ public static final int FUNCTION = 2;// IRutaKeywords.FUNCTION;
+
+ public static final int EXEC_EXPRESSION = 3;// IRutaKeywords.EXEC_EXPRESSION;
+
+ protected ISourceParser parser = null;
+
+ protected ModuleDeclaration module;
+
+ protected ASTNode assistNodeParent = null;
+
+ public RutaAssistParser() {
+ // try {
+ this.parser = DLTKLanguageManager.getSourceParser(RutaNature.NATURE_ID);
+ // } catch (CoreException e) {
+ // if (DLTKCore.DEBUG) {
+ // e.printStackTrace();
+ // }
+ // }
+ }
+
+ public ASTNode getAssistNodeParent() {
+ return assistNodeParent;
+ }
+
+ protected void findElementsTo(List statements, ASTNode node, List elements) {
+ if (statements == null) {
+ return;
+ }
+ Iterator i = statements.iterator();
+ while (i.hasNext()) {
+ ASTNode n = (ASTNode) i.next();
+ if (n.equals(node)) {
+ elements.add(n);
+ return;
+ }
+ if (n.sourceStart() <= node.sourceStart() && node.sourceEnd() <= n.sourceEnd()) {
+ elements.add(n);
+ findElementsTo(RutaASTUtil.getStatements(n), node, elements);
+ return;
+ }
+ }
+
+ }
+
+ protected List findLevelsTo(ASTNode astNodeParent) {
+ List elements = new ArrayList();
+ if (this.module != null || astNodeParent instanceof ModuleDeclaration) {
+ if (this.module == null) {
+ this.module = (ModuleDeclaration) astNodeParent;
+ }
+ elements.add(this.module);
+ findElementsTo(RutaASTUtil.getStatements(this.module), astNodeParent, elements);
+ }
+ return elements;
+ }
+
+ public void setSource(ModuleDeclaration unit) {
+ this.module = unit;
+ }
+
+// public ModuleDeclaration parse(ISourceModule sourceUnit) {
+//
+// module = this.parser.parse(sourceUnit., null);
+// module.rebuild();
+//
+// // RutaASTUtil.extendStatements(module, sourceUnit
+// // .getSourceContents());
+//
+// return module;
+// }
+
+ public ModuleDeclaration getModule() {
+ return this.module;
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaCompletionEngine.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaCompletionEngine.java?rev=1477113&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaCompletionEngine.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaCompletionEngine.java Mon Apr 29 14:50:56 2013
@@ -0,0 +1,618 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.resource.metadata.TypeDescription;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.ruta.ide.core.IRutaKeywords;
+import org.apache.uima.ruta.ide.core.RutaExtensionManager;
+import org.apache.uima.ruta.ide.core.RutaKeywordsManager;
+import org.apache.uima.ruta.ide.core.builder.RutaProjectUtils;
+import org.apache.uima.ruta.ide.core.extensions.ICompletionExtension;
+import org.apache.uima.ruta.ide.core.parser.RutaParseUtils;
+import org.apache.uima.ruta.ide.parser.ast.ComponentDeclaration;
+import org.apache.uima.ruta.ide.parser.ast.ComponentReference;
+import org.apache.uima.ruta.ide.parser.ast.TMTypeConstants;
+import org.apache.uima.ruta.ide.parser.ast.RutaAction;
+import org.apache.uima.ruta.ide.parser.ast.RutaCondition;
+import org.apache.uima.ruta.ide.parser.ast.RutaModuleDeclaration;
+import org.apache.uima.ruta.ide.parser.ast.RutaVariableReference;
+import org.apache.uima.util.InvalidXMLException;
+import org.apache.uima.util.XMLInputSource;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.codeassist.RelevanceConstants;
+import org.eclipse.dltk.codeassist.ScriptCompletionEngine;
+import org.eclipse.dltk.compiler.CharOperation;
+import org.eclipse.dltk.compiler.env.IModuleSource;
+import org.eclipse.dltk.core.CompletionProposal;
+import org.eclipse.dltk.core.IField;
+import org.eclipse.dltk.core.IMethod;
+import org.eclipse.dltk.core.IModelElement;
+import org.eclipse.dltk.core.IType;
+import org.eclipse.dltk.core.ModelException;
+import org.eclipse.dltk.internal.core.SourceField;
+import org.eclipse.dltk.internal.core.SourceMethod;
+import org.eclipse.dltk.internal.core.SourceModule;
+
+public class RutaCompletionEngine extends ScriptCompletionEngine {
+
+ protected RutaCompletionParser parser;
+
+ protected IModuleSource sourceModule;
+
+ protected final static boolean TRACE_COMPLETION_TIME = false;
+
+ private ICompletionExtension[] extensions;
+
+ public RutaCompletionEngine() {
+ extensions = RutaExtensionManager.getDefault().getCompletionExtensions();
+ this.parser = new RutaCompletionParser(extensions);
+ }
+
+ @Override
+ protected int getEndOfEmptyToken() {
+ return actualCompletionPosition;
+ }
+
+ @Override
+ protected String processMethodName(IMethod method, String token) {
+ return RutaParseUtils.processMethodName(method, token);
+ }
+
+ @Override
+ protected String processTypeName(IType type, String token) {
+ return RutaParseUtils.processTypeName(type, token);
+ }
+
+ public void complete(IModuleSource module, int position, int i) {
+ this.sourceModule = module;
+ this.actualCompletionPosition = position;
+ this.offset = i;
+ this.requestor.beginReporting();
+ String content = module.getSourceContents();
+ if (position < 0 || position > content.length()) {
+ return;
+ }
+
+ TMAutoCompletionToolkit tk = new TMAutoCompletionToolkit(content, position);
+
+ String startPart = tk.getWordPrefix();
+ this.setSourceRange(position - startPart.length(), position);
+
+ // 090813:
+ RutaModuleDeclaration parsed = (RutaModuleDeclaration) this.parser.parse(module);
+
+ // types = getShortNames(types);
+
+ ASTNode node;
+ if (parsed != null) {
+ try {
+ RutaReferenceVisitor referenceVisitor = new RutaReferenceVisitor(
+ actualCompletionPosition);
+ parsed.traverse(referenceVisitor);
+ node = referenceVisitor.getResult();
+ if (node == null) {
+ doCompletionOnEmptyStatement(module, position, i);
+ doCompletionOnDeclaration(module, startPart);
+ } else if (node instanceof RutaVariableReference) {
+ int type = ((RutaVariableReference) node).getType();
+ doCompletionOnVarRef(module, parsed, startPart, type,
+ ((RutaVariableReference) node).getName());
+ if(RutaParseUtils.isAtLineStart(node, content)) {
+ doCompletionOnDeclaration(module, startPart);
+ }
+ } else if (node instanceof ComponentDeclaration) {
+ doCompletionOnComponentDeclaration(module, parsed, startPart,
+ ((ComponentDeclaration) node).getType(), startPart);
+ } else if (node instanceof ComponentReference) {
+ doCompletionOnComponentReference(module, parsed, startPart,
+ ((ComponentReference) node).getType(), startPart);
+ } else if (node instanceof RutaAction) {
+ doCompletionOnAction(module, parsed, startPart, TMTypeConstants.TM_TYPE_A, startPart);
+ } else if (node instanceof RutaCondition) {
+ doCompletionOnCondition(module, parsed, startPart, TMTypeConstants.TM_TYPE_C, startPart);
+ }
+ // if(requestor.)
+ // doCompletionOnKeyword(position, i, startPart);
+
+ // }
+ } catch (Exception e) {
+ System.out.println("no completion node found");
+ System.out.println("");
+ } finally {
+ this.requestor.endReporting();
+ }
+ }
+ }
+
+ private void doCompletionOnComponentReference(IModuleSource cu,
+ RutaModuleDeclaration parsed, String startPart, int type, String complString) {
+ Collection<String> importedEngines = parsed.descriptorInfo.getImportedEngines();
+ for (String string : importedEngines) {
+ if (match(complString, string)) {
+ addProposal(complString, string, CompletionProposal.PACKAGE_REF);
+ }
+ }
+ Collection<String> importedScripts = parsed.descriptorInfo.getImportedScripts();
+ for (String each : importedScripts) {
+ String[] split = each.split("\\.");
+ String string = split[split.length - 1];
+ if (match(complString, string)) {
+ addProposal(complString, string, CompletionProposal.PACKAGE_REF);
+ }
+ }
+ List<IMethod> blocks = new ArrayList<IMethod>();
+ try {
+ IModelElement[] children = sourceModule.getModelElement().getModel().getChildren();
+ for (IModelElement iModelElement : children) {
+ if (iModelElement instanceof SourceMethod) {
+ collectBlocks((SourceMethod) iModelElement, blocks);
+ }
+ }
+ } catch (ModelException e) {
+ }
+ for (IMethod m : blocks) {
+ String string = m.getElementName();
+ if (match(complString, string)) {
+ addProposal(complString, string, CompletionProposal.PACKAGE_REF);
+ }
+ }
+ }
+
+ private void doCompletionOnComponentDeclaration(IModuleSource cu,
+ RutaModuleDeclaration parsed, String startPart, int type, String complString)
+ throws Exception {
+ if (type == ComponentDeclaration.SCRIPT) {
+ List<IFolder> scriptFolders = RutaProjectUtils.getAllScriptFolders(sourceModule
+ .getModelElement().getScriptProject());
+
+ List<String> scripts = new ArrayList<String>();
+ for (IFolder folder : scriptFolders) {
+ try {
+ scripts.addAll(collectScripts(folder, ""));
+ } catch (CoreException e) {
+ }
+ }
+ for (String string : scripts) {
+ if (match(complString, string)) {
+ addProposal(complString, string, CompletionProposal.PACKAGE_REF);
+ }
+ }
+ } else if (type == ComponentDeclaration.ENGINE) {
+ List<IFolder> descriptorFolders = RutaProjectUtils.getAllDescriptorFolders(sourceModule
+ .getModelElement().getScriptProject().getProject());
+ List<String> engines = new ArrayList<String>();
+ for (IFolder folder : descriptorFolders) {
+ try {
+ engines.addAll(collectEngines(folder, ""));
+ } catch (CoreException e) {
+ }
+ }
+ for (String string : engines) {
+ if (match(complString, string)) {
+ addProposal(complString, string, CompletionProposal.PACKAGE_REF);
+ }
+ }
+ } else {
+ List<IFolder> descriptorFolders = RutaProjectUtils.getAllDescriptorFolders(sourceModule
+ .getModelElement().getScriptProject().getProject());
+ List<String> tss = new ArrayList<String>();
+ for (IFolder folder : descriptorFolders) {
+ try {
+ tss.addAll(collectTypeSystems(folder, ""));
+ } catch (CoreException e) {
+ }
+ }
+ for (String string : tss) {
+ if (match(complString, string)) {
+ addProposal(complString, string, CompletionProposal.PACKAGE_REF);
+ }
+ }
+ }
+ }
+
+ private List<String> collectEngines(IFolder folder, String prefix) throws CoreException {
+ List<String> result = new ArrayList<String>();
+ IResource[] members = folder.members();
+ for (IResource iResource : members) {
+ if (iResource instanceof IFolder) {
+ IFolder folder2 = (IFolder) iResource;
+ String newPrefix = prefix + folder2.getProjectRelativePath().lastSegment() + ".";
+ result.addAll(collectEngines(folder2, newPrefix));
+ } else if (iResource instanceof IFile) {
+ IFile file = (IFile) iResource;
+ if (file.getFileExtension().equals("xml")) {
+ File f = new File(file.getLocation().toPortableString());
+ if (f.exists()) {
+ try {
+ UIMAFramework.getXMLParser().parseAnalysisEngineDescription(new XMLInputSource(f));
+ result.add(prefix + file.getName().substring(0, file.getName().length() - 4));
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ private List<String> collectTypeSystems(IFolder folder, String prefix) throws CoreException {
+ List<String> result = new ArrayList<String>();
+ IResource[] members = folder.members();
+ for (IResource iResource : members) {
+ if (iResource instanceof IFolder) {
+ IFolder folder2 = (IFolder) iResource;
+ String newPrefix = prefix + folder2.getProjectRelativePath().lastSegment() + ".";
+ result.addAll(collectTypeSystems(folder2, newPrefix));
+ } else if (iResource instanceof IFile) {
+ IFile file = (IFile) iResource;
+ String fileExtension = file.getFileExtension();
+ if (fileExtension != null && fileExtension.equals("xml")) {
+ File f = new File(file.getLocation().toPortableString());
+ if (f.exists()) {
+ try {
+ UIMAFramework.getXMLParser().parseTypeSystemDescription(new XMLInputSource(f));
+ result.add(prefix + file.getName().substring(0, file.getName().length() - 4));
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ private List<String> collectScripts(IFolder folder, String prefix) throws CoreException {
+ List<String> result = new ArrayList<String>();
+ IResource[] members = folder.members();
+ for (IResource iResource : members) {
+ if (iResource instanceof IFolder) {
+ IFolder folder2 = (IFolder) iResource;
+ String newPrefix = prefix + folder2.getProjectRelativePath().lastSegment() + ".";
+ result.addAll(collectScripts(folder2, newPrefix));
+ } else if (iResource instanceof IFile) {
+ IFile file = (IFile) iResource;
+ if (file.getFileExtension().equals("tm")) {
+ result.add(prefix + file.getName().substring(0, file.getName().length() - 3));
+ }
+ }
+ }
+ return result;
+ }
+
+ private void doCompletionOnDeclaration(IModuleSource cu, String startPart) {
+ String[] keywords = RutaKeywordsManager.getKeywords(IRutaKeywords.DECLARATION);
+ for (String string : keywords) {
+ if (match(startPart, string)) {
+ addProposal(startPart, string, CompletionProposal.KEYWORD);
+ }
+ }
+ }
+
+ private Set<String> importTypeSystem(String xmlFilePath, IProject project)
+ throws InvalidXMLException, IOException {
+ IFolder folder = project.getProject().getFolder(
+ RutaProjectUtils.getDefaultDescriptorLocation());
+ xmlFilePath = xmlFilePath.substring(0, xmlFilePath.length() - 3) + "TypeSystem.xml";
+ return getTypes(folder, xmlFilePath);
+ }
+
+ private Set<String> getTypes(IFolder folder, String filePath) throws InvalidXMLException,
+ IOException {
+ Set<String> types = new HashSet<String>();
+ IFile iFile = getFile(folder, filePath);
+ URL url;
+ try {
+ url = iFile.getLocationURI().toURL();
+ ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
+ resMgr.setDataPath(folder.getLocation().toPortableString());
+ types = getTypes(url, resMgr);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ return types;
+ }
+
+ private Set<String> getTypes(URL resource, ResourceManager resMgr) throws IOException,
+ InvalidXMLException {
+ Set<String> types = new HashSet<String>();
+ TypeSystemDescription typeSysDescr = null;
+ typeSysDescr = UIMAFramework.getXMLParser().parseTypeSystemDescription(
+ new XMLInputSource(resource));
+ typeSysDescr.resolveImports(resMgr);
+ for (TypeDescription each : typeSysDescr.getTypes()) {
+ String name = each.getName();
+ String[] nameSpace = name.split("[.]");
+ types.add(nameSpace[nameSpace.length - 1]);
+ StringBuffer fullName = new StringBuffer();
+ if (nameSpace.length > 0) {
+ fullName.append(nameSpace[0]);
+ }
+ for (int i = 1; i < nameSpace.length; i++) {
+ // TODO fix workaround
+ if (!nameSpace[i].equals("type")) {
+ fullName.append('.');
+ fullName.append(nameSpace[i]);
+ }
+ }
+ types.add(fullName.toString());
+ }
+ return types;
+ }
+
+ private IFile getFile(IFolder folder, String filePath) {
+ int lastDot = filePath.lastIndexOf('.');
+ int sndLastDot = filePath.lastIndexOf('.', lastDot - 1);
+ String fName = filePath;
+ if (sndLastDot >= 0) {
+ String subFolder = filePath.substring(0, sndLastDot);
+ folder = folder.getFolder(subFolder);
+ fName = filePath.substring(sndLastDot + 1);
+ }
+ return folder.getFile(fName);
+ }
+
+ private void doCompletionOnAction(IModuleSource cu, RutaModuleDeclaration parsed,
+ String startPart, int type, String complString) {
+ String[] keywords = RutaKeywordsManager.getKeywords(IRutaKeywords.ACTION);
+ for (String string : keywords) {
+ if (match(complString, string)) {
+ addProposal(complString, string + "()", string, CompletionProposal.METHOD_NAME_REFERENCE);
+ }
+ }
+ }
+
+ private void doCompletionOnCondition(IModuleSource cu, RutaModuleDeclaration parsed,
+ String startPart, int type, String complString) {
+ if (complString.startsWith("-")) {
+ complString = complString.substring(1, complString.length());
+ }
+ String[] keywords = RutaKeywordsManager.getKeywords(IRutaKeywords.CONDITION);
+ for (String string : keywords) {
+ if (match(complString, string)) {
+ addProposal(complString, string + "()", string, CompletionProposal.METHOD_NAME_REFERENCE);
+ }
+ }
+ }
+
+ private void doCompletionOnVarRef(IModuleSource cu, RutaModuleDeclaration parsed,
+ String startPart, int type, String complString) {
+ Collection<String> types = new HashSet<String>();
+ if (type == TMTypeConstants.TM_TYPE_AT) {
+ try {
+ IPath path = sourceModule.getModelElement().getPath();
+ path = path.removeFirstSegments(2);
+ types = importTypeSystem(path.toPortableString(), sourceModule.getModelElement()
+ .getScriptProject().getProject());
+ } catch (Exception e) {
+ }
+ for (String string : types) {
+ if (match(complString, string)) {
+ addProposal(complString, string, CompletionProposal.TYPE_REF);
+ }
+ }
+ } else {
+ IModelElement modelElement = sourceModule.getModelElement();
+ if (modelElement instanceof SourceModule) {
+ SourceModule sm = (SourceModule) modelElement;
+ try {
+ IField[] fields = sm.getFields();
+ for (IField iField : fields) {
+ SourceField f = (SourceField) iField;
+ int fieldType = RutaParseUtils.getTypeOfIModelElement(f);
+ if (TMTypeConstants.TM_TYPE_N == type) {
+ if (fieldType == TMTypeConstants.TM_TYPE_N || fieldType == TMTypeConstants.TM_TYPE_I
+ || fieldType == TMTypeConstants.TM_TYPE_D
+ || fieldType == TMTypeConstants.TM_TYPE_F) {
+ addProposal(complString, f.getElementName(), CompletionProposal.LOCAL_VARIABLE_REF);
+ }
+ } else if (type == fieldType) {
+ addProposal(complString, f.getElementName(), CompletionProposal.LOCAL_VARIABLE_REF);
+ }
+
+ }
+ } catch (ModelException e) {
+ }
+
+ }
+ }
+ }
+
+ private boolean match(String complString, String string) {
+ return string.toLowerCase().startsWith(complString.toLowerCase())
+ || removeLowerCase(string).startsWith(complString);
+ }
+
+ private void collectFields(SourceMethod sm, List<IField> fields) throws ModelException {
+ IModelElement[] children = sm.getChildren();
+ for (IModelElement me : children) {
+ if (me instanceof SourceMethod) {
+ collectFields((SourceMethod) me, fields);
+ } else if (me instanceof SourceField) {
+ fields.add((IField) me);
+ }
+ }
+ }
+
+ private void collectBlocks(SourceMethod sm, List<IMethod> blocks) throws ModelException {
+ blocks.add(sm);
+ IModelElement[] children = sm.getChildren();
+ for (IModelElement me : children) {
+ if (me instanceof SourceMethod) {
+ collectBlocks((SourceMethod) me, blocks);
+ }
+ }
+ }
+
+ private String removeLowerCase(String string) {
+ String replaceAll = string.replaceAll("\\p{Lower}|[.]", "");
+ return replaceAll;
+ }
+
+ @SuppressWarnings({ "unchecked" })
+ private void doCompletionOnEmptyStatement(IModuleSource cu, int position, int i) {
+ int kind = CompletionProposal.LOCAL_VARIABLE_REF;
+ // doCompletionOnKeyword(position, i, "");
+ if (!super.requestor.isIgnored(kind)) {
+ suggestFields(cu);
+ }
+ }
+
+ /**
+ * @param cu
+ */
+ private void suggestFields(IModuleSource cu) {
+ // try {
+ // if (cu != null) {
+ // IField[] fieldsArray = sourceModule.getFields();
+ // for (IField field : fieldsArray) {
+ // int relevance = RelevanceConstants.R_EXACT_EXPECTED_TYPE;
+ // // accept result
+ // super.noProposal = false;
+ // int kind = CompletionProposal.LOCAL_VARIABLE_REF;
+ // if (!super.requestor.isIgnored(kind)) {
+ // CompletionProposal proposal = super.createProposal(kind, actualCompletionPosition);
+ // proposal.setRelevance(relevance);
+ // proposal.setModelElement(field);
+ // proposal.setName(field.getElementName());
+ // proposal.setCompletion(field.getElementName());
+ // proposal.setReplaceRange(this.startPosition - this.offset, this.endPosition
+ // - this.offset);
+ // try {
+ // proposal.setFlags(field.getFlags());
+ // } catch (ModelException e) {
+ // }
+ // this.requestor.accept(proposal);
+ // if (DEBUG) {
+ // this.printDebug(proposal);
+ // }
+ // }
+ // }
+ // }
+ // } catch (ModelException e) {
+ // }
+ }
+
+ /**
+ * @param complString
+ * @param names
+ * @param field
+ */
+ private void addProposal(String complString, List names, IField field) {
+ char[] fieldName = field.getElementName().toCharArray();
+ char[] complFragment = complString.toCharArray();
+
+ if (CharOperation.camelCaseMatch(complString.toCharArray(), fieldName)
+ || match(complString, field.getElementName())) {
+
+ int relevance = RelevanceConstants.R_DEFAULT + 1;
+ relevance += computeRelevanceForCaseMatching(complFragment, field.getElementName());
+
+ // accept result
+ super.noProposal = false;
+ int kind = CompletionProposal.LOCAL_VARIABLE_REF;
+ if (!super.requestor.isIgnored(kind)) {
+ CompletionProposal proposal = super.createProposal(kind, actualCompletionPosition);
+ proposal.setRelevance(relevance);
+ proposal.setModelElement(field);
+ proposal.setName(field.getElementName());
+ proposal.setCompletion(field.getElementName());
+ proposal.setReplaceRange(this.startPosition - this.offset, this.endPosition - this.offset);
+ try {
+ proposal.setFlags(field.getFlags());
+ } catch (ModelException e) {
+ }
+ this.requestor.accept(proposal);
+ if (DEBUG) {
+ this.printDebug(proposal);
+ }
+ }
+ }
+ }
+
+ private void addProposal(String complString, String string, int kind) {
+ addProposal(complString, string, string, kind);
+ }
+
+ private void addProposal(String complString, String string, String name, int kind) {
+ char[] fieldName = string.toCharArray();
+ char[] complFragment = complString.toCharArray();
+
+ char[] pattern = null;
+ if (complString.length() > 0) {
+ pattern = complString.toCharArray();
+ }
+ if (CharOperation.camelCaseMatch(pattern, fieldName) || match(complString, string)) {
+
+ int relevance = RelevanceConstants.R_DEFAULT + 1;
+ relevance += computeRelevanceForCaseMatching(complFragment, string);
+
+ // accept result
+ super.noProposal = false;
+ if (!super.requestor.isIgnored(kind)) {
+ CompletionProposal proposal = super.createProposal(kind, actualCompletionPosition);
+ proposal.setRelevance(relevance);
+ proposal.setName(name);
+ proposal.setCompletion(string);
+ proposal.setReplaceRange(this.startPosition - this.offset, this.endPosition - this.offset);
+ // try {
+ // proposal.setFlags(field.getFlags());
+ // } catch (ModelException e) {
+ // }
+ this.requestor.accept(proposal);
+ if (DEBUG) {
+ this.printDebug(proposal);
+ }
+ }
+ }
+ }
+
+ private void doCompletionOnKeyword(int position, int pos, String startPart) {
+ List<String> keywords = new ArrayList<String>();
+ for (int i = 0; i < IRutaKeywords.END_INDEX; i++) {
+ keywords.addAll(Arrays.asList(RutaKeywordsManager.getKeywords(i)));
+ }
+ char[][] keyWordsArray = new char[keywords.size()][];
+ for (int a = 0; a < keywords.size(); a++) {
+ keyWordsArray[a] = keywords.get(a).toCharArray();
+ }
+ findKeywords(startPart.toCharArray(), keywords.toArray(new String[0]), true);
+ }
+
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaCompletionParser.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaCompletionParser.java?rev=1477113&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaCompletionParser.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaCompletionParser.java Mon Apr 29 14:50:56 2013
@@ -0,0 +1,396 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.ruta.ide.core.extensions.ICompletionExtension;
+import org.apache.uima.ruta.ide.core.parser.RutaParseUtils;
+import org.apache.uima.ruta.ide.parser.ast.RutaStatement;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.ASTVisitor;
+import org.eclipse.dltk.ast.declarations.MethodDeclaration;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.declarations.TypeDeclaration;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.ast.references.SimpleReference;
+import org.eclipse.dltk.ast.statements.Block;
+import org.eclipse.dltk.ast.statements.Statement;
+import org.eclipse.dltk.codeassist.complete.CompletionNodeFound;
+import org.eclipse.dltk.compiler.env.IModuleSource;
+
+public class RutaCompletionParser extends RutaAssistParser {
+ private static class RutaEmptyCompleteStatement extends RutaStatement {
+ public RutaEmptyCompleteStatement(List expressions) {
+ super(expressions);
+ }
+ }
+
+ private ICompletionExtension[] extensions;
+
+ public RutaCompletionParser(ICompletionExtension[] extensions) {
+ this.extensions = extensions;
+ }
+
+ /**
+ * Called then element could not be found.
+ */
+ public void handleNotInElement(ASTNode node, int position) {
+ if (node != null && node.sourceStart() <= position && position <= node.sourceEnd()) {
+ // this is empty module case
+ if (node instanceof ModuleDeclaration) {
+ ModuleDeclaration unit = (ModuleDeclaration) node;
+ List exprs = new ArrayList();
+ exprs.add(new SimpleReference(position, position, ""));
+ RutaEmptyCompleteStatement statement = new RutaEmptyCompleteStatement(exprs);
+ unit.addStatement(statement);
+ this.parseBlockStatements(statement, unit, position);
+ } else if (node instanceof MethodDeclaration) {
+ // empty keyword like completion.
+ MethodDeclaration method = (MethodDeclaration) node;
+ List exprs = new ArrayList();
+ exprs.add(new SimpleReference(position, position, ""));
+ RutaEmptyCompleteStatement statement = new RutaEmptyCompleteStatement(exprs);
+ method.getStatements().add(statement);
+ this.parseBlockStatements(statement, method, position);
+ } else if (node instanceof TypeDeclaration) {
+ // empty keyword like completion.
+ TypeDeclaration type = (TypeDeclaration) node;
+ List exprs = new ArrayList();
+ exprs.add(new SimpleReference(position, position, ""));
+ RutaEmptyCompleteStatement statement = new RutaEmptyCompleteStatement(exprs);
+ type.getStatements().add(statement);
+ // this.assistNodeParent = node;
+ this.parseBlockStatements(statement, type, position);
+ // } else if (node instanceof RutaExecuteExpression) {
+ // // empty keyword like completion.
+ // List exprs = new ArrayList();
+ // exprs.add(new SimpleReference(position, position, ""));
+ // RutaEmptyCompleteStatement statement = new
+ // RutaEmptyCompleteStatement(
+ // exprs);
+ // this.parseBlockStatements(statement, node, position);
+ }
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.dltk.codeassist.IAssistParser#parseBlockStatements(org.eclipse.dltk.ast.ASTNode,
+ * org.eclipse.dltk.ast.ASTNode, int)
+ */
+ public void parseBlockStatements(ASTNode node, ASTNode inNode, int position) {
+ // if (node instanceof RutaStatement) {
+ // RutaStatement statement = (RutaStatement) node;
+ // List expressions = statement.getExpressions();
+ // int len = expressions.size();
+ // boolean first = false;
+ // String completionToken = null;
+ // ASTNode completionNode = null;
+ // for (int i = 0; i < len; ++i) {
+ // ASTNode n = (ASTNode) expressions.get(i);
+ // if (n.sourceStart() <= position && n.sourceEnd() >= position
+ // || (node instanceof RutaEmptyCompleteStatement)) {
+ // if (i == 0) {
+ // first = true;
+ // }
+ // completionNode = n;
+ // }
+ // }
+ // if (completionNode == null) {
+ // // TODO: Add inner completion here.
+ // if (len > 0) {
+ // // ASTNode firstNode = (ASTNode) expressions.get(0);
+ // // if (position > firstNode.sourceEnd()) {
+ // // // This could be variable completion.
+ // // boolean provideDollar =
+ // // !checkVariableWithoutDollarCompletion(
+ // // statement, position);
+ // // this.assistNodeParent = inNode;
+ // // SimpleReference ref = new SimpleReference(position,
+ // // position, "");
+ // // ASTNode nde = new CompletionOnVariable("", ref, node,
+ // // inNode, true, provideDollar);
+ // // throw new CompletionNodeFound(nde, null);
+ // // }
+ // String[] keywords = checkKeywords(completionToken, MODULE);
+ // ASTNode nde = new CompletionOnKeywordArgumentOrFunctionArgument("",
+ // (RutaStatement) node, keywords, position);
+ // this.assistNodeParent = inNode;
+ // throw new CompletionNodeFound(nde, null/* ((TypeDeclaration)inNode).scope */);
+ //
+ // } else {
+ // completionToken = "";
+ // }
+ // } else if (completionNode instanceof SimpleReference) {
+ // int maxLen = position - completionNode.sourceStart();
+ // completionToken = ((SimpleReference) completionNode).getName();
+ // // We need to cut some sumbols if node is begger then position.
+ // if (completionToken.length() > maxLen && maxLen > 0) {
+ // completionToken = completionToken.substring(0, maxLen);
+ // }
+ // } /*
+ // * else if (completionNode instanceof RutaBlockExpression) { RutaBlockExpression
+ // * block = (RutaBlockExpression) completionNode;
+ // *
+ // * List s = block.parseBlock(); if (s != null) { int slen = s.size(); for (int u = 0; u <
+ // * slen; ++u) { ASTNode n = (ASTNode) s.get(u); n.setStart(n.sourceStart() -
+ // * block.sourceStart()); n.setEnd(n.sourceEnd() - block.sourceStart());
+ // * RutaASTUtil.extendStatement(n, block.getBlock()); n.setStart(n.sourceStart() +
+ // * block.sourceStart()); n.setEnd(n.sourceEnd() + block.sourceStart()); if (n != null &&
+ // * n.sourceStart() <= position && n.sourceEnd() >= position) { parseBlockStatements(n,
+ // * inNode, position); } } } handleNotInElement(inNode, position); }
+ // */
+ // if (completionNode instanceof StringLiteral) {
+ // int maxLen = position - completionNode.sourceStart();
+ // int pos = maxLen;
+ // SimpleReference tok = RutaParseUtils.extractVariableFromString(
+ // (StringLiteral) completionNode, pos);
+ // if (tok != null) {
+ // this.assistNodeParent = inNode;
+ // ASTNode nde = new CompletionOnVariable(tok.getName(), tok, node, inNode, false);
+ // throw new CompletionNodeFound(nde, null);
+ // } else {
+ // this.assistNodeParent = inNode;
+ // SimpleReference ref = new SimpleReference(position, position, "");
+ // ASTNode nde = new CompletionOnVariable("", ref, node, inNode, true);
+ // throw new CompletionNodeFound(nde, null);
+ // }
+ // }
+ // // if (completionNode instanceof RutaExecuteExpression) {
+ // // RutaExecuteExpression expr = (RutaExecuteExpression)
+ // // completionNode;
+ // // List exprs = expr.parseExpression();
+ // // for (int i = 0; i < exprs.size(); ++i) {
+ // // ASTNode n = (ASTNode) exprs.get(i);
+ // // if (n.sourceStart() <= position
+ // // && n.sourceEnd() >= position) {
+ // // parseBlockStatements(n, expr, position);
+ // // }
+ // // }
+ // // handleNotInElement(expr, position);
+ // // }
+ // if (completionToken != null && completionToken.startsWith("$")) {
+ // // Argument name completion...
+ // this.assistNodeParent = inNode;
+ // ASTNode nde = new CompletionOnVariable(completionToken, completionNode, node, inNode, false);
+ // throw new CompletionNodeFound(nde, null);
+ // } else {
+ // // This is keyword or function completion.
+ // if (inNode instanceof ModuleDeclaration && completionNode != null && first) {
+ // String[] keywords = checkKeywords(completionToken, MODULE);
+ // ASTNode nde = new CompletionOnKeywordOrFunction(completionToken, completionNode, node,
+ // keywords);
+ // this.assistNodeParent = inNode;
+ // throw new CompletionNodeFound(nde, ((ModuleDeclaration) inNode).scope);
+ // } else if (inNode instanceof MethodDeclaration && completionNode != null && first) {
+ // String[] keywords = checkKeywords(completionToken, FUNCTION);
+ // ASTNode nde = new CompletionOnKeywordOrFunction(completionToken, completionNode, node,
+ // keywords);
+ // this.assistNodeParent = inNode;
+ // throw new CompletionNodeFound(nde, ((MethodDeclaration) inNode).scope);
+ // } else if (inNode instanceof TypeDeclaration && completionNode != null && first) {
+ // String[] keywords = checkKeywords(completionToken, NAMESPACE);
+ // ASTNode nde = new CompletionOnKeywordOrFunction(completionToken, completionNode, node,
+ // keywords);
+ // this.assistNodeParent = inNode;
+ // throw new CompletionNodeFound(nde, null/* ((TypeDeclaration)inNode).scope */);
+ // // } else if (inNode instanceof RutaExecuteExpression
+ // // && completionNode != null && first) {
+ // // String[] keywords = checkKeywords(completionToken,
+ // // EXEC_EXPRESSION);
+ // // ASTNode nde = new CompletionOnKeywordOrFunction(
+ // // completionToken, completionNode, node, keywords);
+ // // this.assistNodeParent = inNode;
+ // // throw new CompletionNodeFound(nde, null/*
+ // // ((TypeDeclaration)inNode).scope */);
+ // } else {
+ // if (completionNode != null) {
+ // String[] keywords = checkKeywords(completionToken, MODULE);
+ //
+ // ASTNode nde = new CompletionOnKeywordArgumentOrFunctionArgument(completionToken,
+ // completionNode, (RutaStatement) node, keywords);
+ // this.assistNodeParent = inNode;
+ // throw new CompletionNodeFound(nde, null/* ((TypeDeclaration)inNode).scope */);
+ // } else {
+ // String[] keywords = checkKeywords(completionToken, MODULE);
+ // if (completionToken == null) {
+ // completionToken = "";
+ // }
+ // ASTNode nde = new CompletionOnKeywordArgumentOrFunctionArgument(completionToken,
+ // (RutaStatement) node, keywords, position);
+ // this.assistNodeParent = inNode;
+ // throw new CompletionNodeFound(nde, null/* ((TypeDeclaration)inNode).scope */);
+ // }
+ // }
+ // }
+ // // if (checkVariableWithoutDollarCompletion(statement, position)
+ // // && completionToken != null) {
+ // // this.assistNodeParent = inNode;
+ // // SimpleReference ref = new SimpleReference(completionNode
+ // // .sourceStart(), completionNode.sourceEnd(),
+ // // completionToken);
+ // // ASTNode nde = new CompletionOnVariable(completionToken, ref,
+ // // node, inNode, true);
+ // // throw new CompletionNodeFound(nde, null);
+ // // }
+ //
+ // } else if (node instanceof MethodDeclaration) {
+ // MethodDeclaration method = (MethodDeclaration) node;
+ // List statements = method.getStatements();
+ // boolean inStatement = false;
+ // if (statements != null) {
+ // int length = statements.size();
+ // for (int i = 0; i < length; i++) {
+ // ASTNode nde = (ASTNode) statements.get(i);
+ // if (nde.sourceStart() <= position && nde.sourceEnd() >= position) {
+ // inStatement = true;
+ // parseBlockStatements(nde, method, position);
+ // }
+ // }
+ // }
+ // if (!inStatement) {
+ // this.handleNotInElement(method, position);
+ // }
+ // } else {
+ // visitElements(node, position);
+ // }
+ }
+
+ public class CompletionVisitor extends ASTVisitor {
+ protected int position;
+
+ protected ModuleDeclaration module;
+
+ public CompletionVisitor(int position, ModuleDeclaration module) {
+ this.position = position;
+ this.module = module;
+ }
+
+ @Override
+ public boolean visit(Statement s) throws Exception {
+ // if (s.sourceStart() <= position && s.sourceEnd() >= position) {
+ // for (int i = 0; i < extensions.length; i++) {
+ // extensions[i].visit(s, RutaCompletionParser.this,
+ // position);
+ // }
+ // if (s instanceof RutaStatement) {
+ // ASTNode inNode = RutaParseUtil.getScopeParent(module, s);
+ // RutaCompletionParser.this.parseBlockStatements(s, inNode,
+ // position);
+ // }
+ // if (s instanceof RutaPackageDeclaration) {
+ // RutaPackageDeclaration decl =
+ // (RutaPackageDeclaration) s;
+ // if (decl.getNameStart() <= position
+ // && position <= decl.getNameEnd()) {
+ // ASTNode inNode = RutaParseUtil.getScopeParent(module, s);
+ // assistNodeParent = inNode;
+ // throw new CompletionNodeFound(decl, null/*
+ // ((TypeDeclaration)inNode).scope */);
+ // }
+ // }
+ // }
+ return super.visit(s);
+ }
+
+ @Override
+ public boolean visit(Expression s) throws Exception {
+ if (s.sourceStart() <= position && s.sourceEnd() >= position) {
+ for (int i = 0; i < extensions.length; i++) {
+ extensions[i].visit(s, RutaCompletionParser.this, position);
+ }
+ }
+ return super.visit(s);
+ }
+
+ @Override
+ public boolean endvisit(Expression s) throws Exception {
+ if (s instanceof Block && s.sourceStart() <= position && s.sourceEnd() >= position) {
+ // We are in block, and no in node completion are done.
+ String[] keywords = checkKeywords("", MODULE);
+ ASTNode inNode = RutaParseUtils.getScopeParent(module, s);
+ ASTNode nde = new CompletionOnKeywordOrFunction("", inNode, s, keywords);
+ assistNodeParent = inNode;
+ throw new CompletionNodeFound(nde, null);
+ }
+ return super.endvisit(s);
+ }
+ };
+
+ protected CompletionVisitor createCompletionVisitor(int position) {
+ return new CompletionVisitor(position, this.getModule());
+ }
+
+ private void visitElements(ASTNode node, int position) {
+ if (!(node instanceof RutaStatement)) {
+ CompletionVisitor visitor = createCompletionVisitor(position);
+ try {
+ node.traverse(visitor);
+ } catch (CompletionNodeFound e) {
+ throw e;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private boolean checkVariableWithoutDollarCompletion(RutaStatement statement, int position) {
+ // TODO: Add more compecated check.
+ Expression e = statement.getAt(0);
+ if (e instanceof SimpleReference) {
+ SimpleReference ref = (SimpleReference) e;
+ String name = ref.getName();
+ if (name.equals("set")) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public String[] checkKeywords(String completionToken, int type) {
+ // String[] keywords = RutaKeywordsManager.getKeywords(type);
+ // // TODO: Possible require cases.
+ // if (type == MODULE || type == FUNCTION || type == NAMESPACE
+ // || type == EXEC_EXPRESSION) {
+ // // Suppose we can handle all keywords.
+ // String[] kw = new String[keywords.length];
+ // for (int i = 0; i < keywords.length; ++i) {
+ // kw[i] = keywords[i];
+ // }
+ // return kw;
+ // }
+ return null;
+ }
+
+ public void setAssistNodeParent(ASTNode prevParent) {
+ this.assistNodeParent = prevParent;
+ }
+
+ public ModuleDeclaration parse(IModuleSource sourceModule) {
+ ModuleDeclaration module = (ModuleDeclaration) this.parser.parse(sourceModule, null);
+ module.rebuild();
+ return module;
+ }
+
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaModelElementView.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaModelElementView.java?rev=1477113&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaModelElementView.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaModelElementView.java Mon Apr 29 14:50:56 2013
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+public interface RutaModelElementView {
+
+ void setModelElement(Object element);
+
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaReferenceDeclarationVisitor.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaReferenceDeclarationVisitor.java?rev=1477113&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaReferenceDeclarationVisitor.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaReferenceDeclarationVisitor.java Mon Apr 29 14:50:56 2013
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import org.apache.uima.ruta.ide.parser.ast.RutaDeclarationsStatement;
+import org.apache.uima.ruta.ide.parser.ast.RutaVariableReference;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.ASTVisitor;
+import org.eclipse.dltk.ast.declarations.FieldDeclaration;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.ast.statements.Statement;
+
+public class RutaReferenceDeclarationVisitor extends ASTVisitor {
+ private ASTNode result = null;
+
+ private int start = 0;
+
+ public RutaReferenceDeclarationVisitor(int start) {
+ this.start = start;
+ }
+
+ @Override
+ public boolean visitGeneral(ASTNode node) throws Exception {
+ if (node.sourceStart() <= start && start <= node.sourceEnd()) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean visit(Statement s) throws Exception {
+ if (s instanceof RutaDeclarationsStatement || s instanceof FieldDeclaration) {
+ return true && super.visit(s);
+ }
+ return false;
+ }
+
+ @Override
+ public boolean visit(Expression s) throws Exception {
+ if (s instanceof RutaVariableReference) {
+ result = s;
+ }
+ return super.visit(s);
+ }
+
+ public ASTNode getResult() {
+ return result;
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaReferenceVisitor.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaReferenceVisitor.java?rev=1477113&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaReferenceVisitor.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaReferenceVisitor.java Mon Apr 29 14:50:56 2013
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import org.apache.uima.ruta.ide.parser.ast.ComponentDeclaration;
+import org.apache.uima.ruta.ide.parser.ast.ComponentReference;
+import org.apache.uima.ruta.ide.parser.ast.RutaAction;
+import org.apache.uima.ruta.ide.parser.ast.RutaCondition;
+import org.apache.uima.ruta.ide.parser.ast.RutaExpression;
+import org.apache.uima.ruta.ide.parser.ast.RutaVariableReference;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.ASTVisitor;
+import org.eclipse.dltk.ast.expressions.Expression;
+
+
+public class RutaReferenceVisitor extends ASTVisitor {
+ private ASTNode result = null;
+
+ private int start = 0;
+
+ public RutaReferenceVisitor(int start) {
+ this.start = start;
+ }
+
+ @Override
+ public boolean visitGeneral(ASTNode node) throws Exception {
+ if (result == null && node.sourceStart() <= start && start <= node.sourceEnd()) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean visit(Expression s) throws Exception {
+ if (result != null) {
+ return false;
+ }
+ if (s instanceof RutaVariableReference || s instanceof ComponentDeclaration
+ || s instanceof ComponentReference) {
+ result = s;
+ } else if (s instanceof RutaAction && ((RutaAction) s).getNameStart() <= start
+ && start <= ((RutaAction) s).getNameEnd()) {
+ result = s;
+ } else if (s instanceof RutaAction && ((RutaAction) s).getNameStart()-1 == start
+ && start == ((RutaAction) s).getNameEnd()-1) {
+ result = s;
+ } else if (s instanceof RutaCondition
+ && ((RutaCondition) s).getNameStart() <= start
+ && start <= ((RutaCondition) s).getNameEnd()) {
+ result = s;
+ } else if (s instanceof RutaCondition
+ && ((RutaCondition) s).getNameStart()-1 == start
+ && start == ((RutaCondition) s).getNameEnd()-1) {
+ result = s;
+ } else if(s instanceof RutaExpression && s.sourceStart() == start && s.sourceEnd() == start) {
+ result = s;
+ }
+ return super.visit(s);
+ }
+
+ public ASTNode getResult() {
+ return result;
+ }
+}