Skip to content

Switch block style refactoring needs improvement #2968

Open
@vadviktor

Description

@vadviktor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp-wantedExtra attention is neededpinnedThis issue or pull request is pinned and won't be marked as stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions