From 000b4169d6959602d81ac33fc4d27a1a642b6f54 Mon Sep 17 00:00:00 2001 From: Krchi <997054144@qq.com> Date: Sun, 24 Aug 2025 15:49:10 +0800 Subject: [PATCH] fmt: fix comment line number in file with crlf line separator (fix #23524) (#25163) --- vlib/v/fmt/tests/.gitattributes | 2 ++ .../fmt/tests/vfmt_off_vfmt_on_with_crlf_expected.vv | 12 ++++++++++++ vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_input.vv | 12 ++++++++++++ vlib/v/scanner/scanner.v | 6 +++--- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 vlib/v/fmt/tests/.gitattributes create mode 100644 vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_expected.vv create mode 100644 vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_input.vv diff --git a/vlib/v/fmt/tests/.gitattributes b/vlib/v/fmt/tests/.gitattributes new file mode 100644 index 000000000..20f8e01d8 --- /dev/null +++ b/vlib/v/fmt/tests/.gitattributes @@ -0,0 +1,2 @@ +*_crlf_* binary +*_lf_* binary diff --git a/vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_expected.vv b/vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_expected.vv new file mode 100644 index 000000000..6e366f0b7 --- /dev/null +++ b/vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_expected.vv @@ -0,0 +1,12 @@ +module main + +fn main() { + // vfmt off + a := [ + 1, 2, 3, + 4, 5, 6, + ] + // vfmt on + + println('Hello World!') +} diff --git a/vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_input.vv b/vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_input.vv new file mode 100644 index 000000000..2d6bb1cea --- /dev/null +++ b/vlib/v/fmt/tests/vfmt_off_vfmt_on_with_crlf_input.vv @@ -0,0 +1,12 @@ +module main + +fn main() { + // vfmt off + a := [ + 1, 2, 3, + 4, 5, 6, + ] + // vfmt on + + println('Hello World!') +} diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index b4d621525..7e3a5f4cd 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -1068,11 +1068,11 @@ pub fn (mut s Scanner) text_scan() token.Token { mut comment_line_end := s.pos if s.text[s.pos - 1] == b_cr { comment_line_end-- - } else { - // fix line_nr, \n was read; the comment is marked on the next line s.pos-- - s.line_nr-- } + // fix line_nr, \n was read; the comment is marked on the next line + s.pos-- + s.line_nr-- if s.should_parse_comment() { s.line_comment = s.text[start + 1..comment_line_end] mut comment := s.line_comment -- 2.39.5