Skip to content

Commit

Permalink
Merge pull request #15 from seart-group/enhancement/arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico authored Sep 21, 2023
2 parents 26c5113 + e7823c7 commit 8a67fa9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
22 changes: 4 additions & 18 deletions src/main/java/ch/usi/si/seart/treesitter/OffsetTreeCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ public int getEndByte() {

@Override
public Point getEndPoint() {
Point point = node.getEndPoint();
return new Point(
point.getRow() + offset.getRow(),
point.getColumn() + offset.getColumn()
);
return node.getEndPoint().add(offset);
}

@Override
Expand Down Expand Up @@ -184,11 +180,7 @@ public int getStartByte() {

@Override
public Point getStartPoint() {
Point point = node.getStartPoint();
return new Point(
point.getRow() + offset.getRow(),
point.getColumn() + offset.getColumn()
);
return node.getStartPoint().add(offset);
}

@Override
Expand Down Expand Up @@ -221,14 +213,8 @@ private static class OffsetTreeCursorNode extends TreeCursorNode {
cursorNode.getName(),
cursorNode.getType(),
cursorNode.getContent(),
new Point(
cursorNode.getStartPoint().getRow() + offset.getRow(),
cursorNode.getStartPoint().getColumn() + offset.getColumn()
),
new Point(
cursorNode.getEndPoint().getRow() + offset.getRow(),
cursorNode.getEndPoint().getColumn() + offset.getColumn()
),
cursorNode.getStartPoint().add(offset),
cursorNode.getEndPoint().add(offset),
cursorNode.isNamed()
);
}
Expand Down
52 changes: 49 additions & 3 deletions src/main/java/ch/usi/si/seart/treesitter/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.EqualsAndHashCode;
import lombok.Generated;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldDefaults;

/**
Expand All @@ -20,10 +19,9 @@
* @author Ozren Dabić
*/
@Getter
@Setter(value = AccessLevel.PACKAGE)
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@EqualsAndHashCode
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class Point {

private static final Point ORIGIN = new Point(0, 0);
Expand Down Expand Up @@ -55,4 +53,52 @@ public String toString() {
public boolean isOrigin() {
return equals(ORIGIN);
}

/**
* Adds another point to this point,
* resulting in a new point with coordinates
* equal to the sum of the coordinates
* of this point and the other point.
*
* @param other The point to be added to this point.
* @return A new point representing the sum of this point and the other point.
* @since 1.5.1
*/
public Point add(Point other) {
if (isOrigin()) return other;
if (other.isOrigin()) return this;
if (equals(other)) return multiply(2);
return new Point(row + other.row, column + other.column);
}

/**
* Subtracts another point from this point,
* resulting in a new point with coordinates
* equal to the difference between the coordinates
* of this point and the other point.
*
* @param other The point to be subtracted from this point
* @return A new point representing the difference between this point and the other point
* @since 1.5.1
*/
public Point subtract(Point other) {
if (isOrigin()) return other.multiply(-1);
if (other.isOrigin()) return this;
if (equals(other)) return ORIGIN;
return new Point(row - other.row, column - other.column);
}

/**
* Multiplies the coordinates of this point by a scalar value,
* resulting in a new point with scaled coordinates.
*
* @param value The scalar value by which to multiply the coordinates of this point
* @return A new point representing the scaled coordinates
* @since 1.5.1
*/
public Point multiply(int value) {
if (value == 0) return ORIGIN;
if (value == 1) return this;
return new Point(row * value, column * value);
}
}
27 changes: 27 additions & 0 deletions src/test/java/ch/usi/si/seart/treesitter/PointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class PointTest {

private static final Point _0_0_ = new Point(0, 0);
private static final Point _1_1_ = new Point(1, 1);

@Test
void testIsOrigin() {
Assertions.assertTrue(new Point(0, 0).isOrigin());
Expand All @@ -21,6 +24,30 @@ void testIsNotOrigin(Point point) {
Assertions.assertFalse(point.isOrigin());
}

@Test
void testAdd() {
Assertions.assertEquals(_1_1_, _0_0_.add(_1_1_));
Assertions.assertEquals(_1_1_, _1_1_.add(_0_0_));
Assertions.assertEquals(_0_0_, new Point(-1, -1).add(_1_1_));
Assertions.assertEquals(new Point(2, 2), _1_1_.add(_1_1_));
}

@Test
void testSubtract() {
Assertions.assertEquals(_0_0_, _1_1_.subtract(_1_1_));
Assertions.assertEquals(_1_1_, _1_1_.subtract(_0_0_));
Assertions.assertEquals(new Point(-1, -1), _0_0_.subtract(_1_1_));
Assertions.assertEquals(new Point(-2, -2), new Point(-1, -1).subtract(_1_1_));
}

@Test
void testMultiply() {
Assertions.assertEquals(_0_0_, _0_0_.multiply(2));
Assertions.assertEquals(_0_0_, _1_1_.multiply(0));
Assertions.assertEquals(_1_1_, _1_1_.multiply(1));
Assertions.assertEquals(new Point(2, 2), _1_1_.multiply(2));
}

public static Stream<Arguments> provideNonOriginPoints() {
return Stream.of(
Arguments.of(new Point(1, 0)),
Expand Down

0 comments on commit 8a67fa9

Please sign in to comment.