Skip to content

added XMLLint capability for XML #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions autoload/codefmt/xmllint.vim
Original file line number Diff line number Diff line change
@@ -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).'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"man xmllint" isn't too helpful as installation instructions. It'd only do anything if xmllint were already installed. Could you point to a website with info about how to install on various platforms?


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
3 changes: 3 additions & 0 deletions instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
1 change: 1 addition & 0 deletions plugin/register.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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())