Skip to content

Commit

Permalink
fix: add better python specific handling and use env var for debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgriffing committed May 22, 2021
1 parent 40384fb commit a937b97
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 36 deletions.
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,7 @@ As we make tailored handlers for specific languages we will create a table here

While doing local dev, it can be nice to use the `utils.console_log` command to write runtime logs to `~/vim-biscuits.log`.

In `lua/utils.lua`:

```lua
-- uncomment line below for dev logging
-- local dev = require("nvim-biscuits.dev")

utils.console_log = function (the_string)
-- uncomment line below for dev logging
-- dev.console_log(the_string)
end
```

You can also change the log path in `dev.lua`.

Make sure not to commit these changes.
You can turn this on by passing DEBUG=true as an environment variable when launching Neovim.

## License

Expand Down
10 changes: 3 additions & 7 deletions lua/nvim-biscuits/dev.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
local Path = require("plenary.path")
local json = require "vendor.json"

local dev = {}

local debug = false
local debug_path = '~/vim-biscuits.log'

dev.console_log = function (the_string)
if debug then
Path:new(debug_path):write(the_string..'\n', 'a')
end
Path:new(debug_path):write(json.encode(the_string)..'\n', 'a')
end

dev.clear_log = function ()
if debug then
Path:new(debug_path):write('', 'w')
end
Path:new(debug_path):write('', 'w')
end

return dev
4 changes: 2 additions & 2 deletions lua/nvim-biscuits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ nvim_biscuits.decorate_nodes = function (bufnr, lang)
should_decorate = false
end

if languages.should_decorate(lang, node, text) == false then
if languages.should_decorate(lang, node, text, bufnr) == false then
should_decorate = false
end

Expand Down Expand Up @@ -101,7 +101,7 @@ nvim_biscuits.decorate_nodes = function (bufnr, lang)
local prefix_string = config.get_language_config(final_config, lang, "prefix_string")

-- language specific text filter
text = languages.transform_text(lang, node, text)
text = languages.transform_text(lang, node, text, bufnr)

if utils.trim(text) ~= '' then
text = prefix_string..text
Expand Down
4 changes: 3 additions & 1 deletion lua/nvim-biscuits/languages.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
local html = require("nvim-biscuits.languages.html")
local vue = require("nvim-biscuits.languages.vue")
local javascript = require("nvim-biscuits.languages.javascript")
local python = require("nvim-biscuits.languages.python")

local languages = {}

local handled_languages = {
html = html,
javascript = javascript,
vue = vue
vue = vue,
python = python,
}

languages.should_decorate = function (language_name, ts_node, text)
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-biscuits/languages/html.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ local function get_node_last_line(ts_node)
return end_string
end

language.should_decorate = function (ts_node, text)
language.should_decorate = function (ts_node, text, bufnr)
if string.len(text) < 8 then
local end_string = get_node_last_line(ts_node)
end_string = string.gsub(end_string, '/', '')
Expand All @@ -36,7 +36,7 @@ language.should_decorate = function (ts_node, text)
return true
end

language.transform_text = function (ts_node, text)
language.transform_text = function (ts_node, text, bufnr)
local end_string = get_node_last_line(ts_node)
end_string = string.gsub(end_string, '/', '')
end_string = string.gsub(end_string, '>', '')
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-biscuits/languages/javascript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ local html = require("nvim-biscuits.languages.html")

local language = {}

language.should_decorate = function (ts_node, text)
language.should_decorate = function (ts_node, text, bufnr)
local should_decorate = html.should_decorate(ts_node, text)
return should_decorate
end

language.transform_text = function (ts_node, text)
language.transform_text = function (ts_node, text, bufnr)
text = html.transform_text(ts_node, text)
return utils.trim(text)
end
Expand Down
32 changes: 32 additions & 0 deletions lua/nvim-biscuits/languages/python.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local ts_utils = require('nvim-treesitter.ts_utils')
local utils = require("nvim-biscuits.utils")

local language = {}

language.should_decorate = function (ts_node, text, bufnr)
local should_decorate = true
return should_decorate
end

language.transform_text = function (ts_node, text, bufnr)

-- Dev.console_log("Node")
-- Dev.console_log(ts_node)

local start_line, start_col, end_line, end_col = ts_utils.get_node_range(ts_node)
local parent_start_line, parent_start_col, parent_end_line, parent_end_col = ts_utils.get_node_range(ts_node:parent())
if parent_start_line == start_line - 1 then
start_line = parent_start_line
start_col = parent_start_col
end_line = parent_end_line
end_col = parent_end_col
end

local lines = vim.api.nvim_buf_get_lines(bufnr, start_line, start_line+1, false)
local text = lines[1]

-- text = html.transform_text(ts_node, text)
return utils.trim(text)
end

return language
4 changes: 2 additions & 2 deletions lua/nvim-biscuits/languages/vue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ local html = require("nvim-biscuits.languages.html")

local language = {}

language.should_decorate = function (ts_node, text)
language.should_decorate = function (ts_node, text, bufnr)
local should_decorate = html.should_decorate(ts_node, text)
return should_decorate
end

language.transform_text = function (ts_node, text)
language.transform_text = function (ts_node, text, bufnr)
text = html.transform_text(ts_node, text)
return utils.trim(text)
end
Expand Down
16 changes: 11 additions & 5 deletions lua/nvim-biscuits/utils.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@

local utils = {}

-- uncomment line below for dev logging
-- local dev = require("nvim-biscuits.dev")
local is_debug_mode = os.getenv("DEBUG")

if is_debug_mode then
Dev = require("nvim-biscuits.dev")
end

utils.console_log = function (the_string)
-- uncomment line below for dev logging
-- dev.console_log(the_string)
if is_debug_mode then
Dev.console_log(the_string)
end
end

utils.clear_log = function ()
-- dev.clear_log()
if is_debug_mode then
Dev.clear_log()
end
end

utils.merge_arrays = function(a, b)
Expand Down
Loading

0 comments on commit a937b97

Please sign in to comment.