-
Notifications
You must be signed in to change notification settings - Fork 175
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
Code actions for attribute accessor creation #2739
Open
rogancodes
wants to merge
1
commit into
Shopify:main
Choose a base branch
from
rogancodes:feature/code_action_for_attribute_accessor_creation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
test/expectations/code_action_resolve/create_attribute_accessor.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"params": { | ||
"kind": "", | ||
"title": "Create Attribute Accessor", | ||
"data": { | ||
"range": { | ||
"start": { | ||
"line": 2, | ||
"character": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"character": 16 | ||
} | ||
}, | ||
"uri": "file:///fake" | ||
} | ||
}, | ||
"result": { | ||
"title": "Create Attribute Accessor", | ||
"edit": { | ||
"documentChanges": [ | ||
{ | ||
"textDocument": { | ||
"uri": "file:///fake", | ||
"version": null | ||
}, | ||
"edits": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 1, | ||
"character": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"character": 0 | ||
} | ||
}, | ||
"newText": " attr_accessor :foo\n\n" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
test/expectations/code_action_resolve/create_attribute_reader.exp.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"params": { | ||
"kind": "", | ||
"title": "Create Attribute Reader", | ||
"data": { | ||
"range": { | ||
"start": { | ||
"line": 2, | ||
"character": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"character": 16 | ||
} | ||
}, | ||
"uri": "file:///fake" | ||
} | ||
}, | ||
"result": { | ||
"title": "Create Attribute Reader", | ||
"edit": { | ||
"documentChanges": [ | ||
{ | ||
"textDocument": { | ||
"uri": "file:///fake", | ||
"version": null | ||
}, | ||
"edits": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 1, | ||
"character": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"character": 0 | ||
} | ||
}, | ||
"newText": " attr_reader :foo\n\n" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
test/expectations/code_action_resolve/create_attributer_writer.exp.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"params": { | ||
"kind": "", | ||
"title": "Create Attribute Writer", | ||
"data": { | ||
"range": { | ||
"start": { | ||
"line": 3, | ||
"character": 0 | ||
}, | ||
"end": { | ||
"line": 4, | ||
"character": 0 | ||
} | ||
}, | ||
"uri": "file:///fake" | ||
} | ||
}, | ||
"result": { | ||
"title": "Create Attribute Writer", | ||
"edit": { | ||
"documentChanges": [ | ||
{ | ||
"textDocument": { | ||
"uri": "file:///fake", | ||
"version": null | ||
}, | ||
"edits": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 2, | ||
"character": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"character": 0 | ||
} | ||
}, | ||
"newText": " attr_accessor :foo\n\n" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Foo | ||
def bar | ||
@foo = "foo" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Foo | ||
def bar | ||
@foo = "foo" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo | ||
class << self | ||
def bar | ||
@foo = "foo" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, we've been showing all refactoring code actions unconditionally because we only hand 2 or 3.
As this number grows, we are going to need to start being more selective about when to show things to the user.
What do you think about using the range with
Document#locate_first_within_range
and then we only make these refactors available if we found an instance variable inside?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good; we can proceed with that. For now,
Document#locate_first_within_range
should be sufficient for handling instance variables.However, if we later need to display a toggle block-style action based on the presence of a block in the selected region, we’ll need to call
Document#locate_first_within_range
again to locate the call node.If we create a method that accepts a list of desired nodes and returns only those within a specified range (similar to Document#locate_first_within_range but designed to find all specified options rather than just one). we could then use this list to determine which code actions to provide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vinistock Which approach should we take to move forward for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, just getting back to this now. Let's use
locate_first_within_range
to only show the code actions if we're on an instance variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's consider this user behavior: we have an InstanceVariableWriteNode like @foo = 1, and the user's selection includes only @foo. Using
locate_first_within_range
would return nil since the selection range does not cover the entire node, preventing us from providing attribute-related actions. The same applies to all types of write nodes.Is there an alternative approach or implementation in our library to detect such cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. I think you will need to account for both the case where the whole node is selected or for when the cursor is sitting on the node. Something like this: