mirror of
https://github.com/vim/vim.git
synced 2026-05-28 00:21:37 +02:00
runtime(rust): Fix Rust indentation when string contains "if"
indent/rust.vim behaves incorrectly when a string literal contains the
substring "if".
For example, in this code:
let x = "
motif
";
struct X {
}
indent/rust.vim thinks that the closing "}" should line up with "motif".
This patch fixes the issue by checking whether the "if" is in a string
literal or comment before considering it to be a match for a subsequent
brace (and also by requiring it to start on a word boundary).
Add an indent test to ensure this does not regress.
closes: #19265
Signed-off-by: taylor.fish <contact@taylor.fish>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
427fa3e1e4
commit
663d809194
+19
-3
@@ -5,6 +5,7 @@
|
||||
" 2024 Jul 04 by Vim Project: use shiftwidth() instead of hard-coding shifted values #15138
|
||||
" 2025 Dec 29 by Vim Project: clean up
|
||||
" 2025 Dec 31 by Vim Project: correcly indent after nested array literal #19042
|
||||
" 2026 Jan 28 by Vim Project: fix indentation when a string literal contains 'if' #19265
|
||||
|
||||
" For bugs, patches and license go to https://github.com/rust-lang/rust.vim
|
||||
" Note: upstream seems umaintained: https://github.com/rust-lang/rust.vim/issues/502
|
||||
@@ -139,9 +140,24 @@ function GetRustIndent(lnum)
|
||||
let l:standalone_close = line =~# '\V\^\s\*}\s\*\$'
|
||||
let l:standalone_where = line =~# '\V\^\s\*where\s\*\$'
|
||||
if l:standalone_open || l:standalone_close || l:standalone_where
|
||||
" ToDo: we can search for more items than 'fn' and 'if'.
|
||||
let [l:found_line, l:col, l:submatch] =
|
||||
\ searchpos('\<\(fn\)\|\(if\)\>', 'bnWp')
|
||||
let l:orig_line = line('.')
|
||||
let l:orig_col = col('.')
|
||||
let l:i = 0
|
||||
while 1
|
||||
" ToDo: we can search for more items than 'fn' and 'if'.
|
||||
let [l:found_line, l:col, l:submatch] =
|
||||
\ searchpos('\<\(fn\|if\)\>', 'bWp')
|
||||
if l:found_line ==# 0 || !s:is_string_comment(l:found_line, l:col)
|
||||
break
|
||||
endif
|
||||
let l:i += 1
|
||||
" Limit to 10 iterations as a failsafe against endless looping.
|
||||
if l:i >= 10
|
||||
let l:found_line = 0
|
||||
break
|
||||
endif
|
||||
endwhile
|
||||
call cursor(l:orig_line, l:orig_col)
|
||||
if l:found_line !=# 0
|
||||
" Now we count the number of '{' and '}' in between the match
|
||||
" locations and the current line (there is probably a better
|
||||
|
||||
@@ -40,3 +40,11 @@ fn main() {
|
||||
// file goes out of scope, and the "hello.txt" file gets closed
|
||||
}
|
||||
// END_INDENT
|
||||
|
||||
// START_INDENT
|
||||
let x = "
|
||||
if fn motif
|
||||
";
|
||||
struct X {
|
||||
}
|
||||
// END_INDENT
|
||||
|
||||
@@ -40,3 +40,11 @@ fn main() {
|
||||
// file goes out of scope, and the "hello.txt" file gets closed
|
||||
}
|
||||
// END_INDENT
|
||||
|
||||
// START_INDENT
|
||||
let x = "
|
||||
if fn motif
|
||||
";
|
||||
struct X {
|
||||
}
|
||||
// END_INDENT
|
||||
|
||||
Reference in New Issue
Block a user