diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-08-23 19:32:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-23 19:32:11 +0800 |
commit | 08fa71fd274530be23b6ae6b10222afee0b57522 (patch) | |
tree | c53c140547720c821d377020c9f4b007fcbbc7ee | |
parent | 2234b84a1b85832667ad4a23fd5dee0bd1c92b72 (diff) | |
download | rneovim-08fa71fd274530be23b6ae6b10222afee0b57522.tar.gz rneovim-08fa71fd274530be23b6ae6b10222afee0b57522.tar.bz2 rneovim-08fa71fd274530be23b6ae6b10222afee0b57522.zip |
vim-patch:9.0.1773: cannot distinguish Forth and Fortran *.f files (#24841)
Problem: cannot distinguish Forth and Fortran *.f files
Solution: Add Filetype detection Code
Also add *.4th as a Forth filetype
closes: vim/vim#12251
https://github.com/vim/vim/commit/19a3bc3addf9b4aa8150a01b11b4249c67d15d3b
Don't remove filetype files from Vim patches:
- filetype.vim, script.vim, ft.vim usually contain useful changes
- script.vim and ft.vim don't even have their paths spelled correctly
Co-authored-by: Doug Kearns <dougkearns@gmail.com>
-rw-r--r-- | runtime/doc/filetype.txt | 1 | ||||
-rw-r--r-- | runtime/doc/syntax.txt | 7 | ||||
-rw-r--r-- | runtime/lua/vim/filetype.lua | 5 | ||||
-rw-r--r-- | runtime/lua/vim/filetype/detect.lua | 40 | ||||
-rwxr-xr-x | scripts/vim-patch.sh | 6 | ||||
-rw-r--r-- | test/old/testdir/test_filetype.vim | 64 |
6 files changed, 108 insertions, 15 deletions
diff --git a/runtime/doc/filetype.txt b/runtime/doc/filetype.txt index 9ebdc71eb2..cf60ecdb6b 100644 --- a/runtime/doc/filetype.txt +++ b/runtime/doc/filetype.txt @@ -144,6 +144,7 @@ variables can be used to overrule the filetype used for certain extensions: `*.cls` g:filetype_cls `*.csh` g:filetype_csh |ft-csh-syntax| `*.dat` g:filetype_dat + `*.f` g:filetype_f |ft-forth-syntax| `*.frm` g:filetype_frm |ft-form-syntax| `*.fs` g:filetype_fs |ft-forth-syntax| `*.i` g:filetype_i |ft-progress-syntax| diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index 498c55389a..59f8235ad2 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -1542,9 +1542,10 @@ example, FORM files, use this in your startup vimrc: > FORTH *forth.vim* *ft-forth-syntax* -Files matching "*.fs" could be F# or Forth. If the automatic detection -doesn't work for you, or you don't edit F# at all, use this in your -startup vimrc: > +Files matching "*.f" could be Fortran or Forth and those matching "*.fs" could +be F# or Forth. If the automatic detection doesn't work for you, or you don't +edit F# or Fortran at all, use this in your startup vimrc: > + :let filetype_f = "forth" :let filetype_fs = "forth" diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index ffb284cca7..c310eb3e42 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -398,6 +398,7 @@ local extension = { EXW = detect.euphoria, ex = detect.ex, exp = 'expect', + f = detect.f, factor = 'factor', fal = 'falcon', fan = 'fan', @@ -410,8 +411,9 @@ local extension = { fish = 'fish', focexec = 'focexec', fex = 'focexec', - fth = 'forth', ft = 'forth', + fth = 'forth', + ['4th'] = 'forth', FOR = 'fortran', f77 = 'fortran', f03 = 'fortran', @@ -427,7 +429,6 @@ local extension = { F77 = 'fortran', f95 = 'fortran', FPP = 'fortran', - f = 'fortran', F = 'fortran', F08 = 'fortran', f08 = 'fortran', diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index 59b50f2288..4a7a1cbefe 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -473,6 +473,38 @@ function M.ex(_, bufnr) end end +--- @param bufnr integer +--- @return boolean +local function is_forth(bufnr) + local first_line = nextnonblank(bufnr, 1) + + -- SwiftForth block comment (line is usually filled with '-' or '=') or + -- OPTIONAL (sometimes precedes the header comment) + if first_line and findany(first_line:lower(), { '^%{%s', '^%{$', '^optional%s' }) then + return true + end + + for _, line in ipairs(getlines(bufnr, 1, 100)) do + -- Forth comments and colon definitions + if line:find('^[:(\\] ') then + return true + end + end + return false +end + +-- Distinguish between Forth and Fortran +--- @type vim.filetype.mapfn +function M.f(_, bufnr) + if vim.g.filetype_f then + return vim.g.filetype_f + end + if is_forth(bufnr) then + return 'forth' + end + return 'fortran' +end + -- This function checks the first 15 lines for appearance of 'FoamFile' -- and then 'object' in a following line. -- In that case, it's probably an OpenFOAM file @@ -518,16 +550,14 @@ function M.fvwm_v2(path, _) end end --- Distinguish between Forth and F#. +-- Distinguish between Forth and F# --- @type vim.filetype.mapfn function M.fs(_, bufnr) if vim.g.filetype_fs then return vim.g.filetype_fs end - for _, line in ipairs(getlines(bufnr, 1, 100)) do - if line:find('^[:(\\] ') then - return 'forth' - end + if is_forth(bufnr) then + return 'forth' end return 'fsharp' end diff --git a/scripts/vim-patch.sh b/scripts/vim-patch.sh index 3f411dac49..af73a8006a 100755 --- a/scripts/vim-patch.sh +++ b/scripts/vim-patch.sh @@ -202,9 +202,9 @@ preprocess_patch() { local na_src='auto\|configure.*\|GvimExt\|hardcopy.*\|libvterm\|proto\|tee\|VisVim\|xpm\|xxd\|Make.*\|INSTALL.*\|beval.*\|gui.*\|if_cscop\|if_lua\|if_mzsch\|if_olepp\|if_ole\|if_perl\|if_py\|if_ruby\|if_tcl\|if_xcmdsrv' 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/\S*\<\%(testdir/\)\@<!\%('"${na_src}"'\)\>@norm! d/\v(^diff)|%$
' +w +q "$file" - # Remove runtime files ported to Lua. - local na_rt='filetype\.vim\|scripts\.vim\|autoload\/ft\/dist\.vim\|print\/.*' - 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/runtime/\<\%('"${na_rt}"'\)\>@norm! d/\v(^diff)|%$
' +w +q "$file" + # Remove runtime/print/ + local na_rt='print\/.*' + 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/runtime/\<\%('"${na_rt}"'\)\>@norm! d/\v(^diff)|%$' +w +q "$file" # Remove unwanted Vim doc files. local na_doc='channel\.txt\|if_cscop\.txt\|netbeans\.txt\|os_\w\+\.txt\|print\.txt\|term\.txt\|todo\.txt\|version\d\.txt\|vim9\.txt\|sponsor\.txt\|intro\.txt\|tags' diff --git a/test/old/testdir/test_filetype.vim b/test/old/testdir/test_filetype.vim index f11c0ffc31..3ce8e49b27 100644 --- a/test/old/testdir/test_filetype.vim +++ b/test/old/testdir/test_filetype.vim @@ -236,7 +236,7 @@ func s:GetFilenameChecks() abort \ 'fish': ['file.fish'], \ 'focexec': ['file.fex', 'file.focexec'], \ 'form': ['file.frm'], - \ 'forth': ['file.ft', 'file.fth'], + \ 'forth': ['file.ft', 'file.fth', 'file.4th'], \ 'fortran': ['file.f', 'file.for', 'file.fortran', 'file.fpp', 'file.ftn', 'file.f77', 'file.f90', 'file.f95', 'file.f03', 'file.f08'], \ 'fpcmake': ['file.fpc'], \ 'framescript': ['file.fsl'], @@ -1173,6 +1173,54 @@ func Test_ex_file() filetype off endfunc +func Test_f_file() + filetype on + + call writefile(['looks like Fortran'], 'Xfile.f', 'D') + split Xfile.f + call assert_equal('fortran', &filetype) + bwipe! + + let g:filetype_f = 'forth' + split Xfile.f + call assert_equal('forth', &filetype) + bwipe! + unlet g:filetype_f + + " Test dist#ft#FTf() + + " Forth + + call writefile(['( Forth inline comment )'], 'Xfile.f') + split Xfile.f + call assert_equal('forth', &filetype) + bwipe! + + call writefile(['\ Forth line comment'], 'Xfile.f') + split Xfile.f + call assert_equal('forth', &filetype) + bwipe! + + call writefile([': squared ( n -- n^2 )', 'dup * ;'], 'Xfile.f') + split Xfile.f + call assert_equal('forth', &filetype) + bwipe! + + " SwiftForth + + call writefile(['{ ================', 'Header comment', '================ }'], 'Xfile.f') + split Xfile.f + call assert_equal('forth', &filetype) + bwipe! + + call writefile(['OPTIONAL Maybe Descriptive text'], 'Xfile.f') + split Xfile.f + call assert_equal('forth', &filetype) + bwipe! + + filetype off +endfunc + func Test_foam_file() filetype on call assert_true(mkdir('0', 'pR')) @@ -1263,7 +1311,7 @@ func Test_fs_file() " Test dist#ft#FTfs() - " Forth (Gforth) + " Forth call writefile(['( Forth inline comment )'], 'Xfile.fs') split Xfile.fs @@ -1280,6 +1328,18 @@ func Test_fs_file() call assert_equal('forth', &filetype) bwipe! + " SwiftForth + + call writefile(['{ ================', 'Header comment', '================ }'], 'Xfile.fs') + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + + call writefile(['OPTIONAL Maybe Descriptive text'], 'Xfile.fs') + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + filetype off endfunc |