Skip to content

Commit

Permalink
refactor: Return deleted comment tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
wsutina committed Nov 26, 2024
1 parent 922bcab commit 877ea9a
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions core/src/main/kotlin/cash/grammar/kotlindsl/parse/Rewriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public class Rewriter(
* This is a complicated process because there can be a mix of whitespace, newlines (not
* considered "whitespace" in this context), and comments, and we want to delete exactly as much
* as necessary to "delete the line" -- nothing more, nothing less.
*
* @return deleted comment tokens
*/
public fun deleteCommentsAndBlankSpaceToLeft(
before: Token,
) {
): List<Token>? {
var comments = deleteCommentsToLeft(before)

val ws = Whitespace.getBlankSpaceToLeft(commonTokens, before).onEach {
Expand All @@ -43,11 +45,15 @@ public class Rewriter(
?.onEach { ws -> delete(ws) }
?.first()?.let { deleteNewlineToLeft(it) }
}

return comments
}

/**
* Deletes all comments "to the left of" [before], returning the list of comment tokens, if they
* exist.
*
* @return deleted comment tokens
*/
public fun deleteCommentsToLeft(
before: Token,
Expand All @@ -70,20 +76,26 @@ public class Rewriter(
*
* Note that this algorithm differs from [deleteCommentsAndBlankSpaceToLeft] because comments "to
* the right of" a statement must start on the same line (no intervening newline characters).
*
* @return deleted comment tokens
*/
public fun deleteCommentsAndBlankSpaceToRight(
after: Token
) {
deleteCommentsToRight(after)
): List<Token>? {
val comments = deleteCommentsToRight(after)

Whitespace.getWhitespaceToRight(commonTokens, after)?.forEach {
delete(it)
}

return comments
}

/**
* Deletes all comments "to the right of" [after], returning the list of comment tokens, if they
* exist.
*
* @return deleted comment tokens
*/
public fun deleteCommentsToRight(
after: Token,
Expand Down

0 comments on commit 877ea9a

Please sign in to comment.