Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for python2/3 shebangs in tree-sitter grammar #296

merged 1 commit into from Apr 7, 2019


Copy link

@caleb531 caleb531 commented Apr 6, 2019


  • Filling out the template is required. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.
  • All new code requires tests to ensure against regressions

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 a python2 or python3 shebang now use the tree-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.


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


@50Wliu 50Wliu merged commit a884e4e into atom:master Apr 7, 2019
2 checks passed
2 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
continuous-integration/travis-ci/pr The Travis CI build passed
Copy link

@xax xax left a 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'
@caleb531 caleb531 deleted the caleb531:tree-sitter-first-line branch May 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants