Whether an indent/dedent token is generated for a comment depends on whether the indent/dedent is persisted after the comment. This takes O(comment_length) to check. Checking it for every line takes O(comment_length * comment_lines), which is quadratic.
An optimization was made in a901729 which skips this check when there is no possible indent/dedent, such as in the following:
# comment 1...
# comment 1000
print("foo")
However, the check cannot be skipped in the following case:
This PR optimizes all cases by storing and re-using the next non-comment indent length and re-using it while still valid (requires TSLexer.get_byte from upstream tree-sitter).
Whether an indent/dedent token is generated for a comment depends on whether the indent/dedent is persisted after the comment. This takes O(comment_length) to check. Checking it for every line takes O(comment_length * comment_lines), which is quadratic.
An optimization was made in a901729 which skips this check when there is no possible indent/dedent, such as in the following:
However, the check cannot be skipped in the following case:
which comes up when commenting out code.
This PR optimizes all cases by storing and re-using the next non-comment indent length and re-using it while still valid (requires
TSLexer.get_byte
from upstreamtree-sitter
).Sample file: https://gist.github.com/llllvvuu/bc80d4df5f39cc27c13297e1d3a39190
Before:
After:
Fixes: https://github.com/nvim-treesitter/nvim-treesitter/issues/4839
BREAKING CHANGE: This makes the parser incompatible with the current
tree-sitter
ABI.