atom / language-python

Python package for Atom
Other
190 stars 146 forks source link

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

Closed caleb531 closed 5 years ago

caleb531 commented 5 years ago

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