Search in sources :

Example 1 with GroovyAST

use of eu.esdihumboldt.hale.ui.util.groovy.ast.GroovyAST in project hale by halestudio.

the class GroovyASTCompletionProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int offset) {
    if (proposals == null || proposals.isEmpty()) {
        return null;
    }
    if (viewer instanceof CompilingSourceViewer<?>) {
        CompilingSourceViewer<?> csv = (CompilingSourceViewer<?>) viewer;
        try {
            Object compiled = csv.getCompiled().get();
            if (compiled instanceof GroovyAST) {
                // the Groovy AST
                GroovyAST ast = (GroovyAST) compiled;
                // the line (0-based)
                int line = csv.getDocument().getLineOfOffset(offset);
                // the line column (0-based)
                int column = offset - csv.getDocument().getLineOffset(line);
                // locations in Groovy AST are 1-based
                line++;
                column++;
                List<ICompletionProposal> list = new ArrayList<>();
                // add proposals of individual computers
                for (GroovyCompletionProposals proposal : proposals) {
                    Iterable<? extends ICompletionProposal> computed = proposal.computeProposals(viewer, ast, line, column, offset);
                    if (computed != null) {
                        Iterables.addAll(list, computed);
                    }
                }
                return list.toArray(new ICompletionProposal[list.size()]);
            }
        } catch (Exception e) {
            log.warn("Failed to get AST to compute completion proposals", e);
        }
    }
    return null;
}
Also used : GroovyAST(eu.esdihumboldt.hale.ui.util.groovy.ast.GroovyAST) CompilingSourceViewer(eu.esdihumboldt.hale.ui.util.source.CompilingSourceViewer) GroovyCompletionProposals(eu.esdihumboldt.hale.ui.util.groovy.GroovyCompletionProposals) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList)

Example 2 with GroovyAST

use of eu.esdihumboldt.hale.ui.util.groovy.ast.GroovyAST in project hale by halestudio.

the class GroovyASTTray method createContents.

@Override
protected Control createContents(Composite parent) {
    Composite page = new Composite(parent, SWT.NONE);
    GridLayoutFactory.fillDefaults().numColumns(1).applyTo(page);
    final ASTViewer viewer = new ASTViewer(page, groovyViewer);
    GridDataFactory.fillDefaults().grab(true, true).hint(400, SWT.DEFAULT).applyTo(viewer.getControl());
    if (groovyViewer != null) {
        // current AST
        try {
            GroovyAST ast = groovyViewer.getCompiled().get();
            if (ast != null) {
                viewer.setInput(ast.getNodes());
                viewer.getTreeViewer().expandAll();
            } else {
                viewer.setInput(null);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // listen to AST changes
        final IPropertyChangeListener listener = new IPropertyChangeListener() {

            @Override
            public void propertyChange(final PropertyChangeEvent event) {
                if (CompilingSourceViewer.PROPERTY_COMPILED.equals(event.getProperty())) {
                    PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            if (event.getNewValue() instanceof GroovyAST) {
                                viewer.setInput(((GroovyAST) event.getNewValue()).getNodes());
                                viewer.getTreeViewer().expandAll();
                            } else {
                                viewer.setInput(null);
                            }
                        }
                    });
                }
            }
        };
        groovyViewer.addPropertyChangeListener(listener);
        // ensure listener is removed
        page.addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(DisposeEvent e) {
                groovyViewer.removePropertyChangeListener(listener);
            }
        });
    }
    return page;
}
Also used : IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) ASTViewer(eu.esdihumboldt.hale.ui.util.groovy.ast.viewer.ASTViewer) GroovyAST(eu.esdihumboldt.hale.ui.util.groovy.ast.GroovyAST) DisposeListener(org.eclipse.swt.events.DisposeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) Composite(org.eclipse.swt.widgets.Composite) DisposeEvent(org.eclipse.swt.events.DisposeEvent)

Aggregations

GroovyAST (eu.esdihumboldt.hale.ui.util.groovy.ast.GroovyAST)2 GroovyCompletionProposals (eu.esdihumboldt.hale.ui.util.groovy.GroovyCompletionProposals)1 ASTViewer (eu.esdihumboldt.hale.ui.util.groovy.ast.viewer.ASTViewer)1 CompilingSourceViewer (eu.esdihumboldt.hale.ui.util.source.CompilingSourceViewer)1 ArrayList (java.util.ArrayList)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 IPropertyChangeListener (org.eclipse.jface.util.IPropertyChangeListener)1 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 DisposeListener (org.eclipse.swt.events.DisposeListener)1 Composite (org.eclipse.swt.widgets.Composite)1