robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f3677c71f0
master
1" SQL filetype plugin file 2" Language: SQL (Common for Oracle, Microsoft SQL Server, Sybase) 3" Version: 12.0 4" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com> 5" Last Change: 2017 Mar 07 6" 2024 Jan 14 by Vim Project: browsefilter 7" 2024 May 18 by Vim Project: set comment options 8" 2024 Aug 14 by Vim Project: remove redundant code 9" Download: http://vim.sourceforge.net/script.php?script_id=454 10 11" For more details please use: 12" :h sql.txt 13" 14" This file should only contain values that are common to all SQL languages 15" Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on 16" If additional features are required create: 17" vimfiles/after/ftplugin/sql.vim (Windows) 18" .vim/after/ftplugin/sql.vim (Unix) 19" to override and add any of your own settings. 20 21 22" This file also creates a command, SQLSetType, which allows you to change 23" SQL dialects on the fly. For example, if I open an Oracle SQL file, it 24" is color highlighted appropriately. If I open an Informix SQL file, it 25" will still be highlighted according to Oracles settings. By running: 26" :SQLSetType sqlinformix 27" 28" All files called sqlinformix.vim will be loaded from the indent and syntax 29" directories. This allows you to easily flip SQL dialects on a per file 30" basis. NOTE: you can also use completion: 31" :SQLSetType <tab> 32" 33" To change the default dialect, add the following to your vimrc: 34" let g:sql_type_default = 'sqlanywhere' 35" 36" This file also creates a command, SQLGetType, which allows you to 37" determine what the current dialect is in use. 38" :SQLGetType 39" 40" History 41" 42" Version 12.0 (April 2013) 43" 44" NF: Added support for "BEGIN TRY ... END TRY ... BEGIN CATCH ... END CATCH 45" BF: This plugin is designed to be used with other plugins to enable the 46" SQL completion with Perl, Python, Java, ... The loading mechanism 47" was not checking if the SQL objects were created, which can lead to 48" the plugin not loading the SQL support. 49" 50" Version 11.0 (May 2013) 51" 52" NF: Updated to use SyntaxComplete's new regex support for syntax groups. 53" 54" Version 10.0 (Dec 2012) 55" 56" NF: Changed all maps to use noremap instead of must map 57" NF: Changed all visual maps to use xnoremap instead of vnoremap as they 58" should only be used in visual mode and not select mode. 59" BF: Most of the maps were using doubled up backslashes before they were 60" changed to using the search() function, which meant they no longer 61" worked. 62" 63" Version 9.0 64" 65" NF: Completes 'b:undo_ftplugin' 66" BF: Correctly set cpoptions when creating script 67" 68" Version 8.0 69" 70" NF: Improved the matchit plugin regex (Talek) 71" 72" Version 7.0 73" 74" NF: Calls the sqlcomplete#ResetCacheSyntax() function when calling 75" SQLSetType. 76" 77" Version 6.0 78" 79" NF: Adds the command SQLGetType 80" 81" Version 5.0 82" 83" NF: Adds the ability to choose the keys to control SQL completion, just add 84" the following to your .vimrc: 85" let g:ftplugin_sql_omni_key = '<C-C>' 86" let g:ftplugin_sql_omni_key_right = '<Right>' 87" let g:ftplugin_sql_omni_key_left = '<Left>' 88" 89" BF: format-options - Auto-wrap comments using textwidth was turned off 90" by mistake. 91 92 93" Only do this when not done yet for this buffer 94" This ftplugin can be used with other ftplugins. So ensure loading 95" happens if all elements of this plugin have not yet loaded. 96if exists ( "b:did_ftplugin" ) 97finish 98endif 99 100" Don't load another plugin for this buffer 101let b: did_ftplugin = 1 102 103let s: save_cpo = & cpo 104set cpo & vim 105 106let b: undo_ftplugin = "setl comments< commentstring< formatoptions< define< omnifunc<" . 107 \" | unlet! b:browsefilter b:match_words" 108 109" Disable autowrapping for code, but enable for comments 110" t Auto-wrap text using textwidth 111" c Auto-wrap comments using textwidth, inserting the current comment 112" leader automatically. 113setlocal formatoptions -= t 114setlocal formatoptions += c 115 116setlocal comments = :--commentstring = --\ %s 117 118" Functions/Commands to allow the user to change SQL syntax dialects 119" through the use of :SQLSetType <tab> for completion. 120" This works with both Vim 6 and 7. 121 122if ! exists ( "*SQL_SetType" ) 123" NOTE: You cannot use function! since this file can be 124" sourced from within this function. That will result in 125" an error reported by Vim. 126function SQL_GetList ( ArgLead , CmdLine , CursorPos ) 127 128if ! exists ( 's:sql_list' ) 129" Grab a list of files that contain "sql" in their names 130let list_indent = globpath ( & runtimepath , 'indent/*sql*' ) 131let list_syntax = globpath ( & runtimepath , 'syntax/*sql*' ) 132let list_ftplugin = globpath ( & runtimepath , 'ftplugin/*sql*' ) 133 134let sqls = "\n" . list_indent . "\n" . list_syntax . "\n" . list_ftplugin . "\n" 135 136" Strip out everything (path info) but the filename 137" Regex 138" From between two newline characters 139" Non-greedily grab all characters 140" Followed by a valid filename \w\+\.\w\+ (sql.vim) 141" Followed by a newline, but do not include the newline 142" 143" Replace it with just the filename (get rid of PATH) 144" 145" Recursively, since there are many filenames that contain 146" the word SQL in the indent, syntax and ftplugin directory 147let sqls = substitute ( sqls , 148 \'[\n]\%(.\{-}\)\(\w\+\.\w\+\)\n\@=' , 149 \'\1\n' , 150 \'g' 151 \) 152 153" Remove duplicates, since sqlanywhere.vim can exist in the 154" syntax, indent and ftplugin directory, yet we only want 155" to display the option once 156let index = match ( sqls , '.\{-}\ze\n' ) 157while index > -1 158" Get the first filename 159let file = matchstr ( sqls , '.\{-}\ze\n' , index ) 160" Recursively replace any *other* occurrence of that 161" filename with nothing (ie remove it) 162let sqls = substitute ( sqls , '\%>' . ( index + strlen ( file )) . 'c\<' . file . '\>\n' , '' , 'g' ) 163" Move on to the next filename 164let index = match ( sqls , '.\{-}\ze\n' , ( index + strlen ( file ) + 1 )) 165endwhile 166 167" Sort the list if using version 7 168if v: version >= 700 169let mylist = split ( sqls , "\n" ) 170let mylist = sort ( mylist ) 171let sqls = join ( mylist , "\n" ) 172endif 173 174let s: sql_list = sqls 175endif 176 177return s: sql_list 178 179endfunction 180 181function SQL_SetType ( name ) 182 183" User has decided to override default SQL scripts and 184" specify a vendor specific version 185" (ie Oracle, Informix, SQL Anywhere, ...) 186" So check for an remove any settings that prevent the 187" scripts from being executed, and then source the 188" appropriate Vim scripts. 189if exists ( "b:did_ftplugin" ) 190unlet b: did_ftplugin 191endif 192if exists ( "b:current_syntax" ) 193" echomsg 'SQLSetType - clearing syntax' 194syntax clear 195if exists ( "b:current_syntax" ) 196unlet b: current_syntax 197endif 198endif 199if exists ( "b:did_indent" ) 200" echomsg 'SQLSetType - clearing indent' 201unlet b: did_indent 202" Set these values to their defaults 203setlocal indentkeys & 204setlocal indentexpr & 205endif 206 207" Ensure the name is in the correct format 208let new_sql_type = substitute ( a: name , 209 \'\s*\([^\.]\+\)\(\.\w\+\)\?' , '\L\1' , '' ) 210 211" Do not specify a buffer local variable if it is 212" the default value 213if new_sql_type == 'sql' 214let new_sql_type = 'sqloracle' 215endif 216let b: sql_type_override = new_sql_type 217 218" Remove any cached SQL since a new syntax will have different 219" items and groups 220if ! exists ( 'g:loaded_sql_completion' ) || g: loaded_sql_completion >= 100 221call sqlcomplete#ResetCacheSyntax () 222endif 223 224" Vim will automatically source the correct files if we 225" change the filetype. You cannot do this with setfiletype 226" since that command will only execute if a filetype has 227" not already been set. In this case we want to override 228" the existing filetype. 229let & filetype = 'sql' 230 231if b: sql_compl_savefunc != "" 232" We are changing the filetype to SQL from some other filetype 233" which had OMNI completion defined. We need to activate the 234" SQL completion plugin in order to cache some of the syntax items 235" while the syntax rules for SQL are active. 236call sqlcomplete#PreCacheSyntax () 237endif 238endfunction 239command ! -nargs = * -complete = custom , SQL_GetList SQLSetType :call SQL_SetType(<q-args>) 240 241endif 242 243" Functions/Commands to allow the user determine current SQL syntax dialect 244" This works with both Vim 6 and 7. 245 246if ! exists ( "*SQL_GetType" ) 247function SQL_GetType () 248if exists ( 'b:sql_type_override' ) 249echomsg "Current SQL dialect in use:" . b: sql_type_override 250else 251echomsg "Current SQL dialect in use:" . g: sql_type_default 252endif 253endfunction 254command ! -nargs = 0 SQLGetType :call SQL_GetType() 255endif 256 257if exists ( "b:sql_type_override" ) 258" echo 'sourcing buffer ftplugin/'.b:sql_type_override.'.vim' 259if globpath ( & runtimepath , 'ftplugin/' . b: sql_type_override . '.vim' ) != '' 260exec 'runtime ftplugin/' . b: sql_type_override . '.vim' 261" else 262" echomsg 'ftplugin/'.b:sql_type_override.' not exist, using default' 263endif 264elseif exists ( "g:sql_type_default" ) 265" echo 'sourcing global ftplugin/'.g:sql_type_default.'.vim' 266if globpath ( & runtimepath , 'ftplugin/' . g: sql_type_default . '.vim' ) != '' 267exec 'runtime ftplugin/' . g: sql_type_default . '.vim' 268" else 269" echomsg 'ftplugin/'.g:sql_type_default.'.vim not exist, using default' 270endif 271endif 272 273" Win32 and GTK can filter files in the browse dialog 274if ( has ( "gui_win32" ) || has ( "gui_gtk" )) && ! exists ( "b:browsefilter" ) 275let b: browsefilter = "SQL Files (*.sql)\t*.sql\n" 276if has ( "win32" ) 277let b: browsefilter .= "All Files (*.*)\t*\n" 278else 279let b: browsefilter .= "All Files (*)\t*\n" 280endif 281endif 282 283" Some standard expressions for use with the matchit strings 284let s: notend = '\%(\<end\s\+\)\@<!' 285let s: when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)' 286let s: or_replace = '\%(or\s\+replace\s\+\)\?' 287 288" Define patterns for the matchit macro 289if ! exists ( "b:match_words" ) 290" SQL is generally case insensitive 291let b: match_ignorecase = 1 292 293" Handle the following: 294" if 295" elseif | elsif 296" else [if] 297" end if 298" 299" [while condition] loop 300" leave 301" break 302" continue 303" exit 304" end loop 305" 306" for 307" leave 308" break 309" continue 310" exit 311" end loop 312" 313" do 314" statements 315" doend 316" 317" case 318" when 319" when 320" default 321" end case 322" 323" merge 324" when not matched 325" when matched 326" 327" EXCEPTION 328" WHEN column_not_found THEN 329" WHEN OTHERS THEN 330" 331" begin try 332" end try 333" begin catch 334" end catch 335" 336" create[ or replace] procedure|function|event 337" \ '^\s*\<\%(do\|for\|while\|loop\)\>.*:'. 338 339" For ColdFusion support 340setlocal matchpairs += <:> 341let b: match_words = & matchpairs . 342 \',\%(\<begin\)\%(\s\+\%(try\|catch\)\>\)\@!:\<end\>\W*$,' . 343 \ 344 \'\<begin\s\+try\>:' . 345 \'\<end\s\+try\>:' . 346 \'\<begin\s\+catch\>:' . 347 \'\<end\s\+catch\>,' . 348 \ 349 \s: notend . '\<if\>:' . 350 \'\<elsif\>\|\<elseif\>\|\<else\>:' . 351 \'\<end\s\+if\>,' . 352 \ 353 \'\(^\s*\)\@<=\(\<\%(do\|for\|while\|loop\)\>.*\):' . 354 \'\%(\<exit\>\|\<leave\>\|\<break\>\|\<continue\>\):' . 355 \'\%(\<doend\>\|\%(\<end\s\+\%(for\|while\|loop\>\)\)\),' . 356 \ 357 \'\%(' . s: notend . '\<case\>\):' . 358 \'\%(' . s: when_no_matched_or_others . '\):' . 359 \'\%(\<when\s\+others\>\|\<end\s\+case\>\),' . 360 \ 361 \'\<merge\>:' . 362 \'\<when\s\+not\s\+matched\>:' . 363 \'\<when\s\+matched\>,' . 364 \ 365 \'\%(\<create\s\+' . s: or_replace . '\)\?' . 366 \'\%(function\|procedure\|event\):' . 367 \'\<returns\?\>' 368" \ '\<begin\>\|\<returns\?\>:'. 369" \ '\<end\>\(;\)\?\s*$' 370" \ '\<exception\>:'.s:when_no_matched_or_others. 371" \ ':\<when\s\+others\>,'. 372" 373" \ '\%(\<exception\>\|\%('. s:notend . '\<case\>\)\):'. 374" \ '\%(\<default\>\|'.s:when_no_matched_or_others.'\):'. 375" \ '\%(\%(\<when\s\+others\>\)\|\<end\s\+case\>\),' . 376endif 377 378" Define how to find the macro definition of a variable using the various 379" [d, [D, [_CTRL_D and so on features 380" Match these values ignoring case 381" ie DECLARE varname INTEGER 382let & l: define = '\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>' 383 384 385" Mappings to move to the next BEGIN ... END block 386" \W - no characters or digits 387nnoremap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )< CR > 388nnoremap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )< CR > 389nnoremap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )< CR > 390nnoremap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )< CR > 391xnoremap <buffer> <silent> ]] :< C-U > exec "normal! gv"< Bar > call search('\c^\s*begin\>', 'W' )< CR > 392xnoremap <buffer> <silent> [[ :< C-U > exec "normal! gv"< Bar > call search('\c^\s*begin\>', 'bW' )< CR > 393xnoremap <buffer> <silent> ][ :< C-U > exec "normal! gv"< Bar > call search('\c^\s*end\W*$', 'W' )< CR > 394xnoremap <buffer> <silent> [] :< C-U > exec "normal! gv"< Bar > call search('\c^\s*end\W*$', 'bW' )< CR > 395 396 397" By default only look for CREATE statements, but allow 398" the user to override 399if ! exists ( 'g:ftplugin_sql_statements' ) 400let g: ftplugin_sql_statements = 'create' 401endif 402 403" Predefined SQL objects what are used by the below mappings using 404" the ]} style maps. 405" This global variable allows the users to override its value 406" from within their vimrc. 407" Note, you cannot use \?, since these patterns can be used to search 408" backwards, you must use \{,1} 409if ! exists ( 'g:ftplugin_sql_objects' ) 410let g: ftplugin_sql_objects = 'function,procedure,event,' . 411 \'\(existing\\|global\s\+temporary\s\+\)\{,1}' . 412 \'table,trigger' . 413 \',schema,service,publication,database,datatype,domain' . 414 \',index,subscription,synchronization,view,variable' 415endif 416 417" Key to trigger SQL completion 418if ! exists ( 'g:ftplugin_sql_omni_key' ) 419let g: ftplugin_sql_omni_key = '<C-C>' 420endif 421" Key to trigger drill into column list 422if ! exists ( 'g:ftplugin_sql_omni_key_right' ) 423let g: ftplugin_sql_omni_key_right = '<Right>' 424endif 425" Key to trigger drill out of column list 426if ! exists ( 'g:ftplugin_sql_omni_key_left' ) 427let g: ftplugin_sql_omni_key_left = '<Left>' 428endif 429 430" Replace all ,'s with bars, except ones with numbers after them. 431" This will most likely be a \{,1} string. 432let s: ftplugin_sql_objects = 433 \'\c^\s*' . 434 \'\(\(' . 435 \substitute ( g: ftplugin_sql_statements , ',\d\@!' , '\\\\|' , 'g' ) . 436 \'\)\s\+\(or\s\+replace\s\+\)\{,1}\)\{,1}' . 437 \'\<\(' . 438 \substitute ( g: ftplugin_sql_objects , ',\d\@!' , '\\\\|' , 'g' ) . 439 \'\)\>' 440 441" Mappings to move to the next CREATE ... block 442exec "nnoremap <buffer> <silent> ]} :call search('" . s: ftplugin_sql_objects . "', 'W')<CR>" 443exec "nnoremap <buffer> <silent> [{ :call search('" . s: ftplugin_sql_objects . "', 'bW')<CR>" 444" Could not figure out how to use a :call search() string in visual mode 445" without it ending visual mode 446" Unfortunately, this will add a entry to the search history 447exec 'xnoremap <buffer> <silent> ]} /' . s: ftplugin_sql_objects . '<CR>' 448exec 'xnoremap <buffer> <silent> [{ ?' . s: ftplugin_sql_objects . '<CR>' 449 450" Mappings to move to the next COMMENT 451" 452" Had to double the \ for the \| separator since this has a special 453" meaning on maps 454let b: comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)' 455" Find the start of the next comment 456let b: comment_start = '^\(\s*' . b: comment_leader . '.*\n\)\@<!' . 457 \'\(\s*' . b: comment_leader . '\)' 458" Find the end of the previous comment 459let b: comment_end = '\(^\s*' . b: comment_leader . '.*\n\)' . 460 \'\(^\s*' . b: comment_leader . '\)\@!' 461" Skip over the comment 462let b: comment_jump_over = "call search('" . 463 \'^\(\s*' . b: comment_leader . '.*\n\)\@<!' . 464 \"', 'W')" 465let b: comment_skip_back = "call search('" . 466 \'^\(\s*' . b: comment_leader . '.*\n\)\@<!' . 467 \"', 'bW')" 468" Move to the start and end of comments 469exec 'nnoremap <silent><buffer> ]" :call search(' . "'" . b: comment_start . "'" . ', "W" )<CR>' 470exec 'nnoremap <silent><buffer> [" :call search(' . "'" . b: comment_end . "'" . ', "W" )<CR>' 471exec 'xnoremap <silent><buffer> ]" :<C-U>exec "normal! gv"<Bar>call search(' . "'" . b: comment_start . "'" . ', "W" )<CR>' 472exec 'xnoremap <silent><buffer> [" :<C-U>exec "normal! gv"<Bar>call search(' . "'" . b: comment_end . "'" . ', "W" )<CR>' 473 474" Comments can be of the form: 475" /* 476" * 477" */ 478" or 479" -- 480" or 481" // 482setlocal comments = s1:/*,mb:*,ex:*/,:--,:// 483 484" Set completion with CTRL-X CTRL-O to autoloaded function. 485if exists ( '&omnifunc' ) 486" Since the SQL completion plugin can be used in conjunction 487" with other completion filetypes it must record the previous 488" OMNI function prior to setting up the SQL OMNI function 489let b: sql_compl_savefunc = & omnifunc 490 491" Source it to determine its version 492runtime autoload/sqlcomplete.vim 493" This is used by the sqlcomplete.vim plugin 494" Source it for its global functions 495runtime autoload/syntaxcomplete.vim 496 497setlocal omnifunc = sqlcomplete#Complete 498" Prevent the intellisense plugin from loading 499let b: sql_vis = 1 500if ! exists ( 'g:omni_sql_no_default_maps' ) 501let regex_extra = '' 502if exists ( 'g:loaded_syntax_completion' ) && exists ( 'g:loaded_sql_completion' ) 503if g: loaded_syntax_completion > 120 && g: loaded_sql_completion > 140 504let regex_extra = '\\w*' 505endif 506endif 507" Static maps which use populate the completion list 508" using Vim's syntax highlighting rules 509exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'a <C-\><C-O>:call sqlcomplete#Map("syntax")<CR><C-X><C-O>' 510exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'k <C-\><C-O>:call sqlcomplete#Map("sqlKeyword' . regex_extra . '")<CR><C-X><C-O>' 511exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'f <C-\><C-O>:call sqlcomplete#Map("sqlFunction' . regex_extra . '")<CR><C-X><C-O>' 512exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'o <C-\><C-O>:call sqlcomplete#Map("sqlOption' . regex_extra . '")<CR><C-X><C-O>' 513exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'T <C-\><C-O>:call sqlcomplete#Map("sqlType' . regex_extra . '")<CR><C-X><C-O>' 514exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 's <C-\><C-O>:call sqlcomplete#Map("sqlStatement' . regex_extra . '")<CR><C-X><C-O>' 515" Dynamic maps which use populate the completion list 516" using the dbext.vim plugin 517exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 't <C-\><C-O>:call sqlcomplete#Map("table")<CR><C-X><C-O>' 518exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'p <C-\><C-O>:call sqlcomplete#Map("procedure")<CR><C-X><C-O>' 519exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'v <C-\><C-O>:call sqlcomplete#Map("view")<CR><C-X><C-O>' 520exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'c <C-\><C-O>:call sqlcomplete#Map("column")<CR><C-X><C-O>' 521exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'l <C-\><C-O>:call sqlcomplete#Map("column_csv")<CR><C-X><C-O>' 522" The next 3 maps are only to be used while the completion window is 523" active due to the <CR> at the beginning of the map 524exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'L <C-Y><C-\><C-O>:call sqlcomplete#Map("column_csv")<CR><C-X><C-O>' 525" <C-Right> is not recognized on most Unix systems, so only create 526" these additional maps on the Windows platform. 527" If you would like to use these maps, choose a different key and make 528" the same map in your vimrc. 529" if has('win32') 530exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key_right . ' <C-R>=sqlcomplete#DrillIntoTable()<CR>' 531exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key_left . ' <C-R>=sqlcomplete#DrillOutOfColumns()<CR>' 532" endif 533" Remove any cached items useful for schema changes 534exec 'inoremap <buffer> ' . g: ftplugin_sql_omni_key . 'R <C-\><C-O>:call sqlcomplete#Map("resetCache")<CR><C-X><C-O>' 535endif 536 537if b: sql_compl_savefunc != "" 538" We are changing the filetype to SQL from some other filetype 539" which had OMNI completion defined. We need to activate the 540" SQL completion plugin in order to cache some of the syntax items 541" while the syntax rules for SQL are active. 542call sqlcomplete#ResetCacheSyntax () 543call sqlcomplete#PreCacheSyntax () 544endif 545endif 546 547let & cpo = s: save_cpo 548unlet s: save_cpo 549 550" vim:sw=4: