Skip to content

Commit 14f7753

Browse files
Avoid executing unnecessary operations (#253)
- Filter empty change bounds and change routing points operations - Filter empty compound commands Relates to eclipse-glsp/glsp#1477
1 parent 8604c5d commit 14f7753

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

plugins/org.eclipse.glsp.graph/src/org/eclipse/glsp/graph/util/GraphUtil.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (c) 2019 EclipseSource and others.
2+
* Copyright (c) 2019-2025 EclipseSource and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -24,7 +24,7 @@ public final class GraphUtil {
2424

2525
private GraphUtil() {}
2626

27-
public static GBounds bounds(double x, double y, double width, double height) {
27+
public static GBounds bounds(final double x, final double y, final double width, final double height) {
2828
GBounds bounds = GraphFactory.eINSTANCE.createGBounds();
2929
bounds.setX(x);
3030
bounds.setY(y);
@@ -33,29 +33,48 @@ public static GBounds bounds(double x, double y, double width, double height) {
3333
return bounds;
3434
}
3535

36-
public static GBounds copy(GBounds toCopy) {
36+
public static GBounds copy(final GBounds toCopy) {
3737
return bounds(toCopy.getX(), toCopy.getY(), toCopy.getWidth(), toCopy.getHeight());
3838
}
3939

40-
public static GPoint point(double x, double y) {
40+
public static GPoint point(final double x, final double y) {
4141
GPoint point = GraphFactory.eINSTANCE.createGPoint();
4242
point.setX(x);
4343
point.setY(y);
4444
return point;
4545
}
4646

47-
public static GPoint copy(GPoint toCopy) {
47+
public static GPoint copy(final GPoint toCopy) {
4848
return point(toCopy.getX(), toCopy.getY());
4949
}
5050

51-
public static GDimension dimension(double width, double height) {
51+
public static int compare(final GPoint left, final GPoint right) {
52+
int xCompare = Double.compare(left.getX(), right.getX());
53+
return xCompare != 0 ? xCompare : Double.compare(left.getY(), right.getY());
54+
}
55+
56+
public static boolean equals(final GPoint left, final GPoint right) {
57+
return compare(left, right) == 0;
58+
}
59+
60+
public static GDimension dimension(final double width, final double height) {
5261
GDimension dimension = GraphFactory.eINSTANCE.createGDimension();
5362
dimension.setWidth(width);
5463
dimension.setHeight(height);
5564
return dimension;
5665
}
5766

58-
public static GDimension copy(GDimension toCopy) {
67+
public static GDimension copy(final GDimension toCopy) {
5968
return dimension(toCopy.getWidth(), toCopy.getHeight());
6069
}
70+
71+
public static int compare(final GDimension left, final GDimension right) {
72+
int widthCompare = Double.compare(left.getWidth(), right.getWidth());
73+
return widthCompare != 0 ? widthCompare : Double.compare(left.getHeight(), right.getHeight());
74+
}
75+
76+
public static boolean equals(final GDimension left, final GDimension right) {
77+
return compare(left, right) == 0;
78+
}
79+
6180
}

plugins/org.eclipse.glsp.server/src/org/eclipse/glsp/server/gmodel/GModelChangeBoundsOperationHandler.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (c) 2019-2023 EclipseSource and others.
2+
* Copyright (c) 2019-2025 EclipseSource and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -17,11 +17,13 @@
1717

1818
import static org.eclipse.glsp.server.types.GLSPServerException.getOrThrow;
1919

20+
import java.util.List;
2021
import java.util.Optional;
2122

2223
import org.apache.logging.log4j.LogManager;
2324
import org.apache.logging.log4j.Logger;
2425
import org.eclipse.emf.common.command.Command;
26+
import org.eclipse.glsp.graph.GBoundsAware;
2527
import org.eclipse.glsp.graph.GDimension;
2628
import org.eclipse.glsp.graph.GModelElement;
2729
import org.eclipse.glsp.graph.GModelIndex;
@@ -43,7 +45,20 @@ public class GModelChangeBoundsOperationHandler extends GModelOperationHandler<C
4345

4446
@Override
4547
public Optional<Command> createCommand(final ChangeBoundsOperation operation) {
46-
return commandOf(() -> executeChangeBounds(operation));
48+
List<ElementAndBounds> changedElementAndBounds = operation.getNewBounds().stream().filter(this::hasChanged)
49+
.toList();
50+
return changedElementAndBounds.isEmpty()
51+
? doNothing()
52+
: commandOf(() -> executeChangeBounds(new ChangeBoundsOperation(changedElementAndBounds)));
53+
}
54+
55+
protected boolean hasChanged(final ElementAndBounds elementAndBounds) {
56+
Optional<GModelElement> element = this.modelState.getIndex().get(elementAndBounds.getElementId());
57+
if (element.isEmpty() || !(element.get() instanceof GBoundsAware boundsAware)) {
58+
return true;
59+
}
60+
return !GraphUtil.equals(boundsAware.getSize(), elementAndBounds.getNewSize()) ||
61+
!GraphUtil.equals(boundsAware.getPosition(), elementAndBounds.getNewPosition());
4762
}
4863

4964
protected void executeChangeBounds(final ChangeBoundsOperation operation) {

plugins/org.eclipse.glsp.server/src/org/eclipse/glsp/server/gmodel/GModelChangeRoutingPointsHandler.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (c) 2019-2023 EclipseSource and others.
2+
* Copyright (c) 2019-2025 EclipseSource and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -15,12 +15,18 @@
1515
********************************************************************************/
1616
package org.eclipse.glsp.server.gmodel;
1717

18+
import java.util.Arrays;
19+
import java.util.List;
1820
import java.util.Optional;
1921

2022
import org.eclipse.emf.common.command.Command;
23+
import org.eclipse.glsp.graph.GEdge;
2124
import org.eclipse.glsp.graph.GModelIndex;
25+
import org.eclipse.glsp.graph.GPoint;
26+
import org.eclipse.glsp.graph.util.GraphUtil;
2227
import org.eclipse.glsp.server.operations.ChangeRoutingPointsOperation;
2328
import org.eclipse.glsp.server.operations.GModelOperationHandler;
29+
import org.eclipse.glsp.server.types.ElementAndRoutingPoints;
2430
import org.eclipse.glsp.server.utils.LayoutUtil;
2531

2632
/**
@@ -30,14 +36,25 @@ public class GModelChangeRoutingPointsHandler extends GModelOperationHandler<Cha
3036

3137
@Override
3238
public Optional<Command> createCommand(final ChangeRoutingPointsOperation operation) {
33-
return commandOf(() -> executeChangeRoutingPoints(operation));
39+
List<ElementAndRoutingPoints> changedRoutingPoints = operation.getNewRoutingPoints().stream()
40+
.filter(this::hasChanged)
41+
.toList();
42+
return changedRoutingPoints.isEmpty()
43+
? doNothing()
44+
: commandOf(() -> executeChangeRoutingPoints(new ChangeRoutingPointsOperation(changedRoutingPoints)));
3445
}
3546

36-
protected void executeChangeRoutingPoints(final ChangeRoutingPointsOperation operation) {
37-
if (operation.getNewRoutingPoints() == null) {
38-
throw new IllegalArgumentException("Incomplete change routingPoints action");
47+
protected boolean hasChanged(final ElementAndRoutingPoints routingPoints) {
48+
Optional<GEdge> edge = this.modelState.getIndex().getByClass(routingPoints.getElementId(),
49+
GEdge.class);
50+
if (edge.isEmpty() || edge.get().getRoutingPoints().size() != routingPoints.getNewRoutingPoints().size()) {
51+
return true;
3952
}
53+
return !Arrays.equals(edge.get().getRoutingPoints().toArray(new GPoint[0]),
54+
routingPoints.getNewRoutingPoints().toArray(new GPoint[0]), GraphUtil::compare);
55+
}
4056

57+
protected void executeChangeRoutingPoints(final ChangeRoutingPointsOperation operation) {
4158
GModelIndex index = modelState.getIndex();
4259
operation.getNewRoutingPoints().forEach(routingPoints -> LayoutUtil.applyRoutingPoints(routingPoints, index));
4360
}

0 commit comments

Comments
 (0)