Mercurial > vim
comparison runtime/autoload/sqlcomplete.vim @ 819:23f82b5d2814 v7.0c10
updated for version 7.0c10
author | vimboss |
---|---|
date | Wed, 05 Apr 2006 20:41:53 +0000 |
parents | 1f929f3ca806 |
children | 2c885fab04e3 |
comparison
equal
deleted
inserted
replaced
818:1f929f3ca806 | 819:23f82b5d2814 |
---|---|
1 " Vim completion script | 1 " Vim completion script |
2 " Language: SQL | 2 " Language: SQL |
3 " Maintainer: David Fishburn <fishburn@ianywhere.com> | 3 " Maintainer: David Fishburn <fishburn@ianywhere.com> |
4 " Version: 1.0 | 4 " Version: 2.0 |
5 " Last Change: Tue Mar 28 2006 4:39:49 PM | 5 " Last Change: Mon Apr 03 2006 10:21:36 PM |
6 | 6 |
7 " Set completion with CTRL-X CTRL-O to autoloaded function. | 7 " Set completion with CTRL-X CTRL-O to autoloaded function. |
8 " This check is in place in case this script is | 8 " This check is in place in case this script is |
9 " sourced directly instead of using the autoload feature. | 9 " sourced directly instead of using the autoload feature. |
10 if exists('&omnifunc') | 10 if exists('&omnifunc') |
19 finish | 19 finish |
20 endif | 20 endif |
21 let g:loaded_sql_completion = 1 | 21 let g:loaded_sql_completion = 1 |
22 | 22 |
23 " Maintains filename of dictionary | 23 " Maintains filename of dictionary |
24 let s:sql_file_table = "" | 24 let s:sql_file_table = "" |
25 let s:sql_file_procedure = "" | 25 let s:sql_file_procedure = "" |
26 let s:sql_file_view = "" | 26 let s:sql_file_view = "" |
27 | 27 |
28 " Define various arrays to be used for caching | 28 " Define various arrays to be used for caching |
29 let s:tbl_name = [] | 29 let s:tbl_name = [] |
30 let s:tbl_alias = [] | 30 let s:tbl_alias = [] |
31 let s:tbl_cols = [] | 31 let s:tbl_cols = [] |
32 let s:syn_list = [] | 32 let s:syn_list = [] |
33 let s:syn_value = [] | 33 let s:syn_value = [] |
34 | 34 |
35 " Used in conjunction with the syntaxcomplete plugin | 35 " Used in conjunction with the syntaxcomplete plugin |
36 let s:save_inc = "" | 36 let s:save_inc = "" |
37 let s:save_exc = "" | 37 let s:save_exc = "" |
38 if exists('g:omni_syntax_group_include_sql') | 38 if exists('g:omni_syntax_group_include_sql') |
39 let s:save_inc = g:omni_syntax_group_include_sql | 39 let s:save_inc = g:omni_syntax_group_include_sql |
40 endif | 40 endif |
41 if exists('g:omni_syntax_group_exclude_sql') | 41 if exists('g:omni_syntax_group_exclude_sql') |
42 let s:save_exc = g:omni_syntax_group_exclude_sql | 42 let s:save_exc = g:omni_syntax_group_exclude_sql |
43 endif | 43 endif |
44 | 44 |
45 " Used with the column list | 45 " Used with the column list |
46 let s:save_prev_table = "" | 46 let s:save_prev_table = "" |
47 | 47 |
48 " Default the option to verify table alias | 48 " Default the option to verify table alias |
49 if !exists('g:omni_sql_use_tbl_alias') | 49 if !exists('g:omni_sql_use_tbl_alias') |
50 let g:omni_sql_use_tbl_alias = 'a' | 50 let g:omni_sql_use_tbl_alias = 'a' |
51 endif | |
52 " Default syntax items to precache | |
53 if !exists('g:omni_sql_precache_syntax_groups') | |
54 let g:omni_sql_precache_syntax_groups = [ | |
55 \ 'syntax', | |
56 \ 'sqlKeyword', | |
57 \ 'sqlFunction', | |
58 \ 'sqlOption', | |
59 \ 'sqlType', | |
60 \ 'sqlStatement' | |
61 \ ] | |
51 endif | 62 endif |
52 | 63 |
53 " This function is used for the 'omnifunc' option. | 64 " This function is used for the 'omnifunc' option. |
54 function! sqlcomplete#Complete(findstart, base) | 65 function! sqlcomplete#Complete(findstart, base) |
55 | 66 |
58 " Allow maps to specify what type of object completion they want | 69 " Allow maps to specify what type of object completion they want |
59 if exists('b:sql_compl_type') | 70 if exists('b:sql_compl_type') |
60 let compl_type = b:sql_compl_type | 71 let compl_type = b:sql_compl_type |
61 endif | 72 endif |
62 | 73 |
74 " First pass through this function determines how much of the line should | |
75 " be replaced by whatever is chosen from the completion list | |
63 if a:findstart | 76 if a:findstart |
64 " Locate the start of the item, including "." | 77 " Locate the start of the item, including "." |
65 let line = getline('.') | 78 let line = getline('.') |
66 let start = col('.') - 1 | 79 let start = col('.') - 1 |
67 let lastword = -1 | 80 let lastword = -1 |
68 while start > 0 | 81 while start > 0 |
69 if line[start - 1] =~ '\w' | 82 if line[start - 1] =~ '\w' |
70 let start -= 1 | 83 let start -= 1 |
71 elseif line[start - 1] =~ '\.' && compl_type =~ 'column\|table' | 84 elseif line[start - 1] =~ '\.' && compl_type =~ 'column' |
72 " If the completion type is table or column | 85 " If the completion type is column then assume we are looking |
73 " Then assume we are looking for column completion | 86 " for column completion column_type can be either |
74 " column_type can be either 'column' or 'column_csv' | 87 " 'column' or 'column_csv' |
75 if lastword == -1 | 88 if lastword == -1 && compl_type == 'column' |
89 " Do not replace the table name prefix or alias | |
90 " if completing only a single column name | |
76 let lastword = start | 91 let lastword = start |
77 endif | 92 endif |
78 let start -= 1 | 93 let start -= 1 |
79 let b:sql_compl_type = 'column' | |
80 else | 94 else |
81 break | 95 break |
82 endif | 96 endif |
83 endwhile | 97 endwhile |
84 | 98 |
90 endif | 104 endif |
91 let s:prepended = strpart(line, start, lastword - start) | 105 let s:prepended = strpart(line, start, lastword - start) |
92 return lastword | 106 return lastword |
93 endif | 107 endif |
94 | 108 |
109 " Second pass through this function will determine what data to put inside | |
110 " of the completion list | |
111 " s:prepended is set by the first pass | |
95 let base = s:prepended . a:base | 112 let base = s:prepended . a:base |
96 | 113 |
114 " Default the completion list to an empty list | |
97 let compl_list = [] | 115 let compl_list = [] |
98 | 116 |
99 " Default to table name completion | 117 " Default to table name completion |
100 let compl_type = 'table' | 118 let compl_type = 'table' |
101 " Allow maps to specify what type of object completion they want | 119 " Allow maps to specify what type of object completion they want |
176 let s:tbl_name = [] | 194 let s:tbl_name = [] |
177 let s:tbl_alias = [] | 195 let s:tbl_alias = [] |
178 let s:tbl_cols = [] | 196 let s:tbl_cols = [] |
179 let s:syn_list = [] | 197 let s:syn_list = [] |
180 let s:syn_value = [] | 198 let s:syn_value = [] |
181 return [] | |
182 else | 199 else |
183 " Default to empty or not found | 200 let compl_list = s:SQLCGetSyntaxList(compl_type) |
184 let compl_list = [] | |
185 " Check if we have already cached the syntax list | |
186 let list_idx = index(s:syn_list, compl_type, 0, &ignorecase) | |
187 if list_idx > -1 | |
188 " Return previously cached value | |
189 let compl_list = s:syn_value[list_idx] | |
190 else | |
191 " Request the syntax list items from the | |
192 " syntax completion plugin | |
193 if compl_type == 'syntax' | |
194 " Handle this special case. This allows the user | |
195 " to indicate they want all the syntax items available, | |
196 " so do not specify a specific include list. | |
197 let g:omni_syntax_group_include_sql = '' | |
198 else | |
199 " The user has specified a specific syntax group | |
200 let g:omni_syntax_group_include_sql = compl_type | |
201 endif | |
202 let g:omni_syntax_group_exclude_sql = '' | |
203 let syn_value = OmniSyntaxList() | |
204 let g:omni_syntax_group_include_sql = s:save_inc | |
205 let g:omni_syntax_group_exclude_sql = s:save_exc | |
206 " Cache these values for later use | |
207 let s:syn_list = add( s:syn_list, compl_type ) | |
208 let s:syn_value = add( s:syn_value, syn_value ) | |
209 let compl_list = syn_value | |
210 endif | |
211 endif | 201 endif |
212 | 202 |
213 if base != '' | 203 if base != '' |
214 " Filter the list based on the first few characters the user | 204 " Filter the list based on the first few characters the user |
215 " entered | 205 " entered |
216 let expr = 'v:val =~ "^'.base.'"' | 206 let expr = 'v:val =~ "^'.base.'"' |
217 let compl_list = filter(copy(compl_list), expr) | 207 let compl_list = filter(copy(compl_list), expr) |
218 endif | 208 endif |
219 | 209 |
210 if exists('b:sql_compl_savefunc') && b:sql_compl_savefunc != "" | |
211 let &omnifunc = b:sql_compl_savefunc | |
212 endif | |
213 | |
220 return compl_list | 214 return compl_list |
221 endfunc | 215 endfunc |
222 | 216 |
223 function! s:SQLCWarningMsg(msg) | 217 function! s:SQLCWarningMsg(msg) |
224 echohl WarningMsg | 218 echohl WarningMsg |
230 echohl ErrorMsg | 224 echohl ErrorMsg |
231 echomsg a:msg | 225 echomsg a:msg |
232 echohl None | 226 echohl None |
233 endfunction | 227 endfunction |
234 | 228 |
229 function! sqlcomplete#PreCacheSyntax(...) | |
230 let syn_group_arr = [] | |
231 if a:0 > 0 | |
232 let syn_group_arr = a:1 | |
233 else | |
234 let syn_group_arr = g:omni_sql_precache_syntax_groups | |
235 endif | |
236 if !empty(syn_group_arr) | |
237 for group_name in syn_group_arr | |
238 call s:SQLCGetSyntaxList(group_name) | |
239 endfor | |
240 endif | |
241 endfunction | |
242 | |
243 function! sqlcomplete#Map(type) | |
244 " Tell the SQL plugin what you want to complete | |
245 let b:sql_compl_type=a:type | |
246 " Record previous omnifunc, if the SQL completion | |
247 " is being used in conjunction with other filetype | |
248 " completion plugins | |
249 if &omnifunc != "" && &omnifunc != 'sqlcomplete#Complete' | |
250 " Record the previous omnifunc, the plugin | |
251 " will automatically set this back so that it | |
252 " does not interfere with other ftplugins settings | |
253 let b:sql_compl_savefunc=&omnifunc | |
254 endif | |
255 " Set the OMNI func for the SQL completion plugin | |
256 let &omnifunc='sqlcomplete#Complete' | |
257 endfunction | |
258 | |
259 function! s:SQLCGetSyntaxList(syn_group) | |
260 let syn_group = a:syn_group | |
261 let compl_list = [] | |
262 | |
263 " Check if we have already cached the syntax list | |
264 let list_idx = index(s:syn_list, syn_group, 0, &ignorecase) | |
265 if list_idx > -1 | |
266 " Return previously cached value | |
267 let compl_list = s:syn_value[list_idx] | |
268 else | |
269 " Request the syntax list items from the | |
270 " syntax completion plugin | |
271 if syn_group == 'syntax' | |
272 " Handle this special case. This allows the user | |
273 " to indicate they want all the syntax items available, | |
274 " so do not specify a specific include list. | |
275 let g:omni_syntax_group_include_sql = '' | |
276 else | |
277 " The user has specified a specific syntax group | |
278 let g:omni_syntax_group_include_sql = syn_group | |
279 endif | |
280 let g:omni_syntax_group_exclude_sql = '' | |
281 let syn_value = OmniSyntaxList() | |
282 let g:omni_syntax_group_include_sql = s:save_inc | |
283 let g:omni_syntax_group_exclude_sql = s:save_exc | |
284 " Cache these values for later use | |
285 let s:syn_list = add( s:syn_list, syn_group ) | |
286 let s:syn_value = add( s:syn_value, syn_value ) | |
287 let compl_list = syn_value | |
288 endif | |
289 | |
290 return compl_list | |
291 endfunction | |
292 | |
235 function! s:SQLCCheck4dbext() | 293 function! s:SQLCCheck4dbext() |
236 if !exists('g:loaded_dbext') | 294 if !exists('g:loaded_dbext') |
237 let msg = "The dbext plugin must be loaded for dynamic SQL completion" | 295 let msg = "The dbext plugin must be loaded for dynamic SQL completion" |
238 call s:SQLCErrorMsg(msg) | 296 call s:SQLCErrorMsg(msg) |
239 " Leave time for the user to read the error message | 297 " Leave time for the user to read the error message |