Skip to content

add #trim-end predicate #1888

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,25 @@ class ShrinkToMatch extends QueryPredicateOperator<ShrinkToMatch> {
}
}

/**
* A predicate operator that modifies the range of the match by trimming trailing whitespace,
* similar to the javascript trimEnd function.
*/
class TrimEnd extends QueryPredicateOperator<TrimEnd> {
name = "trim-end!" as const;
schema = z.tuple([q.node]);

run(nodeInfo: MutableQueryCapture) {
const { document, range } = nodeInfo;
const text = document.getText(range);
const trimmed = text.trimEnd();
const endOffset =
document.offsetAt(range.end) + trimmed.length - text.length;
nodeInfo.range = new Range(range.start, document.positionAt(endOffset));
return true;
}
}

class AllowMultiple extends QueryPredicateOperator<AllowMultiple> {
name = "allow-multiple!" as const;
schema = z.tuple([q.node]);
Expand Down Expand Up @@ -179,6 +198,7 @@ class InsertionDelimiter extends QueryPredicateOperator<InsertionDelimiter> {

export const queryPredicateOperators = [
new NotType(),
new TrimEnd(),
new NotEmpty(),
new NotParentType(),
new IsNthChild(),
Expand Down
4 changes: 2 additions & 2 deletions queries/talon.scm
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
) @_.domain
(#not-empty? @condition)
(#not-empty? @_.trailing)
(#shrink-to-match! @condition "^(?<keep>.*)(\s|\n|\r)+-$")
(#shrink-to-match! @_.trailing "^.*(?<keep>(\s|\n|\r)+-)$")
(#trim-end! @condition)
(#trim-end! @_.trailing)
)

;;!! slap: key(enter)
Expand Down