1
0
Fork 0
mirror of https://github.com/vim/vim synced 2025-03-16 23:08:07 +01:00
vim/runtime/indent/sqlanywhere.vim

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

400 lines
13 KiB
VimL
Raw Normal View History

2006-03-09 22:27:48 +00:00
" Vim indent file
2006-04-30 18:54:39 +00:00
" Language: SQL
2012-12-05 19:01:43 +01:00
" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
2021-10-16 15:23:36 +01:00
" Last Change: 2021 Oct 11
" Version: 4.0
2006-03-09 22:27:48 +00:00
" Download: http://vim.sourceforge.net/script.php?script_id=495
" Notes:
" Indenting keywords are based on Oracle and Sybase Adaptive Server
" Anywhere (ASA). Test indenting was done with ASA stored procedures and
2021-09-09 21:55:11 +02:00
" functions and Oracle packages which contain stored procedures and
2006-03-09 22:27:48 +00:00
" functions.
2006-04-30 18:54:39 +00:00
" This has not been tested against Microsoft SQL Server or
2006-03-09 22:27:48 +00:00
" Sybase Adaptive Server Enterprise (ASE) which use the Transact-SQL
" syntax. That syntax does not have end tags for IF's, which makes
" indenting more difficult.
"
" Known Issues:
" The Oracle MERGE statement does not have an end tag associated with
" it, this can leave the indent hanging to the right one too many.
2012-12-05 19:01:43 +01:00
"
" History:
2021-10-16 15:23:36 +01:00
" 4.0 (Oct 2021)
" Added b:undo_indent
"
2012-12-05 19:01:43 +01:00
" 3.0 (Dec 2012)
" Added cpo check
"
" 2.0
" Added the FOR keyword to SQLBlockStart to handle (Alec Tica):
" for i in 1..100 loop
" |<-- I expect to have indentation here
" end loop;
"
2006-03-09 22:27:48 +00:00
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
let b:current_indent = "sqlanywhere"
setlocal indentkeys-=0{
setlocal indentkeys-=0}
setlocal indentkeys-=:
setlocal indentkeys-=0#
setlocal indentkeys-=e
2006-04-30 18:54:39 +00:00
" This indicates formatting should take place when one of these
2006-03-09 22:27:48 +00:00
" expressions is used. These expressions would normally be something
" you would type at the BEGINNING of a line
" SQL is generally case insensitive, so this files assumes that
" These keywords are something that would trigger an indent LEFT, not
" an indent right, since the SQLBlockStart is used for those keywords
setlocal indentkeys+==~end,=~else,=~elseif,=~elsif,0=~when,0=)
2006-04-30 18:54:39 +00:00
" GetSQLIndent is executed whenever one of the expressions
2006-03-09 22:27:48 +00:00
" in the indentkeys is typed
setlocal indentexpr=GetSQLIndent()
2021-10-16 15:23:36 +01:00
let b:undo_indent = "setl indentexpr< indentkeys<"
2013-01-30 14:18:00 +01:00
" Only define the functions once.
if exists("*GetSQLIndent")
finish
endif
2021-10-16 15:23:36 +01:00
2013-01-30 14:18:00 +01:00
let s:keepcpo= &cpo
set cpo&vim
2006-03-09 22:27:48 +00:00
" List of all the statements that start a new block.
" These are typically words that start a line.
2006-04-30 18:54:39 +00:00
" IS is excluded, since it is difficult to determine when the
2006-03-09 22:27:48 +00:00
" ending block is (especially for procedures/functions).
let s:SQLBlockStart = '^\s*\%('.
2021-10-16 15:23:36 +01:00
\ 'if\|else\|elseif\|elsif\|'.
\ 'while\|loop\|do\|for\|'.
\ 'begin\|'.
2006-03-09 22:27:48 +00:00
\ 'case\|when\|merge\|exception'.
\ '\)\>'
let s:SQLBlockEnd = '^\s*\(end\)\>'
2020-06-07 21:07:18 +02:00
" The indent level is also based on unmatched parentheses
2006-03-09 22:27:48 +00:00
" If a line has an extra "(" increase the indent
" If a line has an extra ")" decrease the indent
2020-06-07 21:07:18 +02:00
function! s:CountUnbalancedParen( line, paren_to_check )
2006-03-09 22:27:48 +00:00
let l = a:line
let lp = substitute(l, '[^(]', '', 'g')
let l = a:line
let rp = substitute(l, '[^)]', '', 'g')
2020-06-07 21:07:18 +02:00
if a:paren_to_check =~ ')'
" echom 'CountUnbalancedParen ) returning: ' .
2006-03-09 22:27:48 +00:00
" \ (strlen(rp) - strlen(lp))
return (strlen(rp) - strlen(lp))
2020-06-07 21:07:18 +02:00
elseif a:paren_to_check =~ '('
" echom 'CountUnbalancedParen ( returning: ' .
2006-03-09 22:27:48 +00:00
" \ (strlen(lp) - strlen(rp))
return (strlen(lp) - strlen(rp))
else
2020-06-07 21:07:18 +02:00
" echom 'CountUnbalancedParen unknown paren to check: ' .
" \ a:paren_to_check
2006-03-09 22:27:48 +00:00
return 0
endif
endfunction
" Unindent commands based on previous indent level
2020-06-07 21:07:18 +02:00
function! s:CheckToIgnoreRightParen( prev_lnum, num_levels )
2006-03-09 22:27:48 +00:00
let lnum = a:prev_lnum
let line = getline(lnum)
let ends = 0
2020-06-07 21:07:18 +02:00
let num_right_paren = a:num_levels
let ignore_paren = 0
2006-03-09 22:27:48 +00:00
let vircol = 1
2020-06-07 21:07:18 +02:00
while num_right_paren > 0
2006-03-09 22:27:48 +00:00
silent! exec 'norm! '.lnum."G\<bar>".vircol."\<bar>"
2020-06-07 21:07:18 +02:00
let right_paren = search( ')', 'W' )
if right_paren != lnum
2006-03-09 22:27:48 +00:00
" This should not happen since there should be at least
2020-06-07 21:07:18 +02:00
" num_right_paren matches for this line
2006-03-09 22:27:48 +00:00
break
endif
let vircol = virtcol(".")
" if getline(".") =~ '^)'
2020-06-07 21:07:18 +02:00
let matching_paren = searchpair('(', '', ')', 'bW',
2008-06-24 20:39:31 +00:00
\ 's:IsColComment(line("."), col("."))')
2006-03-09 22:27:48 +00:00
2020-06-07 21:07:18 +02:00
if matching_paren < 1
2006-03-09 22:27:48 +00:00
" No match found
" echom 'CTIRP - no match found, ignoring'
break
endif
2020-06-07 21:07:18 +02:00
if matching_paren == lnum
" This was not an unmatched parentheses, start the search again
2006-03-09 22:27:48 +00:00
" again after this column
" echom 'CTIRP - same line match, ignoring'
continue
endif
" echom 'CTIRP - match: ' . line(".") . ' ' . getline(".")
2020-06-07 21:07:18 +02:00
if getline(matching_paren) =~? '\(if\|while\)\>'
2006-03-09 22:27:48 +00:00
" echom 'CTIRP - if/while ignored: ' . line(".") . ' ' . getline(".")
2020-06-07 21:07:18 +02:00
let ignore_paren = ignore_paren + 1
2006-03-09 22:27:48 +00:00
endif
" One match found, decrease and check for further matches
2020-06-07 21:07:18 +02:00
let num_right_paren = num_right_paren - 1
2006-03-09 22:27:48 +00:00
endwhile
" Fallback - just move back one
" return a:prev_indent - shiftwidth()
2020-06-07 21:07:18 +02:00
return ignore_paren
2006-03-09 22:27:48 +00:00
endfunction
" Based on the keyword provided, loop through previous non empty
2020-06-07 21:07:18 +02:00
" non comment lines to find the statement that initiated the keyword.
2006-03-09 22:27:48 +00:00
" Return its indent level
" CASE ..
" WHEN ...
" Should return indent level of CASE
" EXCEPTION ..
" WHEN ...
" something;
" WHEN ...
" Should return indent level of exception.
2012-12-05 19:01:43 +01:00
function! s:GetStmtStarterIndent( keyword, curr_lnum )
2006-03-09 22:27:48 +00:00
let lnum = a:curr_lnum
" Default - reduce indent by 1
let ind = indent(a:curr_lnum) - shiftwidth()
2006-03-09 22:27:48 +00:00
if a:keyword =~? 'end'
exec 'normal! ^'
let stmts = '^\s*\%('.
\ '\<begin\>\|' .
\ '\%(\%(\<end\s\+\)\@<!\<loop\>\)\|' .
\ '\%(\%(\<end\s\+\)\@<!\<case\>\)\|' .
\ '\%(\%(\<end\s\+\)\@<!\<for\>\)\|' .
\ '\%(\%(\<end\s\+\)\@<!\<if\>\)'.
\ '\)'
let matching_lnum = searchpair(stmts, '', '\<end\>\zs', 'bW',
2008-06-24 20:39:31 +00:00
\ 's:IsColComment(line("."), col(".")) == 1')
2006-03-09 22:27:48 +00:00
exec 'normal! $'
if matching_lnum > 0 && matching_lnum < a:curr_lnum
let ind = indent(matching_lnum)
endif
elseif a:keyword =~? 'when'
exec 'normal! ^'
let matching_lnum = searchpair(
2006-04-30 18:54:39 +00:00
\ '\%(\<end\s\+\)\@<!\<case\>\|\<exception\>\|\<merge\>',
\ '',
\ '\%(\%(\<when\s\+others\>\)\|\%(\<end\s\+case\>\)\)',
2006-03-09 22:27:48 +00:00
\ 'bW',
2008-06-24 20:39:31 +00:00
\ 's:IsColComment(line("."), col(".")) == 1')
2006-03-09 22:27:48 +00:00
exec 'normal! $'
if matching_lnum > 0 && matching_lnum < a:curr_lnum
let ind = indent(matching_lnum)
else
let ind = indent(a:curr_lnum)
endif
endif
return ind
endfunction
" Check if the line is a comment
2012-12-05 19:01:43 +01:00
function! s:IsLineComment(lnum)
2006-03-09 22:27:48 +00:00
let rc = synIDattr(
2006-04-30 18:54:39 +00:00
\ synID(a:lnum,
2006-03-09 22:27:48 +00:00
\ match(getline(a:lnum), '\S')+1, 0)
2006-04-30 18:54:39 +00:00
\ , "name")
\ =~? "comment"
2006-03-09 22:27:48 +00:00
return rc
endfunction
" Check if the column is a comment
2012-12-05 19:01:43 +01:00
function! s:IsColComment(lnum, cnum)
2006-04-30 18:54:39 +00:00
let rc = synIDattr(synID(a:lnum, a:cnum, 0), "name")
\ =~? "comment"
2006-03-09 22:27:48 +00:00
return rc
endfunction
2008-06-24 20:39:31 +00:00
" Instead of returning a column position, return
" an appropriate value as a factor of shiftwidth.
2012-12-05 19:01:43 +01:00
function! s:ModuloIndent(ind)
2006-03-09 22:27:48 +00:00
let ind = a:ind
2006-04-30 18:54:39 +00:00
2006-03-09 22:27:48 +00:00
if ind > 0
let modulo = ind % shiftwidth()
2006-03-09 22:27:48 +00:00
if modulo > 0
let ind = ind - modulo
endif
endif
return ind
endfunction
" Find correct indent of a new line based upon the previous line
2012-12-05 19:01:43 +01:00
function! GetSQLIndent()
2006-04-30 18:54:39 +00:00
let lnum = v:lnum
2006-03-09 22:27:48 +00:00
let ind = indent(lnum)
" If the current line is a comment, leave the indent as is
" Comment out this additional check since it affects the
" indenting of =, and will not reindent comments as it should
2008-06-24 20:39:31 +00:00
" if s:IsLineComment(lnum) == 1
2006-03-09 22:27:48 +00:00
" return ind
" endif
2012-12-05 19:01:43 +01:00
" Get previous non-blank line
let prevlnum = prevnonblank(lnum - 1)
if prevlnum <= 0
return ind
endif
2006-03-09 22:27:48 +00:00
2012-12-05 19:01:43 +01:00
if s:IsLineComment(prevlnum) == 1
if getline(v:lnum) =~ '^\s*\*'
let ind = s:ModuloIndent(indent(prevlnum))
return ind + 1
2006-03-09 22:27:48 +00:00
endif
2012-12-05 19:01:43 +01:00
" If the previous line is a comment, then return -1
" to tell Vim to use the formatoptions setting to determine
" the indent to use
" But only if the next line is blank. This would be true if
" the user is typing, but it would not be true if the user
" is reindenting the file
if getline(v:lnum) =~ '^\s*$'
return -1
endif
endif
2006-03-09 22:27:48 +00:00
2006-04-30 18:54:39 +00:00
" echom 'PREVIOUS INDENT: ' . indent(prevlnum) . ' LINE: ' . getline(prevlnum)
2006-03-09 22:27:48 +00:00
" This is the line you just hit return on, it is not the current line
" which is new and empty
" Based on this line, we can determine how much to indent the new
" line
2006-04-30 18:54:39 +00:00
2006-03-09 22:27:48 +00:00
" Get default indent (from prev. line)
let ind = indent(prevlnum)
let prevline = getline(prevlnum)
" Now check what's on the previous line to determine if the indent
" should be changed, for example IF, BEGIN, should increase the indent
" where END IF, END, should decrease the indent.
if prevline =~? s:SQLBlockStart
" Move indent in
let ind = ind + shiftwidth()
2006-03-09 22:27:48 +00:00
" echom 'prevl - SQLBlockStart - indent ' . ind . ' line: ' . prevline
elseif prevline =~ '[()]'
if prevline =~ '('
2020-06-07 21:07:18 +02:00
let num_unmatched_left = s:CountUnbalancedParen( prevline, '(' )
2006-03-09 22:27:48 +00:00
else
let num_unmatched_left = 0
endif
if prevline =~ ')'
2020-06-07 21:07:18 +02:00
let num_unmatched_right = s:CountUnbalancedParen( prevline, ')' )
2006-03-09 22:27:48 +00:00
else
let num_unmatched_right = 0
2020-06-07 21:07:18 +02:00
" let num_unmatched_right = s:CountUnbalancedParen( prevline, ')' )
2006-03-09 22:27:48 +00:00
endif
if num_unmatched_left > 0
2020-06-07 21:07:18 +02:00
" There is a open left parenthesis
2006-03-09 22:27:48 +00:00
" increase indent
let ind = ind + ( shiftwidth() * num_unmatched_left )
2006-03-09 22:27:48 +00:00
elseif num_unmatched_right > 0
2020-06-07 21:07:18 +02:00
" if it is an unbalanced parenthesis only unindent if
2006-03-09 22:27:48 +00:00
" it was part of a command (ie create table(..) )
" instead of part of an if (ie if (....) then) which should
" maintain the indent level
2020-06-07 21:07:18 +02:00
let ignore = s:CheckToIgnoreRightParen( prevlnum, num_unmatched_right )
2006-03-09 22:27:48 +00:00
" echom 'prevl - ) unbalanced - CTIRP - ignore: ' . ignore
if prevline =~ '^\s*)'
let ignore = ignore + 1
" echom 'prevl - begins ) unbalanced ignore: ' . ignore
endif
if (num_unmatched_right - ignore) > 0
let ind = ind - ( shiftwidth() * (num_unmatched_right - ignore) )
2006-03-09 22:27:48 +00:00
endif
endif
endif
" echom 'CURRENT INDENT: ' . ind . ' LINE: ' . getline(v:lnum)
" This is a new blank line since we just typed a carriage return
" Check current line; search for simplistic matching start-of-block
let line = getline(v:lnum)
if line =~? '^\s*els'
2006-04-30 18:54:39 +00:00
" Any line when you type else will automatically back up one
2006-03-09 22:27:48 +00:00
" ident level (ie else, elseif, elsif)
let ind = ind - shiftwidth()
2006-03-09 22:27:48 +00:00
" echom 'curr - else - indent ' . ind
elseif line =~? '^\s*end\>'
let ind = s:GetStmtStarterIndent('end', v:lnum)
" General case for end
" let ind = ind - shiftwidth()
2006-03-09 22:27:48 +00:00
" echom 'curr - end - indent ' . ind
elseif line =~? '^\s*when\>'
let ind = s:GetStmtStarterIndent('when', v:lnum)
" If the WHEN clause is used with a MERGE or EXCEPTION
" clause, do not change the indent level, since these
" statements do not have a corresponding END statement.
" if stmt_starter =~? 'case'
" let ind = ind - shiftwidth()
2006-03-09 22:27:48 +00:00
" endif
" elseif line =~ '^\s*)\s*;\?\s*$'
" elseif line =~ '^\s*)'
elseif line =~ '^\s*)'
2020-06-07 21:07:18 +02:00
let num_unmatched_right = s:CountUnbalancedParen( line, ')' )
let ignore = s:CheckToIgnoreRightParen( v:lnum, num_unmatched_right )
2006-03-09 22:27:48 +00:00
" If the line ends in a ), then reduce the indent
" This catches items like:
" CREATE TABLE T1(
2006-04-30 18:54:39 +00:00
" c1 int,
2006-03-09 22:27:48 +00:00
" c2 int
" );
" But we do not want to unindent a line like:
2006-04-30 18:54:39 +00:00
" IF ( c1 = 1
2006-03-09 22:27:48 +00:00
" AND c2 = 3 ) THEN
2020-06-07 21:07:18 +02:00
" let num_unmatched_right = s:CountUnbalancedParen( line, ')' )
2006-03-09 22:27:48 +00:00
" if num_unmatched_right > 0
" elseif strpart( line, strlen(line)-1, 1 ) =~ ')'
" let ind = ind - shiftwidth()
2006-03-09 22:27:48 +00:00
if line =~ '^\s*)'
" let ignore = ignore + 1
" echom 'curr - begins ) unbalanced ignore: ' . ignore
endif
if (num_unmatched_right - ignore) > 0
let ind = ind - ( shiftwidth() * (num_unmatched_right - ignore) )
2006-03-09 22:27:48 +00:00
endif
" endif
endif
" echom 'final - indent ' . ind
2008-06-24 20:39:31 +00:00
return s:ModuloIndent(ind)
2006-03-09 22:27:48 +00:00
endfunction
2012-12-05 19:01:43 +01:00
" Restore:
let &cpo= s:keepcpo
unlet s:keepcpo
2012-12-05 19:01:43 +01:00
" vim: ts=4 fdm=marker sw=4