Skip to content

VS Code Extension not auto-closing quotes on ERB files #3504

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

Open
TerrellAW opened this issue May 19, 2025 · 7 comments
Open

VS Code Extension not auto-closing quotes on ERB files #3504

TerrellAW opened this issue May 19, 2025 · 7 comments
Labels
bug Something isn't working good-first-issue Good for newcomers help-wanted Extra attention is needed

Comments

@TerrellAW
Copy link

Description

Ruby LSP Information

VS Code Version

1.100.1

Ruby LSP Extension Version

0.9.24

Ruby LSP Server Version

0.23.20

Ruby LSP Add-ons

  • Ruby LSP Rails
  • RuboCop

Ruby Version

3.4.3

Ruby Version Manager

mise

Installed Extensions

Click to expand
  • csdevkit (1.19.63)
  • csharp (2.76.27)
  • ruby-lsp (0.9.24)
  • rust-analyzer (0.3.2457)
  • vscode-docker (1.29.6)
  • vscode-dotnet-runtime (2.3.3)

Ruby LSP Settings

Click to expand
Workspace
{}
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": "auto"
  },
  "customRubyCommand": "",
  "formatter": "auto",
  "linters": null,
  "bundleGemfile": "",
  "testTimeout": 30,
  "branch": "",
  "pullDiagnosticsOn": "both",
  "useBundlerCompose": false,
  "bypassTypechecker": false,
  "rubyExecutablePath": "/home/mr-bones/.local/share/mise/installs/ruby/3.4.3/bin/ruby",
  "indexing": {},
  "erbSupport": true,
  "featureFlags": {},
  "sigOpacityLevel": "1"
}

Reproduction steps

  1. Try using quotes in an html.erb file
  2. Quotes don't auto close unless nothing else is on that line
@TerrellAW TerrellAW added the bug Something isn't working label May 19, 2025
@TerrellAW
Copy link
Author

The problem seems to occur specifically within tags when defining things such as CSS class.

@vinistock vinistock changed the title VS Code Extension not auto-closing quotes VS Code Extension not auto-closing quotes on ERB files May 20, 2025
@vinistock
Copy link
Member

Thanks for the report! This is likely some issue with our ERB language configuration for auto closing pairs.

@vinistock vinistock added good-first-issue Good for newcomers help-wanted Extra attention is needed labels May 20, 2025
@domingo2000
Copy link
Contributor

This behaviour is related to Auto closing before configuration of the language. As vscode says "By default, VS Code only autocloses pairs if there is whitespace right after the cursor.".

Example:

<h1>|</h1>

Will not autoclose the configured auto closing pairs.

It is possible to add the character < to the list in autoCloseBefore. But this configuration applies to all the characters defined in autoclosing rules. So it generates a tradeoff:

  • pros:
    • Help with autoclosing <% %> or <%= %> syntax common in erb.
  • cons:
    • Gives strange behaviour when writing text like <h1>this shouldn't be autoclosed</h1>. Because when writing the ' character it is going to be autoclosed. So the writter should have to move left and errase one char.

I belive the cons are worse. So in my opinion if its really needed the user can set the configuration always for autoclosing in their own settings.json file like this.

"[erb]": {
        "editor.autoClosingQuotes": "always",
        "editor.autoClosingBrackets": "always", # This also if brackets are also wanted.
    }

On the other hand, when the cursor is in <%|%> tags the autoclose should be enabled, so we can add % to the autoclose before config 🤔.

¿Do you guys see any border case that could give wierd behaviour?

@vinistock
Copy link
Member

Is it possible to configure the auto close to happen only inside ERB tags (<% %>)? If so, then I think it's fair to have it on inside, for Ruby code only and not the host language.

I agree that in other cases, the experience will likely be worse.

@TerrellAW
Copy link
Author

The behaviour could be double-quote only, or have separate configuration for double and single quotes to avoid problems with apostrophes. Alternatively the auto-closing can occur only within a <tag> rather than between them. If this is not enough, the auto-closing can still require a white space after so long as it works in multi-line tags.

@domingo2000
Copy link
Contributor

Is it possible to configure the auto close to happen only inside ERB tags (<% %>)? If so, then I think it's fair to have it on inside, for Ruby code only and not the host language.

I agree that in other cases, the experience will likely be worse.

I researched a little more here. The problem seems related to this issue in the VSCode implementation for embedded languages.

If we look at the grammar in erb.json we have not only the auto closing pairs for the ERB syntax, but also the grammar for ruby syntax. That is necessary right now because of the current bug. And if fixed should improve the auto closing in the ruby embedded language as defined here.

In the case that bug is resolved, the grammars in this repository should be updated for each to have the correct auto closing configuration for each language, the erb host, and the ruby embedded.

But the issue is from 2021 so probably is not going to be solved any soon.

@domingo2000
Copy link
Contributor

Another thing to consider is that when working on embedded languages when the cursor is just before the closing tag, the language is still the host language.

Here are some exaples for ERB and for Markdown

Case 1:
Image
Case 2:
Image
Case 3: Similar in markdown
Image

So even if the issue get's solved we are still in the erb context when typing "<%" because we land at case 2.

This could be improved by adding

  "autoClosingPairs": [
    ...
    ["<% ", " %>"],
    ...
   ]
  "autoCloseBefore": ";:.,=}])> \n\t%",

Based on the default autoCloseBefore we only add % to the autoCloseBefore to have better autoclosing moving from case 2 to case 1 with one space. Following erb lint recomendation and leaving vscode in the ruby context.

Here I have a demo that showcase that configuration:

Before:

Screencast.from.27-05-25.23.39.06.webm

After:

Screencast.from.27-05-25.23.35.15.webm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good-first-issue Good for newcomers help-wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants