]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.2.0649: filetype: tf files sometimes incorrectly recognized master v9.2.0649
authorChristian Brabandt <cb@256bit.org>
Sun, 14 Jun 2026 16:03:22 +0000 (16:03 +0000)
committerChristian Brabandt <cb@256bit.org>
Sun, 14 Jun 2026 16:04:16 +0000 (16:04 +0000)
Problem:  filetype: tf files sometimes incorrectly recognized
          (Christian Robinson)
Solution: Add support for g;filetype_tf variable to override detection,
          re-write the filetype detection loop

closes: #20510

Supported by AI

Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/autoload/dist/ft.vim
runtime/doc/filetype.txt
src/testdir/test_filetype.vim
src/version.c

index f59d95af49593ae19f8892a71024ea4070a9e345..8de0176216528d83bc241aa18301ced322e45010 100644 (file)
@@ -3,7 +3,7 @@ vim9script
 # Vim functions for file type detection
 #
 # Maintainer:          The Vim Project <https://github.com/vim/vim>
-# Last Change:         2026 May 29
+# Last Change:         2026 Jun 14
 # Former Maintainer:   Bram Moolenaar <Bram@vim.org>
 
 # These functions are moved here from runtime/filetype.vim to make startup
@@ -1498,14 +1498,25 @@ enddef
 
 # Determine if a *.tf file is TF mud client or terraform
 export def FTtf()
-  var numberOfLines = line('$')
-  for i in range(1, numberOfLines)
-    var currentLine = trim(getline(i))
-    var firstCharacter = currentLine[0]
-    if firstCharacter !=? ";" && firstCharacter !=? "/" && firstCharacter !=? ""
-      setf terraform
-      return
+  if exists("g:filetype_tf")
+    exe "setf " .. g:filetype_tf
+    return
+  endif
+
+  var continuation: bool = false
+  for lnum in range(1, min([line("$"), 100]))
+    var line = getline(lnum)
+    # TF supports backslash line continuation, so a continued line may begin
+    # with any character.  Only test the first character of a line that does
+    # not continue a previous one.
+    if !continuation
+      var firstchar = trim(line)[0]
+      if firstchar !=? ";" && firstchar !=? "/" && firstchar !=? ""
+        setf terraform
+        return
+      endif
     endif
+    continuation = line =~ '\\$'
   endfor
   setf tf
 enddef
index 36edcaab2f976744367b049551bad8c951ff01a1..23156275814a2214d026455922f32d94c0f29498 100644 (file)
@@ -1,4 +1,4 @@
-*filetype.txt* For Vim version 9.2.  Last change: 2026 Jun 13
+*filetype.txt* For Vim version 9.2.  Last change: 2026 Jun 14
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -183,6 +183,7 @@ variables can be used to overrule the filetype used for certain extensions:
        *.sys           g:filetype_sys
        *.sh            g:bash_is_sh            |ft-sh-syntax|
        *.tex           g:tex_flavor            |ft-tex-plugin|
+       *.tf            g:filetype_tf
        *.typ           g:filetype_typ
        *.v             g:filetype_v
        *.w             g:filetype_w            |ft-cweb-syntax|
index 30f803f2411e9baa063e1d6533a772d8991a8f91..61a72fd1f328cdb10e2f454de3689a8182ec1a29 100644 (file)
@@ -2546,6 +2546,25 @@ func Test_tf_file_v2()
   call assert_equal('terraform', &filetype)
   bwipe!
 
+  " A backslash continuation line may start with any character
+  let lines =<< trim END
+    /def greet = \
+        plain words that start the continued line
+    ;a comment
+  END
+  call writefile(lines, "Xfile.tf", "D")
+  split Xfile.tf
+  call assert_equal('tf', &filetype)
+  bwipe!
+
+  " The user override wins regardless of content
+  let g:filetype_tf = 'terraform'
+  call writefile([';;; looks like tf'], 'Xfile.tf', 'D')
+  split Xfile.tf
+  call assert_equal('terraform', &filetype)
+  bwipe!
+  unlet g:filetype_tf
+
   filetype off
 endfunc
 
index 329bf5a73e94fdb5c7130f52157129bc44b74ba3..392bae84c343ae5e8c08090e09ed551f7a41eee3 100644 (file)
@@ -759,6 +759,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    649,
 /**/
     648,
 /**/