This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Add support for python2/3 shebangs in tree-sitter grammar #296
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Requirements
Description of the Change
I add shebang lines atop all my Python files. If I use
#!/usr/bin/python
or#!/usr/bin/env python
, the buffer will use the Python tree-sitter grammar (instead of first-mate). However, if I use#!/usr/bin/env python2
or#!/usr/bin/env python3
, the tree-sitter grammar is always ignored (in favor of first-mate).This PR updates the
firstLineRegex
field in the tree-sitter grammar to exactly match what's in the first-mate grammar. As a result,.py
files with apython2
orpython3
shebang now use thetree-sitter
grammar (if tree-sitter is enabled in Atom, of course).Alternate Designs
The tree-sitter
firstLineRegex
could've been rewritten to not be quite as lenient as the first-mate equivalent, but I didn't feel this was a major concern. The current first-mate regex is simple and understandable enough as-is.Benefits
Consistent syntax highlighting and the benefits of tree-sitter across more types of Python files (i.e. with or without the "versioned" shebang).
Possible Drawbacks
I know the tree-sitter grammar still lacks some highlighting compared to the first-mate grammar, which may make it undesirable to apply more broadly. I plan to submit a few more PRs soon to resolve some of these issues (e.g. tokenizing function parameter names). However, I still think the tree-sitter grammar, for all of its benefits, is worthy of adopting more broadly despite these minor issues (at least those few I've noticed).
Applicable Issues
N/A