vinibispo / ruby.nvim

Ruby development plugin for neovim
MIT License
9 stars 0 forks source link

:RubyAlternate not working on "not test" file, just in test file #10

Open vinibispo opened 2 years ago

vinibispo commented 2 years ago

Step

gem install rails -v '7.0.0'
rails new test-app
cd test-app
rails g model User email:string
nvim app/models/user.rb -c RubyAlternate

@cuducos

cuducos commented 2 years ago

I was considering completely redesigning the implementation. Let me draft something here before we start writing code!

What we have now

Two hardcoded arrays, one for where to find the app/lib source code {"app", "lib", ""} and another one to find testes, {"test", "spec"}. Basically, we take the path of the current file, and use these locations to:

  1. replace the relative root directory and the file suffix (eg app/model/whatever.rb to spec/model/whatever_spec.rb)
  2. test if this new path exists (same in line 97 BTW)

In theory, it looks fine, but every now and then I find edge cases.

My new idea

Comparison

This will be a table like:

{
    path: "app/models/whatever.rb",
    parts: {"models", "whatever"},
}

Steps to build it:

Step App example Test example
Start with a path app/models/whatever.rb spec/models/whatever_spec.rb
Drop extension app/models/whatever spec/models/whatever_spec
Drop default directory names such as lib, app, test, spec models/whatever models/whatever_spec
Drop default test suffixes models/whatever models/whatever
Convert to an array {"models", "whatever"} {"models", "whatever"}

Cache

Probably at first use, we can walk the project directory and create these tables for every Ruby file in the project. This can take a while, but we cache.

:RubyAlternate

  1. Calculates the comparison object for the current path
  2. Find the closest match in the cache using something like Levenshtein algorithm to be tolerant to small deviances (e.g. app/controllers/login.rb would probably match with spec/requests/login_spec.rb)
  3. If we cannot find, we rebuild the cache once
  4. if we still cannot find, return nil
vinibispo commented 2 years ago

I think I'm going to work on this in the following weeks

cuducos commented 1 year ago

Or… maybe… we leave it to a 3rd party https://github.com/rgroli/other.nvim