Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions autoload/fireplace.vim
Original file line number Diff line number Diff line change
Expand Up @@ -192,37 +192,45 @@ function! s:candidate(val) abort
\ }
endfunction

function! s:get_complete_context() abort
" Find toplevel form
" If cursor is on start parenthesis we don't want to find the form
" If cursor is on end parenthesis we want to find the form
let [line1, col1] = searchpairpos('(', '', ')', 'Wrnb', g:fireplace#skip)
let [line2, col2] = searchpairpos('(', '', ')', 'Wrnc', g:fireplace#skip)
function! s:internal_get_complete_context() abort
" Find toplevel form
" If cursor is on start parenthesis we don't want to find the form
" If cursor is on end parenthesis we want to find the form
let [line1, col1] = searchpairpos('(', '', ')', 'Wrnb', g:fireplace#skip)
let [line2, col2] = searchpairpos('(', '', ')', 'Wrnc', g:fireplace#skip)

if (line1 == 0 && col1 == 0) || (line2 == 0 && col2 == 0)
return ""
endif
if (line1 == 0 && col1 == 0) || (line2 == 0 && col2 == 0)
return ""
endif

if line1 == line2
let expr = getline(line1)[col1-1 : col2-1]
else
let expr = getline(line1)[col1-1 : -1] . ' '
\ . join(getline(line1+1, line2-1), ' ')
\ . getline(line2)[0 : col2-1]
endif
if line1 == line2
let expr = getline(line1)[col1-1 : col2-1]
else
let expr = getline(line1)[col1-1 : -1] . ' '
\ . join(getline(line1+1, line2-1), ' ')
\ . getline(line2)[0 : col2-1]
endif

" Calculate the position of cursor inside the expr
if line1 == line('.')
let p = col('.') - col1
else
let p = strlen(getline(line1)[col1-1 : -1])
\ + strlen(join(getline(line1 + 1, line('.') - 1), ' '))
\ + col('.')
endif
" Calculate the position of cursor inside the expr
if line1 == line('.')
let p = col('.') - col1
else
let p = strlen(getline(line1)[col1-1 : -1])
\ + strlen(join(getline(line1 + 1, line('.') - 1), ' '))
\ + col('.')
endif

return strpart(expr, 0, p) . ' __prefix__ ' . strpart(expr, p)
return strpart(expr, 0, p) . ' __prefix__ ' . strpart(expr, p)
Comment on lines +196 to +223
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you botched the indent here.

endfunction

if has('nvim-0.5')
let s:lua_impl = luaeval("require'_fireplace'")
else
let s:lua_impl = {}
endif

let s:get_complete_context = get(s:lua_impl, 'get_completion_context', function('s:internal_get_complete_context'))

function! s:complete_extract(msg) abort
let trans = '{"word": (v:val =~# ''[./]'' ? "" : matchstr(a:base, ''^.\+/'')) . v:val}'
let value = get(a:msg, 'value', get(a:msg, 'completions'))
Expand Down
43 changes: 43 additions & 0 deletions lua/_fireplace.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local fireplace_impl = {[vim.type_idx]=vim.types.dictionary}

if vim.treesitter then
local has_ts, ts = pcall(require, 'nvim-treesitter.ts_utils')

if has_ts and vim.treesitter.require_language('clojure', nil, true) then
fireplace_impl['get_completion_context'] = function()
vim.treesitter.get_parser(0, 'clojure'):parse()
local node = ts.get_node_at_cursor()
local one_ago = nil
local two_ago = nil

while(node ~= nil) do
two_ago = one_ago
one_ago = node
node = node:parent()
end

local root_node = two_ago or one_ago or node

if root_node ~= nil then
local strs = ts.get_node_text(root_node)
local nrow1,ncol1,nline2,ncol2 = root_node:range()
local crow,ccol = unpack(vim.api.nvim_win_get_cursor(0))
Comment on lines +23 to +24
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give these commas some breathing room, along with the = up top?


local idx = crow - nrow1
local line = strs[idx]
local coloffset = 0
if crow == nrow1 then
coloffset = ncol1
end

strs[idx] = string.sub(line, 1 + coloffset, ccol + coloffset) .. ' __prefix__ ' .. string.sub(line, ccol+coloffset+1, -1)

return table.concat(strs, "\n")
else
return ""
end
end
end
end

return fireplace_impl