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

token source position tracking #794

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dylanscott
Copy link

This PR adds tracking of token source positions, accessible with new pos and length attributes on the Token class and its subclasses. As mentioned in the corresponding issue (#793), we've been using a forked version of sqlparse with this addition to power syntax highlighting in a production system for awhile, and were hoping to upstream the change if it's acceptable.

  • ran the tests (pytest)
  • all style issues addressed (flake8)
  • your changes are covered by tests
  • your changes are documented, if needed

Comment on lines +182 to +187
# this is a bit of a hack to avoid problems with the super constructor
# trying to set these attributes, which we want to compute dynamically
@pos.setter
def pos(self, value): ...
@length.setter
def length(self, value): ...
Copy link
Author

@dylanscott dylanscott Oct 18, 2024

Choose a reason for hiding this comment

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

I initially had the Token constructor do a hasattr(self, 'pos') check before assigning, which I didn't love either. But that approach broke one test where deepcopy was called on a parsed statement, whereas this approach is robust to that case.

It seemed worth making these properties dynamic given that TokenList exposes methods for inserting tokens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant