Skip to content

Commit

Permalink
Merge pull request #27 from gokcekrky/contentAssist
Browse files Browse the repository at this point in the history
Pull request for Content assist (issue #2)
  • Loading branch information
kruegers authored Sep 18, 2019
2 parents 4db14df + a256006 commit 0948942
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 1 deletion.
6 changes: 5 additions & 1 deletion de.darmstadt.tu.crossing.CryptSL.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ Require-Bundle: de.darmstadt.tu.crossing.CryptSL,
org.eclipse.xtext.xbase.lib;bundle-version="2.13.0",
org.eclipse.xtext.common.types.ui,
org.eclipse.xtext.xbase.ui,
org.eclipse.jdt.debug.ui
org.eclipse.jdt.debug.ui,
org.eclipse.core.runtime,
org.eclipse.core.resources,
org.eclipse.ui.forms,
org.eclipse.jdt.core
Import-Package: org.apache.log4j
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: de.darmstadt.tu.crossing.CryptSL.ui.internal,
Expand Down
6 changes: 6 additions & 0 deletions de.darmstadt.tu.crossing.CryptSL.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,18 @@
class="de.darmstadt.tu.crossing.ui.CryptSLExecutableExtensionFactory:org.eclipse.xtext.ui.compare.InjectableViewerCreator"
extensions="cryptsl">
</viewer>
<contentTypeBinding
contentTypeId="de.darmstadt.tu.crossing.CryptSL.contenttype"
contentViewerId="de.darmstadt.tu.crossing.CryptSL.compare.contentViewers" />
</extension>
<extension point="org.eclipse.compare.contentMergeViewers">
<viewer id="de.darmstadt.tu.crossing.CryptSL.compare.contentMergeViewers"
class="de.darmstadt.tu.crossing.ui.CryptSLExecutableExtensionFactory:org.eclipse.xtext.ui.compare.InjectableViewerCreator"
extensions="cryptsl" label="CryptSL Compare">
</viewer>
<contentTypeBinding
contentTypeId="de.darmstadt.tu.crossing.CryptSL.contenttype"
contentMergeViewerId="de.darmstadt.tu.crossing.CryptSL.compare.contentMergeViewers" />
</extension>
<extension point="org.eclipse.ui.editors.documentProviders">
<provider id="de.darmstadt.tu.crossing.CryptSL.editors.documentProviders"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package de.darmstadt.tu.crossing.ui

import org.eclipse.ui.plugin.AbstractUIPlugin
import org.eclipse.xtext.ui.editor.contentassist.IContentProposalProvider
import de.darmstadt.tu.crossing.ui.contentassist.CryptSLSprayProposalProvider

/**
* Use this class to register components to be used within the Eclipse IDE.
Expand All @@ -13,5 +15,9 @@ class CryptSLUiModule extends AbstractCryptSLUiModule {
new(AbstractUIPlugin plugin) {
super(plugin)
}
override
public bindIContentProposalProvider() {
return CryptSLSprayProposalProvider;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package de.darmstadt.tu.crossing.ui.contentassist;

import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.nodemodel.BidiIterable;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext;
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor;
import com.google.inject.Inject;

import de.darmstadt.tu.crossing.services.CryptSLGrammarAccess;
import de.darmstadt.tu.crossing.ui.utils.ClassPathSolver;
import de.darmstadt.tu.crossing.ui.utils.ClassLoader;

public class CryptSLSprayProposalProvider extends AbstractCryptSLProposalProvider {
@Inject CryptSLGrammarAccess grammarAccess;
private static Keyword keyword;
@Override
public void completeMethod_MethName( EObject model, Assignment assignment,
ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
String className=getSelectedClassName(context);
keyword = grammarAccess.getAggregateAccess().getSemicolonKeyword_1_3();
try {
Class c = Class.forName(className);
addMethodstoProposal(c, context, acceptor);
} catch (ClassNotFoundException e) {
Collection<String> classpath = getProjectClassPath(context);
if(classpath != null) {
Class<?> c = getClassMethods(className,classpath);
addMethodstoProposal(c, context, acceptor);
}else {
System.out.println(className+" is not found");
}
}
}
@Override
public void completeAggregate_Lab( EObject model,
Assignment assignment,
ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
keyword = grammarAccess.getAggregateAccess().getSemicolonKeyword_1_3();
context.getRootNode().getChildren().forEach(item->{
if(item.getText().equals("EVENTS")) {
item.getNextSibling().getLeafNodes().forEach(item2->{
if(item2.getText().equals(":") || item2.getText().equals(":=")) {
String text=null;
while(item2.getPreviousSibling().getText().equals(" ") ) {
text = item2.getPreviousSibling().getText();
item2=(ILeafNode) item2.getPreviousSibling();
}
text=item2.getPreviousSibling().getText();
completeproposal(text,context, acceptor);
}
});
}
});

}
@Override
public void completeAggregateExpression_Value(EObject model, Assignment assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
INode currNode = context.getCurrentNode();
if (currNode.hasPreviousSibling() && currNode.getPreviousSibling().hasPreviousSibling()
&& currNode.getPreviousSibling().getPreviousSibling().getText().equals("CONSTRAINTS")) {
completeAggregateProposal(context, acceptor);
} else {
System.out.println("inconstraint");
super.completeAggregateExpression_Value(model, assignment, context, acceptor);
}
}
@Override
public void completeKeyword(Keyword keyword, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
INode currNode = context.getCurrentNode();
if (currNode.hasPreviousSibling() && currNode.getPreviousSibling().hasPreviousSibling()
&& currNode.getPreviousSibling().getPreviousSibling().getText().equals("CONSTRAINTS")) {
String prevNodeText = currNode.getPreviousSibling().getText();
if (prevNodeText.length() <= 0 || prevNodeText.charAt(prevNodeText.length() - 1) == ';') {
completeAggregateProposal(context, acceptor);
completeConstraintProposal(context, acceptor);
} else {
super.completeKeyword(keyword, context, acceptor);
}
} else {
super.completeKeyword(keyword, context, acceptor);
}
}
public void completeConstraintProposal(ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
Keyword predicate;
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameCallToKeyword_2_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameLengthKeyword_4_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameNeverTypeOfKeyword_0_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameNoCallToKeyword_1_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameNotHardCodedKeyword_3_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getConsPredAccess().getConsPredAlgKeyword_0_0_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getConsPredAccess().getConsPredModeKeyword_0_1_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getConsPredAccess().getConsPredPadKeyword_0_2_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getConsPredAccess().getPartPartKeyword_1_0_0();
completeproposal(predicate.getValue(),context, acceptor);
predicate = (Keyword)grammarAccess.getArrayElementsAccess(). getElElementsKeyword_0_0_0();
completeproposal(predicate.getValue(),context, acceptor);
}

public void completeproposal(String word, ContentAssistContext contentAssistContext,
ICompletionProposalAcceptor acceptor) {
ICompletionProposal proposal = createCompletionProposal(word, word,
getImage(keyword), contentAssistContext);
getPriorityHelper().adjustKeywordPriority(proposal, contentAssistContext.getPrefix());
acceptor.accept(proposal);
}
public void completeAggregateProposal(ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
context.getRootNode().getChildren().forEach(item->{
if(item.getText().equals("OBJECTS")) {
item.getNextSibling().getLeafNodes().forEach(item2->{
if(item2.getText().equals(";") ) {
String text=null;
text=item2.getPreviousSibling().getText().substring(1);
completeproposal(text,context, acceptor);
}
});
}
});
}
public String getSelectedClassName(ContentAssistContext context) {
String classname=null;
BidiIterable<INode> childrens =context.getRootNode().getChildren();
for(INode children:childrens) {
if(children.getText().equals("SPEC")) {
classname=children.getNextSibling().getText().replaceAll("\\s+","");
}
}
return classname;
}
public Collection<String> getProjectClassPath(ContentAssistContext context) {
URI uri = context.getRootModel().eResource().getURI();
if(uri.isPlatform()) {
String platformString = uri.toPlatformString(true);
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();

IFile file = root.getFile(new Path(platformString));
IProject project = file.getProject();
IJavaProject targetProject;
try {
if (project.hasNature(JavaCore.NATURE_ID)) {
targetProject = JavaCore.create(project);
Collection<String> classpath=ClassPathSolver.getClasspath(targetProject);
return classpath;
}

} catch (CoreException e) {
System.out.println(e.getMessage());
}

}
return null;
}
public Class<?> getClassMethods(String classname, Collection<String> classpath) {
Class<?> c;
for(String path:classpath) {
c=ClassLoader.LoadFromJar(classname, path);
if(c != null) {
return c;
}
}
return null;
}
public void addMethodstoProposal(Class<?> claz, ContentAssistContext context, ICompletionProposalAcceptor acceptor ) {
ArrayList<String> methodList= new ArrayList<>();
Method[] methods = claz.getDeclaredMethods();
for (Method method : methods) {
if (Modifier.isPublic(method.getModifiers())){
if(!methodList.contains(method.getName())) {
methodList.add(method.getName()+ "()");
completeproposal(method.getName()+ "()",context, acceptor);
}
}
}
Constructor constructors[] = claz.getDeclaredConstructors();
for(Constructor constructor:constructors) {
completeproposal(constructor.getName().substring(constructor.getName().lastIndexOf(".")+1)+ "()",context, acceptor);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.darmstadt.tu.crossing.ui.utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.*;

public class ClassLoader {

public static Class<?> LoadFromJar(String keyClass,String jarPath) {
URL jarUrl;
try {
jarUrl = new File(jarPath).toURI().toURL();
URLClassLoader loader = new URLClassLoader(new URL[]{jarUrl});
JarFile jar;
try {
jar = new JarFile(jarPath);
Enumeration<JarEntry> e = jar.entries();
boolean found = false;
while (e.hasMoreElements() && !found) {
JarEntry je = (JarEntry) e.nextElement();
if(je.isDirectory() || !je.getName().endsWith(".class")){
continue;
}
String classname =
je.getName().replace('/', '.').substring(0, je.getName().length() - 6).split("\\$")[0];
if (classname.equals(keyClass)){
try {
Class<?> c = loader.loadClass(classname);
//Method [] methods=c.getMethods();
//return methods;
return c;
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println(classname + "is not found");
}
found = true;
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package de.darmstadt.tu.crossing.ui.utils;

import java.util.Collection;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import com.google.common.collect.Sets;
public class ClassPathSolver {
public static Collection<String> getClasspath(final IJavaProject javaProject) {
Collection<String> libraryClassPath = libraryClassPath(javaProject);
return libraryClassPath;
}
private static Collection<String> libraryClassPath(IJavaProject project) {
Collection<String> libraryClassPath = Sets.newHashSet();
IClasspathEntry[] rentries;
try {
rentries = project.getRawClasspath();
for (IClasspathEntry entry : rentries) {
resolveClassPathEntry(entry, libraryClassPath, project);
}

} catch (CoreException e1) {
e1.printStackTrace();
}

return libraryClassPath;
}

private static void resolveClassPathEntry(IClasspathEntry entry, Collection<String> libraryClassPath, IJavaProject project) {
IClasspathEntry[] rentries;
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_SOURCE:
//libraryClassPath.addAll(applicationClassPath(project));
break;
case IClasspathEntry.CPE_PROJECT:
IJavaProject requiredProject = JavaCore.create((IProject) ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath()));
try {
rentries = project.getRawClasspath();
for (IClasspathEntry e : rentries) {
resolveClassPathEntry(e, libraryClassPath, requiredProject);
}
} catch (JavaModelException e1) {
e1.printStackTrace();
}
break;
case IClasspathEntry.CPE_LIBRARY:
IPath path = entry.getPath();
if(path.toString().contains(".m2/repository")) {
libraryClassPath.add(path.toString());
}
break;
case IClasspathEntry.CPE_VARIABLE:
// JRE entry
break;
case IClasspathEntry.CPE_CONTAINER:
try {
IClasspathContainer container = JavaCore.getClasspathContainer(
entry.getPath(), project);
IClasspathEntry[] subEntries = container.getClasspathEntries();
for(IClasspathEntry subEntry : subEntries) {
resolveClassPathEntry(subEntry, libraryClassPath, project);
}
} catch (JavaModelException e) {
e.printStackTrace();
}
break;
}
}
}

0 comments on commit 0948942

Please sign in to comment.