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

Auto indent after end keyword #1014

Merged
merged 13 commits into from
Dec 6, 2023
Merged

Auto indent after end keyword #1014

merged 13 commits into from
Dec 6, 2023

Conversation

giovannism20
Copy link
Contributor

Motivation

Closes #272

Implementation

I wrote something based on actual code and not too complex. Basically, I just verified the exact position and back to the place where the indentation was wrong, adjusted it, and then back to the final position.

Automated Tests

Added test case for formatting indentation.

@giovannism20 giovannism20 requested a review from a team as a code owner September 17, 2023 00:13
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

This is looking great!

lib/ruby_lsp/requests/on_type_formatting.rb Outdated Show resolved Hide resolved
lib/ruby_lsp/requests/on_type_formatting.rb Outdated Show resolved Hide resolved
@vinistock
Copy link
Member

We need to add d as a completion character in the on type formatter provider configuration here.

Also, can we change the implementation to use node locating? I think it's going to be more robust. Something like this

# ...
        when "d"
          auto_indent_after_end_keyword
# ...

      def auto_indent_after_end_keyword
        # We need to look for the end in the current line, not the previous one
        current_line = @lines[@position[:line]]
        return unless current_line&.strip == "end"

        # Instead of relying on regexes, we can try to find a Statements node on top
        # of the triggered position
        target, _parent, _nesting = T.cast(
          @document.locate_node(@position, node_types: [YARP::StatementsNode]),
          [T.nilable(YARP::StatementsNode), T.nilable(YARP::Node), T::Array[String]],
        )
        return unless target

        # If there is a statements node, then we want to push every node inside body
        # two characters forward
        target.body.each do |node|
          add_edit_with_text(...)
        end

        move_cursor_to(@position[:line], @position[:character])
      end

@giovannism20
Copy link
Contributor Author

Ok, I'll change it!

@andyw8 andyw8 removed their request for review November 1, 2023 13:35
@giovannism20
Copy link
Contributor Author

Should I close this pull request ?
@vinistock

Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

I double checked the implementation and made some fixes. With the updated code, the auto-indent works as intended.

Can you please apply the suggestions and rebase the PR? Then we can ship it

lib/ruby_lsp/requests/on_type_formatting.rb Show resolved Hide resolved
test/requests/on_type_formatting_test.rb Show resolved Hide resolved
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

Thank you very much for the contribution!

@vinistock vinistock merged commit 57bd213 into Shopify:main Dec 6, 2023
13 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto format indentation on type
2 participants