This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Repository navigation
Add support for python2/3 shebangs in tree-sitter grammar - #296
Merged
Merged
Conversation
xax
reviewed
Apr 18, 2019
There was a problem hiding this comment.
@caleb531: Acknowledging you didn't invent this patricular firstLineRegex, nevertheless the period "." inside brackets "[…]" in the regular expression need not (and should not) be escaped by a backslash "".
- '^#![ \\t]*/.*\\bpython[\\d\\.]*\\b'
+ '^#![ \\t]*/.*\\bpython[\\d.]*\\b'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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/pythonor#!/usr/bin/env python, the buffer will use the Python tree-sitter grammar (instead of first-mate). However, if I use#!/usr/bin/env python2or#!/usr/bin/env python3, the tree-sitter grammar is always ignored (in favor of first-mate).This PR updates the
firstLineRegexfield in the tree-sitter grammar to exactly match what's in the first-mate grammar. As a result,.pyfiles with apython2orpython3shebang now use thetree-sittergrammar (if tree-sitter is enabled in Atom, of course).Alternate Designs
The tree-sitter
firstLineRegexcould'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