comparison runtime/autoload/sqlcomplete.vim @ 3996:b3f3237a3d72

Update runtime files.
author Bram Moolenaar <bram@vim.org>
date Wed, 05 Dec 2012 19:01:43 +0100
parents 8dcf3ea92b63
children eb6ab7e78925
comparison
equal deleted inserted replaced
3995:c2ea289a5b7f 3996:b3f3237a3d72
1 " Vim OMNI completion script for SQL 1 " Vim OMNI completion script for SQL
2 " Language: SQL 2 " Language: SQL
3 " Maintainer: David Fishburn <dfishburn dot vim at gmail dot com> 3 " Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
4 " Version: 12.0 4 " Version: 14.0
5 " Last Change: 2012 Feb 08 5 " Last Change: 2012 Dec 04
6 " Homepage: http://www.vim.org/scripts/script.php?script_id=1572
6 " Usage: For detailed help 7 " Usage: For detailed help
7 " ":help sql.txt" 8 " ":help sql.txt"
8 " or ":help ft-sql-omni" 9 " or ":help ft-sql-omni"
9 " or read $VIMRUNTIME/doc/sql.txt 10 " or read $VIMRUNTIME/doc/sql.txt
10 11
11 " History 12 " History
12 " Version 12.0 13 "
14 " Version 14.0 (Dec 2012)
15 " - BF: Added check for cpo
16 "
17 " Version 13.0 (Dec 2012)
18 " - NF: When completing column lists or drilling into a table
19 " and g:omni_sql_include_owner is enabled, the
20 " only the table name would be replaced with the column
21 " list instead of the table name and owner (if specified).
22 " - NF: When completing column lists using table aliases
23 " and g:omni_sql_include_owner is enabled, account
24 " for the owner name when looking up the table
25 " list instead of the table name and owner (if specified).
26 " - BF: When completing column lists or drilling into a table
27 " and g:omni_sql_include_owner is enabled, the
28 " column list could often not be found for the table.
29 " - BF: When OMNI popped up, possibly the wrong word
30 " would be replaced for column and column list options.
31 "
32 " Version 12.0 (Feb 2012)
13 " - Partial column name completion did not work when a table 33 " - Partial column name completion did not work when a table
14 " name or table alias was provided (Jonas Enberg). 34 " name or table alias was provided (Jonas Enberg).
15 " - Improved the handling of column completion. First we match any 35 " - Improved the handling of column completion. First we match any
16 " columns from a previous completion. If not matches are found, we 36 " columns from a previous completion. If not matches are found, we
17 " consider the partial name to be a table or table alias for the 37 " consider the partial name to be a table or table alias for the
18 " query and attempt to match on it. 38 " query and attempt to match on it.
19 " 39 "
20 " Version 11.0 40 " Version 11.0 (Jan 2012)
21 " Added g:omni_sql_default_compl_type variable 41 " Added g:omni_sql_default_compl_type variable
22 " - You can specify which type of completion to default to 42 " - You can specify which type of completion to default to
23 " when pressing <C-X><C-O>. The entire list of available 43 " when pressing <C-X><C-O>. The entire list of available
24 " choices can be found in the calls to sqlcomplete#Map in: 44 " choices can be found in the calls to sqlcomplete#Map in:
25 " ftplugin/sql.vim 45 " ftplugin/sql.vim
38 " the warning. 58 " the warning.
39 " Updated SQLCErrorMsg() 59 " Updated SQLCErrorMsg()
40 " - Prepends error message with SQLComplete so you know who issued 60 " - Prepends error message with SQLComplete so you know who issued
41 " the error. 61 " the error.
42 " 62 "
43 " Version 9.0 63 " Version 9.0 (May 2010)
44 " This change removes some of the support for tables with spaces in their 64 " This change removes some of the support for tables with spaces in their
45 " names in order to simplify the regexes used to pull out query table 65 " names in order to simplify the regexes used to pull out query table
46 " aliases for more robust table name and column name code completion. 66 " aliases for more robust table name and column name code completion.
47 " Full support for "table names with spaces" can be added in again 67 " Full support for "table names with spaces" can be added in again
48 " after 7.3. 68 " after 7.3.
49 " 69 "
50 " Version 8.0 70 " Version 8.0
51 " Incorrectly re-executed the g:ftplugin_sql_omni_key_right and g:ftplugin_sql_omni_key_left 71 " Incorrectly re-executed the g:ftplugin_sql_omni_key_right and g:ftplugin_sql_omni_key_left
52 " when drilling in and out of a column list for a table. 72 " when drilling in and out of a column list for a table.
53 " 73 "
54 " Version 7.0 74 " Version 7.0 (Jan 2010)
55 " Better handling of object names 75 " Better handling of object names
56 " 76 "
57 " Version 6.0 77 " Version 6.0 (Apr 2008)
58 " Supports object names with spaces "my table name" 78 " Supports object names with spaces "my table name"
59 " 79 "
60 " Set completion with CTRL-X CTRL-O to autoloaded function. 80 " Set completion with CTRL-X CTRL-O to autoloaded function.
61 " This check is in place in case this script is 81 " This check is in place in case this script is
62 " sourced directly instead of using the autoload feature. 82 " sourced directly instead of using the autoload feature.
69 endif 89 endif
70 90
71 if exists('g:loaded_sql_completion') 91 if exists('g:loaded_sql_completion')
72 finish 92 finish
73 endif 93 endif
74 let g:loaded_sql_completion = 120 94 let g:loaded_sql_completion = 130
95 let s:keepcpo= &cpo
96 set cpo&vim
75 97
76 " Maintains filename of dictionary 98 " Maintains filename of dictionary
77 let s:sql_file_table = "" 99 let s:sql_file_table = ""
78 let s:sql_file_procedure = "" 100 let s:sql_file_procedure = ""
79 let s:sql_file_view = "" 101 let s:sql_file_view = ""
135 if !exists('g:omni_sql_default_compl_type') 157 if !exists('g:omni_sql_default_compl_type')
136 let g:omni_sql_default_compl_type = 'table' 158 let g:omni_sql_default_compl_type = 'table'
137 endif 159 endif
138 160
139 " This function is used for the 'omnifunc' option. 161 " This function is used for the 'omnifunc' option.
162 " It is called twice by omni and it is responsible
163 " for returning the completion list of items.
164 " But it must also determine context of what to complete
165 " and what to "replace" with the completion.
166 " The a:base, is replaced directly with what the user
167 " chooses from the choices.
168 " The s:prepend provides context for the completion.
140 function! sqlcomplete#Complete(findstart, base) 169 function! sqlcomplete#Complete(findstart, base)
141 170
142 " Default to table name completion 171 " Default to table name completion
143 let compl_type = 'table' 172 let compl_type = 'table'
144 " Allow maps to specify what type of object completion they want 173 " Allow maps to specify what type of object completion they want
145 if exists('b:sql_compl_type') 174 if exists('b:sql_compl_type')
146 let compl_type = b:sql_compl_type 175 let compl_type = b:sql_compl_type
147 endif 176 endif
177 let begindot = 0
148 178
149 " First pass through this function determines how much of the line should 179 " First pass through this function determines how much of the line should
150 " be replaced by whatever is chosen from the completion list 180 " be replaced by whatever is chosen from the completion list
151 if a:findstart 181 if a:findstart
152 " Locate the start of the item, including "." 182 " Locate the start of the item, including "."
153 let line = getline('.') 183 let line = getline('.')
154 let start = col('.') - 1 184 let start = col('.') - 1
155 let lastword = -1 185 let lastword = -1
156 let begindot = 0
157 " Check if the first character is a ".", for column completion 186 " Check if the first character is a ".", for column completion
158 if line[start - 1] == '.' 187 if line[start - 1] == '.'
159 let begindot = 1 188 let begindot = 1
160 endif 189 endif
161 while start > 0 190 while start > 0
177 " owner_name.table_name 206 " owner_name.table_name
178 207
179 " If lastword has already been set for column completion 208 " If lastword has already been set for column completion
180 " break from the loop, since we do not also want to pickup 209 " break from the loop, since we do not also want to pickup
181 " a table name if it was also supplied. 210 " a table name if it was also supplied.
211 " Unless g:omni_sql_include_owner == 1, then we can
212 " include the ownername.
182 if lastword != -1 && compl_type == 'column' 213 if lastword != -1 && compl_type == 'column'
214 \ && g:omni_sql_include_owner == 0
183 break 215 break
184 endif 216 endif
185 " If column completion was specified stop at the "." if 217 " If column completion was specified stop at the "." if
186 " a . was specified, otherwise, replace all the way up 218 " a . was specified, otherwise, replace all the way up
187 " to the owner name (if included). 219 " to the owner name (if included).
189 let lastword = start 221 let lastword = start
190 endif 222 endif
191 " If omni_sql_include_owner = 0, do not include the table 223 " If omni_sql_include_owner = 0, do not include the table
192 " name as part of the substitution, so break here 224 " name as part of the substitution, so break here
193 if lastword == -1 && 225 if lastword == -1 &&
194 \ compl_type =~ 'table\|view\|procedure\column_csv' && 226 \ compl_type =~ '\<\(table\|view\|procedure\|column\|column_csv\)\>' &&
195 \ g:omni_sql_include_owner == 0 227 \ g:omni_sql_include_owner == 0
196 let lastword = start 228 let lastword = start
197 break 229 break
198 endif 230 endif
199 let start -= 1 231 let start -= 1
285 if base =~ '\.' 317 if base =~ '\.'
286 " Check if the owner/creator has been specified 318 " Check if the owner/creator has been specified
287 let owner = matchstr( base, '^\zs.*\ze\..*\..*' ) 319 let owner = matchstr( base, '^\zs.*\ze\..*\..*' )
288 let table = matchstr( base, '^\(.*\.\)\?\zs.*\ze\..*' ) 320 let table = matchstr( base, '^\(.*\.\)\?\zs.*\ze\..*' )
289 let column = matchstr( base, '.*\.\zs.*' ) 321 let column = matchstr( base, '.*\.\zs.*' )
322
323 if g:omni_sql_include_owner == 1 && owner == '' && table != '' && column != ''
324 let owner = table
325 let table = column
326 let column = ''
327 endif
290 328
291 " It is pretty well impossible to determine if the user 329 " It is pretty well impossible to determine if the user
292 " has entered: 330 " has entered:
293 " owner.table 331 " owner.table
294 " table.column_prefix 332 " table.column_prefix
368 " list of values instead of multiple array entries 406 " list of values instead of multiple array entries
369 " for each column in the table. 407 " for each column in the table.
370 let list_type = 'csv' 408 let list_type = 'csv'
371 endif 409 endif
372 410
373 let compl_list = s:SQLCGetColumns(table, list_type) 411 " If we are including the OWNER for the objects, then for
412 " table completion, if we have it, it should be included
413 " as there can be the same table names in a database yet
414 " with different owner names.
415 if g:omni_sql_include_owner == 1 && owner != '' && table != ''
416 let compl_list = s:SQLCGetColumns(owner.'.'.table, list_type)
417 else
418 let compl_list = s:SQLCGetColumns(table, list_type)
419 endif
420
374 if column != '' 421 if column != ''
375 " If no column prefix has been provided and the table 422 " If no column prefix has been provided and the table
376 " name was provided, append it to each of the items 423 " name was provided, append it to each of the items
377 " returned. 424 " returned.
378 let compl_list = map(compl_list, 'table.".".v:val') 425 let compl_list = map(compl_list, 'table.".".v:val')
391 let compl_list = [join(compl_list, ', ')] 438 let compl_list = [join(compl_list, ', ')]
392 endif 439 endif
393 endif 440 endif
394 elseif compl_type == 'resetCache' 441 elseif compl_type == 'resetCache'
395 " Reset all cached items 442 " Reset all cached items
396 let s:tbl_name = [] 443 let s:tbl_name = []
397 let s:tbl_alias = [] 444 let s:tbl_alias = []
398 let s:tbl_cols = [] 445 let s:tbl_cols = []
399 let s:syn_list = [] 446 let s:syn_list = []
400 let s:syn_value = [] 447 let s:syn_value = []
448 let s:sql_file_table = ""
449 let s:sql_file_procedure = ""
450 let s:sql_file_view = ""
401 451
402 let msg = "All SQL cached items have been removed." 452 let msg = "All SQL cached items have been removed."
403 call s:SQLCWarningMsg(msg) 453 call s:SQLCWarningMsg(msg)
404 " Leave time for the user to read the error message 454 " Leave time for the user to read the error message
405 :sleep 2 455 :sleep 2
421 let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|^\\(\\w\\+\\.\\)\\?'.base.'\\)"' 471 let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|^\\(\\w\\+\\.\\)\\?'.base.'\\)"'
422 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\)"' 472 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\)"'
423 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\(\\.\\)\\?'.base.'\\)"' 473 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\(\\.\\)\\?'.base.'\\)"'
424 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\([^.]*\\)\\?'.base.'\\)"' 474 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\([^.]*\\)\\?'.base.'\\)"'
425 let compl_list = filter(deepcopy(compl_list), expr) 475 let compl_list = filter(deepcopy(compl_list), expr)
476
477 if empty(compl_list) && compl_type == 'table' && base =~ '\.$'
478 " It is possible we could be looking for column name completion
479 " and the user simply hit C-X C-O to lets try it as well
480 " since we had no hits with the tables.
481 " If the base ends with a . it is hard to know if we are
482 " completing table names or column names.
483 let list_type = ''
484
485 let compl_list = s:SQLCGetColumns(base, list_type)
486 endif
426 endif 487 endif
427 488
428 if exists('b:sql_compl_savefunc') && b:sql_compl_savefunc != "" 489 if exists('b:sql_compl_savefunc') && b:sql_compl_savefunc != ""
429 let &omnifunc = b:sql_compl_savefunc 490 let &omnifunc = b:sql_compl_savefunc
491 endif
492
493 if empty(compl_list)
494 call s:SQLCWarningMsg( 'Could not find type['.compl_type.'] using prepend[.'.s:prepended.'] base['.a:base.']' )
430 endif 495 endif
431 496
432 return compl_list 497 return compl_list
433 endfunc 498 endfunc
434 499
662 let owner = matchstr( a:object, '^\("\|\[\)\?\zs\.\{-}\ze\("\|\]\)\?\.' ) 727 let owner = matchstr( a:object, '^\("\|\[\)\?\zs\.\{-}\ze\("\|\]\)\?\.' )
663 return owner 728 return owner
664 endfunction 729 endfunction
665 730
666 function! s:SQLCGetColumns(table_name, list_type) 731 function! s:SQLCGetColumns(table_name, list_type)
732 if a:table_name =~ '\.'
733 " Check if the owner/creator has been specified
734 let owner = matchstr( a:table_name, '^\zs.*\ze\..*\..*' )
735 let table = matchstr( a:table_name, '^\(.*\.\)\?\zs.*\ze\..*' )
736 let column = matchstr( a:table_name, '.*\.\zs.*' )
737
738 if g:omni_sql_include_owner == 1 && owner == '' && table != '' && column != ''
739 let owner = table
740 let table = column
741 let column = ''
742 endif
743 else
744 let owner = ''
745 let table = matchstr(a:table_name, '^["\[\]a-zA-Z0-9_ ]\+\ze\.\?')
746 let column = ''
747 endif
748
667 " Check if the table name was provided as part of the column name 749 " Check if the table name was provided as part of the column name
668 let table_name = matchstr(a:table_name, '^["\[\]a-zA-Z0-9_ ]\+\ze\.\?') 750 " let table_name = matchstr(a:table_name, '^["\[\]a-zA-Z0-9_ ]\+\ze\.\?')
751 let table_name = table
669 let table_cols = [] 752 let table_cols = []
670 let table_alias = '' 753 let table_alias = ''
671 let move_to_top = 1 754 let move_to_top = 1
672 755
673 let table_name = substitute(table_name, '\s*\(.\{-}\)\s*$', '\1', 'g') 756 let table_name = substitute(table_name, '\s*\(.\{-}\)\s*$', '\1', 'g')
784 \ '.*' 867 \ '.*'
785 \ ) 868 \ )
786 869
787 if table_name_new != '' 870 if table_name_new != ''
788 let table_alias = table_name 871 let table_alias = table_name
789 let table_name = matchstr( table_name_new, '^\(.*\.\)\?\zs.*\ze' ) 872 if g:omni_sql_include_owner == 1
873 let table_name = matchstr( table_name_new, '^\zs\(.\{-}\.\)\?\(.\{-}\.\)\?.*\ze' )
874 else
875 " let table_name = matchstr( table_name_new, '^\(.*\.\)\?\zs.*\ze' )
876 let table_name = matchstr( table_name_new, '^\(.\{-}\.\)\?\zs\(.\{-}\.\)\?.*\ze' )
877 endif
790 878
791 let list_idx = index(s:tbl_name, table_name, 0, &ignorecase) 879 let list_idx = index(s:tbl_name, table_name, 0, &ignorecase)
792 if list_idx > -1 880 if list_idx > -1
793 let table_cols = split(s:tbl_cols[list_idx]) 881 let table_cols = split(s:tbl_cols[list_idx])
794 let s:tbl_name[list_idx] = table_name 882 let s:tbl_name[list_idx] = table_name
826 endif 914 endif
827 915
828 if empty(table_cols) 916 if empty(table_cols)
829 " Specify silent mode, no messages to the user (tbl, 1) 917 " Specify silent mode, no messages to the user (tbl, 1)
830 " Specify do not comma separate (tbl, 1, 1) 918 " Specify do not comma separate (tbl, 1, 1)
831 let table_cols_str = DB_getListColumn(table_name, 1, 1) 919 " let table_cols_str = DB_getListColumn(table_name, 1, 1)
920 let table_cols_str = DB_getListColumn((owner!=''?owner.'.':'').table_name, 1, 1)
832 921
833 if table_cols_str != "" 922 if table_cols_str != ""
834 let s:tbl_name = add( s:tbl_name, table_name ) 923 let s:tbl_name = add( s:tbl_name, table_name )
835 let s:tbl_alias = add( s:tbl_alias, table_alias ) 924 let s:tbl_alias = add( s:tbl_alias, table_alias )
836 let s:tbl_cols = add( s:tbl_cols, table_cols_str ) 925 let s:tbl_cols = add( s:tbl_cols, table_cols_str )
852 let table_cols = [cols] 941 let table_cols = [cols]
853 endif 942 endif
854 943
855 return table_cols 944 return table_cols
856 endfunction 945 endfunction
946 " Restore:
947 let &cpo= s:keepcpo
948 unlet s:keepcpo
949 " vim: ts=4 fdm=marker