Search in sources :

Example 1 with DuplicatedCodeElement

use of org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement in project erlide_eclipse by erlang.

the class DuplicateDetectionParser method parseDuplicates.

// { [{ {filename(), integer(), integer()} , {filename(), integer(),
// integer()} }], integer(), integer(), string()}
protected DuplicatedCodeElement parseDuplicates(final OtpErlangObject object) throws OtpErlangRangeException {
    final OtpErlangTuple listElementTuple = (OtpErlangTuple) object;
    final OtpErlangList duplicateCodeList = (OtpErlangList) listElementTuple.elementAt(0);
    final Map<IFile, List<DuplicatedCodeInstanceElement>> values = new LinkedHashMap<>();
    final OtpErlangString suggestion = (OtpErlangString) listElementTuple.elementAt(3);
    final String suggStr = suggestion.stringValue();
    final OtpErlangObject[] elements = duplicateCodeList.elements();
    for (final OtpErlangObject element : elements) {
        OtpErlangTuple elementPair = (OtpErlangTuple) element;
        String replicationFunction = "";
        final OtpErlangTuple checkable = (OtpErlangTuple) elementPair.elementAt(0);
        if (checkable.elementAt(0) instanceof OtpErlangTuple) {
            final OtpErlangString repFunStr = (OtpErlangString) elementPair.elementAt(1);
            replicationFunction = repFunStr.stringValue();
            elementPair = checkable;
        }
        final OtpErlangTuple firstElement = (OtpErlangTuple) elementPair.elementAt(0);
        final OtpErlangTuple secondElement = (OtpErlangTuple) elementPair.elementAt(1);
        final OtpErlangString fileName = (OtpErlangString) firstElement.elementAt(0);
        final OtpErlangLong startLine = (OtpErlangLong) firstElement.elementAt(1);
        final OtpErlangLong startCol = (OtpErlangLong) firstElement.elementAt(2);
        final OtpErlangLong endLine = (OtpErlangLong) secondElement.elementAt(1);
        final OtpErlangLong endCol = (OtpErlangLong) secondElement.elementAt(2);
        final String fileNameStr = fileName.stringValue();
        final IFile file = WranglerUtils.getFileFromPath(fileNameStr);
        final DuplicatedCodeInstanceElement instance = new DuplicatedCodeInstanceElement(file, startLine.intValue(), startCol.intValue(), endLine.intValue(), endCol.intValue() + 1);
        instance.setSuggestedCode(suggStr);
        instance.setReplicationFunction(replicationFunction);
        if (values.containsKey(file)) {
            values.get(file).add(instance);
        } else {
            final List<DuplicatedCodeInstanceElement> dupList = new ArrayList<>();
            dupList.add(instance);
            values.put(file, dupList);
        }
    }
    final DuplicatedCodeElement result = new DuplicatedCodeElement(values.entrySet().iterator().next().getValue().get(0));
    result.setSuggestedCode(suggStr);
    for (final Map.Entry<IFile, List<DuplicatedCodeInstanceElement>> entry : values.entrySet()) {
        final DuplicatedFileElement dupFile = new DuplicatedFileElement(entry.getKey());
        dupFile.setSuggestedCode(suggStr);
        for (final DuplicatedCodeInstanceElement instance : entry.getValue()) {
            dupFile.addChild(instance);
        }
        result.addChild(dupFile);
    }
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) ArrayList(java.util.ArrayList) DuplicatedCodeElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) LinkedHashMap(java.util.LinkedHashMap) DuplicatedFileElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedFileElement) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) ArrayList(java.util.ArrayList) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) List(java.util.List) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) DuplicatedCodeInstanceElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeInstanceElement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 2 with DuplicatedCodeElement

use of org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement in project erlide_eclipse by erlang.

the class ExpressionSearchParser method parse.

@Override
public void parse(final OtpErlangObject object) {
    try {
        final OtpErlangTuple res = (OtpErlangTuple) object;
        if (!"ok".equals(res.elementAt(0).toString())) {
            setUnSuccessful(((OtpErlangString) res.elementAt(1)).stringValue());
            return;
        }
        if (res.elementAt(1).equals(new OtpErlangList())) {
            setUnSuccessful("No more instances found!");
            return;
        }
        final OtpErlangList posList = (OtpErlangList) res.elementAt(1);
        OtpErlangTuple actPos;
        OtpErlangLong startLine;
        OtpErlangLong startColumn;
        OtpErlangLong endLine;
        OtpErlangLong endColumn;
        final List<DuplicatedCodeInstanceElement> instances = new ArrayList<>();
        for (final OtpErlangObject aPosList : posList) {
            actPos = (OtpErlangTuple) aPosList;
            startLine = (OtpErlangLong) ((OtpErlangTuple) actPos.elementAt(0)).elementAt(0);
            startColumn = (OtpErlangLong) ((OtpErlangTuple) actPos.elementAt(0)).elementAt(1);
            endLine = (OtpErlangLong) ((OtpErlangTuple) actPos.elementAt(1)).elementAt(0);
            endColumn = (OtpErlangLong) ((OtpErlangTuple) actPos.elementAt(1)).elementAt(1);
            final IErlSelection sel = GlobalParameters.getWranglerSelection();
            instances.add(new DuplicatedCodeInstanceElement((IFile) sel.getErlElement().getResource(), startLine.intValue(), startColumn.intValue(), endLine.intValue(), endColumn.intValue() + 1));
        }
        final DuplicatedCodeInstanceElement defaultInstance = instances.get(0);
        final DuplicatedCodeElement result = new DuplicatedCodeElement(defaultInstance);
        for (final DuplicatedCodeInstanceElement instance : instances) {
            result.addChild(instance);
        }
        isSuccessful = true;
        errorMessage = null;
        duplicates = new ArrayList<>();
        duplicates.add(result);
    } catch (final Exception e) {
        setUnSuccessful(e.getMessage());
    }
}
Also used : OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) IFile(org.eclipse.core.resources.IFile) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) ArrayList(java.util.ArrayList) IErlSelection(org.erlide.wrangler.refactoring.selection.IErlSelection) DuplicatedCodeElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) DuplicatedCodeInstanceElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeInstanceElement)

Example 3 with DuplicatedCodeElement

use of org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement in project erlide_eclipse by erlang.

the class SimilarExpressionSearchParser method parseDuplicates.

// { [{ {filename(), integer(), integer()} , {filename(), integer(),
// integer()} }], integer(), integer(), string()}
protected DuplicatedCodeElement parseDuplicates(final OtpErlangObject object) throws OtpErlangRangeException {
    final OtpErlangTuple listElementTuple = (OtpErlangTuple) object;
    final OtpErlangList duplicateCodeList = (OtpErlangList) listElementTuple.elementAt(0);
    final Map<IFile, List<DuplicatedCodeInstanceElement>> values = new LinkedHashMap<>();
    final OtpErlangString suggestion = (OtpErlangString) listElementTuple.elementAt(1);
    final String suggStr = suggestion.stringValue();
    final OtpErlangObject[] elements = duplicateCodeList.elements();
    for (final OtpErlangObject element : elements) {
        final OtpErlangTuple elementPair = (OtpErlangTuple) element;
        final OtpErlangTuple firstElement = (OtpErlangTuple) elementPair.elementAt(0);
        final OtpErlangTuple secondElement = (OtpErlangTuple) elementPair.elementAt(1);
        final OtpErlangString fileName = (OtpErlangString) firstElement.elementAt(0);
        final OtpErlangLong startLine = (OtpErlangLong) firstElement.elementAt(1);
        final OtpErlangLong startCol = (OtpErlangLong) firstElement.elementAt(2);
        final OtpErlangLong endLine = (OtpErlangLong) secondElement.elementAt(1);
        final OtpErlangLong endCol = (OtpErlangLong) secondElement.elementAt(2);
        final String fileNameStr = fileName.stringValue();
        final IFile file = WranglerUtils.getFileFromPath(fileNameStr);
        final DuplicatedCodeInstanceElement instance = new DuplicatedCodeInstanceElement(file, startLine.intValue(), startCol.intValue(), endLine.intValue(), endCol.intValue() + 1);
        instance.setSuggestedCode(suggStr);
        if (values.containsKey(file)) {
            values.get(file).add(instance);
        } else {
            final List<DuplicatedCodeInstanceElement> dupList = new ArrayList<>();
            dupList.add(instance);
            values.put(file, dupList);
        }
    }
    final DuplicatedCodeElement result = new DuplicatedCodeElement(values.entrySet().iterator().next().getValue().get(0));
    result.setSuggestedCode(suggStr);
    for (final Map.Entry<IFile, List<DuplicatedCodeInstanceElement>> entry : values.entrySet()) {
        final DuplicatedFileElement dupFile = new DuplicatedFileElement(entry.getKey());
        dupFile.setSuggestedCode(suggStr);
        for (final DuplicatedCodeInstanceElement instance : entry.getValue()) {
            dupFile.addChild(instance);
        }
        result.addChild(dupFile);
    }
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) ArrayList(java.util.ArrayList) DuplicatedCodeElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) LinkedHashMap(java.util.LinkedHashMap) DuplicatedFileElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedFileElement) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) ArrayList(java.util.ArrayList) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) List(java.util.List) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) DuplicatedCodeInstanceElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeInstanceElement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 4 with DuplicatedCodeElement

use of org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement in project erlide_eclipse by erlang.

the class DoubleClickListener method doubleClick.

@Override
public void doubleClick(final DoubleClickEvent event) {
    final ISelection selection = event.getSelection();
    final Object obj = ((IStructuredSelection) selection).getFirstElement();
    if (obj instanceof DuplicatedCodeInstanceElement) {
        higlightCodePart((DuplicatedCodeInstanceElement) obj);
    } else if (obj instanceof DuplicatedCodeElement) {
        higlightCodePart(((DuplicatedCodeElement) obj).getCodePart());
    } else if (obj instanceof DuplicatedFileElement) {
        final DuplicatedFileElement obj2 = (DuplicatedFileElement) obj;
        WranglerUtils.openFile(obj2.getContainingFile());
    }
}
Also used : DuplicatedFileElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedFileElement) ISelection(org.eclipse.jface.viewers.ISelection) DuplicatedCodeElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DuplicatedCodeInstanceElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeInstanceElement)

Example 5 with DuplicatedCodeElement

use of org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement in project erlide_eclipse by erlang.

the class DuplicatesViewContentProvider method showResult.

@Override
public void showResult(final List<DuplicatedCodeElement> result) {
    invisibleRoot.dropChildren();
    if (result != null) {
        for (final DuplicatedCodeElement d : result) {
            invisibleRoot.addChild(d);
        }
    }
    duplicatedCodeView.refresh();
}
Also used : DuplicatedCodeElement(org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement)

Aggregations

DuplicatedCodeElement (org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeElement)6 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)4 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)4 DuplicatedCodeInstanceElement (org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedCodeInstanceElement)4 OtpErlangLong (com.ericsson.otp.erlang.OtpErlangLong)3 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)3 ArrayList (java.util.ArrayList)3 IFile (org.eclipse.core.resources.IFile)3 DuplicatedFileElement (org.erlide.wrangler.refactoring.duplicatedcode.ui.elements.DuplicatedFileElement)3 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 OtpErlangRangeException (com.ericsson.otp.erlang.OtpErlangRangeException)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 IErlSelection (org.erlide.wrangler.refactoring.selection.IErlSelection)1