withered-magic / starpls

An LSP implementation for Starlark, the configuration language used by Bazel and Buck2.
Apache License 2.0
98 stars 10 forks source link

go to bazel rule(s) #100

Open zaucy opened 5 months ago

zaucy commented 5 months ago

I was wondering if it was possible to create an lsp command or something like that, that goes to the bazel rules a source file is being used in.

Essentially a way to do this:

bazel query --infer_universe_scope ... intersect 'allrdeps("path/to/source/file/here.whatever")'

For instance if you're in your editor editing crates/starpls/src/main.rs and you were to 'go to bazel rule' it would take you to these rules:

//crates/starpls:starpls
//editors/code:copy_starpls

That's the output of

bazel query --infer_universe_scope ... intersect 'allrdeps("crates/starpls/src/main.rs")'

Very similar to 'find all references', but specific to starlark/bazel.

This would be especially helpful when working with C++ where I constantly have to refer to the cc_library / cc_binary where a source file is being used.

I'm happy to give a shot at developing this if you think it's appropriate in starpls. Let me know!

withered-magic commented 5 months ago

I definitely think this functionality would be super useful and should be pretty straightforward to implement with how everything's currently set up! I think the only outstanding thing would be what the UX for this command would look like - typically you'd use the Find References request to handle this, but that expects a specific position in the file, whereas this command would refer to the whole file in general?

The other approach that comes to mind is, as you mentioned, a custom command (one not defined by the LSP spec). I have a couple of these here but they only work with the extension bundled in this repo (which is pretty useless outside of debugging parser issues), and not the official vscode-bazel extension. So if we went w/ the custom command approach, then we'd probably have to upstream support for it into vscode-bazel as well which could potentially be a blocker.

Alternatively, we could just do something simple like, if you issue a Find References from blank space (or something otherwise meaningless) in a file, then it would just default to the behavior described here!

But definitely feel free to take a crack at it and lmk what you're thinking re: UX! Happy to answer questions about how stuff is set up as well