├── .gitignore ├── .stylua.toml ├── README.md ├── denops └── @ddc-sources │ └── treesitter.ts ├── doc └── ddc-treesitter.txt └── lua └── ddc-treesitter.lua /.gitignore: -------------------------------------------------------------------------------- 1 | ### https://raw.github.com/github/gitignore/b0012e4930d0a8c350254a3caeedf7441ea286a3/Global/Vim.gitignore 2 | 3 | # Swap 4 | [._]*.s[a-v][a-z] 5 | !*.svg # comment out if you don't need vector files 6 | [._]*.sw[a-p] 7 | [._]s[a-rt-v][a-z] 8 | [._]ss[a-gi-z] 9 | [._]sw[a-p] 10 | 11 | # Session 12 | Session.vim 13 | Sessionx.vim 14 | 15 | # Temporary 16 | .netrwhist 17 | *~ 18 | # Auto-generated tag files 19 | tags 20 | # Persistent undo 21 | [._]*.un~ 22 | 23 | 24 | ### https://raw.github.com/github/gitignore/b0012e4930d0a8c350254a3caeedf7441ea286a3/Global/macOS.gitignore 25 | 26 | # General 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Icon must end with two \r 32 | Icon 33 | 34 | # Thumbnails 35 | ._* 36 | 37 | # Files that might appear in the root of a volume 38 | .DocumentRevisions-V100 39 | .fseventsd 40 | .Spotlight-V100 41 | .TemporaryItems 42 | .Trashes 43 | .VolumeIcon.icns 44 | .com.apple.timemachine.donotpresent 45 | 46 | # Directories potentially created on remote AFP share 47 | .AppleDB 48 | .AppleDesktop 49 | Network Trash Folder 50 | Temporary Items 51 | .apdisk 52 | 53 | 54 | ### https://raw.github.com/github/gitignore/b0012e4930d0a8c350254a3caeedf7441ea286a3/Node.gitignore 55 | 56 | # Logs 57 | logs 58 | *.log 59 | npm-debug.log* 60 | yarn-debug.log* 61 | yarn-error.log* 62 | lerna-debug.log* 63 | .pnpm-debug.log* 64 | 65 | # Diagnostic reports (https://nodejs.org/api/report.html) 66 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 67 | 68 | # Runtime data 69 | pids 70 | *.pid 71 | *.seed 72 | *.pid.lock 73 | 74 | # Directory for instrumented libs generated by jscoverage/JSCover 75 | lib-cov 76 | 77 | # Coverage directory used by tools like istanbul 78 | coverage 79 | *.lcov 80 | 81 | # nyc test coverage 82 | .nyc_output 83 | 84 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 85 | .grunt 86 | 87 | # Bower dependency directory (https://bower.io/) 88 | bower_components 89 | 90 | # node-waf configuration 91 | .lock-wscript 92 | 93 | # Compiled binary addons (https://nodejs.org/api/addons.html) 94 | build/Release 95 | 96 | # Dependency directories 97 | node_modules/ 98 | jspm_packages/ 99 | 100 | # Snowpack dependency directory (https://snowpack.dev/) 101 | web_modules/ 102 | 103 | # TypeScript cache 104 | *.tsbuildinfo 105 | 106 | # Optional npm cache directory 107 | .npm 108 | 109 | # Optional eslint cache 110 | .eslintcache 111 | 112 | # Microbundle cache 113 | .rpt2_cache/ 114 | .rts2_cache_cjs/ 115 | .rts2_cache_es/ 116 | .rts2_cache_umd/ 117 | 118 | # Optional REPL history 119 | .node_repl_history 120 | 121 | # Output of 'npm pack' 122 | *.tgz 123 | 124 | # Yarn Integrity file 125 | .yarn-integrity 126 | 127 | # dotenv environment variables file 128 | .env 129 | .env.test 130 | .env.production 131 | 132 | # parcel-bundler cache (https://parceljs.org/) 133 | .cache 134 | .parcel-cache 135 | 136 | # Next.js build output 137 | .next 138 | out 139 | 140 | # Nuxt.js build / generate output 141 | .nuxt 142 | dist 143 | 144 | # Gatsby files 145 | .cache/ 146 | # Comment in the public line in if your project uses Gatsby and not Next.js 147 | # https://nextjs.org/blog/next-9-1#public-directory-support 148 | # public 149 | 150 | # vuepress build output 151 | .vuepress/dist 152 | 153 | # Serverless directories 154 | .serverless/ 155 | 156 | # FuseBox cache 157 | .fusebox/ 158 | 159 | # DynamoDB Local files 160 | .dynamodb/ 161 | 162 | # TernJS port file 163 | .tern-port 164 | 165 | # Stores VSCode versions used for testing VSCode extensions 166 | .vscode-test 167 | 168 | # yarn v2 169 | .yarn/cache 170 | .yarn/unplugged 171 | .yarn/build-state.yml 172 | .yarn/install-state.gz 173 | .pnp.* 174 | 175 | 176 | -------------------------------------------------------------------------------- /.stylua.toml: -------------------------------------------------------------------------------- 1 | column_width = 120 2 | line_endings = "Unix" 3 | indent_type = "Spaces" 4 | indent_width = 2 5 | quote_style = "AutoPreferDouble" 6 | no_call_parentheses = true 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ddc-treesitter 2 | 3 | Treesitter completion for ddc.vim 4 | 5 | This source collects candidates from [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter). 6 | 7 | NOTE: This plugin needs Neovim >=v0.5.0 8 | 9 | ## Required 10 | 11 | ### denops.vim 12 | 13 | https://github.com/vim-denops/denops.vim 14 | 15 | ### ddc.vim 16 | 17 | https://github.com/Shougo/ddc.vim 18 | 19 | ### nvim-treesitter 20 | 21 | https://github.com/nvim-treesitter/nvim-treesitter 22 | 23 | ## Configuration 24 | 25 | ```vim 26 | " Use treesitter source. 27 | call ddc#custom#patch_global('sources', ['treesitter']) 28 | 29 | " Change source options 30 | call ddc#custom#patch_global('sourceOptions', { 31 | \ 'treesitter': {'mark': 'T'}, 32 | \ }) 33 | ``` 34 | -------------------------------------------------------------------------------- /denops/@ddc-sources/treesitter.ts: -------------------------------------------------------------------------------- 1 | import { Denops, fn } from "https://lib.deno.dev/x/ddc_vim@v4.3.1/deps.ts"; 2 | import { 3 | BaseSource, 4 | Item, 5 | } from "https://lib.deno.dev/x/ddc_vim@v4.3.1/types.ts"; 6 | import { 7 | GatherArguments, 8 | OnInitArguments, 9 | } from "https://lib.deno.dev/x/ddc_vim@v4.3.1/base/source.ts"; 10 | 11 | interface NodeInfo { 12 | word: string; 13 | kind: string; 14 | parent?: string; 15 | grandparent?: string; 16 | } 17 | 18 | type Params = Record; 19 | 20 | export class Source extends BaseSource { 21 | private available = false; 22 | 23 | async onInit({ denops }: OnInitArguments): Promise { 24 | if (!(await fn.has(denops, "nvim-0.5"))) { 25 | await this.print_error(denops, "This source needs Neovim >=0.5.0"); 26 | return; 27 | } 28 | this.available = true; 29 | } 30 | 31 | async gather({ 32 | denops, 33 | }: GatherArguments): Promise { 34 | if (!this.available) { 35 | return []; 36 | } 37 | const nodeInfos = (await denops.call( 38 | "luaeval", 39 | "require'ddc-treesitter'.gather_items()", 40 | )) as NodeInfo[]; 41 | return nodeInfos.map(({ word, kind, parent, grandparent }) => { 42 | const item: Item = { word, kind }; 43 | if (grandparent) { 44 | item.menu = `→ ${grandparent}`; 45 | } else if (parent) { 46 | item.menu = `→ ${parent}`; 47 | } 48 | return item; 49 | }); 50 | } 51 | 52 | params(): Params { 53 | return {}; 54 | } 55 | 56 | private async print_error(denops: Denops, message: string): Promise { 57 | await denops.call("ddc#util#print_error", message, "ddc-treesitter"); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /doc/ddc-treesitter.txt: -------------------------------------------------------------------------------- 1 | *ddc-treesitter.txt* Treesitter completion for ddc.vim 2 | 3 | Author: delphinus 4 | License: MIT license 5 | 6 | CONTENTS *ddc-treesitter-contents* 7 | 8 | Introduction |ddc-treesitter-introduction| 9 | Install |ddc-treesitter-install| 10 | Examples |ddc-treesitter-examples| 11 | 12 | 13 | ============================================================================== 14 | INTRODUCTION *ddc-treesitter-introduction* 15 | 16 | This source collects candidates from nodes parsed by nvim-treesitter. 17 | 18 | 19 | ============================================================================== 20 | INSTALL *ddc-treesitter-install* 21 | 22 | NOTE: This plugin needs Neovim >=v0.5.0 23 | 24 | Please install both "ddc.vim" and "denops.vim". 25 | 26 | https://github.com/Shougo/ddc.vim 27 | https://github.com/vim-denops/denops.vim 28 | 29 | And install nvim-treesitter. 30 | 31 | https://github.com/nvim-treesitter/nvim-treesitter 32 | 33 | 34 | ============================================================================== 35 | EXAMPLES *ddc-treesitter-examples* 36 | > 37 | " Use treesitter source. 38 | call ddc#custom#patch_global('sources', ['treesitter']) 39 | 40 | " Change source options 41 | call ddc#custom#patch_global('sourceOptions', { 42 | \ 'treesitter': {'mark': 'T'}, 43 | \ }) 44 | < 45 | 46 | 47 | ============================================================================== 48 | vim:tw=78:ts=8:ft=help:norl:noet:fen:noet: 49 | -------------------------------------------------------------------------------- /lua/ddc-treesitter.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | M.gather_items = function() 4 | local candidates = {} 5 | local ok, parser = pcall(vim.treesitter.get_parser) 6 | if not ok then 7 | return candidates 8 | end 9 | local query = vim.treesitter.query.get(parser:lang(), "highlights") 10 | if not query then 11 | return candidates 12 | end 13 | for _, tree in pairs(parser:parse()) do 14 | for i, node in query:iter_captures(tree:root(), 0) do 15 | local parent = node:parent() 16 | local grandparent = parent and parent:parent() or nil 17 | local word = vim.treesitter.get_node_text(node, 0) 18 | if word then 19 | table.insert(candidates, { 20 | word = word, 21 | kind = query.captures[i], 22 | parent = parent and vim.treesitter.get_node_text(parent, 0) or nil, 23 | grandparent = grandparent and vim.treesitter.get_node_text(grandparent, 0) or nil, 24 | }) 25 | end 26 | end 27 | end 28 | return candidates 29 | end 30 | 31 | return M 32 | --------------------------------------------------------------------------------