From d891037fa2b470cc6fdd207d4aad8768e921cf47 Mon Sep 17 00:00:00 2001 From: Rupert Bailey Date: Sun, 25 Mar 2018 16:14:51 +1100 Subject: [PATCH] added XMLLint capability for XML --- autoload/codefmt/xmllint.vim | 72 ++++++++++++++++++++++++++++++++++++ instant/flags.vim | 3 ++ plugin/register.vim | 1 + 3 files changed, 76 insertions(+) create mode 100644 autoload/codefmt/xmllint.vim diff --git a/autoload/codefmt/xmllint.vim b/autoload/codefmt/xmllint.vim new file mode 100644 index 0000000..bb2f84a --- /dev/null +++ b/autoload/codefmt/xmllint.vim @@ -0,0 +1,72 @@ +" Copyright 2017 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. + + +let s:plugin = maktaba#plugin#Get('codefmt') + +"" +" @private +" +" Formatter provider for Bazel BUILD files using xmllint. +function! codefmt#xmllint#GetFormatter() abort + let l:formatter = { + \ 'name': 'xmllint', + \ 'setup_instructions': 'Install xmllint. ' . + \ '(man xmllint).'} + + function l:formatter.IsAvailable() abort + return executable(s:plugin.Flag('xmllint_executable')) + endfunction + + function l:formatter.AppliesToBuffer() abort + return &filetype is# 'xml' + endfunction + + "" + " Reformat the current buffer with xmllint or the binary named in + " @flag(xmllint) + " @throws ShellError + function l:formatter.Format() abort + let l:cmd = [ s:plugin.Flag('xmllint_executable'), '--format', '-' ] + let l:input = join(getline(1, line('$')), "\n") + try + let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() + let l:formatted = split(l:result.stdout, "\n") + call maktaba#buffer#Overwrite(1, line('$'), l:formatted) + catch + " Parse all the errors and stick them in the quickfix list. + let l:errors = [] + for line in split(v:exception, "\n") + let l:tokens = matchlist(line, '\C\v^stdin:(\d+):(\d+):\s*(.*)') + if !empty(l:tokens) + call add(l:errors, { + \ "filename": @%, + \ "lnum": l:tokens[1], + \ "col": l:tokens[2], + \ "text": l:tokens[3]}) + endif + endfor + + if empty(l:errors) + " Couldn't parse xmllint error format; display it all. + call maktaba#error#Shout('Error formatting file: %s', v:exception) + else + call setqflist(l:errors, 'r') + cc 1 + endif + endtry + endfunction + + return l:formatter +endfunction diff --git a/instant/flags.vim b/instant/flags.vim index 2bf834c..6c4da09 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -93,3 +93,6 @@ call s:plugin.Flag('buildifier_executable', 'buildifier') " form: " `java -jar /path/to/google-java` call s:plugin.Flag('google_java_executable', 'google-java-format') + +" The path to the xmllint executable xmllint --format - +call s:plugin.Flag('xmllint_executable', 'xmllint') diff --git a/plugin/register.vim b/plugin/register.vim index e82b2b0..b61328e 100644 --- a/plugin/register.vim +++ b/plugin/register.vim @@ -30,3 +30,4 @@ call s:registry.AddExtension(codefmt#autopep8#GetFormatter()) call s:registry.AddExtension(codefmt#gn#GetFormatter()) call s:registry.AddExtension(codefmt#buildifier#GetFormatter()) call s:registry.AddExtension(codefmt#googlejava#GetFormatter()) +call s:registry.AddExtension(codefmt#xmllint#GetFormatter())