ruby-lsp icon indicating copy to clipboard operation
ruby-lsp copied to clipboard

Add code action to add attribute for instance variable

Open vinistock opened this issue 1 year ago • 5 comments

Add new code actions to add a new attr_reader, attr_writer or attr_accessor for the given instance variable.

# BEFORE
class Foo
  def initialize
    @name = "Bar"
  end
end

# AFTER
class Foo
  attr_reader :name
 
  def initialize
    @name = "Bar"
  end
end

Here's an example PR adding a refactor end to end https://github.com/Shopify/ruby-lsp/pull/2372.

vinistock avatar Aug 28 '24 17:08 vinistock

Hi Team, I am looking at this issue.

What should be the code action kind for this?

ro-gun avatar Oct 16 '24 08:10 ro-gun

@rogancodes for now, just use your judgement to pick a name, we can easily change it before merging.

(I would argue that it shouldn't use the term 'refactor', since we are changing the external interface of the class).

andyw8 avatar Oct 16 '24 12:10 andyw8

The kind is only used to sort the actions in the editor's dropdown. I agree that this isn't exactly a refactor, so maybe we can go with empty? I think those appear at the bottom of the list.

vinistock avatar Oct 16 '24 13:10 vinistock

@vinistock, whenever only block node is selected for toggling block style. It gives InvalidTargetRange error.

https://github.com/user-attachments/assets/2bfc57c4-b7da-41e3-952c-0f7c914e6580

When I debugged it, I found that the locate_first_within_range method in RubyDocument doesn't capture the desired node if the start character of the selected region is the desired node itself.

I'm relying on this method to capture the first occurrence of the instance variable node. Is this behavior intentional, or should I address it?

ro-gun avatar Oct 19 '24 15:10 ro-gun

My bad, didnt realized that filtered node is call node, not the block node.

ro-gun avatar Oct 20 '24 05:10 ro-gun