From 0e9127e4dc45ee2c31724e7f5ce52e6f95ec7c30 Mon Sep 17 00:00:00 2001 From: Shidong Wang Date: Sat, 13 Nov 2021 14:16:21 +0800 Subject: [PATCH] feat(lang#toml): add `SPC l j` to preview toml --- autoload/SpaceVim/layers/core/statusline.vim | 2 + autoload/SpaceVim/layers/lang/toml.vim | 10 ++- bundle/README.md | 1 + bundle/vim-toml/LICENSE | 22 ++++++ bundle/vim-toml/README.md | 40 ++++++++++ bundle/vim-toml/autoload/toml.vim | 33 ++++++++ bundle/vim-toml/ftdetect/toml.vim | 2 + bundle/vim-toml/ftplugin/toml.vim | 23 ++++++ bundle/vim-toml/syntax/toml.vim | 81 ++++++++++++++++++++ bundle/vim-toml/test/test.toml | 35 +++++++++ 10 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 bundle/vim-toml/LICENSE create mode 100644 bundle/vim-toml/README.md create mode 100644 bundle/vim-toml/autoload/toml.vim create mode 100644 bundle/vim-toml/ftdetect/toml.vim create mode 100644 bundle/vim-toml/ftplugin/toml.vim create mode 100644 bundle/vim-toml/syntax/toml.vim create mode 100644 bundle/vim-toml/test/test.toml diff --git a/autoload/SpaceVim/layers/core/statusline.vim b/autoload/SpaceVim/layers/core/statusline.vim index bb3637c1f..f2386d343 100644 --- a/autoload/SpaceVim/layers/core/statusline.vim +++ b/autoload/SpaceVim/layers/core/statusline.vim @@ -545,6 +545,8 @@ function! SpaceVim#layers#core#statusline#get(...) abort return '%#SpaceVim_statusline_a# Transient State %#SpaceVim_statusline_a_SpaceVim_statusline_b#' elseif &filetype ==# 'SpaceVimLog' return '%#SpaceVim_statusline_a# SpaceVim Runtime log %#SpaceVim_statusline_a_SpaceVim_statusline_b#' + elseif &filetype ==# 'SpaceVimTomlViewer' + return '%#SpaceVim_statusline_a# Toml Json Viewer %#SpaceVim_statusline_a_SpaceVim_statusline_b#' elseif &filetype ==# 'vimcalc' return '%#SpaceVim_statusline_a#' . s:winnr() . ' VimCalc %#SpaceVim_statusline_a_SpaceVim_statusline_b#' elseif &filetype ==# 'HelpDescribe' diff --git a/autoload/SpaceVim/layers/lang/toml.vim b/autoload/SpaceVim/layers/lang/toml.vim index ee35982a2..5323431cd 100644 --- a/autoload/SpaceVim/layers/lang/toml.vim +++ b/autoload/SpaceVim/layers/lang/toml.vim @@ -17,10 +17,18 @@ function! SpaceVim#layers#lang#toml#plugins() abort let plugins = [] - call add(plugins, ['cespare/vim-toml', {'merged' : 0}]) + call add(plugins, [g:_spacevim_root_dir . 'bundle/vim-toml', {'merged' : 0}]) return plugins endfunction +function! SpaceVim#layers#lang#toml#config() abort + call SpaceVim#mapping#space#regesit_lang_mappings('toml', function('s:toml_lang_mappings')) +endfunction + +function! s:toml_lang_mappings() abort + call SpaceVim#mapping#space#langSPC('nmap', ['l','j'], 'call toml#preview()', 'toml-to-json', 1) +endfunction + function! SpaceVim#layers#lang#toml#health() abort call SpaceVim#layers#lang#toml#plugins() return 1 diff --git a/bundle/README.md b/bundle/README.md index d8c80a93b..ca69cbef9 100644 --- a/bundle/README.md +++ b/bundle/README.md @@ -6,6 +6,7 @@ In `bundle/` directory, there are two kinds of plugins: forked plugins without c - `vim-bookmarks`: based on [`MattesGroeger/vim-bookmarks@3adeae1`](https://github.com/MattesGroeger/vim-bookmarks/commit/3adeae10639edcba29ea80dafa1c58cf545cb80e) - `delimitMate`: based on [`Raimondi/delimitMate@537a1da`](https://github.com/Raimondi/delimitMate/tree/537a1da0fa5eeb88640425c37e545af933c56e1b) +- `vim-toml`: based on [cespare/vim-toml@717bd87ef9](https://github.com/cespare/vim-toml/tree/717bd87ef928293e0cc6cfc12ebf2e007cb25311) ### No changed plugins diff --git a/bundle/vim-toml/LICENSE b/bundle/vim-toml/LICENSE new file mode 100644 index 000000000..d04c50223 --- /dev/null +++ b/bundle/vim-toml/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2013 Caleb Spare + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/bundle/vim-toml/README.md b/bundle/vim-toml/README.md new file mode 100644 index 000000000..bdd631c0e --- /dev/null +++ b/bundle/vim-toml/README.md @@ -0,0 +1,40 @@ +# vim-toml + +Vim syntax for [TOML](https://github.com/toml-lang/toml). + +## Installation + +### Vim packages (recommended; Vim 8+ only) + +Clone or submodule this repo into your Vim packages location. Example: + +``` +mkdir -p ~/.vim/pack/plugins/start +cd ~/.vim/pack/plugins/start +git clone https://github.com/cespare/vim-toml.git +``` + +### Pathogen + +Set up [Pathogen](https://github.com/tpope/vim-pathogen) then clone/submodule +this repo into `~/.vim/bundle/toml`, or wherever you've pointed your Pathogen. + +### Vundle + +Set up [Vundle](https://github.com/VundleVim/Vundle.vim) then add `Plugin +'cespare/vim-toml'` to your vimrc and run `:PluginInstall` from a fresh vim. + +### vim-plug + +Set up [vim-plug](https://github.com/junegunn/vim-plug). In your .vimrc, between +the lines for `call plug#begin()` and `call plug#end()`, add the line `Plug +'cespare/vim-toml', { 'branch': 'main' }`. Reload your .vimrc and then run `:PlugInstall`. + +### Janus + +Set up [Janus](https://github.com/carlhuda/janus) and then clone/submodule this +repo into `~/.janus` and restart vim. + +## Contributing + +Contributions are very welcome! Just open a PR. diff --git a/bundle/vim-toml/autoload/toml.vim b/bundle/vim-toml/autoload/toml.vim new file mode 100644 index 000000000..6e1640ec9 --- /dev/null +++ b/bundle/vim-toml/autoload/toml.vim @@ -0,0 +1,33 @@ +"============================================================================= +" toml.vim --- toml autoload plugin +" Copyright (c) 2016-2019 Wang Shidong & Contributors +" Author: Wang Shidong < wsdjeg@outlook.com > +" URL: https://spacevim.org +" License: GPLv3 +"============================================================================= + + +let s:preview_bufnr = -1 +let s:toml_bufnr = -1 + +let s:TOML = SpaceVim#api#import('data#toml') +let s:JSON = SpaceVim#api#import('data#json') + +function! toml#preview() abort + let s:toml_bufnr = bufnr() + let context = join(getbufline(s:toml_bufnr, 1, "$"), "\n") + let json_obj = s:TOML.parse(context) + let json = s:JSON.json_encode(json_obj) + " close other windows + silent only + " open preview windows + rightbelow vsplit __toml_json_preview__ + set ft=SpaceVimTomlViewer + set syntax=json + setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonu norelativenumber winfixwidth + let s:preview_bufnr = bufnr() + setlocal modifiable + call setline(1, json) + Neoformat! json + setlocal nomodifiable +endfunction diff --git a/bundle/vim-toml/ftdetect/toml.vim b/bundle/vim-toml/ftdetect/toml.vim new file mode 100644 index 000000000..e975cd1d5 --- /dev/null +++ b/bundle/vim-toml/ftdetect/toml.vim @@ -0,0 +1,2 @@ +" Go dep and Rust use several TOML config files that are not named with .toml. +autocmd BufNewFile,BufRead *.toml,Gopkg.lock,Cargo.lock,*/.cargo/config,*/.cargo/credentials,Pipfile set filetype=toml diff --git a/bundle/vim-toml/ftplugin/toml.vim b/bundle/vim-toml/ftplugin/toml.vim new file mode 100644 index 000000000..1ef09a16e --- /dev/null +++ b/bundle/vim-toml/ftplugin/toml.vim @@ -0,0 +1,23 @@ +" Vim filetype plugin +" Language: TOML +" Homepage: https://github.com/cespare/vim-toml +" Maintainer: Aman Verma +" Author: Kevin Ballard +" Last Change: Sep 21, 2021 + +if exists('b:did_ftplugin') + finish +endif +let b:did_ftplugin = 1 + +let s:save_cpo = &cpo +set cpo&vim +let b:undo_ftplugin = 'setlocal commentstring< comments<' + +setlocal commentstring=#\ %s +setlocal comments=:# + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim: et sw=2 sts=2 diff --git a/bundle/vim-toml/syntax/toml.vim b/bundle/vim-toml/syntax/toml.vim new file mode 100644 index 000000000..bcb1b0b9c --- /dev/null +++ b/bundle/vim-toml/syntax/toml.vim @@ -0,0 +1,81 @@ +" Vim syntax file +" Language: TOML +" Homepage: https://github.com/cespare/vim-toml +" Maintainer: Aman Verma +" Previous Maintainer: Caleb Spare +" Last Change: Oct 8, 2021 + +if exists('b:current_syntax') + finish +endif + +syn match tomlEscape /\\[btnfr"/\\]/ display contained +syn match tomlEscape /\\u\x\{4}/ contained +syn match tomlEscape /\\U\x\{8}/ contained +syn match tomlLineEscape /\\$/ contained + +" Basic strings +syn region tomlString oneline start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=tomlEscape +" Multi-line basic strings +syn region tomlString start=/"""/ end=/"""/ contains=tomlEscape,tomlLineEscape +" Literal strings +syn region tomlString oneline start=/'/ end=/'/ +" Multi-line literal strings +syn region tomlString start=/'''/ end=/'''/ + +syn match tomlInteger /[+-]\=\<[1-9]\(_\=\d\)*\>/ display +syn match tomlInteger /[+-]\=\<0\>/ display +syn match tomlInteger /[+-]\=\<0x[[:xdigit:]]\(_\=[[:xdigit:]]\)*\>/ display +syn match tomlInteger /[+-]\=\<0o[0-7]\(_\=[0-7]\)*\>/ display +syn match tomlInteger /[+-]\=\<0b[01]\(_\=[01]\)*\>/ display +syn match tomlInteger /[+-]\=\<\(inf\|nan\)\>/ display + +syn match tomlFloat /[+-]\=\<\d\(_\=\d\)*\.\d\+\>/ display +syn match tomlFloat /[+-]\=\<\d\(_\=\d\)*\(\.\d\(_\=\d\)*\)\=[eE][+-]\=\d\(_\=\d\)*\>/ display + +syn match tomlBoolean /\<\%(true\|false\)\>/ display + +" https://tools.ietf.org/html/rfc3339 +syn match tomlDate /\d\{4\}-\d\{2\}-\d\{2\}/ display +syn match tomlDate /\d\{2\}:\d\{2\}:\d\{2\}\%(\.\d\+\)\?/ display +syn match tomlDate /\d\{4\}-\d\{2\}-\d\{2\}[T ]\d\{2\}:\d\{2\}:\d\{2\}\%(\.\d\+\)\?\%(Z\|[+-]\d\{2\}:\d\{2\}\)\?/ display + +syn match tomlDotInKey /\v[^.]+\zs\./ contained display +syn match tomlKey /\v(^|[{,])\s*\zs[[:alnum:]._-]+\ze\s*\=/ contains=tomlDotInKey display +syn region tomlKeyDq oneline start=/\v(^|[{,])\s*\zs"/ end=/"\ze\s*=/ contains=tomlEscape +syn region tomlKeySq oneline start=/\v(^|[{,])\s*\zs'/ end=/'\ze\s*=/ + +syn region tomlTable oneline start=/^\s*\[[^\[]/ end=/\]/ contains=tomlKey,tomlKeyDq,tomlKeySq,tomlDotInKey + +syn region tomlTableArray oneline start=/^\s*\[\[/ end=/\]\]/ contains=tomlKey,tomlKeyDq,tomlKeySq,tomlDotInKey + +syn region tomlKeyValueArray start=/=\s*\[\zs/ end=/\]/ contains=@tomlValue + +syn region tomlArray start=/\[/ end=/\]/ contains=@tomlValue contained + +syn cluster tomlValue contains=tomlArray,tomlString,tomlInteger,tomlFloat,tomlBoolean,tomlDate,tomlComment + +syn keyword tomlTodo TODO FIXME XXX BUG contained + +syn match tomlComment /#.*/ contains=@Spell,tomlTodo + +hi def link tomlComment Comment +hi def link tomlTodo Todo +hi def link tomlTableArray Title +hi def link tomlTable Title +hi def link tomlDotInKey Normal +hi def link tomlKeySq Identifier +hi def link tomlKeyDq Identifier +hi def link tomlKey Identifier +hi def link tomlDate Constant +hi def link tomlBoolean Boolean +hi def link tomlFloat Float +hi def link tomlInteger Number +hi def link tomlString String +hi def link tomlLineEscape SpecialChar +hi def link tomlEscape SpecialChar + +syn sync minlines=500 +let b:current_syntax = 'toml' + +" vim: et sw=2 sts=2 diff --git a/bundle/vim-toml/test/test.toml b/bundle/vim-toml/test/test.toml new file mode 100644 index 000000000..c81aba12e --- /dev/null +++ b/bundle/vim-toml/test/test.toml @@ -0,0 +1,35 @@ +# Visual test file. +# You can run +# +# nnoremap echo synIDattr(synID(line('.'), col('.'), 1), 'name') +# +# and then press F10 to get the highlight group under the cursor. + +# https://github.com/cespare/vim-toml/issues/9 +issue9 = [ + ["a", "b", "c"] +] + +# https://github.com/cespare/vim-toml/issues/10 +issue10_1 = -12 +issue10_2 = +200.3 + +# https://github.com/cespare/vim-toml/issues/11 +issue11 = -210_000.0 + +# https://github.com/cespare/vim-toml/issues/13 +issue13 = { version="1.0", features=["derive"] } + +# https://github.com/cespare/vim-toml/pull/52 +[foo.baz] +apple.type = "fruit" +3.14159 = "pi" +"127.0.0.1" = "value" + +[[foo.quux]] +e = 2 + +# https://github.com/cespare/vim-toml/issues/58 +site."google.com" = true + +# vim: et sw=2 sts=2