atom / bracket-matcher

Jump to brackets
MIT License
142 stars 98 forks source link

Find brackets to highlight using the Tree-sitter syntax tree #367

Closed maxbrunsfeld closed 5 years ago

maxbrunsfeld commented 5 years ago

Description of the Change

Currently, this package finds matching brackets by performing a regex search and keeping track of the nesting level of bracket characters. The code for finding matching HTML tags is even more complicated.

In this PR, I've updated the package to find matching tokens using syntax trees provided by Tree-sitter, when they are available.

Benefits

The performance of bracket matching is vastly improved. Here's a flame graph that I recorded in the current version of Atom when moving the cursor across this opening parenthesis in Atom's text-editor-component-spec.js.

screen shot 2018-11-16 at 2 46 03 pm

This operation took 300ms.

Here's a flame graph that I recorded of the same operation with this fix:

screen shot 2018-11-16 at 3 08 30 pm

The operation now takes 0.75ms; the syntax tree makes the operation basically free.

Possible Drawbacks

This improvement only takes effect for languages with Tree-sitter parsers.

Applicable Issues

Fixes #348 Fixes #355