joereynolds / sql-lint

An SQL linter
MIT License
436 stars 40 forks source link

Vim plugin #30

Open joereynolds opened 6 years ago

joereynolds commented 6 years ago

How it should work

"Double check the syntax for +=, just assuming that's correct
set makeprg+=sql-lint\ --file=%
set errorformat+=%f:%l\ %m
Plug 'joereynolds/sql-lint.vim', {'do': 'npm install'}

This will do npm install sql-lint under the hood.

A nice to have is that it would work in non .sql files.

i.e. for PHP, it should try its best to recognise an SQL query in the buffer and run sql-lint on that query.

joereynolds commented 6 years ago

Ale patch

" ale_linters/sql/sqllint.vim
" Author: Joe Reynolds <joereynolds952@gmail.com>
" Description: sql-lint for SQL files

function! ale_linters#sql#sqllint#Handle(buffer, lines) abort
    " Matches patterns like the following:
    "
    " stdin:1 [ER_NO_DB_ERROR] No database selected
    let l:pattern = '\v^[^:]+:(\d+) (.*)'
    let l:output = []

    for l:match in ale#util#GetMatches(a:lines, l:pattern)
    " echom l:match[0]
        call add(l:output, {
        \   'lnum': l:match[1] + 0,
        \   'col': l:match[2] + 0,
        \   'type': l:match[3][0],
        \   'text': l:match[0],
        \})
    endfor

    return l:output
endfunction

call ale#linter#Define('sql', {
\   'name': 'sqllint',
\   'executable': 'sql-lint',
\   'command': 'sql-lint',
\   'callback': 'ale_linters#sql#sqllint#Handle',
\})
andrewimeson commented 5 years ago

It'd be nice to get support added into ALE directly so that it automatically uses sql-lint if it's installed. I'll take a look at past PRs in w0rp/ALE to add linters and see if it's something I can knock out.

EDIT: Oops, looks like it is already here https://github.com/w0rp/ale/pull/2221

joereynolds commented 5 years ago

Hey @andrewimeson you're welcome to take a stab at it, I doubt I'll get around to finishing it off any time soon

joereynolds commented 4 years ago

As an update, I've opened up a new PR on the ale repo here:

https://github.com/dense-analysis/ale/pull/2988

joereynolds commented 4 years ago

Hey @andrewimeson

My patch has been merged into ale. Enjoy!

joereynolds commented 3 years ago

It'd be good if vim users could just have this installed locally and not as a global dependency. See #170