Comments (14)
Well, since the initial goal is to get feature parity with a new core using RA I think this issue is fine to remain open.
I'm working on a prototype right now, in fact. 🙂
I however ended up requiring (and writing) a HIR visitor, not an AST visitor (as your crate provides), as we need canonicalized paths and other semantic info.
from cargo-modules.
Hi @pksunkara,
first of all, thanks for coming around! 👋🏻
From rust-analyzer/rust-analyzer#4405, you guys can use RA to run this.
That's exactly what I'm looking into right now (and reason for me commenting on the aforementioned issue). 😃
I have a visitor trait code (I can extract this to a separate crate) you guys can use.
That would be great, actually! Something like ra_ap_hir_visitor
?
But from what I understand, you guys do not need it and can do something like this
There are quite a few feature requests that would benefit from a full-featured visit, so having access to your trait Visitor
would be huge!
from cargo-modules.
Here is the visitor code (notice the macros to enable us to develop it faster). I haven't made it public but it's trivial to do so.
You can see it's usage here. And the call is here.
Look at it and if you are sure that you want to use this, I will extract it out immediately and publish. And then we can both keep adding to it as we need.
from cargo-modules.
@pksunkara I took the liberty to create a PR with an improved Visitor, adding missing node types as we would definitely need a few more for cargo-modules.
Also, yes please create a crate with it! 🙏🏻
from cargo-modules.
Thanks! I was actually just about to do start working on what you did. 😄
I will create a crate and publish it.
from cargo-modules.
@regexident Shall we close this? I assume you have everything you need to start and you have other issues that actually track the features.
from cargo-modules.
Hopefully you release that as a separate crate. I currently use a small subset of it too, but more manually written instead of visitor.
from cargo-modules.
I definitely will!
Or maybe we should rename rust_visitor::Visitor
to rust_visitor::ast::Visitor
and add it to rust_visitor
as rust_visitor::hir::Visitor
?
This would make discovery easier. You wouldn't have to guess that there's a separate 3rd party crate with a hir visitor.
And rust_visitor
is a sufficiently generic name to provide both.
from cargo-modules.
Sounds good to me. I added you to the repo. Feel free to commit to that crate directly
from cargo-modules.
👍
from cargo-modules.
Can you please point me to the hir visitor? I would like to extract it out and copy it.
from cargo-modules.
@pksunkara I ended up not using it. This was it before I remove the file:
hir_visitor.rs
use ra_ap_hir::{
self as hir, db::HirDatabase, Adt, BuiltinType, Const, Function, Module, ModuleDef, Static,
Trait, TypeAlias, Variant,
};
use ra_ap_ide_db::RootDatabase;
pub use ra_ap_syntax;
pub type Semantics<'db> = ra_ap_hir::Semantics<'db, RootDatabase>;
macro_rules! visiting {
() => {};
($($kind:ident => $method:ident as $type:ident,)*) => {
trait Visitable: Sized {
fn accept<T: Visitor>(&self, visitor: &mut T, db: &dyn HirDatabase);
}
impl Visitable for ModuleDef {
fn accept<T: Visitor>(&self, visitor: &mut T, db: &dyn HirDatabase) {
visitor.pre_visit(self);
match self {
$(ModuleDef::$kind(def) => def.accept(visitor, db),)*
};
visitor.post_visit(self);
}
}
pub trait Visitor: Sized {
/// Call this method to perform a in-order traversal on `node` and its children.
fn walk(&mut self, module_def: &ModuleDef, db: &dyn HirDatabase) {
module_def.accept(self, db);
}
/// This method is called before visiting a node.
fn pre_visit(&mut self, _module_def: &ModuleDef) {}
/// This method is called after visiting a node.
fn post_visit(&mut self, _module_def: &ModuleDef) {}
/// This method is called when visiting a node of the given type.
$(fn $method(&mut self, _def: &hir::$type) {})*
}
};
}
visiting!(
Module => visit_module as Module,
Function => visit_function as Function,
Adt => visit_adt as Adt,
Variant => visit_variant as Variant,
Const => visit_const as Const,
Static => visit_static as Static,
Trait => visit_trait as Trait,
TypeAlias => visit_type_alias as TypeAlias,
BuiltinType => visit_builtin_type as BuiltinType,
);
impl Visitable for Module {
fn accept<T: Visitor>(&self, visitor: &mut T, db: &dyn HirDatabase) {
visitor.visit_module(self);
for child in self.clone().declarations(db) {
child.accept(visitor, db);
}
}
}
impl Visitable for Function {
fn accept<T: Visitor>(&self, visitor: &mut T, _db: &dyn HirDatabase) {
visitor.visit_function(self);
}
}
impl Visitable for Adt {
fn accept<T: Visitor>(&self, visitor: &mut T, _db: &dyn HirDatabase) {
visitor.visit_adt(self);
}
}
impl Visitable for Variant {
fn accept<T: Visitor>(&self, visitor: &mut T, _db: &dyn HirDatabase) {
visitor.visit_variant(self);
}
}
impl Visitable for Const {
fn accept<T: Visitor>(&self, visitor: &mut T, _db: &dyn HirDatabase) {
visitor.visit_const(self);
}
}
impl Visitable for Static {
fn accept<T: Visitor>(&self, visitor: &mut T, _db: &dyn HirDatabase) {
visitor.visit_static(self);
}
}
impl Visitable for Trait {
fn accept<T: Visitor>(&self, visitor: &mut T, _db: &dyn HirDatabase) {
visitor.visit_trait(self);
}
}
impl Visitable for TypeAlias {
fn accept<T: Visitor>(&self, visitor: &mut T, _db: &dyn HirDatabase) {
visitor.visit_type_alias(self);
}
}
impl Visitable for BuiltinType {
fn accept<T: Visitor>(&self, visitor: &mut T, _db: &dyn HirDatabase) {
visitor.visit_builtin_type(self);
}
}
Feel free to add it to your rust-visitor crate. 👍
from cargo-modules.
Any reason why you didn't end up using it? I thought it would be perfect use case for you
from cargo-modules.
I needed more fine-grained control over how to walk the graph, i.e. drive the walk, rather than be driven by it.
from cargo-modules.
Related Issues (20)
- Dependency Dashboard
- Action Required: Fix Renovate Configuration
- Cargo install fails for latest version 0.5.12 HOT 14
- Allow generating tree/graph for a union of subgraphs of the project HOT 3
- Failed to install cargo-modules `0.6.0` HOT 2
- Option to remove external nodes? HOT 11
- Typo? HOT 1
- Add `--use-parents` option? HOT 1
- feat: dependency graph between files
- Does not build with latest `lsp-types` (0.94.1) HOT 4
- fails to install on windows HOT 3
- NetBSD package merged HOT 2
- Typo HOT 3
- feat: Sort by visibility HOT 3
- Analyze and forbid dependency from one mod to another? HOT 6
- "Error: Multiple packages present in workspace" but package is not a workspace HOT 1
- Installing cargo-modules 0.12.0 fails on 'ra_ap_hir_ty' HOT 13
- Fails to build for Alpine Linux (0.13.0) HOT 7
- cargo-modules structure --types --traits --fns --tests ==> error: unexpected argument '--types' found HOT 1
- dependencies command panicked: impl type node HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cargo-modules.