-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 64964b2
Showing
7 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2021 Chris Griffing | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# nvim-biscuits | ||
|
||
Every dev needs something sweet sometimes. Code Biscuits are in-editor annotations usually at the end of a closing tag/bracket/parenthisis/etc. They help you get the context of the end of that AST node so you don't have to navigate to find it. | ||
|
||
## Installation | ||
|
||
In your nvim config, add the Plug dependencies: | ||
|
||
```lua | ||
call plug#begin() | ||
Plug 'nvim-lua/plenary.nvim' | ||
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'} | ||
Plug 'code-biscuits/nvim-biscuits' | ||
call plug#end() | ||
``` | ||
|
||
You will also need to configure which language parsers you want to have enabled for tree-sitter. "maintained" currently will install 40 languages. "all" will install even more. | ||
|
||
```lua | ||
lua <<EOF | ||
require'nvim-treesitter.configs'.setup { | ||
ensure_installed = "maintained", | ||
... | ||
} | ||
EOF | ||
``` | ||
|
||
## Configuration | ||
|
||
Basic configuration is simple: | ||
|
||
```lua | ||
lua require('nvim-biscuits').setup({}) | ||
``` | ||
|
||
You can also configure your own global defaults as well as language specific defaults. | ||
|
||
This is just an example config. | ||
|
||
```lua | ||
lua <<EOF | ||
require('nvim-biscuits').setup({ | ||
default_config = { | ||
max_length = 12, | ||
min_destance = 5, | ||
prefix_string = " 📎 " | ||
}, | ||
language_config = { | ||
html = { | ||
prefix_string = " 🌐 " | ||
}, | ||
javascript = { | ||
prefix_string = " ✨ ", | ||
max_length = 80 | ||
} | ||
} | ||
}) | ||
EOF | ||
``` | ||
|
||
## Supported Languages | ||
|
||
We currently support all the languages supported in tree-sitter. Not all languages have specialized support, though most will probably need some. | ||
|
||
As we make tailored handlers for specific languages we will create a table here to track that. | ||
|
||
## License | ||
|
||
Copyright 2021 Chris Griffing | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
local config = {} | ||
|
||
config.default_config = function () | ||
return { | ||
min_distance = 5, | ||
max_length = 80, | ||
prefix_string = " // " | ||
} | ||
end | ||
|
||
|
||
local function get_default_config(final_config, config_key) | ||
if final_config == nil then | ||
return config.default_config()[config_key] | ||
end | ||
|
||
return final_config[config_key] | ||
end | ||
|
||
config.get_language_config = function(final_config, language, config_key) | ||
|
||
if final_config == nil then | ||
return get_default_config(final_config, config_key) | ||
end | ||
|
||
if final_config.language_config == nil then | ||
return get_default_config(final_config, config_key) | ||
end | ||
|
||
if final_config.language_config[language] == nil then | ||
return get_default_config(final_config, config_key) | ||
end | ||
|
||
if final_config.language_config[language][config_key] == nil then | ||
return get_default_config(final_config, config_key) | ||
end | ||
|
||
return final_config.language_config[language][config_key] | ||
|
||
end | ||
|
||
|
||
|
||
return config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
require('nvim-treesitter') | ||
local utils = require("nvim-biscuits.utils") | ||
local config = require("nvim-biscuits.config") | ||
|
||
local final_config = config.default_config() | ||
|
||
local has_ts, _ = pcall(require, 'nvim-treesitter') | ||
if not has_ts then | ||
error("nvim-treesitter must be installed") | ||
end | ||
|
||
local ts_parsers = require('nvim-treesitter.parsers') | ||
local ts_utils = require('nvim-treesitter.ts_utils') | ||
local nvim_biscuits = {} | ||
|
||
local decorateNodes = function (bufnr, lang) | ||
|
||
local parser = ts_parsers.get_parser(bufnr, lang) | ||
|
||
if parser == nil then | ||
utils.console_log('no parser for for '..lang) | ||
return | ||
end | ||
|
||
local root = parser:parse()[1]:root() | ||
|
||
local nodes = ts_utils.get_named_children(root) | ||
local children = {} | ||
local has_nodes = true | ||
|
||
while has_nodes do | ||
for index, node in ipairs(nodes) do | ||
children = utils.merge_arrays(children, ts_utils.get_named_children(node)) | ||
|
||
local start_line, start_col, end_line, end_col = ts_utils.get_node_range(node) | ||
local text = ts_utils.get_node_text(node)[1] | ||
|
||
text = utils.trim(text) | ||
|
||
local should_decorate = true | ||
|
||
if text == '' then | ||
should_decorate = false | ||
end | ||
|
||
if string.len(text) <= 1 then | ||
should_decorate = false | ||
end | ||
|
||
if start_line == end_line then | ||
should_decorate = false | ||
end | ||
|
||
if end_line - start_line < final_config.min_distance then | ||
should_decorate = false | ||
end | ||
|
||
if should_decorate then | ||
|
||
local max_length = config.get_language_config(final_config, lang, "max_length") | ||
|
||
if string.len(text) >= max_length then | ||
text = string.sub(text, 1, max_length) | ||
end | ||
|
||
text = text:gsub('"', '\\"') | ||
|
||
local prefix_string = config.get_language_config(final_config, lang, "prefix_string") | ||
|
||
text = prefix_string..text | ||
|
||
local nvim_clear_script = "nvim_buf_clear_namespace("..bufnr..", 0, "..end_line..", "..(end_line + 1)..")" | ||
vim.api.nvim_eval(nvim_clear_script) | ||
|
||
local nvim_script = "nvim_buf_set_virtual_text("..bufnr..", 0, "..end_line..", [[\""..text.."\"]], [])" | ||
vim.api.nvim_eval(nvim_script) | ||
else | ||
utils.console_log('empty') | ||
end | ||
|
||
end | ||
|
||
nodes = children | ||
children = {} | ||
|
||
if table.getn(nodes) == 0 then | ||
has_nodes = false | ||
end | ||
end | ||
end | ||
|
||
nvim_biscuits.setup = function (user_config) | ||
final_config = utils.merge_tables(final_config, user_config) | ||
|
||
if user_config.default_config then | ||
final_config = utils.merge_tables(final_config, user_config.default_config) | ||
end | ||
|
||
utils.clear_log() | ||
end | ||
|
||
nvim_biscuits.BufferAttach = function() | ||
local bufnr = vim.fn.bufnr() | ||
local lang = ts_parsers.get_buf_lang(bufnr) | ||
decorateNodes(bufnr, lang) | ||
|
||
-- edit event | ||
vim.api.nvim_buf_attach(bufnr, false, { | ||
on_lines=function(lines_string, edited_bufnr) | ||
decorateNodes(edited_bufnr, lang) | ||
end | ||
}) | ||
end | ||
|
||
return nvim_biscuits |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
local Path = require("plenary.path") | ||
|
||
local utils = {} | ||
|
||
local debug = false | ||
local debug_path = '~/vim-biscuits.log' | ||
|
||
|
||
utils.console_log = function (the_string) | ||
if debug then | ||
Path:new(debug_path):write(the_string..'\n', 'a') | ||
end | ||
end | ||
|
||
utils.merge_arrays = function(a, b) | ||
local result = {unpack(a)} | ||
table.move(b, 1, #b, #result + 1, result) | ||
return result | ||
end | ||
|
||
utils.merge_tables = function(t1, t2) | ||
for k,v in pairs(t2) do | ||
if type(v) == "table" then | ||
if type(t1[k] or false) == "table" then | ||
tableMerge(t1[k] or {}, t2[k] or {}) | ||
else | ||
t1[k] = v | ||
end | ||
else | ||
t1[k] = v | ||
end | ||
end | ||
return t1 | ||
end | ||
|
||
utils.trim = function(s) | ||
return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)' | ||
end | ||
|
||
utils.clear_log = function () | ||
if debug == true then | ||
Path:new(debug_path):write('', 'w') | ||
end | ||
end | ||
|
||
return utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
:lua require('nvim-biscuits') | ||
|
||
augroup NVIM_BISCUITS | ||
autocmd! | ||
autocmd BufEnter * :lua require('nvim-biscuits').BufferAttach() | ||
augroup END |