DevBoost / EMFText

EMFText is an Eclipse plug-in that allows you to define text syntax for languages described by an Ecore metamodel. EMFText enables developers to define textual Domain Specific Languages quickly and without the need to learn new technologies and concepts.
14 stars 12 forks source link

Outline-View, "Link with Editor": Click in Editor often does not select the right Element in the Outline View #38

Closed marcusmunzert closed 11 years ago

marcusmunzert commented 11 years ago

When I toggle the "Link with Editor" button in the outline view and select something in the editor, often the selection in the outline view does not correspond to the selection in the editor. This problem seems to occur, when I select an element in the editor where the corresponding element in the outline view lays inside a branch that is not yet expanded. When I expand the whole tree in the outline view before selecting something in the editor, the selction in the ouline view is always correct.

mirkoseifert commented 11 years ago

I didn't experience this behavior, but it sounds like a bug. Did you try to debug the outline view page? I think there should be a piece of code that automatically expands elements which are not visible upon selection?

marcusmunzert commented 11 years ago

I found the root-cause for my problem and a solution that at least works for me. The problem is in the generated code in XYZOutlinePageTreeViewer.java. There is an equals() method that uses FlatEObjectComparer to compare two instances of EObject. In my meta-model I use classes that represent a body of an element (starting with '{' and ending with '}'). Those classes do not have any attributes or relations defined. When there are two instances of such a class, FlatEObjectComparer says that those instances are equal to each other, which leads to the problem. In my code I changed the implementation of the equals() method to be like this, which works for me:

public boolean equals(Object o1, Object o2) {
    if (o1 instanceof EObject && o2 instanceof EObject) {
        EObject eo1 = (EObject) o1;
        EObject eo2 = (EObject) o2;
        boolean equalThroughFlatEObjectComparer = new FlatEObjectComparer().equals(eo1, eo2);               
        if (equalThroughFlatEObjectComparer && eo1.eClass().getEAttributes().size() == 0 &&
                        eo2.eClass().getEAttributes().size() == 0) {
            return eo1 == eo2;
        } else {
            return equalThroughFlatEObjectComparer;
        }
    }
    String s1 = toString(o1);
    String s2 = toString(o2);
    if (s1 != null) {
        return s1.equals(s2);
    }
    return o1.equals(o2);
}