Skip to content

Commit

Permalink
feat: Add elin.function.sexp
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Jan 18, 2024
1 parent ec6db9b commit 2a16a26
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
57 changes: 57 additions & 0 deletions lua/vim-elin/sexp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local get_node_from_cursor_position = function(cursor_row, cursor_col)
local parser = vim.treesitter.get_parser(0, vim.api.nvim_buf_get_option(0, 'filetype'))
local tree = parser:parse()[1]
return tree:root():named_descendant_for_range(cursor_row, cursor_col, cursor_row, cursor_col)
end

local function get_code(node)
local node_type = node:type()
local start_row, start_col, end_row, end_col = node:range()
local node_text = vim.api.nvim_buf_get_lines(0, start_row, end_row + 1, false)
node_text[1] = string.sub(node_text[1], start_col + 1)

node_text[end_row - start_row + 1] = (start_row == end_row)
and string.sub(node_text[end_row - start_row + 1], 1, end_col - start_col)
or string.sub(node_text[end_row - start_row + 1], 1, end_col)

return table.concat(node_text, "\n")
end

local function find_list_lit(node)
while (node and node:type() ~= 'list_lit') do
node = node:parent()
end
return node
end

local function find_top_list_lit(node)
while (node:parent():type() ~= "source") do
node = node:parent()
end

return node
end

local get_top_list = function(cursor_row, cursor_col)
local node = get_node_from_cursor_position(cursor_row, cursor_col)
local top_node = node and find_top_list_lit(node) or nil
return top_node and get_code(top_node) or nil
end

local get_list = function(cursor_row, cursor_col)
local node = get_node_from_cursor_position(cursor_row, cursor_col)
local list_node = node and find_list_lit(node) or nil
return list_node and get_code(list_node) or nil
end

local get_form = function(cursor_row, cursor_col)
local node = get_node_from_cursor_position(cursor_row, cursor_col)
return node and get_code(node) or nil
end

return {
get_top_list = get_top_list,
get_list = get_list,
get_form = get_form,
}

37 changes: 37 additions & 0 deletions src/elin/function/sexp.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns elin.function.sexp
(:require
[elin.constant.host :as e.c.host]
[elin.function.host :as e.f.host]
[elin.schema.server :as e.s.server]
[malli.core :as m]))

(m/=> luaeval [:=> [:cat e.s.server/?Message string? [:sequential any?]] any?])
(defn- luaeval [msg code args]
(e.f.host/call-function msg "luaeval" [code args]))

(m/=> get-current-top-list [:=> [:cat e.s.server/?Message int? int?] string?])
(defn get-current-top-list
[msg cursor-row cursor-col]
(if (= e.c.host/vim (:host msg))
"TODO"
(luaeval msg "require('vim-elin.sexp').get_top_list(_A[1], _A[2])"
[(dec cursor-row)
(dec cursor-col)])))

(m/=> get-current-list [:=> [:cat e.s.server/?Message int? int?] string?])
(defn get-current-list
[msg cursor-row cursor-col]
(if (= e.c.host/vim (:host msg))
"TODO"
(luaeval msg "require('vim-elin.sexp').get_list(_A[1], _A[2])"
[(dec cursor-row)
(dec cursor-col)])))

(m/=> get-current-form [:=> [:cat e.s.server/?Message int? int?] string?])
(defn get-current-form
[msg cursor-row cursor-col]
(if (= e.c.host/vim (:host msg))
"TODO"
(luaeval msg "require('vim-elin.sexp').get_form(_A[1], _A[2])"
[(dec cursor-row)
(dec cursor-col)])))

0 comments on commit 2a16a26

Please sign in to comment.