Skip to content

Change endPosition to be the end of the node's length and add endPositionBeforeTrailingTrivia in its place #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/SwiftSyntax/SourceLength.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,15 @@ extension AbsolutePosition {
public static func +=(lhs: inout AbsolutePosition, rhs: SourceLength) {
lhs = lhs + rhs
}

public static func -(
lhs: AbsolutePosition, rhs: SourceLength
) -> AbsolutePosition {
let utf8Offset = lhs.utf8Offset - rhs.utf8Length
return AbsolutePosition(utf8Offset: utf8Offset)
}

public static func -=(lhs: inout AbsolutePosition, rhs: SourceLength) {
lhs = lhs - rhs
}
}
26 changes: 12 additions & 14 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,14 @@ extension _SyntaxBase {
return data.positionAfterSkippingLeadingTrivia
}

/// The absolute position where this node (excluding its trailing trivia)
/// ends.
var endPosition: AbsolutePosition {
return data.endPosition
/// The end position of this node's content, before any trailing trivia.
var endPositionBeforeTrailingTrivia: AbsolutePosition {
return data.endPositionBeforeTrailingTrivia
}

/// The absolute position where this node's trailing trivia ends
var endPositionAfterTrailingTrivia: AbsolutePosition {
return data.endPositionAfterTrailingTrivia
/// The end position of this node, including its trivia.
var endPosition: AbsolutePosition {
return data.endPosition
}

/// The textual byte length of this node including leading and trailing trivia.
Expand Down Expand Up @@ -384,15 +383,14 @@ extension Syntax {
return base.positionAfterSkippingLeadingTrivia
}

/// The absolute position where this node (excluding its trailing trivia)
/// ends.
public var endPosition: AbsolutePosition {
return base.endPosition
/// The end position of this node's content.
public var endPositionBeforeTrailingTrivia: AbsolutePosition {
return base.endPositionBeforeTrailingTrivia
}

/// The absolute position where this node's trailing trivia ends
public var endPositionAfterTrailingTrivia: AbsolutePosition {
return base.endPositionAfterTrailingTrivia
/// The end position of this node, including its trivia
public var endPosition: AbsolutePosition {
return base.endPosition
}

/// The textual byte length of this node including leading and trailing trivia.
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftSyntax/SyntaxData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ struct SyntaxData {
return position + raw.leadingTriviaLength
}

/// The end position of this node's content, excluding its trivia
var endPosition: AbsolutePosition {
return positionAfterSkippingLeadingTrivia + raw.contentLength
/// The end position of this node's content, before any trailing trivia.
var endPositionBeforeTrailingTrivia: AbsolutePosition {
return endPosition - raw.trailingTriviaLength
}

/// The end position of this node's trivia
var endPositionAfterTrailingTrivia: AbsolutePosition {
return endPosition + raw.trailingTriviaLength
/// The end position of this node, including its trivia.
var endPosition: AbsolutePosition {
return position + raw.totalLength
}

/// Creates a `SyntaxData` with the provided raw syntax and parent.
Expand Down
25 changes: 25 additions & 0 deletions Tests/SwiftSyntaxTest/SyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class SyntaxAPITestCase: XCTestCase {

public static let allTests = [
("testSyntaxAPI", testSyntaxAPI),
("testPositions", testPositions),
]

public func testSyntaxAPI() {
Expand Down Expand Up @@ -63,4 +64,28 @@ public class SyntaxAPITestCase: XCTestCase {
XCTAssertEqual(toks[4].uniqueIdentifier, rtoks[1].uniqueIdentifier)
XCTAssertEqual(toks[5].uniqueIdentifier, rtoks[0].uniqueIdentifier)
}

public func testPositions() {
func testFuncKw(_ funcKW: TokenSyntax) {
XCTAssertEqual("\(funcKW)", " func ")
XCTAssertEqual(funcKW.position, AbsolutePosition(utf8Offset: 0))
XCTAssertEqual(funcKW.positionAfterSkippingLeadingTrivia, AbsolutePosition(utf8Offset: 2))
XCTAssertEqual(funcKW.endPositionBeforeTrailingTrivia, AbsolutePosition(utf8Offset: 6))
XCTAssertEqual(funcKW.endPosition, AbsolutePosition(utf8Offset: 7))
XCTAssertEqual(funcKW.contentLength, SourceLength(utf8Length: 4))
}
do {
let source = " func f() {}"
let tree = try! SyntaxParser.parse(source: source)
let funcKW = tree.firstToken!
testFuncKw(funcKW)
}
do {
let leading = Trivia(pieces: [ .spaces(2) ])
let trailing = Trivia(pieces: [ .spaces(1) ])
let funcKW = SyntaxFactory.makeFuncKeyword(
leadingTrivia: leading, trailingTrivia: trailing)
testFuncKw(funcKW)
}
}
}