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

Switch block style refactoring needs improvement #2968

Open
vadviktor opened this issue Dec 10, 2024 · 3 comments · May be fixed by #3005
Open

Switch block style refactoring needs improvement #2968

vadviktor opened this issue Dec 10, 2024 · 3 comments · May be fixed by #3005
Labels
bug Something isn't working help-wanted Extra attention is needed pinned This issue or pull request is pinned and won't be marked as stale

Comments

@vadviktor
Copy link

Description

Ruby LSP Information

VS Code Version

1.95.3

Ruby LSP Extension Version

0.8.16

Ruby LSP Server Version

0.22.1

Ruby LSP Addons

  • Ruby LSP RSpec
  • Ruby LSP Rails

Ruby Version

3.3.6

Ruby Version Manager

none

Installed Extensions

Click to expand
  • bash-ide-vscode (1.43.0)
  • copilot (1.249.0)
  • copilot-chat (0.22.4)
  • gitlab-workflow (5.21.0)
  • gitlens (16.0.5)
  • gitmoji-vscode (1.2.5)
  • gremlins (0.26.0)
  • hexeditor (1.11.1)
  • markdown-all-in-one (3.6.2)
  • markdown-mermaid (1.27.0)
  • ruby-lsp (0.8.16)
  • sass-indented (1.8.31)
  • shellcheck (0.37.1)
  • sorbet-vscode-extension (0.3.37)
  • sort-json (19.1.2)
  • sql-formatter-vsc (4.1.3)
  • svg (1.5.4)
  • text-power-tools (1.50.0)
  • todo-tree (0.0.226)
  • vscode-env (0.1.0)
  • vscode-eslint (3.0.10)
  • vscode-gitweblinks (2.12.0)
  • vscode-gutter-preview (0.32.2)
  • vscode-rdbg (0.2.2)
  • vscode-rubocop (0.7.0)

Ruby LSP Settings

Click to expand
Workspace
{
  "rubyVersionManager": {
    "identifier": "none"
  },
  "formatter": "none"
}
User
{
  "enabledFeatures": {
    "codeActions": true,
    "diagnostics": true,
    "documentHighlights": true,
    "documentLink": true,
    "documentSymbols": true,
    "foldingRanges": true,
    "formatting": true,
    "hover": true,
    "inlayHint": true,
    "onTypeFormatting": true,
    "selectionRanges": true,
    "semanticHighlighting": true,
    "completion": true,
    "codeLens": true,
    "definition": true,
    "workspaceSymbol": true,
    "signatureHelp": true,
    "typeHierarchy": true
  },
  "featuresConfiguration": {},
  "addonSettings": {},
  "rubyVersionManager": {
    "identifier": "none"
  },
  "customRubyCommand": "",
  "formatter": "none",
  "linters": null,
  "bundleGemfile": "",
  "testTimeout": 30,
  "branch": "",
  "pullDiagnosticsOn": "both",
  "useBundlerCompose": false,
  "bypassTypechecker": false,
  "rubyExecutablePath": "",
  "indexing": {},
  "erbSupport": true,
  "featureFlags": {}
}

Reproduction steps

  1. Start the Ruby LSP using a VSCode
  2. Open a Ruby file
  3. Find a code with a block returning a Hash like this one:
arr.map do |a|
  {
    id: a.id,
    name: a.name
  }
end
  1. We'd expect (or at least I do) the Hash to be preserved as it is and only the do ... end pairs to be turned into { ... } ones. Instead, we get this:
arr.map { |a| {;    id: a.id,;    name: a.name;  } }

LSP reports no errors; thus, I assume the support for this operation needs improvements.

I was let know of the code for this operation to be here:

def switch_block_body(body, indentation)
# Check if there are any nested blocks inside of the current block
body_loc = body.location
nested_block = @document.locate_first_within_range(
{
start: { line: body_loc.start_line - 1, character: body_loc.start_column },
end: { line: body_loc.end_line - 1, character: body_loc.end_column },
},
node_types: [Prism::BlockNode],
)
body_content = body.slice.dup
# If there are nested blocks, then we change their style too and we have to mutate the string using the
# relative position in respect to the beginning of the body
if nested_block.is_a?(Prism::BlockNode)
location = nested_block.location
correction_start = location.start_offset - body_loc.start_offset
correction_end = location.end_offset - body_loc.start_offset
next_indentation = indentation ? "#{indentation} " : nil
body_content[correction_start...correction_end] =
recursively_switch_nested_block_styles(nested_block, next_indentation)
end
indentation ? body_content.gsub(";", "\n") : "#{body_content.gsub("\n", ";")} "
end
end

@vadviktor vadviktor added bug Something isn't working vscode This pull request should be included in the VS Code extension's release notes labels Dec 10, 2024
@andyw8 andyw8 removed the vscode This pull request should be included in the VS Code extension's release notes label Dec 10, 2024
@vinistock vinistock added the pinned This issue or pull request is pinned and won't be marked as stale label Dec 13, 2024
@vinistock
Copy link
Member

Thank you for the bug report! This is an issue with the way we're handling the body of the block, which is too simplistic. It currently only converts the line breaks into semi colons, but that will not always produce valid code.

@vinistock vinistock added the help-wanted Extra attention is needed label Dec 13, 2024
@Hungle2911
Copy link
Contributor

@vinistock I would love to work on this if possible

@vinistock
Copy link
Member

Of course! Don't hesitate to reach out if you want to discuss the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help-wanted Extra attention is needed pinned This issue or pull request is pinned and won't be marked as stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants