Skip to content
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

Add jump to view code lenses #412

Merged
merged 1 commit into from
Jul 19, 2024
Merged

Add jump to view code lenses #412

merged 1 commit into from
Jul 19, 2024

Conversation

vinistock
Copy link
Member

@vinistock vinistock commented Jul 17, 2024

Closes #384

This PR adds the ability to jump to related views from a controller action.

Implementation:

  1. Updated the ruby-lsp just to get proper ERB support
  2. Started returning the Rails.root from the server initialization response. We often need to know where the application exists from the addon side
  3. Used the Root and the controller information to discover which views exist for a given action
Screen.Recording.2024-07-17.at.9.57.46.AM.mov

@vinistock vinistock added the enhancement New feature or request label Jul 17, 2024
@vinistock vinistock self-assigned this Jul 17, 2024
@vinistock vinistock requested a review from a team as a code owner July 17, 2024 14:02
@vinistock vinistock requested review from andyw8 and st0012 July 17, 2024 14:02
@vinistock vinistock force-pushed the vs/add_jump_to_view_lenses branch from 1a7e37d to 5356284 Compare July 17, 2024 16:59
def add_jump_to_view(node)
class_name, _ = T.must(@constant_name_stack.last)
action_name = node.name
controller_name = class_name.delete_suffix("Controller").gsub(/([a-z])([A-Z])/, "\\1_\\2").downcase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the intention behind the gsub here, can you explain?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, now I see, it's to find the individual words, since that's where there's a change in case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(is this copied straight from Rails?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I wrote it myself. It's turning the controller name into the directory path.

action_name = node.name
controller_name = class_name.delete_suffix("Controller").gsub(/([a-z])([A-Z])/, "\\1_\\2").downcase

view_uris = Dir.glob("#{@client.rails_root}/app/views/#{controller_name}/#{action_name}*").map! do |path|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could get this programatically:

irb(main):004> UsersController.view_paths.map(&:path)
=> ["/Users/andyw8/src/github.com/Shopify/ruby-lsp-rails/test/dummy/app/views"]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(would require a little more plumbing for the server request of course)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like that method is returning what we want though. It's returning the views directory, but not the directory for the UsersController views. Is there a way to make it return it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but couldn't find an obvious way.

lib/ruby_lsp/ruby_lsp_rails/runner_client.rb Show resolved Hide resolved
@vinistock vinistock force-pushed the vs/add_jump_to_view_lenses branch from 5356284 to 3256ff4 Compare July 17, 2024 20:06
@vinistock vinistock requested review from st0012 and andyw8 July 17, 2024 20:08
@vinistock vinistock merged commit b0f848f into main Jul 19, 2024
40 checks passed
@vinistock vinistock deleted the vs/add_jump_to_view_lenses branch July 19, 2024 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code Lens for view lookup
3 participants