@vim-geek/coc-db 0.0.0-stage → 0.0.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,167 @@
1
+ *vim-dadbod-completion*
2
+
3
+ ================================================================================
4
+ CONTENTS *vim-dadbod-completion-contents*
5
+
6
+ 1. vim-dadbod-completion.............|vim-dadbod-completion-vim-dadbod-completion|
7
+ 1.1. Install...................................|vim-dadbod-completion-install|
8
+ 1.2. Features.................................|vim-dadbod-completion-features|
9
+ 1.3. How it works.........................|vim-dadbod-completion-how_it_works|
10
+ 1.4. Settings.................................|vim-dadbod-completion-settings|
11
+ 1.5. Todo.........................................|vim-dadbod-completion-todo|
12
+
13
+ ================================================================================
14
+ VIM-DADBOD-COMPLETION *vim-dadbod-completion-vim-dadbod-completion*
15
+
16
+ Database auto completion powered by vim-dadbod (https://github.com/tpope/vim-dadbod).
17
+ Supports:
18
+ * coc.nvim (https://github.com/neoclide/coc.nvim)
19
+ * deoplete.nvim (https://github.com/Shougo/deoplete.nvim)
20
+ * completion-nvim (https://github.com/haorenW1025/completion-nvim)
21
+ * nvim-compe (https://github.com/hrsh7th/nvim-compe)
22
+ * ddc.vim (https://github.com/Shougo/ddc.vim)
23
+ * Built in `omnifunc`
24
+
25
+
26
+
27
+ --------------------------------------------------------------------------------
28
+ INSTALL *vim-dadbod-completion-install*
29
+
30
+ Dependencies:
31
+ * vim-dadbod (https://github.com/tpope/vim-dadbod)
32
+
33
+ For coc.nvim (https://github.com/neoclide/coc.nvim)
34
+ >
35
+ :CocInstall coc-db
36
+ <
37
+
38
+ For `deoplete`, `completion-nvim`, `nvim-compe`, `ddc` and `omnifunc`, install it with your favorite plugin manager.
39
+
40
+ >
41
+ Plug 'tpope/vim-dadbod'
42
+ Plug 'kristijanhusak/vim-dadbod-ui' "Optional
43
+ Plug 'kristijanhusak/vim-dadbod-completion'
44
+
45
+ " For built in omnifunc
46
+ autocmd FileType sql setlocal omnifunc=vim_dadbod_completion#omni
47
+
48
+ " hrsh7th/nvim-compe
49
+ let g:compe.source.vim_dadbod_completion = v:true
50
+
51
+ " hrsh7th/nvim-cmp
52
+ autocmd FileType sql,mysql,plsql lua require('cmp').setup.buffer({ sources = {{ name = 'vim-dadbod-completion' }} })
53
+
54
+ " Shougo/ddc.vim
55
+ call ddc#custom#patch_filetype(['sql', 'mysql', 'plsql'], 'sources', 'dadbod-completion')
56
+ call ddc#custom#patch_filetype(['sql', 'mysql', 'plsql'], 'sourceOptions', {
57
+ \ 'dadbod-completion': {
58
+ \ 'mark': 'DB',
59
+ \ 'isVolatile': v:true,
60
+ \ },
61
+ \ })
62
+ <
63
+
64
+ Configuration using lazy.nvim (https://github.com/folke/lazy.nvim) with vim-dadbod-ui (https://github.com/kristijanhusak/vim-dadbod-ui)
65
+ >
66
+ return {
67
+ 'kristijanhusak/vim-dadbod-ui',
68
+ dependencies = {
69
+ { 'tpope/vim-dadbod', lazy = true },
70
+ { 'kristijanhusak/vim-dadbod-completion', ft = { 'sql', 'mysql', 'plsql' }, lazy = true },
71
+ },
72
+ cmd = {
73
+ 'DBUI',
74
+ 'DBUIToggle',
75
+ 'DBUIAddConnection',
76
+ 'DBUIFindBuffer',
77
+ },
78
+ init = function()
79
+ -- Your DBUI configuration
80
+ vim.g.db_ui_use_nerd_fonts = 1
81
+ end,
82
+ }
83
+ <
84
+
85
+ --------------------------------------------------------------------------------
86
+ FEATURES *vim-dadbod-completion-features*
87
+
88
+ * Autocomplete table names, with automatic quoting where needed. Works for all schemes that vim-dadbod (https://github.com/tpope/vim-dadbod) supports.
89
+ * Autocomplete table columns, context aware. Also knows to read aliases (). Currently works for `PostgreSQL`, `MySQL`, `Oracle`, `SQLite` (requires version `3.37.0 (2021-11-27)`) and `SQLserver/MSSQL`.
90
+ * Out of the box integration with vim-dadbod-ui (https://github.com/kristijanhusak/vim-dadbod-ui)
91
+
92
+ --------------------------------------------------------------------------------
93
+ HOW IT WORKS *vim-dadbod-completion-how_it_works*
94
+
95
+ * If an sql buffer is created by vim-dadbod-ui (https://github.com/kristijanhusak/vim-dadbod-ui), it reads all the configuration from there. It should work out of the box.
96
+ * If `vim-dadbod-ui` is not used, vim-dadbod (https://github.com/tpope/vim-dadbod) environment variable `$DATABASE_URL` or `w:db`, `t:db`, `b:db`, `g:db` used, whichever is found first. If you want, you can also add `b:db_table` to limit autocompletions to that table only.
97
+
98
+ --------------------------------------------------------------------------------
99
+ SETTINGS *vim-dadbod-completion-settings*
100
+
101
+ :
102
+ *g:vim_dadbod_completion_mark*
103
+ g:vim_dadbod_completion_mark
104
+ Mark for completion items. To change it, add this to vimrc
105
+ >
106
+ let g:vim_dadbod_completion_mark = 'MYMARK'
107
+ <
108
+
109
+ Default value: `[DB]`
110
+
111
+ *g:vim_dadbod_completion_source_limits*
112
+ g:vim_dadbod_completion_source_limits
113
+ Completion items are fetched from multiple sources.
114
+ Each source is by default limited to maximum of `200` entries.
115
+ Example configuration:
116
+ >
117
+ let g:vim_dadbod_completion_source_limits = {
118
+ \ 'schemas': 150,
119
+ \ 'tables': 100,
120
+ \ 'columns': 120,
121
+ \ 'reserved_words': 140,
122
+ \ 'functions': 50
123
+ \ }
124
+ <
125
+ NOTE: `functions` source is currently available only for `PostgreSQL`
126
+ and it returns list of sql functions available in the
127
+ database.
128
+ Source type that is not in the configuration variable defaults
129
+ to `200`.
130
+
131
+ Default value: `{}`
132
+
133
+ *g:vim_dadbod_completion_lowercase_keywords*
134
+ g:vim_dadbod_completion_lowercase_keywords
135
+ Use lower case for keywords completions. Add this to vimrc
136
+ >
137
+ let g:vim_dadbod_completion_lowercase_keywords = 1
138
+ <
139
+
140
+ Default value: `0`
141
+
142
+ *g:vim_dadbod_completion_disable_notifications*
143
+ g:vim_dadbod_completion_disable_notifications
144
+ Disable notifications.
145
+ >
146
+ let g:vim_dadbod_completion_disable_notifications = 1
147
+ <
148
+
149
+ NOTE: vim-dadbod-ui configuration option `let g:db_ui_disable_info_notifications = 1`
150
+ also disables the notification in this plugin.
151
+
152
+ Default value: `0`
153
+
154
+ --------------------------------------------------------------------------------
155
+ COMMANDS *vim-dadbod-completion-commands*
156
+
157
+ This plugin caches the database tables and columns to leverage maximum performance. If you want to clear the cache at any point just run:
158
+ >
159
+ :DBCompletionClearCache
160
+ <
161
+
162
+ --------------------------------------------------------------------------------
163
+ TODO *vim-dadbod-completion-todo*
164
+
165
+ * [ ] Integration for column autocompletion with more database types
166
+
167
+ vim:tw=78:ts=8:ft=help:norl:noet:fen:noet:
@@ -0,0 +1,88 @@
1
+ ---@type blink.cmp.Source
2
+ local M = {}
3
+
4
+ local map_kind_to_cmp_lsp_kind = {
5
+ F = 3, -- Function -> Function
6
+ C = 5, -- Column -> Field
7
+ A = 6, -- Alias -> Variable
8
+ T = 7, -- Table -> Class
9
+ R = 14, -- Reserved -> Keyword
10
+ S = 19, -- Schema -> Folder
11
+ }
12
+
13
+ function M.new()
14
+ return setmetatable({}, { __index = M })
15
+ end
16
+
17
+ function M:get_trigger_characters()
18
+ return { '"', '`', '[', ']', '.' }
19
+ end
20
+
21
+ function M:enabled()
22
+ local filetypes = { "sql", "mysql", "plsql" }
23
+ return vim.tbl_contains(filetypes, vim.bo.filetype)
24
+ end
25
+
26
+ function M:get_completions(ctx, callback)
27
+ local cursor_col = ctx.cursor[2]
28
+ local line = ctx.line
29
+ local word_start = cursor_col + 1
30
+
31
+ local triggers = self:get_trigger_characters()
32
+ while word_start > 1 do
33
+ local char = line:sub(word_start - 1, word_start - 1)
34
+ if vim.tbl_contains(triggers, char) or char:match('%s') then
35
+ break
36
+ end
37
+ word_start = word_start - 1
38
+ end
39
+
40
+ -- Get text from word start to cursor
41
+ local input = line:sub(word_start, cursor_col)
42
+
43
+ if input ~= '' and input:match('[^0-9A-Za-z_]+') then
44
+ input = ''
45
+ end
46
+
47
+ local transformed_callback = function(items)
48
+ callback({
49
+ context = ctx,
50
+ is_incomplete_forward = true,
51
+ is_incomplete_backward = true,
52
+ items = items,
53
+ })
54
+ end
55
+
56
+ local results = vim.api.nvim_call_function('vim_dadbod_completion#omni', { 0, input })
57
+
58
+ if not results then
59
+ transformed_callback({})
60
+ return function() end
61
+ end
62
+
63
+ local by_word = {}
64
+ for _, item in ipairs(results) do
65
+ local key = item.word..item.kind
66
+ if by_word[key] == nil then
67
+ by_word[key] = item
68
+ end
69
+ end
70
+ local items = {} ---@type table<string,lsp.CompletionItem>
71
+
72
+ for _, item in pairs(by_word) do
73
+ table.insert(items, {
74
+ label = item.abbr or item.word,
75
+ dup = 0,
76
+ insertText = item.word,
77
+ labelDetails = item.menu and { description = item.menu } or nil,
78
+ documentation = item.info or '',
79
+ kind = map_kind_to_cmp_lsp_kind[item.kind] or vim.lsp.protocol.CompletionItemKind.Text
80
+ })
81
+ end
82
+
83
+ transformed_callback(items)
84
+
85
+ return function() end
86
+ end
87
+
88
+ return M
@@ -0,0 +1,71 @@
1
+ local M = {}
2
+
3
+ function M.getCompletionItems(prefix, score_func)
4
+ local items = vim.api.nvim_call_function('vim_dadbod_completion#omni',{0, prefix})
5
+ for _, item in pairs(items) do
6
+ item.user_data = vim.json.encode({ hover = item.info })
7
+ item.dup = 0
8
+ end
9
+
10
+ return items
11
+ end
12
+
13
+ M.complete_item = {
14
+ item = M.getCompletionItems
15
+ }
16
+
17
+ local nvim_cmp_source = {}
18
+
19
+ ---Source constructor.
20
+ nvim_cmp_source.new = function()
21
+ local self = setmetatable({}, { __index = nvim_cmp_source })
22
+ return self
23
+ end
24
+
25
+ nvim_cmp_source.get_debug_name = function()
26
+ return 'vim-dadbod-completion'
27
+ end
28
+
29
+ function nvim_cmp_source:is_available()
30
+ return true
31
+ end
32
+
33
+ function nvim_cmp_source:get_trigger_characters(_)
34
+ return { '"', '`', '[', ']', '.' }
35
+ end
36
+
37
+ local map_kind_to_cmp_lsp_kind = {
38
+ F = 3, -- Function -> Function
39
+ C = 5, -- Column -> Field
40
+ A = 6, -- Alias -> Variable
41
+ T = 7, -- Table -> Class
42
+ R = 14, -- Reserved -> Keyword
43
+ S = 19, -- Schema -> Folder
44
+ }
45
+
46
+ function nvim_cmp_source:complete(params, callback)
47
+ local input = string.sub(params.context.cursor_before_line, params.offset)
48
+ local results = vim.fn['vim_dadbod_completion#omni'](0, input)
49
+ local items = {}
50
+ for _, item in ipairs(results) do
51
+ table.insert(items, {
52
+ label = item.abbr,
53
+ dup = 0,
54
+ insertText = item.word,
55
+ labelDetails = {
56
+ description = item.menu,
57
+ },
58
+ documentation = item.info,
59
+ kind = map_kind_to_cmp_lsp_kind[item.kind],
60
+ })
61
+ end
62
+
63
+ callback({
64
+ items = items,
65
+ isIncomplete = true
66
+ })
67
+ end
68
+
69
+ M.nvim_cmp_source = nvim_cmp_source.new()
70
+
71
+ return M
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@vim-geek/coc-db",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.45",
4
+ "description": "db source for coc.nvim",
5
+ "scripts": {},
6
+ "main": "src/index.js",
7
+ "engines": {
8
+ "coc": ">=0.0.57"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/kristijanhusak/vim-dadbod-completion.git"
13
+ },
14
+ "keywords": [
15
+ "coc.nvim",
16
+ "db"
17
+ ],
18
+ "activationEvents": [
19
+ "*"
20
+ ],
21
+ "contributes": {
22
+ "configuration": {
23
+ "type": "object",
24
+ "properties": {
25
+ "coc.source.db.enable": {
26
+ "type": "boolean",
27
+ "default": true
28
+ },
29
+ "coc.source.db.shortcut": {
30
+ "type": "string",
31
+ "default": "DB"
32
+ },
33
+ "coc.source.db.triggerCharacters": {
34
+ "type": "array",
35
+ "default": [
36
+ "\"",
37
+ ".",
38
+ "`",
39
+ "["
40
+ ]
41
+ },
42
+ "coc.source.db.priority": {
43
+ "type": "integer",
44
+ "default": 95
45
+ },
46
+ "coc.source.db.filetypes": {
47
+ "type": [
48
+ "array"
49
+ ],
50
+ "default": [
51
+ "sql",
52
+ "mysql",
53
+ "plsql"
54
+ ],
55
+ "items": {
56
+ "type": "string"
57
+ }
58
+ }
59
+ }
60
+ }
61
+ },
62
+ "author": "husakkristijan@gmail.com",
63
+ "license": "MIT",
64
+ "bugs": {
65
+ "url": "https://github.com/kristijanhusak/vim-dadbod-completion/issues"
66
+ },
67
+ "homepage": "https://github.com/kristijanhusak/vim-dadbod-completion#readme",
68
+ "devDependencies": {
69
+ "coc.nvim": ">=0.0.57"
70
+ }
71
+ }
@@ -0,0 +1,12 @@
1
+ if exists('g:vim_dadbod_completion_loaded')
2
+ finish
3
+ endif
4
+
5
+ let g:vim_dadbod_completion_loaded = 1
6
+
7
+ augroup vim_dadbod_completion
8
+ autocmd!
9
+ autocmd FileType sql,mysql,plsql call vim_dadbod_completion#fetch(str2nr(expand('<abuf>')))
10
+ augroup END
11
+
12
+ command DBCompletionClearCache call vim_dadbod_completion#clear_cache()
@@ -0,0 +1,23 @@
1
+ from .base import Base
2
+
3
+
4
+ class Source(Base):
5
+ def __init__(self, vim):
6
+ Base.__init__(self, vim)
7
+ self.name = 'vim-dadbod-completion'
8
+ self.mark = ''
9
+ self.filetypes = ['sql', 'mysql', 'plsql']
10
+ self.input_pattern = '\w+|\.|"|\[|`'
11
+ self.rank = 500
12
+ self.max_pattern_length = -1
13
+ self.matchers = ['matcher_full_fuzzy']
14
+
15
+ def get_complete_position(self, context):
16
+ return self.vim.call('vim_dadbod_completion#omni', 1, '')
17
+
18
+ def gather_candidates(self, context):
19
+ items = self.vim.call('vim_dadbod_completion#omni', 0,
20
+ context['complete_str'])
21
+ context['is_async'] = self.vim.call('vim_dadbod_completion#refresh_deoplete')
22
+
23
+ return items
package/src/index.js ADDED
@@ -0,0 +1,44 @@
1
+ const { sources, workspace } = require('coc.nvim');
2
+ const { nvim } = workspace;
3
+ const path = require('path');
4
+ const rtpPath = path.resolve(__dirname, '../');
5
+ const validFiletypes = ['sql', 'mysql', 'plsql']
6
+
7
+
8
+ exports.activate = async (context) => {
9
+ await nvim.command(`source ${rtpPath}/autoload/vim_dadbod_completion/reserved_keywords.vim`);
10
+ await nvim.command(`source ${rtpPath}/autoload/vim_dadbod_completion/schemas.vim`);
11
+ await nvim.command(`source ${rtpPath}/plugin/vim_dadbod_completion.vim`);
12
+ await nvim.command(`source ${rtpPath}/autoload/vim_dadbod_completion.vim`);
13
+ await nvim.command(`source ${rtpPath}/autoload/vim_dadbod_completion/alias_parser.vim`);
14
+ await nvim.command(`source ${rtpPath}/autoload/vim_dadbod_completion/job.vim`);
15
+ await nvim.command(`source ${rtpPath}/autoload/vim_dadbod_completion/utils.vim`);
16
+
17
+ const currentDoc = await workspace.document;
18
+ const dbui_key_name = await nvim.call('getbufvar', [currentDoc.bufnr, 'dbui_db_key_name']);
19
+ if (validFiletypes.indexOf(currentDoc.filetype) > -1 || dbui_key_name) {
20
+ nvim.call('vim_dadbod_completion#fetch', [currentDoc.bufnr]);
21
+ }
22
+
23
+ workspace.onDidOpenTextDocument(async e => {
24
+ const doc = workspace.getDocument(e.uri);
25
+ const dbui_key_name = await nvim.call('getbufvar', [doc.bufnr, 'dbui_db_key_name']);
26
+ if (validFiletypes.indexOf(doc.filetype) > -1 || dbui_key_name) {
27
+ return nvim.call('vim_dadbod_completion#fetch', [doc.bufnr]);
28
+ }
29
+ });
30
+
31
+ const source = {
32
+ name: 'db',
33
+ doComplete: async function (opt) {
34
+ const { input, triggerCharacter } = opt;
35
+ const isTriggerCharacter = this.getConfig('triggerCharacters').includes(triggerCharacter);
36
+ let base = isTriggerCharacter ? '' : input;
37
+ const items = await nvim.call('vim_dadbod_completion#omni', [0, base]);
38
+
39
+ return { items };
40
+ },
41
+ };
42
+
43
+ context.subscriptions.push(sources.createSource(source));
44
+ };