diff options
author | Adam Stankiewicz <sheerun@sher.pl> | 2020-08-18 23:04:38 +0200 |
---|---|---|
committer | Adam Stankiewicz <sheerun@sher.pl> | 2020-08-18 23:04:38 +0200 |
commit | 20a388cbc83cda89ee045c06c63f17fb9d4c9b39 (patch) | |
tree | 93c5d95e2aa982e5b208ce92dce76d02d5c16682 | |
parent | 81e61a3c5d4a60916c7fdcee04562ff3f7e712eb (diff) | |
download | vim-polyglot-20a388cbc83cda89ee045c06c63f17fb9d4c9b39.tar.gz vim-polyglot-20a388cbc83cda89ee045c06c63f17fb9d4c9b39.zip |
Add jsonnet support, closes #515
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | autoload/jsonnet.vim | 129 | ||||
-rwxr-xr-x | build | 3 | ||||
-rw-r--r-- | ftplugin/jsonnet.vim | 13 | ||||
-rw-r--r-- | syntax/jsonnet.vim | 138 |
5 files changed, 285 insertions, 3 deletions
@@ -10,7 +10,7 @@ A collection of language packs for Vim. > One to rule them all, one to find them, one to bring them all and in the darkness bind them. - It **won't affect your startup time**, as scripts are loaded only on demand\*. -- It **installs and updates 120+ times faster** than the <!--Package Count-->152<!--/Package Count--> packages it consists of. +- It **installs and updates 120+ times faster** than the <!--Package Count-->153<!--/Package Count--> packages it consists of. - Solid syntax and indentation support (other features skipped). Only the best language packs. - All unnecessary files are ignored (like enormous documentation from php support). - No support for esoteric languages, only most popular ones (modern too, like `slim`). @@ -69,7 +69,7 @@ If you need full functionality of any plugin, please use it directly with your p - [cucumber](https://github.com/tpope/vim-cucumber) (syntax, indent, compiler, ftplugin) - [cue](https://github.com/mgrabovsky/vim-cuesheet) (syntax) - [dart](https://github.com/dart-lang/dart-vim-plugin) (syntax, indent, autoload, ftplugin) -- [dhall](https://github.com/sheerun/dhall-vim) (syntax, ftplugin) +- [dhall](https://github.com/vmchale/dhall-vim) (syntax, ftplugin) - [dlang](https://github.com/JesseKPhillips/d.vim) (syntax, indent) - [dockerfile](https://github.com/ekalinin/Dockerfile.vim) (syntax, indent, ftplugin) - [elixir](https://github.com/elixir-lang/vim-elixir) (syntax, indent, compiler, autoload, ftplugin) @@ -107,6 +107,7 @@ If you need full functionality of any plugin, please use it directly with your p - [jinja](https://github.com/lepture/vim-jinja) (syntax, indent) - [json5](https://github.com/GutenYe/json5.vim) (syntax) - [json](https://github.com/elzr/vim-json) (syntax, indent, ftplugin) +- [jsonnet](https://github.com/google/vim-jsonnet) (syntax, autoload, ftplugin) - [jst](https://github.com/briancollins/vim-jst) (syntax, indent) - [jsx](https://github.com/MaxMEllon/vim-jsx-pretty) (autoload, after) - [julia](https://github.com/JuliaEditorSupport/julia-vim) (syntax, indent, autoload, ftplugin) diff --git a/autoload/jsonnet.vim b/autoload/jsonnet.vim new file mode 100644 index 00000000..6607ac0d --- /dev/null +++ b/autoload/jsonnet.vim @@ -0,0 +1,129 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'jsonnet') == -1 + + + +" Options. + +if !exists("g:jsonnet_command") + let g:jsonnet_command = "jsonnet" +endif + +if !exists("g:jsonnet_fmt_command") + let g:jsonnet_fmt_command = "jsonnetfmt" +endif + +if !exists('g:jsonnet_fmt_options') + let g:jsonnet_fmt_options = '' +endif + +if !exists('g:jsonnet_fmt_fail_silently') + let g:jsonnet_fmt_fail_silently = 1 +endif + + +" System runs a shell command. It will reset the shell to /bin/sh for Unix-like +" systems if it is executable. +function! jsonnet#System(str, ...) + let l:shell = &shell + if executable('/bin/sh') + let &shell = '/bin/sh' + endif + + try + let l:output = call("system", [a:str] + a:000) + return l:output + finally + let &shell = l:shell + endtry +endfunction + + +" CheckBinPath checks whether the given binary exists or not and returns the +" path of the binary. It returns an empty string if it doesn't exists. +function! jsonnet#CheckBinPath(binName) + + if executable(a:binName) + if exists('*exepath') + let binPath = exepath(a:binName) + return binPath + else + return a:binName + endif + else + echo "vim-jsonnet: could not find '" . a:binName . "'." + return "" + endif + +endfunction + +" Format calls `jsonnetfmt ... ` on the file and replaces the file with the +" auto formatted version. Does some primitive error checking of the +" jsonnetfmt command too. +function! jsonnet#Format() + + " Save cursor position and many other things. + let l:curw = winsaveview() + + " Write current unsaved buffer to a temp file + let l:tmpname = tempname() + call writefile(getline(1, '$'), l:tmpname) + + " get the command first so we can test it + let l:binName = g:jsonnet_fmt_command + + " check if the user has installed command binary. + let l:binPath = jsonnet#CheckBinPath(l:binName) + if empty(l:binPath) + return + endif + + + " Populate the final command. + let l:command = l:binPath + " The inplace modification is default. Makes file management easier + let l:command = l:command . ' -i ' + let l:command = l:command . g:jsonnet_fmt_options + + " Execute the compiled jsonnetfmt command and save the return value + let l:out = jsonnet#System(l:command . " " . l:tmpname) + let l:errorCode = v:shell_error + + if l:errorCode == 0 + " The format command succeeded Move the formatted temp file over the + " current file and restore other settings + + " stop undo recording + try | silent undojoin | catch | endtry + + let l:originalFileFormat = &fileformat + if exists("*getfperm") + " save old file permissions + let l:originalFPerm = getfperm(expand('%')) + endif + " Overwrite current file with the formatted temp file + call rename(l:tmpname, expand('%')) + + if exists("*setfperm") && l:originalFPerm != '' + call setfperm(expand('%'), l:originalFPerm) + endif + " the file has been changed outside of vim, enable reedit + silent edit! + let &fileformat = l:originalFileFormat + let &syntax = &syntax + elseif g:jsonnet_fmt_fail_silently == 0 + " FixMe: We could leverage the errors coming from the `jsonnetfmt` and + " give immediate feedback to the user at every save time. + " Our inspiration, vim-go, opens a new list below the current edit + " window and shows the errors (the output of the fmt command). + " We are not sure whether this is desired in the vim-jsonnet community + " or not. Nevertheless, this else block is a suitable place to benefit + " from the `jsonnetfmt` errors. + endif + + " Restore our cursor/windows positions. + call winrestview(l:curw) +endfunction + + + +endif @@ -163,7 +163,7 @@ PACKS=" cucumber:tpope/vim-cucumber cue:mgrabovsky/vim-cuesheet dart:dart-lang/dart-vim-plugin - dhall:sheerun/dhall-vim + dhall:vmchale/dhall-vim dlang:JesseKPhillips/d.vim dockerfile:ekalinin/Dockerfile.vim elixir:elixir-lang/vim-elixir @@ -201,6 +201,7 @@ PACKS=" jinja:lepture/vim-jinja json5:GutenYe/json5.vim json:elzr/vim-json + jsonnet:google/vim-jsonnet jst:briancollins/vim-jst jsx:MaxMEllon/vim-jsx-pretty:_ALL julia:JuliaEditorSupport/julia-vim diff --git a/ftplugin/jsonnet.vim b/ftplugin/jsonnet.vim new file mode 100644 index 00000000..41fa83ff --- /dev/null +++ b/ftplugin/jsonnet.vim @@ -0,0 +1,13 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'jsonnet') == -1 + + + + +" -- fmt +command! -nargs=0 JsonnetFmt call jsonnet#Format() + +setlocal commentstring=//\ %s + + + +endif diff --git a/syntax/jsonnet.vim b/syntax/jsonnet.vim new file mode 100644 index 00000000..c4b1e625 --- /dev/null +++ b/syntax/jsonnet.vim @@ -0,0 +1,138 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'jsonnet') == -1 + +" Copyright 2014 Google Inc. All rights reserved. +" +" Licensed under the Apache License, Version 2.0 (the "License"); +" you may not use this file except in compliance with the License. +" You may obtain a copy of the License at +" +" http://www.apache.org/licenses/LICENSE-2.0 +" +" Unless required by applicable law or agreed to in writing, software +" distributed under the License is distributed on an "AS IS" BASIS, +" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +" See the License for the specific language governing permissions and +" limitations under the License. + +syntax match Number "\<\d*\([Ee][+-]\?\d\+\)\?\>" +syntax match Number "\<\d\+[.]\d*\([Ee][+-]\?\d\+\)\?\>" +syntax match Number "\<[.]\d\+\([Ee][+-]\?\d\+\)\?\>" + +" builtins +syn match Constant "std.acos" +syn match Constant "std.asin" +syn match Constant "std.atan" +syn match Constant "std.ceil" +syn match Constant "std.char" +syn match Constant "std.codepoint" +syn match Constant "std.cos" +syn match Constant "std.exp" +syn match Constant "std.exponent" +syn match Constant "std.extVar" +syn match Constant "std.filter" +syn match Constant "std.floor" +syn match Constant "std.force" +syn match Constant "std.length" +syn match Constant "std.log" +syn match Constant "std.makeArray" +syn match Constant "std.mantissa" +syn match Constant "std.md5" +syn match Constant "std.modulo" +syn match Constant "std.native" +syn match Constant "std.objectFieldsEx" +syn match Constant "std.objectHasEx" +syn match Constant "std.pow" +syn match Constant "std.primitiveEquals" +syn match Constant "std.sin" +syn match Constant "std.sqrt" +syn match Constant "std.tan" +syn match Constant "std.thisFile" +syn match Constant "std.type" + +" std.jsonnet +syn match Constant "std.abs" +syn match Constant "std.asciiLower" +syn match Constant "std.asciiUpper" +syn match Constant "std.assertEqual" +syn match Constant "std.base64" +syn match Constant "std.base64Decode" +syn match Constant "std.base64DecodeBytes" +syn match Constant "std.count" +syn match Constant "std.endsWith" +syn match Constant "std.equals" +syn match Constant "std.escapeStringBash" +syn match Constant "std.escapeStringDollars" +syn match Constant "std.escapeStringJson" +syn match Constant "std.escapeStringPython" +syn match Constant "std.filterMap" +syn match Constant "std.flattenArrays" +syn match Constant "std.foldl" +syn match Constant "std.foldr" +syn match Constant "std.format" +syn match Constant "std.join" +syn match Constant "std.lines" +syn match Constant "std.manifestIni" +syn match Constant "std.manifestJson" +syn match Constant "std.manifestJsonEx" +syn match Constant "std.manifestPython" +syn match Constant "std.manifestPythonVars" +syn match Constant "std.manifestYamlStream" +syn match Constant "std.map" +syn match Constant "std.mapWithIndex" +syn match Constant "std.max" +syn match Constant "std.mergePatch" +syn match Constant "std.min" +syn match Constant "std.mod" +syn match Constant "std.objectFields" +syn match Constant "std.objectFieldsAll" +syn match Constant "std.objectHas" +syn match Constant "std.objectHasAll" +syn match Constant "std.parseInt" +syn match Constant "std.prune" +syn match Constant "std.range" +syn match Constant "std.resolvePath" +syn match Constant "std.set" +syn match Constant "std.setDiff" +syn match Constant "std.setInter" +syn match Constant "std.setMember" +syn match Constant "std.setUnion" +syn match Constant "std.slice" +syn match Constant "std.sort" +syn match Constant "std.split" +syn match Constant "std.splitLimit" +syn match Constant "std.startsWith" +syn match Constant "std.stringChars" +syn match Constant "std.strReplace" +syn match Constant "std.substr" +syn match Constant "std.toString" +syn match Constant "std.uniq" + + +syn match Type "\$" + +syn region String start='L\="' skip='\\\\\|\\"' end='"' +syn region String start='L\=\'' skip='\\\\\|\\\'' end='\'' +syn region String start='|||\s*\n\+\z(\s*\)' end='^\z1\@!\s*|||' + +" Highlight python style string formatting. +syn match Special "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=String +syn match Special "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=String + +syn region Comment start="/[*]" end="[*]/" +syn match Comment "//.*$" +syn match Comment "#.*$" + +syn match Keyword "\<[a-zA-Z_][a-z0-9A-Z_]*\s*\(([^)]*)\)\?\s*+\?::\?:\?" + +syn region Object start="{" end="}" fold transparent + +syntax keyword Include import importstr +syntax keyword Type function self super +syntax keyword Statement assert if then else for in +syntax keyword Special local tailstrict +syntax keyword Constant true false null +syntax keyword Underlined error + + + +endif |