Search in sources :

Example 41 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class ErlangAbstractHandler method getTextSelection.

/**
 * Provide the text selection that is needed to execute the command. Default
 * implementation, extend to Erlang elements selected.
 *
 * @param document
 *            text {@link IDocument}
 * @param selection
 *            selection affected by command (extended by extendSelection)
 * @return new {@link ITextSelection} with all text up to selection
 */
protected ITextSelection getTextSelection(final IDocument document, final ITextSelection selection, final ITextEditor editor) {
    if (editor instanceof ErlangEditor) {
        final AbstractErlangEditor erlangEditor = (AbstractErlangEditor) editor;
        final IErlModule module = erlangEditor.getModule();
        if (module != null) {
            final int offset1 = selection.getOffset();
            final int offset2 = offset1 + selection.getLength();
            try {
                final IErlElement e1 = module.getElementAt(offset1);
                final IErlElement e2 = module.getElementAt(offset2);
                if (e1 instanceof ISourceReference) {
                    final ISourceReference ref1 = (ISourceReference) e1;
                    final ISourceRange r1 = ref1.getSourceRange();
                    final int offset = r1.getOffset();
                    int length = r1.getLength();
                    if (e1 == e2) {
                        final int docLength = document.getLength();
                        if (offset + length > docLength) {
                            length = docLength - offset;
                        }
                        return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, length));
                    } else if (e2 == null) {
                        return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, selection.getLength() + selection.getOffset() - offset));
                    } else if (e2 instanceof ISourceReference) {
                        final ISourceReference ref2 = (ISourceReference) e2;
                        final ISourceRange r2 = ref2.getSourceRange();
                        return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, r2.getOffset() - offset + r2.getLength()));
                    }
                }
            } catch (final ErlModelException e) {
            }
        }
    }
    return ErlangAbstractHandler.extendSelectionToWholeLines(document, selection);
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) ErlModelException(org.erlide.engine.model.ErlModelException) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) IErlModule(org.erlide.engine.model.root.IErlModule) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) AbstractErlangEditor(org.erlide.ui.editors.erl.AbstractErlangEditor) ErlangEditor(org.erlide.ui.editors.erl.ErlangEditor) AbstractErlangEditor(org.erlide.ui.editors.erl.AbstractErlangEditor) ISourceRange(org.erlide.engine.model.erlang.ISourceRange)

Example 42 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class EditorUtility method isOpenInEditor.

/**
 * Tests if a CU is currently shown in an editor
 *
 * @return the IEditorPart if shown, null if element is not open in an
 *         editor
 */
public static IEditorPart isOpenInEditor(final Object inputElement) {
    final Collection<IEditorPart> allErlangEditors = EditorUtility.getAllErlangEditors();
    for (final IEditorPart editorPart : allErlangEditors) {
        if (inputElement instanceof IErlElement) {
            final IErlElement element = (IErlElement) inputElement;
            final IErlModule module = ErlangEngine.getInstance().getModelUtilService().getModule(element);
            final AbstractErlangEditor editor = (AbstractErlangEditor) editorPart;
            if (module.equals(editor.getModule())) {
                return editorPart;
            }
        }
    }
    final IEditorInput input = EditorUtility.getEditorInput(inputElement);
    if (input != null) {
        for (final IEditorPart editorPart : allErlangEditors) {
            if (editorPart.getEditorInput().equals(input)) {
                return editorPart;
            }
        }
    }
    return null;
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IErlModule(org.erlide.engine.model.root.IErlModule) IEditorPart(org.eclipse.ui.IEditorPart) IEditorInput(org.eclipse.ui.IEditorInput) AbstractErlangEditor(org.erlide.ui.editors.erl.AbstractErlangEditor)

Example 43 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class SearchUtil method getSelectionScopeDescription.

public static String getSelectionScopeDescription(final ISelection selection) {
    if (selection instanceof IStructuredSelection) {
        final StringBuilder sb = new StringBuilder();
        final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        int i = 0;
        final List<?> list = structuredSelection.toList();
        if (list.isEmpty()) {
            return "";
        }
        for (final Object o : list) {
            String name;
            if (o instanceof IResource) {
                final IResource resource = (IResource) o;
                name = resource.getName();
            } else if (o instanceof IErlElement) {
                final IErlElement element = (IErlElement) o;
                name = element.getName();
            } else {
                continue;
            }
            sb.append('\'').append(name).append("', ");
            i++;
            if (i == 2) {
                break;
            }
        }
        if (structuredSelection.size() > 2) {
            return sb.append("...").toString();
        }
        return sb.substring(0, sb.length() - 2);
    }
    return "";
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IResource(org.eclipse.core.resources.IResource)

Example 44 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class WranglerUtils method notifyErlide.

/**
 * Notifies Erlide about the changed files.
 *
 * @param changedFiles
 *            changed files
 */
public static void notifyErlide(final List<ChangedFile> changedFiles) {
    final IErlModel model = ErlangEngine.getInstance().getModel();
    for (final ChangedFile f : changedFiles) {
        IFile file;
        try {
            file = WranglerUtils.getFileFromPath(f.getNewPath());
            final IErlElement element = model.findElement(file);
            final IErlModule m = (IErlModule) element;
            m.resourceChanged(null);
            final IEditorPart editor = GlobalParameters.getEditor();
            if (editor instanceof ErlangEditor) {
                ((ErlangEditor) editor).resetAndCacheScannerAndParser();
            }
            model.notifyChange(m);
        } catch (final Exception e) {
            ErlLogger.error(e);
        }
    }
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IFile(org.eclipse.core.resources.IFile) IErlModel(org.erlide.engine.model.root.IErlModel) IErlModule(org.erlide.engine.model.root.IErlModule) IEditorPart(org.eclipse.ui.IEditorPart) ChangedFile(org.erlide.wrangler.refactoring.backend.ChangedFile) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) OtpErlangRangeException(com.ericsson.otp.erlang.OtpErlangRangeException) ErlangEditor(org.erlide.ui.editors.erl.ErlangEditor)

Example 45 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class AddRefacHandler method checkType.

// check if the refactoring is elementary or composite
private RefacType checkType(final String callbackModule) {
    try {
        final IErlModule module = ErlangEngine.getInstance().getModel().findModule(callbackModule);
        final ScannerService scanner = module.getScanner();
        try {
            module.resetAndCacheScannerAndParser(null);
        } finally {
            scanner.dispose();
        }
        for (final IErlElement el : module.getChildrenOfKind(ErlElementKind.ATTRIBUTE)) {
            final IErlAttribute attr = (IErlAttribute) el;
            if ("behaviour".equals(attr.getName()) || "behavior".equals(attr.getName())) {
                if (attr.getValue().toString().contains("gen_refac")) {
                    return RefacType.ELEMENTARY;
                } else if (attr.getValue().toString().contains("gen_composite_refac")) {
                    return RefacType.COMPOSITE;
                }
            }
        }
        return null;
    } catch (final ErlModelException e) {
        return null;
    }
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) ErlModelException(org.erlide.engine.model.ErlModelException) IErlModule(org.erlide.engine.model.root.IErlModule) IErlAttribute(org.erlide.engine.model.erlang.IErlAttribute) ScannerService(org.erlide.engine.services.parsing.ScannerService)

Aggregations

IErlElement (org.erlide.engine.model.IErlElement)123 Test (org.junit.Test)36 IErlModule (org.erlide.engine.model.root.IErlModule)35 ErlModelException (org.erlide.engine.model.ErlModelException)28 IParent (org.erlide.engine.model.IParent)17 IFile (org.eclipse.core.resources.IFile)16 ArrayList (java.util.ArrayList)12 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)11 IResource (org.eclipse.core.resources.IResource)11 IErlProject (org.erlide.engine.model.root.IErlProject)10 IOpenable (org.erlide.engine.model.root.IOpenable)10 IProject (org.eclipse.core.resources.IProject)9 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)8 IErlModel (org.erlide.engine.model.root.IErlModel)8 WranglerException (org.erlide.wrangler.refactoring.exception.WranglerException)8 OtpErlangRangeException (com.ericsson.otp.erlang.OtpErlangRangeException)7 IFolder (org.eclipse.core.resources.IFolder)7 CoreException (org.eclipse.core.runtime.CoreException)7 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)6 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)6