Last active
August 11, 2017 06:30
-
-
Save grantmacken/9074a178c7ccb0db4cfc5682b41deea6 to your computer and use it in GitHub Desktop.
stdin xqlint source to lint xQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
var fs = require('fs'); | |
var encoding = 'utf-8'; | |
var source = '' | |
if (process.argv.length <= 2) { | |
console.log("Usage: " + __filename + " file"); | |
process.exit(-1); | |
} | |
var file = process.argv[2]; | |
process.stdin.setEncoding(encoding); | |
process.stdin.on('readable', function() { | |
var chunk; | |
while (chunk = process.stdin.read()) { | |
source += chunk; | |
} | |
}); | |
process.stdin.on('end', function () { | |
// There will be a trailing \n from the user hitting enter. Get rid of it. | |
source = source.replace(/\n$/, ''); | |
var XQLint = require('xqlint').XQLint; | |
var linter = new XQLint(source, { fileName: file, styleCheck: false }); | |
var markers = linter.getMarkers().sort(function(a, b){ return a.sl - b.sl; }); | |
var lines = source.split('\n'); | |
if(markers.length !== 0) { | |
markers.forEach(function(marker){ | |
console.log( | |
file + ':' + | |
(marker.pos.sl + 1) + ':' + | |
(marker.pos.sc) + ':' + | |
marker.type.charAt(0).toUpperCase() + | |
': ' + | |
marker.message); | |
}); | |
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Author: Grant MacKenzie <grantmacken@gmail.com> | |
function! ale_linters#xquery#xqlint#Handle(buffer, lines) abort | |
" <file>:<line>:<col>:<code>: <detailed text> | |
let l:pattern = '\v^.*:(\d+):(\d+):(W|E): (.+)$' | |
let l:output = [] | |
for l:match in ale#util#GetMatches(a:lines, l:pattern) | |
let l:code = l:match[3] | |
call add(l:output, { | |
\ 'lnum': l:match[1] + 0, | |
\ 'col': l:match[2] + 0, | |
\ 'type': l:match[3] is# 'E' ? 'E' : 'W', | |
\ 'text': l:match[4], | |
\}) | |
endfor | |
return l:output | |
endfunction | |
call ale#linter#Define('xquery', { | |
\ 'name': 'xqlint', | |
\ 'executable': 'xQlint', | |
\ 'command': 'xQlint' . ' %s' , | |
\ 'callback': 'ale_linters#xquery#xqlint#Handle', | |
\}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I created this so I could use ale
to lint xQuery . Ale can lint when text-is-changed, by using vim buffers with timers,
rather than linting after file on disk changes.
I placed this file name xQlint in my projects
node_modules/.bin
It simply calls the xqlint module functions created by
William Candillon, so the xqlint module must be in your node_modules path.
I use eXist-db as my xQuery engine so my xqlint module in package.json, is the
devDependency: { "xqlint": "git://github.com/eXistSolutions/xqlint.git#exist-syntax"}
The second file xquery.vim is in an
ale_linters/xquery
directory which should be created on vims runtimepath.~/.local/share/nvim/site/ale_linters
I actually symlink to the ale dir itself so xqlint.vim is in my plugged dir
~/.local/share/nvim/site/plugged/ale/ale_linters/xquery/xqlint.vim