[vim/vim] vim filetype, wrong commentstring (Issue #20647)

4 views
Skip to first unread message

Maxim Kim

unread,
12:32 AM (5 hours ago) 12:32 AM
to vim/vim, Subscribed
habamax created an issue (vim/vim#20647)

Steps to reproduce

  1. Open vim, paste following
" Test for the comment package

CheckRunVimInTerminal
func Test_basic_comment()
  let lines =<< trim END
    vim9script

    def Hello()
      echo "Hello"
    enddef
  END

  let input_file = "test_basic_comment_input.vim"
  call writefile(lines, input_file, "D")

  let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
  call term_sendkeys(buf, "gcc")
  call term_sendkeys(buf, "2jgcip")
  let output_file = "comment_basic_test.vim"
  call term_sendkeys(buf, $":w {output_file}\<CR>")
  defer delete(output_file)

  call StopVimInTerminal(buf)
  let result = readfile(output_file)
  call assert_equal(["# vim9script", "", "# def Hello()", '#   echo "Hello"', "# enddef"], result)
endfunc
  1. :set ft=vim

commentstring is set to vim9script's #

Expected behaviour

commentstring should be set for legacy vim.

Version of Vim

9.2.735

Environment

debian13, bash

Logs and stack traces


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/20647@github.com>

Christian Brabandt

unread,
4:36 AM (1 hour ago) 4:36 AM
to vim/vim, Subscribed
chrisbra left a comment (vim/vim#20647)

Oh, that is not so nice. I see @dkearns already self-assigned (thanks for that 🙏 ), I think we may need to make the guessing a bit smarter.

The following should skip over heredocs and:

diff --git a/runtime/ftplugin/vim.vim b/runtime/ftplugin/vim.vim
index 21ca040fa..533f592e1 100644
--- a/runtime/ftplugin/vim.vim
+++ b/runtime/ftplugin/vim.vim
@@ -19,6 +19,26 @@ let b:did_ftplugin = 1
 let s:cpo_save = &cpo
 set cpo&vim

+func s:IsVim9Script()
+  let is_vim9 = v:false
+  let marker = ''
+  for line in getline(1, 32)
+    if !empty(marker)
+      " Inside a heredoc: look for the end marker
+      if line =~# $'^\s*{marker}$'
+        let marker = ''
+      endif
+      continue
+    endif
+    if line =~# '^\s*vim9\%[script]\>'
+      let is_vim9 = v:true
+      break
+    endif
+    let marker = matchstr(line, '=<<\s*\%(trim\s*\)\?\%(eval\s*\)\?\zs\w\+$')
+  endfor
+  return is_vim9
+endfunc
+
 if !exists('*VimFtpluginUndo')
   func VimFtpluginUndo()
     setl fo< isk< com< tw< commentstring< include< define< keywordprg< omnifunc< path<
@@ -107,7 +127,7 @@ command! -buffer -nargs=1 VimKeywordPrg :exe 'help' s:Help(<q-args>)
 setlocal keywordprg=:VimKeywordPrg

 " Comments starts with # in Vim9 script.  We have to guess which one to use.
-if "\n" .. getline(1, 32)->join("\n") =~# '\n\s*vim9\%[script]\>'
+if s:IsVim9Script()
   setlocal commentstring=#\ %s
   " Set 'comments' to format dashed lists in comments, for Vim9 script.
   setlocal com=sO:#\ -,mO:#\ \ ,eO:##,:#\\\ ,:#


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/20647/4816117704@github.com>

Reply all
Reply to author
Forward
0 new messages