Skip to content
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

Merged
winstliu merged 1 commit into
atom:masterfrom
caleb531:tree-sitter-first-line
Apr 7, 2019
Merged

winstliu merged 1 commit into
atom:masterfrom
caleb531:tree-sitter-first-line

Conversation

@caleb531

@caleb531 caleb531 commented Apr 6, 2019

Copy link
Copy Markdown
Contributor

Requirements

  • 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.

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

@xax xax left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 tree-sitter-first-line branch May 12, 2019 23:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants