List of usage examples for org.eclipse.jdt.core IJavaElement getElementName
String getElementName();
From source file:org.codehaus.groovy.eclipse.astviews.ASTView.java
License:Apache License
private void hookGroovy() { partListener = new IPartListener() { public void partActivated(IWorkbenchPart part) { }/* w w w .j av a 2 s . c o m*/ public void partBroughtToTop(IWorkbenchPart part) { try { if (part instanceof IEditorPart) { IFile file = (IFile) ((IEditorPart) part).getEditorInput().getAdapter(IFile.class); if (file != null && ContentTypeUtils.isGroovyLikeFileName(file.getName())) { ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file); if (unit instanceof GroovyCompilationUnit) { if (editor != part) { editor = (IEditorPart) part; Object[] treePaths = viewer.getExpandedElements(); viewer.setInput(((GroovyCompilationUnit) unit).getModuleNode()); viewer.setExpandedElements(treePaths); } else { // nothing to do! } return; } } } } catch (Exception e) { GroovyCore.logException("Error updating AST Viewer", e); //$NON-NLS-1$ } editor = null; // This is a guard - the content provider should not be null, // but sometimes this happens when the // part is disposed of for various reasons (unhandled exceptions // AFAIK). Without this guard, // error message popups continue until Eclipse if forcefully // killed. if (viewer.getContentProvider() != null) { viewer.setInput(null); } } public void partClosed(IWorkbenchPart part) { } public void partDeactivated(IWorkbenchPart part) { } public void partOpened(IWorkbenchPart part) { } }; getSite().getPage().addPartListener(partListener); // Warm the listener up. if (getSite().getPage().getActiveEditor() instanceof GroovyEditor) { partListener.partBroughtToTop(getSite().getPage().getActiveEditor()); } listener = new IElementChangedListener() { public void elementChanged(ElementChangedEvent event) { // The editor is currently not a GroovyEditor, so // there is not // ASTView to refresh. if (editor == null) { return; } IJavaElementDelta delta = event.getDelta(); IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class); final GroovyCompilationUnit unit = (GroovyCompilationUnit) JavaCore.createCompilationUnitFrom(file); // determine if the delta contains the ICompUnit under question if (isUnitInDelta(delta, unit)) { Display.getDefault().asyncExec(new Runnable() { public void run() { Object[] treePaths = viewer.getExpandedElements(); viewer.setInput(unit.getModuleNode()); viewer.setExpandedElements(treePaths); } }); } } private boolean isUnitInDelta(IJavaElementDelta delta, GroovyCompilationUnit unit) { IJavaElement elt = delta.getElement(); if (elt.getElementType() == IJavaElement.COMPILATION_UNIT) { // comparing with a compilation unit // if test fails, no need to go further if (elt.getElementName().equals(unit.getElementName())) { return true; } else { return false; } } ICompilationUnit candidateUnit = (ICompilationUnit) elt.getAncestor(IJavaElement.COMPILATION_UNIT); if (candidateUnit != null) { // now if test fails, no need to go further if (candidateUnit.getElementName().equals(unit.getElementName())) { return true; } else { return false; } } // delta is a potential ancestor of this compilationUnit IJavaElementDelta[] deltas = delta.getAffectedChildren(); if (deltas != null) { for (IJavaElementDelta delta2 : deltas) { if (isUnitInDelta(delta2, unit)) { return true; } } } return false; } }; JavaCore.addElementChangedListener(listener, ElementChangedEvent.POST_RECONCILE); }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.CompletionTestCase.java
License:Open Source License
protected void assertExtendedContextElements(GroovyExtendedCompletionContext context, String signature, String... expectedNames) { IJavaElement[] visibleElements = context.getVisibleElements(signature); assertEquals("Incorrect number of visible elements\nexpected: " + Arrays.toString(expectedNames) + "\nfound: " + elementsToNames(visibleElements), expectedNames.length, visibleElements.length); for (String name : expectedNames) { boolean found = false; for (IJavaElement element : visibleElements) { if (element.getElementName().equals(name)) { found = true;//from w w w .ja v a2 s. c o m break; } } if (!found) { fail("couldn't find element named " + name + " in " + elementsToNames(visibleElements)); } } }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java
License:Apache License
public void testExtendedContextInScript1() throws Exception { String contents = "def x = 9\ndef y = ''\ndef z = []\nint a\nString b\nList c\nz"; GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents), contents.lastIndexOf('z') + 1); IJavaElement enclosing = context.getEnclosingElement(); assertEquals("run", enclosing.getElementName()); assertExtendedContextElements(context, INTEGER_SIG, "x", "a"); assertExtendedContextElements(context, STRING_SIG, "y", "b"); assertExtendedContextElements(context, LIST_SIG, "z", "c"); }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java
License:Apache License
public void testExtendedContextInScript2() throws Exception { String contents = "int[] x\nString[] y\nList[] z\nint a\nString b\nList c\nz"; GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents), contents.lastIndexOf('z') + 1); IJavaElement enclosing = context.getEnclosingElement(); assertEquals("run", enclosing.getElementName()); assertExtendedContextElements(context, INTEGER_ARR_SIG, "x"); assertExtendedContextElements(context, STRING_ARR_SIG, "y"); assertExtendedContextElements(context, LIST_ARR_SIG, "z"); }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java
License:Apache License
public void testExtendedContextInScript3() throws Exception { String contents = "class Sub extends Super{ }\nclass Super { }\ndef x = new Super()\ndef y = new Sub()\ndef z\nz"; GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents), contents.lastIndexOf('z') + 1); IJavaElement enclosing = context.getEnclosingElement(); assertEquals("run", enclosing.getElementName()); assertExtendedContextElements(context, "LSuper;", "x", "y"); assertExtendedContextElements(context, "LSub;", "y"); }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java
License:Apache License
public void testExtendedContextInScript4() throws Exception { String contents = "class Sub extends Super{ }\nclass Super { }\ndef x = new Super[0]\ndef y = new Sub[0]\ndef z\nz"; GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents), contents.lastIndexOf('z') + 1); IJavaElement enclosing = context.getEnclosingElement(); assertEquals("run", enclosing.getElementName()); assertExtendedContextElements(context, "[LSuper;", "x", "y"); assertExtendedContextElements(context, "[LSub;", "y"); assertExtendedContextElements(context, "LSuper;"); assertExtendedContextElements(context, "LSub;"); }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java
License:Apache License
public void testExtendedContextInClass1() throws Exception { String contents = "class Sub extends Super{ }\nclass Super {\n def foo() { \ndef x = new Super[0]\ndef y = new Sub[0]\ndef z\nz } }"; GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents), contents.lastIndexOf('z') + 1); IJavaElement enclosing = context.getEnclosingElement(); assertEquals("foo", enclosing.getElementName()); assertExtendedContextElements(context, "[LSuper;", "x", "y"); assertExtendedContextElements(context, "[LSub;", "y"); assertExtendedContextElements(context, "LSuper;"); assertExtendedContextElements(context, "LSub;"); }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java
License:Apache License
public void testExtendedContextInClass2() throws Exception { String contents = "class Sub extends Super{ }\nclass Super { \nSuper x\nSub y\ndef z\n def foo() { \nz } }"; GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents), contents.lastIndexOf('z') + 1); IJavaElement enclosing = context.getEnclosingElement(); assertEquals("foo", enclosing.getElementName()); assertExtendedContextElements(context, "LSuper;", "x", "y"); assertExtendedContextElements(context, "LSub;", "y"); }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java
License:Apache License
public void testExtendedContextInClass3() throws Exception { String contents = "class Super{ \nSuper a\nSub b\ndef c }\nclass Sub extends Super { \nSuper x\nSub y\ndef z\n def foo() { \nSuper z } }"; GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents), contents.lastIndexOf('z') + 1); IJavaElement enclosing = context.getEnclosingElement(); assertEquals("foo", enclosing.getElementName()); assertExtendedContextElements(context, "LSub;", "y", "b"); assertExtendedContextElements(context, "LSuper;", "x", "y", "a", "b", "z"); }
From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java
License:Apache License
public void testExtendedContextWithGenerics() throws Exception { String contents = "Map<Integer, Class> x\n" + "HashMap<Class, Integer> y\n" + "z"; GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents), contents.lastIndexOf('z') + 1); IJavaElement enclosing = context.getEnclosingElement(); assertEquals("run", enclosing.getElementName()); assertExtendedContextElements(context, "Ljava.util.Map;", "y", "x"); assertExtendedContextElements(context, "Ljava.util.HashMap;", "y"); }