From 43c4ccc45437e28e880213cd89075346c89f14c1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 12:22:28 +0000 Subject: [PATCH 1/5] fix(server): format ERB files with inline script and style helpers One `<%= tag.style x %>` made the formatter leave a whole forem view unchanged: any script or style helper call was taken for a raw-text block. Only a helper that opens a block holds raw text, so only that still makes the formatter skip the file. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012jSfzj1UNFUHV9YmmbjLg4 --- CHANGELOG.md | 1 + server/src/erb/tags.rs | 5 +++-- server/src/format/erb.rs | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6e5cac..6db314b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `Rails/MissingPartial` finds a partial in the format of the template that renders it, whichever gem or initializer registered that format: `index.mobile.haml` renders `_card.mobile.haml` (`mobile-fu`'s `:mobile`, a custom `.amp`) without a report. - `Rails/UnsafeOutput` no longer reports a safe call with a trailing `if` or `unless` (`== render "a" if x`, `<%== render "a" unless x %>`, `!= render "a" if x`). - `ruby-templates check` and `format` skip `lib/templates` (Rails generator templates), `vendor`, `tmp`, `public` and `log` when walking a folder, as well as hidden folders and `node_modules`; `app/views` may hold folders with those names, and files named on the command line are always processed. `.gitignore` is not read. +- ERB formatting no longer refuses a file for an inline `<%= tag.style x %>`, `<%= tag.script(src: x) %>`, `<%= javascript_tag "x" %>` or `content_tag :script, x`; only the block forms (`javascript_tag do`) still leave the file as it is. - **Behaviour change:** `Rails/InstanceVariableInPartial` is information instead of a warning, so `ruby-templates check` no longer fails on it; the editor still shows it. - **Breaking:** the kebab-case diagnostic codes from before 0.11 (`img-missing-alt`, `unresolved-render`, ...) are no longer accepted in `disabledDiagnostics`, in the VS Code settings or `.ruby-templates.toml`. Use the rule names (`Html/ImgAlt`, `Rails/MissingPartial`), listed in the diagnostics reference; the toml reports an old code as an unknown rule. - **Breaking:** `ruby-templates-ls --check` and `ruby-templates-ls --format` are removed. Use `ruby-templates check` and `ruby-templates format`. diff --git a/server/src/erb/tags.rs b/server/src/erb/tags.rs index f4935f5..224bfc6 100644 --- a/server/src/erb/tags.rs +++ b/server/src/erb/tags.rs @@ -403,7 +403,7 @@ pub(crate) fn tag_end(html: &[u8], from: usize) -> Option { /// HTML with ERB tags blanked out, plus every ERB tag as an event, in order. Tags are Erubi's; a `<%%` literal /// renders as text, so it stays in the HTML. With `skip_raw_blocks` the JS/CSS block helpers' bodies are blanked and -/// their events dropped; without, any such helper makes the result `None`. +/// their events dropped; without, any such helper with a block makes the result `None`. fn split<'a>( text: &'a str, analyzed: &[(erb::ruby::ErbTag<'a>, BlockEffect)], @@ -423,7 +423,8 @@ fn split<'a>( (BlockEffect::Neutral, "", "") } else { let snippet = &text[tag.code.clone()]; - if RAW_BLOCK.is_match(snippet) && (!skip_raw_blocks || BRACE_BLOCK_OPEN.is_match(snippet)) { + let unreadable_block = BRACE_BLOCK_OPEN.is_match(snippet) || (!skip_raw_blocks && matches!(effect, BlockEffect::Open(_))); + if RAW_BLOCK.is_match(snippet) && unreadable_block { return None; } (*effect, snippet.split_whitespace().next().unwrap_or(""), snippet) diff --git a/server/src/format/erb.rs b/server/src/format/erb.rs index 55ee245..57e4a9b 100644 --- a/server/src/format/erb.rs +++ b/server/src/format/erb.rs @@ -823,6 +823,14 @@ mod tests { } } + #[test] + fn inline_script_and_style_helpers_without_a_block_are_indented() { + for helper in ["tag.style x", "tag.script(src: x)", "javascript_tag \"x\"", "content_tag :script, x"] { + let out = indent(&format!("
\n<%= {helper} %>\n
\n"), " ", false).unwrap(); + assert_eq!(out.text, format!("
\n <%= {helper} %>\n
\n"), "{helper}"); + } + } + fn with_blank_lines(text: &str, settings: &Settings) -> String { let indented = indent(text, " ", false).unwrap(); crate::format::blank_lines::apply(&indented.text, &indented.lines, settings, text) From 4cf2779a3d59c27f008a2508a745d2402ea856e4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 12:22:28 +0000 Subject: [PATCH 2/5] fix(server): honour insert_final_newline from .editorconfig for ERB Layout/TrailingEmptyLines is off by default for ERB, so an .editorconfig asking for a final newline had no effect there. The setting now turns the rule on unless .ruby-templates.toml turns it off. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012jSfzj1UNFUHV9YmmbjLg4 --- CHANGELOG.md | 1 + server/src/config.rs | 36 ++++++++++++++------ server/src/format.rs | 3 ++ server/src/rules/resolve.rs | 38 ++++++++++++++++++---- site/src/content/docs/guides/formatting.md | 2 +- 5 files changed, 63 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6db314b..2508fe5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - `Rails/UnsafeOutput` no longer reports a safe call with a trailing `if` or `unless` (`== render "a" if x`, `<%== render "a" unless x %>`, `!= render "a" if x`). - `ruby-templates check` and `format` skip `lib/templates` (Rails generator templates), `vendor`, `tmp`, `public` and `log` when walking a folder, as well as hidden folders and `node_modules`; `app/views` may hold folders with those names, and files named on the command line are always processed. `.gitignore` is not read. - ERB formatting no longer refuses a file for an inline `<%= tag.style x %>`, `<%= tag.script(src: x) %>`, `<%= javascript_tag "x" %>` or `content_tag :script, x`; only the block forms (`javascript_tag do`) still leave the file as it is. +- `insert_final_newline = true` in `.editorconfig` turns `Layout/TrailingEmptyLines` on for ERB (off by default there); `enabled = false` in a `.ruby-templates.toml` still wins. - **Behaviour change:** `Rails/InstanceVariableInPartial` is information instead of a warning, so `ruby-templates check` no longer fails on it; the editor still shows it. - **Breaking:** the kebab-case diagnostic codes from before 0.11 (`img-missing-alt`, `unresolved-render`, ...) are no longer accepted in `disabledDiagnostics`, in the VS Code settings or `.ruby-templates.toml`. Use the rule names (`Html/ImgAlt`, `Rails/MissingPartial`), listed in the diagnostics reference; the toml reports an old code as an unknown rule. - **Breaking:** `ruby-templates-ls --check` and `ruby-templates-ls --format` are removed. Use `ruby-templates check` and `ruby-templates format`. diff --git a/server/src/config.rs b/server/src/config.rs index 15ed206..0098c88 100644 --- a/server/src/config.rs +++ b/server/src/config.rs @@ -1,5 +1,5 @@ -//! The indentation width and style of a file, from the project's `.editorconfig`. The editor's tab size and `insertSpaces` go on top -//! in the language server (`Settings::with_editor`). Every other property is ignored and listed in the notes. +//! The indentation width and style and the final newline of a file, from the project's `.editorconfig`. The editor's tab size and +//! `insertSpaces` go on top in the language server (`Settings::with_editor`). Every other property is ignored and listed in the notes. use crate::format::{IndentStyle, Settings}; use crate::glob::Glob; @@ -15,17 +15,21 @@ pub struct Resolved { pub fn settings_for(path: &Path) -> Resolved { let mut notes = Vec::new(); let defaults = Settings::default(); - let (size, style) = editorconfig_indent(path, &mut notes); - let settings = - Settings { indent_size: size.unwrap_or(defaults.indent_size), indent_style: style.unwrap_or(defaults.indent_style), ..defaults }; + let properties = editorconfig_properties(path, &mut notes); + let settings = Settings { + indent_size: indent_size(&properties).unwrap_or(defaults.indent_size), + indent_style: indent_style(&properties).unwrap_or(defaults.indent_style), + insert_final_newline: property(&properties, "insert_final_newline") == Some("true"), + ..defaults + }; Resolved { settings, notes } } -/// The indentation width and style from the `.editorconfig` properties that apply to `path`: files from the root (or the topmost -/// `root = true`) down, later sections and nearer files winning, as the EditorConfig spec has it. -fn editorconfig_indent(path: &Path, notes: &mut Vec) -> (Option, Option) { +/// The `.editorconfig` properties that apply to `path`: files from the root (or the topmost `root = true`) down, later sections +/// and nearer files winning, as the EditorConfig spec has it. +fn editorconfig_properties(path: &Path, notes: &mut Vec) -> Vec<(String, String)> { if path.file_name().is_none() { - return (None, None); + return Vec::new(); } let mut properties = Vec::new(); for (dir, text) in editorconfig_files(path).iter().rev() { @@ -37,7 +41,7 @@ fn editorconfig_indent(path: &Path, notes: &mut Vec) -> (Option, for (key, value) in properties.iter().filter(|(k, v)| v != "unset" && !is_understood(k, v)) { notes.push(format!(".editorconfig: ignored {key} = {value}")); } - (indent_size(&properties), indent_style(&properties)) + properties } /// The folders above `path` that hold an `.editorconfig`, with its text: nearest first, up to the first `root = true`. @@ -109,6 +113,8 @@ fn is_understood(key: &str, value: &str) -> bool { "indent_style" => matches!(value, "space" | "tab"), "indent_size" => value == "tab" || positive_number(value).is_some(), "tab_width" => positive_number(value).is_some(), + // `false` changes nothing: the final newline is only ever added. + "insert_final_newline" => value == "true", _ => false, } } @@ -195,6 +201,16 @@ mod tests { assert!(project.settings("a.html.erb").notes.iter().all(|n| !n.contains("ignored")), "indent_style is understood"); } + #[test] + fn editorconfig_insert_final_newline_true_is_read() { + let text = "[*.erb]\ninsert_final_newline = true\n[*.slim]\ninsert_final_newline = false\n"; + let project = Project::new("newline", &[(".editorconfig", text), ("a.html.erb", ""), ("b.html.slim", ""), ("c.html.haml", "")]); + let newline = |file: &str| project.settings(file).settings.insert_final_newline; + assert_eq!((newline("a.html.erb"), newline("b.html.slim"), newline("c.html.haml")), (true, false, false)); + let notes = project.settings("a.html.erb").notes; + assert!(notes.iter().all(|n| !n.contains("ignored")), "{notes:?}"); + } + #[test] fn editorconfig_tab_width_and_unreadable_values() { let text = "[*]\nindent_style = space\ntab_width = 8\ntrim_trailing_whitespace = yes\n[[z-a].erb]\nindent_size = 3\n"; diff --git a/server/src/format.rs b/server/src/format.rs index 5af900b..7f1101d 100644 --- a/server/src/format.rs +++ b/server/src/format.rs @@ -26,6 +26,8 @@ use tower_lsp_server::ls_types::{Diagnostic, DiagnosticSeverity, FormattingOptio pub struct Settings { /// Columns per nesting level. pub indent_size: usize, + /// `.editorconfig`'s `insert_final_newline = true`: turns `Layout/TrailingEmptyLines` on where it is off by default (ERB). + pub insert_final_newline: bool, /// `Layout/TrailingEmptyLines`: end the file with exactly one line ending; off for a range, which can't change the lines. pub trailing_empty_lines: bool, /// `Layout/EmptyLines`: the longest run of blank lines kept; `None` leaves them all. @@ -76,6 +78,7 @@ impl Default for Settings { fn default() -> Self { Self { indent_size: 2, + insert_final_newline: false, trailing_empty_lines: true, max_empty_lines: None, align_comments: false, diff --git a/server/src/rules/resolve.rs b/server/src/rules/resolve.rs index eb88501..e8d22f7 100644 --- a/server/src/rules/resolve.rs +++ b/server/src/rules/resolve.rs @@ -34,6 +34,8 @@ pub struct ResolvedRules { /// `Layout/EmptyLines`: the longest run of blank lines kept; `None` when the rule is off. max_empty_lines: Option, trailing_empty_lines: bool, + /// `trailing_empty_lines` as an `insert_final_newline = true` in `.editorconfig` sets it: on unless the config turns the rule off. + trailing_empty_lines_with_final_newline: bool, around_block_body: Option