Skip to content

Commit

Permalink
feat: lang prefix can be omitted, remove indentation, help message
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroSpace committed Dec 13, 2024
1 parent 392bd01 commit ab697d4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ There are two functions available within the console:

#### Setting up

- It is possible to setup external code executors for other languages. Evaluators for `ruby` and `racket` are working out of the box, support for other languages is coming.
- It is possible to setup external code executors for other languages. Evaluators for `ruby`,`racket` and `python` are working out of the box, support for other languages is coming.
Meanwhile, you can easily setup your own language.
- Below is the default configuration which can be overridden or extended by your custom config (`default_process_opts` will be
replaced by language specific opts), e.g. a possible config for `python` could be:
- Below is the default configuration, which can be overridden or extended by your custom config, where `default_process_opts` will be
replaced by language specific opts, e.g. a possible config for `python` could be:

```lua
require('lua-console').setup {
Expand Down Expand Up @@ -218,8 +218,7 @@ There are two functions available within the console:

- The language evaluator is determined either from (in order of precedence):

- The code prefix `===lang` on the line above your code snippet, in which case it only applies to the snippet directly below and it should be included in the selection
for evaluation. The prefix can be changed in the config.
- The code prefix `===lang` on the line above your code snippet, in which case it only applies to the snippet directly below. The prefix can be changed in the config.
- The code prefix on the top line of the console/buffer, in which case it applies to the whole buffer.
- The file type of the buffer.

Expand All @@ -228,7 +227,6 @@ There are two functions available within the console:
```racket
===racket
(define (log str)
(displayln (format "~v" str)))
Expand All @@ -246,6 +244,17 @@ There are two functions available within the console:
5.times { puts 'Hey' }
```

- Code inside Lua comments will be sytax highlighted.

```lua
[[===python
list = [1, 3, 5, 7, 9]
for val in a:
print(list)
]]
```

## Alternatives and comparison

There are a number of alternatives available, notably:
Expand Down
47 changes: 23 additions & 24 deletions lua/lua-console/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local to_string = function(tbl, sep, trim)
for _, pat in ipairs(patterns) do
line = line:gsub(pat, '')
end
-- compact strings by removing redundant spaces
line = line:gsub('(["\'])%s+', '%1'):gsub('%s+(["\'])', '%1'):gsub('%s%s+', ' ')
end

Expand All @@ -22,6 +23,13 @@ local to_table = function(str)
return vim.split(str or '', '\n', { trimempty = true })
end

local function remove_indentation(tbl)
local indent = tbl[1]:match('(%s*)%w') or tbl[1]:match('(\t*)%w')
return vim.tbl_map(function(line)
return line:sub(#indent + 1)
end, tbl)
end

---Shows virtual text in the buffer
---@param buf number buffer
---@param id number namespace id
Expand Down Expand Up @@ -60,9 +68,9 @@ local toggle_help = function(buf)
vim.api.nvim_buf_del_extmark(buf, ns, 1)

message =
[[%s - eval a line or selection, %s - open file, %s - load messages, %s - save console, %s - load console, %s/%s - resize window, %s - toggle help]]
[[%s - eval a line or selection, %s - eval buffer, %s - open file, %s - load messages, %s - save console, %s - load console, %s/%s - resize window, %s - toggle help]]
message =
string.format(message, cm.eval, cm.open, cm.messages, cm.save, cm.load, cm.resize_up, cm.resize_down, cm.help)
string.format(message, cm.eval, cm.eval_buffer, cm.open, cm.messages, cm.save, cm.load, cm.resize_up, cm.resize_down, cm.help)

local visible_line = vim.fn.line('w0')
show_virtual_text(buf, 2, message, visible_line - 1, 'overlay', 'Comment')
Expand Down Expand Up @@ -331,7 +339,7 @@ local get_external_evaluator = function(buf, lang)

return function(lines)
local cmd = vim.tbl_extend('force', {}, lang_config.cmd)
local code = (lang_config.code_prefix or '') .. to_string(lines)
local code = (lang_config.code_prefix or '') .. to_string(remove_indentation(lines)) -- some languages, like python are concerned with indentation
table.insert(cmd, code)

local status, id = pcall(vim.system, cmd, opts, opts.on_exit)
Expand All @@ -346,42 +354,33 @@ end
---Determines the language of the code/console/buffer
---mutates lines array to remove the lang_prefix
---@param buf number
---@param lines string[]
---@param range number[]
---@return string
local function get_lang(buf, lines)
local pattern = ('^.*' .. config.external_evaluators.lang_prefix .. '(.-)%s*$'):gsub('%[', '%%%[')
local function get_lang(buf, range)
local pattern = ('^.*' .. config.external_evaluators.lang_prefix .. '(.-)%s*$')
local line, lang

line = lines[1]
line = vim.api.nvim_buf_get_lines(buf, math.max(0, range[1] - 2), range[2], false)[1]
lang = line:match(pattern)
if lang then
table.remove(lines, 1)
return lang
end
if lang then return lang end

line = vim.fn.getbufline(buf, 1)[1]
line = vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1]
lang = line:match(pattern)
if lang then return lang end

return vim.bo[buf].filetype
end

local get_evaluator = function(buf, lines)
local evaluator, lang
lang = get_lang(buf, lines)
local get_evaluator = function(buf, range)
local lang = get_lang(buf, range)

if lang == '' then
vim.notify('Plese specify the language to evaluate or set the filetype', vim.log.levels.WARN)
return
end

if lang == 'lua' then
evaluator = lua_evaluator
elseif lang == 'lua' then
return lua_evaluator
else
evaluator = get_external_evaluator(buf, lang)
return get_external_evaluator(buf, lang)
end

return evaluator
end

---Evaluates code in the current line or visual selection and appends to buffer
Expand Down Expand Up @@ -409,7 +408,7 @@ local eval_code_in_buffer = function(buf, full)
lines = remove_empty_lines(lines)
if #lines == 0 then return end

local evaluator = get_evaluator(buf, lines)
local evaluator = get_evaluator(buf, { v_start, v_end })
if not evaluator then return end

local result = evaluator(lines)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/external_evals_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('external evaluators', function()

h.set_buffer(buf, content)

h.send_keys('4gg')
h.send_keys('5gg')
h.send_keys('Vj')
utils.eval_code_in_buffer()

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ describe('lua-console.utils', function()
assert.has_string(result, expected)
end)

it('gives syntax error message', function()
it('#wip gives syntax error message', function()
vim.api.nvim_win_set_cursor(win, { 1, 0 })

content = h.to_table([[ for i, ]])
Expand Down

0 comments on commit ab697d4

Please sign in to comment.