Skip to content

Commit

Permalink
treesitter search
Browse files Browse the repository at this point in the history
  • Loading branch information
sammyshear committed Nov 18, 2024
1 parent 8a34ae8 commit 4fcb508
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 1 deletion.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@

[![Ci](https://github.com/sammyshear/drash.nvim/actions/workflows/ci.yml/badge.svg)](https://github.com/sammyshear/drash.nvim/actions/workflows/ci.yml)

A Neovim plugin for helping to write a Drash/D'var Torah in without leaving Neovim. It relies on the Sefaria API to provide info on the Parsha and any supporting texts you might need.
A Neovim plugin for helping to write a Drash/D'var Torah without leaving Neovim. It relies on the Sefaria API to provide info on the Parsha and any supporting texts you might need.

## Installation

### lazy.nvim

```lua
{
"sammyshear/drash.nvim",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-telescope/telescope.nvim",
},
opts = {}
}
```

## Development

Expand Down
1 change: 1 addition & 0 deletions doc/tags
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
drash-drash.nvim drash.txt /*drash-drash.nvim*
drash-drash.nvim-development drash.txt /*drash-drash.nvim-development*
drash-links drash.txt /*drash-links*
drash-table-of-contents drash.txt /*drash-table-of-contents*
drash.txt drash.txt /*drash.txt*
15 changes: 15 additions & 0 deletions lua/drash/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
local sefaria = require('drash.sefaria')
local telescope = require('telescope')
_ = sefaria
local M = {}

M.setup = function(spec, opts)
_ = spec
_ = opts

vim.api.nvim_create_user_command('PostSearch', function(opt)
local query = opt.args
local data = sefaria.post_search(query)

if data == nil then
vim.notify('failed', vim.log.levels.ERROR, {})
return
end

vim.notify(vim.fn.string(data.hits.hits or ''), vim.log.levels.DEBUG, {})
end, { nargs = '?' })

telescope.load_extension('drash')
end

return M
43 changes: 43 additions & 0 deletions lua/drash/sefaria.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local plenary_curl = require('plenary.curl')
local drash_util = require('drash.util')

local M = {}

Expand All @@ -17,4 +18,46 @@ M.get_calendar = function()
return data
end

M.post_search = function(query)
if query == nil then
return nil
end
local url = 'https://www.sefaria.org/api/search-wrapper'

local response = plenary_curl.post(url, {
body = '{ "query":"' .. query .. '" }',
headers = {
['Content-Type'] = 'application/json',
},
})

if response.status ~= 200 then
return nil
end

local body = response.body

local data = vim.json.decode(body)
return data
end

M.get_text = function(id)
id = drash_util.url_encode(id)
local url = 'https://www.sefaria.org/api/v3/texts/'
.. id
.. '?version=english&return_format=text_only'
print(url)

local response = plenary_curl.get(url)

if response.status ~= 200 then
return nil
end

local body = response.body
local data = vim.json.decode(body)

return data
end

return M
11 changes: 11 additions & 0 deletions lua/drash/util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local M = {}

M.url_encode = function(str)
str = string.gsub(str, '([^%w%.%- ])', function(c)
return string.format('%%%02X', string.byte(c))
end)
str = string.gsub(str, '%s+', '%%20')
return str
end

return M
96 changes: 96 additions & 0 deletions lua/telescope/_extensions/drash.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
local telescope = require('telescope')

local pickers = require('telescope.pickers')
local finders = require('telescope.finders')
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local previewers = require('telescope.previewers')
local make_entry = require('telescope.make_entry')
local conf = require('telescope.config').values

local M = {}

M.search_sefaria = function(opts)
opts = opts or { prompt = '' }
opts.entry_maker = make_entry.gen_from_string(opts)
print(opts.prompt)

local searcher = function(prompt)
if not prompt or prompt == '' then
return {}
end

local search_list = require('drash.sefaria').post_search(prompt)
if search_list == nil then
return {}
end

local hits = search_list.hits.hits
for i, hit in ipairs(search_list.hits.hits) do
hits[i] = hit._id
end

return hits
end

pickers
.new(opts, {
prompt_title = 'Search Sefaria',
finder = finders.new_table({
results = searcher(opts.prompt),
entry_maker = opts.entry_maker,
}),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selected = action_state.get_selected_entry()
print(vim.inspect(selected))
actions.close(prompt_bufnr)
if selected == nil then
return
end

local response = require('drash.sefaria').get_text(selected[1])
local text = {}
if response == nil then
text = { 'Error fetching text' }
else
text = require('telescope.utils').flatten({ response.versions[1].text })
end
local bufnr = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, text)
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
vim.api.nvim_open_win(bufnr, true, {
split = 'right',
win = 0,
})
end)
return true
end,
previewer = previewers.new_buffer_previewer({
define_preview = function(self, entry)
local response = require('drash.sefaria').get_text(entry[1])
local text = {}
if response == nil then
text = { 'Error fetching text' }
else
text = require('telescope.utils').flatten({ response.versions[1].text })
end
self.state.bufnr = self.state.bufnr or vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, text)
end,
}),
sorter = conf.generic_sorter(opts),
})
:find()
end

return telescope.register_extension({
setup = function()
vim.api.nvim_create_user_command('SearchSefaria', function(opts)
M.search_sefaria({ prompt = opts.args })
end, { nargs = '?' })
end,
exports = {
search_sefaria = M.search_sefaria,
},
})
1 change: 1 addition & 0 deletions test/busted.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ load(vim.fn.system('curl -s https://raw.githubusercontent.com/folke/lazy.nvim/ma
require('lazy.minit').busted({
spec = {
'nvim-lua/plenary.nvim',
'nvim-telescope/telescope.nvim',
},
})
1 change: 1 addition & 0 deletions test/sefaria_spec.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---@diagnostic disable: undefined-global
local sefaria = require('drash.sefaria')

describe('sefaria', function()
Expand Down

0 comments on commit 4fcb508

Please sign in to comment.