Skip to content

Commit

Permalink
include leading whitespace in $.message
Browse files Browse the repository at this point in the history
see helix-editor/helix#1338 (comment)

Leading whitespace is important when injecting diff highlights into
messages trailing the scissors.

Without this change, some adverserial context lines can end up being
mistakenly parsed as non-$.context rules. For example, in the
screenshot of the linked issue comment, a context line is being parsed
by a tricky link that looks like a malformed $.similarity rule. Because
NEWLINE is not a child of $._line but $.source in tree-sitter-git-diff,
part of the line is re-parsed into another valid $._line rule, namely
$.addition in this case.

For an example COMMIT_EDITMSG which has a second diff line 6 characters
to the right of the newline, this commit changes the start column of
the parsed $.message node to include the whitespace:

 (source [0, 0] - [7, 0]
   (subject [0, 0] - [0, 53])
   (comment [2, 0] - [4, 38]
     (scissors [2, 0] - [4, 38]))
   (message [5, 0] - [5, 36])
-  (message [6, 5] - [6, 80]))
+  (message [6, 0] - [6, 80]))

This change probably restricts this grammar to tree-sitter 0.20.1+
because the WHITE_SPACE 'extra' is now used as an extra and within
a rule
(see tree-sitter/tree-sitter#1444 (comment))
but trailing diffs are not meant to be edited anyways, so it's probably
not a big deal.
  • Loading branch information
the-mikedavis committed Dec 25, 2021
1 parent 5cd4776 commit 066e395
Show file tree
Hide file tree
Showing 4 changed files with 2,912 additions and 2,808 deletions.
9 changes: 4 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ module.exports = grammar({
),

message: ($) =>
choice(
seq(
choice($.user, /[^\s]+/),
optional(repeat(choice($.user, $.item, $._word)))
)
seq(
repeat(WHITE_SPACE),
choice($.user, /[^\s]+/),
optional(repeat(choice($.user, $.item, $._word)))
),

comment: ($) =>
Expand Down
78 changes: 40 additions & 38 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,51 +118,53 @@
]
},
"message": {
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "SEQ",
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t\\f\\v ]+"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "user"
},
{
"type": "PATTERN",
"value": "[^\\s]+"
}
]
"type": "SYMBOL",
"name": "user"
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "user"
},
{
"type": "SYMBOL",
"name": "item"
},
{
"type": "SYMBOL",
"name": "_word"
}
]
"type": "PATTERN",
"value": "[^\\s]+"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "user"
},
{
"type": "SYMBOL",
"name": "item"
},
{
"type": "SYMBOL",
"name": "_word"
}
},
{
"type": "BLANK"
}
]
]
}
},
{
"type": "BLANK"
}
]
}
Expand Down
Loading

1 comment on commit 066e395

@the-mikedavis
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a tricky link

-> a tricky line

Please sign in to comment.