vim-patch:9.0.2128: runtime(swig): add syntax and filetype plugins

Add syntax and filetype plugins for SWIG (Simplified Wrapper Interface
Generator) description files.

The default syntax for .i files highlights comments in a reverse
color scheme which doesn't look well.  This syntax builds
on vim's c++ syntax by adding highlighting for common swig
directives and user defined directives.  For an alternative
syntax, see vimscript vim/vim#1247 (which I found after writing this).

closes: vim/vim#13562

2e31065a65

Co-authored-by: Julien Marrec <julien.marrec@gmail.com>
Co-authored-by: Matěj Cepl <mcepl@cepl.eu>
This commit is contained in:
Christian Clason 2023-11-25 16:06:58 +01:00
parent ba88fd886a
commit 38e98754a5
5 changed files with 171 additions and 21 deletions

13
runtime/ftplugin/swig.vim Normal file
View File

@ -0,0 +1,13 @@
" Vim filetype plugin file
" Language: SWIG
" Maintainer: Julien Marrec <julien.marrec 'at' gmail com>
" Last Change: 2023 November 23
" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
finish
endif
let b:did_ftplugin = 1
let b:undo_ftplugin = "setlocal iskeyword<"
setlocal iskeyword+=%

View File

@ -984,6 +984,8 @@ local extension = {
svelte = 'svelte',
svg = 'svg',
swift = 'swift',
swig = 'swig',
swg = 'swig',
svh = 'systemverilog',
sv = 'systemverilog',
cmm = 'trace32',
@ -1134,7 +1136,7 @@ local extension = {
web = detect.web,
pl = detect.pl,
pp = detect.pp,
i = detect.progress_asm,
i = detect.i,
w = detect.progress_cweb,
p = detect.progress_pascal,
pro = detect_seq(detect.proto, 'idlang'),

View File

@ -619,6 +619,31 @@ function M.hw(_, bufnr)
return 'virata'
end
-- This function checks for an assembly comment or a SWIG keyword or verbatim
-- block in the first 50 lines.
-- If not found, assume Progress.
--- @type vim.filetype.mapfn
function M.i(path, bufnr)
if vim.g.filetype_i then
return vim.g.filetype_i
end
-- These include the leading '%' sign
local ft_swig_keywords =
[[^\s*%\%(addmethods\|apply\|beginfile\|clear\|constant\|define\|echo\|enddef\|endoffile\|extend\|feature\|fragment\|ignore\|import\|importfile\|include\|includefile\|inline\|insert\|keyword\|module\|name\|namewarn\|native\|newobject\|parms\|pragma\|rename\|template\|typedef\|typemap\|types\|varargs\|warn\)]]
-- This is the start/end of a block that is copied literally to the processor file (C/C++)
local ft_swig_verbatim_block_start = '^%s*%%{'
for _, line in ipairs(getlines(bufnr, 1, 50)) do
if line:find('^%s*;') or line:find('^%*') then
return M.asm(path, bufnr)
elseif matchregex(line, ft_swig_keywords) or line:find(ft_swig_verbatim_block_start) then
return 'swig'
end
end
return 'progress'
end
--- @type vim.filetype.mapfn
function M.idl(_, bufnr)
for _, line in ipairs(getlines(bufnr, 1, 50)) do
@ -1016,26 +1041,6 @@ function M.printcap(ptcap_type)
end
end
-- This function checks for an assembly comment in the first ten lines.
-- If not found, assume Progress.
--- @type vim.filetype.mapfn
function M.progress_asm(path, bufnr)
if vim.g.filetype_i then
return vim.g.filetype_i
end
for _, line in ipairs(getlines(bufnr, 1, 10)) do
if line:find('^%s*;') or line:find('^/%*') then
return M.asm(path, bufnr)
elseif not line:find('^%s*$') or line:find('^/%*') then
-- Not an empty line: doesn't look like valid assembly code
-- or it looks like a Progress /* comment.
break
end
end
return 'progress'
end
--- @type vim.filetype.mapfn
function M.progress_cweb(_, bufnr)
if vim.g.filetype_w then

99
runtime/syntax/swig.vim Normal file
View File

@ -0,0 +1,99 @@
" Vim syntax file
" Language: SWIG
" Maintainer: Julien Marrec <julien.marrec 'at' gmail com>
" Last Change: 2023 November 23
if exists("b:current_syntax")
finish
endif
" Read the C++ syntax to start with
runtime! syntax/cpp.vim
unlet b:current_syntax
" SWIG extentions
syn keyword swigInclude %include %import %importfile %includefile %module
syn keyword swigMostCommonDirective %alias %apply %beginfile %clear %constant %define %echo %enddef %endoffile
syn keyword swigMostCommonDirective %extend %feature %director %fragment %ignore %inline
syn keyword swigMostCommonDirective %keyword %name %namewarn %native %newobject %parms %pragma
syn keyword swigMostCommonDirective %rename %template %typedef %typemap %types %varargs
" SWIG: Language specific macros
syn keyword swigOtherLanguageSpecific %luacode %go_import
syn keyword swigCSharp %csattributes %csconst %csconstvalue %csmethodmodifiers %csnothrowexception
syn keyword swigCSharp %dconstvalue %dmanifestconst %dmethodmodifiers
syn keyword swigJava %javaconstvalue %javaexception %javamethodmodifiers %javaconst %nojavaexception
syn keyword swigGuile %multiple_values %values_as_list %values_as_vector
syn keyword swigPHP %rinit %rshutdown %minit %mshutdown
syn keyword swigPython %pybinoperator %pybuffer_binary %pybuffer_mutable_binary %pybuffer_mutable_string %pybuffer_string
syn keyword swigPython %pythonappend %pythonbegin %pythoncode %pythondynamic %pythonnondynamic %pythonprepend
syn keyword swigRuby %markfunc %trackobjects %bang
syn keyword swigScilab %scilabconst
" SWIG: Insertion
syn keyword swigInsertSection %insert %begin %runtime %header %wrapper %init
" SWIG: Other directives
syn keyword swigCstring %cstring_bounded_mutable %cstring_bounded_output %cstring_chunk_output %cstring_input_binary %cstring_mutable
syn keyword swigCstring %cstring_output_allocate %cstring_output_allocate_size %cstring_output_maxsize %cstring_output_withsize
syn keyword swigCWstring %cwstring_bounded_mutable %cwstring_bounded_output %cwstring_chunk_output %cwstring_input_binary %cwstring_mutable
syn keyword swigCWstring %cwstring_output_allocate %cwstring_output_allocate_size %cwstring_output_maxsize %cwstring_output_withsize
syn keyword swigCMalloc %malloc %calloc %realloc %free %sizeof %allocators
syn keyword swigExceptionHandling %catches %raise %allowexception %exceptionclass %warn %warnfilter %exception
syn keyword swigContract %contract %aggregate_check
syn keyword swigDirective %addmethods %array_class %array_functions %attribute %attribute2 %attribute2ref
syn keyword swigDirective %attribute_ref %attributeref %attributestring %attributeval %auto_ptr %callback
syn keyword swigDirective %delete_array %delobject %extend_smart_pointer %factory %fastdispatch %freefunc %immutable
syn keyword swigDirective %implicit %implicitconv %interface %interface_custom %interface_impl %intrusive_ptr %intrusive_ptr_no_wrap
syn keyword swigDirective %mutable %naturalvar %nocallback %nocopyctor %nodefaultctor %nodefaultdtor %nonaturalvar %nonspace
syn keyword swigDirective %nspace %pointer_cast %pointer_class %pointer_functions %predicate %proxycode
syn keyword swigDirective %refobject %set_output %shared_ptr %std_comp_methods
syn keyword swigDirective %std_nodefconst_type %typecheck %typemaps_string %unique_ptr %unrefobject %valuewrapper
syn match swigVerbatimStartEnd "%[{}]"
syn match swigUserDef "%\w\+"
syn match swigVerbatimMacro "^\s*%#\w\+\%( .*\)\?$"
" SWIG: typemap var and typemap macros (eg: $1, $*1_type, $&n_ltype, $self)
syn match swigTypeMapVars "\$[*&_a-zA-Z0-9]\+"
" Default highlighting
hi def link swigInclude Include
hi def link swigMostCommonDirective Structure
hi def link swigDirective Macro
hi def link swigContract swigExceptionHandling
hi def link swigExceptionHandling Exception
hi def link swigUserDef Function
hi def link swigCMalloc Statement
hi def link swigCstring Type
hi def link swigCWstring Type
hi def link swigCSharp swigOtherLanguageSpecific
hi def link swigJava swigOtherLanguageSpecific
hi def link swigGuile swigOtherLanguageSpecific
hi def link swigPHP swigOtherLanguageSpecific
hi def link swigPython swigOtherLanguageSpecific
hi def link swigRuby swigOtherLanguageSpecific
hi def link swigScilab swigOtherLanguageSpecific
hi def link swigOtherLanguageSpecific Special
hi def link swigInsertSection PreProc
hi def link swigVerbatimStartEnd Statement
hi def link swigVerbatimMacro Macro
hi def link swigTypeMapVars SpecialChar
let b:current_syntax = "swig"
" vim: ts=8

View File

@ -633,6 +633,7 @@ func s:GetFilenameChecks() abort
\ 'swayconfig': ['/home/user/.sway/config', '/home/user/.config/sway/config', '/etc/sway/config', '/etc/xdg/sway/config'],
\ 'swift': ['file.swift'],
\ 'swiftgyb': ['file.swift.gyb'],
\ 'swig': ['file.swg', 'file.swig'],
\ 'sysctl': ['/etc/sysctl.conf', '/etc/sysctl.d/file.conf', 'any/etc/sysctl.conf', 'any/etc/sysctl.d/file.conf'],
\ 'systemd': ['any/systemd/file.automount', 'any/systemd/file.dnssd', 'any/systemd/file.link', 'any/systemd/file.mount', 'any/systemd/file.netdev', 'any/systemd/file.network', 'any/systemd/file.nspawn', 'any/systemd/file.path', 'any/systemd/file.service', 'any/systemd/file.slice', 'any/systemd/file.socket', 'any/systemd/file.swap', 'any/systemd/file.target', 'any/systemd/file.timer', '/etc/systemd/some.conf.d/file.conf', '/etc/systemd/system/some.d/file.conf', '/etc/systemd/system/some.d/.#file', '/etc/systemd/system/.#otherfile', '/home/user/.config/systemd/user/some.d/mine.conf', '/home/user/.config/systemd/user/some.d/.#file', '/home/user/.config/systemd/user/.#otherfile', '/.config/systemd/user/.#', '/.config/systemd/user/.#-file', '/.config/systemd/user/file.d/.#', '/.config/systemd/user/file.d/.#-file', '/.config/systemd/user/file.d/file.conf', '/etc/systemd/file.conf.d/file.conf', '/etc/systemd/system/.#', '/etc/systemd/system/.#-file', '/etc/systemd/system/file.d/.#', '/etc/systemd/system/file.d/.#-file', '/etc/systemd/system/file.d/file.conf', '/systemd/file.automount', '/systemd/file.dnssd', '/systemd/file.link', '/systemd/file.mount', '/systemd/file.netdev', '/systemd/file.network', '/systemd/file.nspawn', '/systemd/file.path', '/systemd/file.service', '/systemd/file.slice', '/systemd/file.socket', '/systemd/file.swap', '/systemd/file.target', '/systemd/file.timer', 'any/.config/systemd/user/.#', 'any/.config/systemd/user/.#-file', 'any/.config/systemd/user/file.d/.#', 'any/.config/systemd/user/file.d/.#-file', 'any/.config/systemd/user/file.d/file.conf', 'any/etc/systemd/file.conf.d/file.conf', 'any/etc/systemd/system/.#', 'any/etc/systemd/system/.#-file', 'any/etc/systemd/system/file.d/.#', 'any/etc/systemd/system/file.d/.#-file', 'any/etc/systemd/system/file.d/file.conf'],
\ 'systemverilog': ['file.sv', 'file.svh'],
@ -2248,4 +2249,34 @@ func Test_vba_file()
filetype off
endfunc
func Test_i_file()
filetype on
" Swig: keyword
call writefile(['%module mymodule', '/* a comment */'], 'Xfile.i', 'D')
split Xfile.i
call assert_equal('swig', &filetype)
bwipe!
" Swig: verbatim block
call writefile(['%{', '#include <header.hpp>', '%}'], 'Xfile.i', 'D')
split Xfile.i
call assert_equal('swig', &filetype)
bwipe!
" ASM
call writefile(['; comment', ';'], 'Xfile.i', 'D')
split Xfile.i
call assert_equal('asm', &filetype)
bwipe!
" *.i defaults to progress
call writefile(['looks like progress'], 'Xfile.i', 'D')
split Xfile.i
call assert_equal('progress', &filetype)
bwipe!
filetype off
endfunc
" vim: shiftwidth=2 sts=2 expandtab