Giter Club home page Giter Club logo

Comments (14)

regexident avatar regexident commented on May 28, 2024 1

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.

regexident avatar regexident commented on May 28, 2024

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.

pksunkara avatar pksunkara commented on May 28, 2024

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.

regexident avatar regexident commented on May 28, 2024

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

pksunkara avatar pksunkara commented on May 28, 2024

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.

pksunkara avatar pksunkara commented on May 28, 2024

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

pksunkara avatar pksunkara commented on May 28, 2024

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.

regexident avatar regexident commented on May 28, 2024

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.

pksunkara avatar pksunkara commented on May 28, 2024

Sounds good to me. I added you to the repo. Feel free to commit to that crate directly

from cargo-modules.

regexident avatar regexident commented on May 28, 2024

👍

from cargo-modules.

pksunkara avatar pksunkara commented on May 28, 2024

Can you please point me to the hir visitor? I would like to extract it out and copy it.

from cargo-modules.

regexident avatar regexident commented on May 28, 2024

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

pksunkara avatar pksunkara commented on May 28, 2024

Any reason why you didn't end up using it? I thought it would be perfect use case for you

from cargo-modules.

regexident avatar regexident commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.