From 15e2560a3c09cb880d3e7eff0184e3d193e21ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 30 May 2026 11:44:39 +0300 Subject: [PATCH 1/2] Support ex: in vim modelines Ref https://vimdoc.sourceforge.net/htmldoc/options.html#ex%3A Release Notes: - Support ex: identifier in vim modelines. --- crates/language/src/modeline.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/language/src/modeline.rs b/crates/language/src/modeline.rs index 8b7e6044492fc6..725844045b063b 100644 --- a/crates/language/src/modeline.rs +++ b/crates/language/src/modeline.rs @@ -209,9 +209,9 @@ static VIM_MODELINE_PATTERNS: LazyLock> = LazyLock::new(|| { [ // Second form: [text{white}]{vi:vim:Vim:}[white]se[t] {options}:[text] // Allow escaped colons in options: match non-colon chars or backslash followed by any char - r"(?:^|\s)(vi|vim|Vim):(?:\s*)se(?:t)?\s+((?:[^\\:]|\\.)*):", + r"(?:^|\s)(vi|vim|Vim|ex):(?:\s*)se(?:t)?\s+((?:[^\\:]|\\.)*):", // First form: [text{white}]{vi:vim:}[white]{options} - r"(?:^|\s+)(vi|vim):(?:\s*(.+))", + r"(?:^|\s+)(vi|vim|ex):(?:\s*(.+))", ] .iter() .map(|pattern| Regex::new(pattern).expect("valid regex")) @@ -465,6 +465,17 @@ mod tests { ..Default::default() } ); + + // Test ex: + let content = "ex: filetype=shell"; + let settings = parse_modeline(&[content], &[]).unwrap(); + assert_eq!( + settings, + ModelineSettings { + mode: Some("shell".to_string()), + ..Default::default() + } + ); } #[test] From ea22456f9263efcc58ab1e5c2e50c830e3686f61 Mon Sep 17 00:00:00 2001 From: dino Date: Mon, 1 Jun 2026 19:43:20 +0100 Subject: [PATCH 2/2] refactor: require whitespace before ex marker --- crates/language/src/modeline.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/language/src/modeline.rs b/crates/language/src/modeline.rs index 725844045b063b..056b93e716d6a6 100644 --- a/crates/language/src/modeline.rs +++ b/crates/language/src/modeline.rs @@ -205,13 +205,16 @@ fn parse_vim_modelines(modelines: &[&str], settings: &mut ModelineSettings) { } } +// Vim requires whitespace before the `{vi:|vim:|ex:}` marker. As an exception, +// `vi:` and `vim:` may also appear at the start of a line, but `ex:` may not as +// Vim ignores a leading `ex:` since it could be short for `example:`. static VIM_MODELINE_PATTERNS: LazyLock> = LazyLock::new(|| { [ - // Second form: [text{white}]{vi:vim:Vim:}[white]se[t] {options}:[text] + // Second form: [text{white}]{vi:|vim:|Vim:|ex:}[white]se[t] {options}:[text] // Allow escaped colons in options: match non-colon chars or backslash followed by any char - r"(?:^|\s)(vi|vim|Vim|ex):(?:\s*)se(?:t)?\s+((?:[^\\:]|\\.)*):", - // First form: [text{white}]{vi:vim:}[white]{options} - r"(?:^|\s+)(vi|vim|ex):(?:\s*(.+))", + r"(?:(?:^|\s)(?:vi|vim|Vim)|\sex):\s*se(?:t)?\s+((?:[^\\:]|\\.)*):", + // First form: [text{white}]{vi:|vim:|ex:}[white]{options} + r"(?:(?:^|\s)(?:vi|vim)|\sex):\s*(.+)", ] .iter() .map(|pattern| Regex::new(pattern).expect("valid regex")) @@ -225,7 +228,7 @@ static VIM_MODELINE_PATTERNS: LazyLock> = LazyLock::new(|| { fn parse_vim_modeline(line: &str, settings: &mut ModelineSettings) { for re in VIM_MODELINE_PATTERNS.iter() { if let Some(captures) = re.captures(line) { - if let Some(options) = captures.get(2) { + if let Some(options) = captures.get(1) { parse_vim_settings(options.as_str().trim(), settings); break; } @@ -466,8 +469,12 @@ mod tests { } ); - // Test ex: + // Using `ex:` should only be interpreted when whitespace separates it + // from the beginning of the line. let content = "ex: filetype=shell"; + assert_eq!(parse_modeline(&[content], &[]), None); + + let content = "\tex: filetype=shell"; let settings = parse_modeline(&[content], &[]).unwrap(); assert_eq!( settings,