-
Notifications
You must be signed in to change notification settings - Fork 8
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
--[lang]-query
can read tree-sitter (scm) query files
#144
Conversation
lang-query
can read fileslang-query
can read tree-sitter (scm) query files
lang-query
can read tree-sitter (scm) query files--[lang]-query
can read tree-sitter (scm) query files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work, thanks.
I was able to give it a first quick look, but not fully groked yet.
The CodeQuery
enum
was always bizarre, never liked it. It had this weird cyclic feel to it regarding trait impls. I get the feeling your refactor cleared that up.
It might be 2-3 weeks until I have time to look again properly, sorry! Feel free to reiterate, I will still take a look and unlock CI runs.
|
4e28dd4
to
d3fcc9e
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #144 +/- ##
==========================================
- Coverage 86.72% 86.61% -0.11%
==========================================
Files 32 32
Lines 1876 1898 +22
==========================================
+ Hits 1627 1644 +17
- Misses 249 254 +5 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look good, thank you so much!
The PhantomData
approach existed to be extra strict. I expected those types to be shuffled about a lot, where confusing them would be bad. But turns out that doesn't really happen, with construction only occurring in a single place.
Getting rid of the weirdly cyclic CodeQuery
constructs is a breath of fresh air and appreciated. I'm happy to move forward with this PR if you are.
No worries, happy to help out.
I'm happy to merge this into main when you are 😉 I'll fix up the docs first and we should consider a test for the file opening on Windows. |
There are still some doc test failures in CI: https://github.com/alexpovel/srgn/actions/runs/11314113061/job/31463705601?pr=144#step:7:928 |
Fixed in 2b2ea9d |
@alexpovel I'll also add documentation for the new file mode of |
Nice, thank you. One possible approach is:
This will all be picked up as part of unit testing via Lines 1 to 4 in 9238fc1
Lastly, docs like Line 1303 in 9238fc1
and Line 1224 in 9238fc1
(should read something like Line 1341 in 9238fc1
|
Oh and I'll squash all commits! If you feel like it you can do that yourself as well, otherwise I'll squash and come up with a suitable commit message. Ideally and alternatively, instead of squashing into 1 commit, the languages refactor is one |
Yea no worries I'll squash the commits down to something reasonable. |
All good, I'll give this a try and maybe come back with questions |
@alexpovel as a start of making a test for the See 94f68dc This will make the query data easily available for various uses. |
I am convinced 🙂 This guy will cause trouble though: Line 129 in 9238fc1
It is interpolated via |
Yea there's no way around the use of It may be worth it already though as, I think we could then modify the #[rstest]
#[case(
"base.py_comments",
include_str!("python/base.py"),
python::CompiledQuery::new(&python::PreparedQuery::Comments.into()),
)] I don't have a lot of experience with Then the existing prepared query tests could be rephrased to test the given query as a literal string and read the query from the file. That might be OTT though, as we don't need to test every single prepared query twice. So maybe we just modify the existing prepared query tests to only test the queries contained in the files. |
And having the queries as files would allow the --rust <RUST>
Scope Rust code using a prepared query.
[env: RUST=]
[aliases: rs]
Possible values:
- comments: Comments (line and block styles; excluding doc comments;
comment chars incl.)
- doc-comments: Doc comments (comment chars included)
... |
@alexpovel We could move the prepared ( It's moving to a more black-box test, but you can always choose do perform the tests on the lib API and the CLI. See: e24d015 |
@alexpovel it's probably a good idea to keep testing the entire set of prepared queries in the lib itself so that the lib's API is tested. The CLI mod can just test that a single prepared, inline and file based query works. |
A couple thoughts:
|
Oh I wasn't worried about the effort it takes to add a new test, more that we could reduce the boilerplate.
All good, it's not a big issue.
I'll roll back the last two commits and pursue another solution.
It's good to know your attitude here, that the CLI tests are the most important. |
@alexpovel I think the handiest is for me to make a new PR with the major renaming etc and rebase that onto this PR. The working week has begun here so I'll try make some time for it during the week some evening. |
e24d015
to
4a69100
Compare
@alexpovel After merging #151 this is the resulting change needed to be able to provide a query as a file to I'll add a test as discussed before and address comment #144 (comment). |
@alexpovel I've added a test to the I'll update the docs for the CLI args now and call it a day. How do you recommend testing the case where the file path doesn't exist etc? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be fine not testing the "non-existent file" scenario, as it's kinda "obviously correct" (bubbles the error up, as many other I/O errors do that can occur in main.rs
, most of which aren't tested).
The error message for a non-existent file is a bit confusing of course:
Error: Error occurred while creating a tree-sitter query: Query error at 1:1. Invalid syntax:
docs/python_cond_query-file-does-not-exist.scm
^
but...
-
if you're already passing a file name, you must have read the docs which mention that this is even possible. So you know what you're doing at will quickly be able to tell that the file doesn't exist
-
I don't know how to report this better. On "file not found" error, we could check "does this LOOK like a file path", but I think that's fraught with peril... maybe a very simple, best-effort regex roughly like
/?([a-zA-Z0-9]+/)+
, and then if that matcheswarn!()
the user.Alternatively, is it possible the queries ALWAYS have to start with
(
? If yes, that could also be a give-away.
Yea I would avoid trying to guess if the string "looks" like a file. # run literal query,
# The first byte being a `(` signals that the content is intended as a literal query.
srgn -l rust --query '((macro_invocation macro: (identifier) @_SRGN_IGNORE_name) @macro)'
# run `test-mod` prepared query
# The first byte being a `#` (or whatever we chose) signals that the content is intended as a prepared query.
# This change is clearly for another PR if it's agreed upon.
srgn -l rust --query '#test-mod'
# run contents of `./query/file.scm`
# If the string doesn't start with a `(` or a `#` then we presume it's a file path
srgn -l rust --query ./query/file.scm` |
ef162fb
to
3114b8d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI still has minor problems, but happy to merge otherwise.
I might look into improving the error message/query-file-path-disambiguation more in the future.
All good, I think this PR is a nice contained change. The disambiguation issue is definitely for another PR. |
Co-authored-by: Alex Povel <git@alexpovel.de>
Co-authored-by: Alex Povel <git@alexpovel.de>
Co-authored-by: Alex Povel <git@alexpovel.de>
Co-authored-by: Alex Povel <git@alexpovel.de>
Co-authored-by: Alex Povel <git@alexpovel.de>
d1ef82d
to
cbfa14b
Compare
I asked about what tree-sitter queries can potentially look like at: tree-sitter/tree-sitter#3862 (comment) . The reply makes me think disambiguation isn't going to be straightforward. Even if we get it correct now, updates to tree-sitter might break us. Currently, I'm leaning more towards a dedicated flag, like Apologies for beating around the bush here, and changing my mind. I think I originally suggested overloading the existing option. What do you think about merging this PR as a |
Yea I agree, it's best to be as explicit as possible.
No worries, that's how exploring an implementation works.
I can also make the change, it's no problem at all. |
Alright! Thanks for your work again and your patience. Your change is merged now. The first of us to get to it can now build on top of that to implement something like |
Relates to #143
@alexpovel I've added a proof of concept to this PR.
The most logical place to add the file check and read is to the
struct CustomRustQuery
.If so the
QueryError
needs to be wrapped in a compoundError
that can reportio::Error
's along with the parsing errorsQueryError
is now designed to report.What do you think?