Skip to content

Commit 20b6d77

Browse files
committed
updating lsp, workspace
1 parent 9c974d0 commit 20b6d77

File tree

16 files changed

+1295
-204
lines changed

16 files changed

+1295
-204
lines changed

lua/down/mod/data/init.lua

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
local mod = require 'down.mod'
2-
local log = require 'down.util.log'
3-
local config = require 'down.config'
1+
local config = require("down.config")
2+
local log = require("down.util.log")
3+
local mod = require("down.mod")
44

55
---@class down.mod.Data: down.Mod
6-
local M = mod.new 'data'
6+
local M = mod.new("data")
77

8-
---@class down.mod..Data
8+
---@class down.mod.data.Data
99
M.data = {}
1010
---@type down.Store<down.File>
1111
M.file = {
@@ -14,7 +14,7 @@ M.file = {
1414

1515
--- @return down.mod.Setup
1616
M.setup = function()
17-
vim.api.nvim_create_autocmd('VimLeavePre', {
17+
vim.api.nvim_create_autocmd("VimLeavePre", {
1818
callback = function()
1919
M.flush()
2020
end,
@@ -31,14 +31,14 @@ M.load = function() end
3131

3232
---@class down.mod..Config
3333
M.config = {
34-
path = vim.fn.stdpath 'data' .. '/down.mpack',
34+
path = vim.fn.stdpath("data") .. "/down.mpack",
3535
dir = {
36-
vim = vim.fs.joinpath(vim.fn.stdpath 'data', 'down/'),
37-
home = vim.fs.joinpath(os.getenv 'HOME' or '~/', '.down/'),
36+
vim = vim.fs.joinpath(vim.fn.stdpath("data"), "down/"),
37+
home = vim.fs.joinpath(os.getenv("HOME") or "~/", ".down/"),
3838
},
3939
file = {
40-
vim = vim.fs.joinpath(vim.fn.stdpath 'data', 'down/', 'down.json'),
41-
home = vim.fs.joinpath(os.getenv 'HOME' or '~/', '.down/', 'down.json'),
40+
vim = vim.fs.joinpath(vim.fn.stdpath("data"), "down/", "down.json"),
41+
home = vim.fs.joinpath(os.getenv("HOME") or "~/", ".down/", "down.json"),
4242
},
4343
}
4444

@@ -51,11 +51,11 @@ end
5151
--- @return table<string>
5252
M.files = function(path, cond)
5353
local f = {}
54-
local dir = path or vim.fs.root(vim.fn.cwd(), '.down/')
54+
local dir = path or vim.fs.root(vim.fn.cwd(), ".down/")
5555
for name, type in vim.fs.dir(dir) do
56-
if type == 'file' and cond or name:endswith '.md' then
56+
if type == "file" and cond or name:endswith(".md") then
5757
table.insert(f, name)
58-
elseif type == 'directory' and not name:startswith '.' then
58+
elseif type == "directory" and not name:startswith(".") then
5959
local fs = M.get_files(M.concat(path, name))
6060
for _, v in ipairs(fs) do
6161
table.insert(f, v)
@@ -66,7 +66,7 @@ end
6666

6767
M.directory_map = function(path, callback)
6868
for name, type in vim.fs.dir(path) do
69-
if type == 'directory' then
69+
if type == "directory" then
7070
M.directory_map(M.concat(path, name), callback)
7171
else
7272
callback(name, type, path)
@@ -80,22 +80,24 @@ end
8080
--- succeed if the directory already exists.
8181
---@return boolean #If true, the directory copying succeeded
8282
M.copy_directory = function(old_path, new_path)
83-
local file_permissions = tonumber('744', 8)
83+
local file_permissions = tonumber("744", 8)
8484
local ok, err = vim.loop.fs_mkdir(new_path, file_permissions)
8585

8686
if not ok then
8787
return ok, err ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
8888
end
8989

9090
for name, type in vim.fs.dir(old_path) do
91-
if type == 'file' then
92-
ok, err = vim.loop.fs_copyfile(M.concat(old_path, name), M.concat(new_path, name))
91+
if type == "file" then
92+
ok, err =
93+
vim.loop.fs_copyfile(M.concat(old_path, name), M.concat(new_path, name))
9394

9495
if not ok then
9596
return ok, err ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
9697
end
97-
elseif type == 'directory' and not vim.endswith(new_path, name) then
98-
ok, err = M.copy_directory(M.concat(old_path, name), M.concat(new_path, name))
98+
elseif type == "directory" and not vim.endswith(new_path, name) then
99+
ok, err =
100+
M.copy_directory(M.concat(old_path, name), M.concat(new_path, name))
99101

100102
if not ok then
101103
return ok, err ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
@@ -107,11 +109,11 @@ M.copy_directory = function(old_path, new_path)
107109
end
108110
--- Grabs the data present on disk and overwrites it with the data present in memory
109111
M.sync = function()
110-
local file = io.open(M.config.path, 'r')
112+
local file = io.open(M.config.path, "r")
111113
if not file then
112114
return
113115
end
114-
local content = file:read '*a'
116+
local content = file:read("*a")
115117
file:close()
116118
M.data = vim.mpack.decode and vim.mpack.decode(content)
117119
end
@@ -121,6 +123,7 @@ end
121123
---@param data any #The data to store at the specific key
122124
M.put = function(key, data)
123125
M.data[key] = data
126+
M.flush()
124127
end
125128

126129
M.set = M.put
@@ -141,18 +144,20 @@ end
141144
M.json = function(path)
142145
local dir = M.config.dir.home
143146
local vim = M.config.dir.vim
144-
vim.fn.mkdir(dir, 'p')
145-
vim.fn.mkdir(vim, 'p')
146-
local f = io.open(path or M.config.file.vim, 'w')
147+
vim.fn.mkdir(dir, "p")
148+
vim.fn.mkdir(vim, "p")
149+
local f = io.open(path or M.config.file.vim, "w")
147150
f:write(vim.json.encode(M.data))
148151
end
149152
--- Flushes the contents in memory to the location specified
150153
M.flush = function(path)
151-
local file = io.open(path or M.config.path, 'w')
154+
local file = io.open(path or M.config.path, "w")
152155
if not file then
153156
return
154157
end
155-
file:write(vim.mpack.encode and vim.mpack.encode(M.data) or vim.mpack.pack(M.data))
158+
file:write(
159+
vim.mpack.encode and vim.mpack.encode(M.data) or vim.mpack.pack(M.data)
160+
)
156161
file:close()
157162
end
158163

lua/down/mod/find/telescope/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local picker = require("down.mod.find.telescope.picker")
12
---@class down.mod.find.telescope.Telescope: down.Mod
23
local T = require("down.mod").new("find.telescope")
34

@@ -22,7 +23,7 @@ T.setup = function()
2223
end
2324

2425
T.load = function()
25-
T.picker = { down = require("down.mod.find.telescope.picker") }
26+
T.picker = picker
2627
tel.register_extension({
2728
exports = T.picker.down,
2829
})
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
return function() end
1+
local lsp = require("down.mod.lsp")
2+
local ws = require("down.mod.workspace")
3+
4+
local actions = require("telescope.actions")
5+
local astate = require("telescope.actions.state")
6+
local autil = require("telescope.actions.utils")
7+
local entry = require("telescope.from_entry")
8+
local finders = require("telescope.finders")
9+
local gen = require("telescope.actions.generate")
10+
local hist = require("telescope.actions.history")
11+
local mt = require("telescope.actions.mt")
12+
local pick = require("telescope.pickers")
13+
local preview = require("telescope.previewers")
14+
local set = require("telescope.actions.set")
15+
local sorters = require("telescope.sorters")
16+
local ws = require("down.mod.workspace")
17+
18+
---@param o table
19+
return function(o)
20+
pick
21+
.new(o or {}, {
22+
prompt_title = "Down workspaces",
23+
prompt_prefix = "",
24+
results_title = "Workspaces",
25+
sorter = sorters.get_generic_fuzzy_sorter({}),
26+
finder = finders.new_table({
27+
results = ws.as_lsp_workspaces(),
28+
}),
29+
previewer = preview.vim_buffer_vimgrep.new({}),
30+
result_display = function(entry)
31+
return entry.value
32+
end,
33+
attach_mappings = function(prompt_bufnr, map)
34+
actions.select_default:replace(function()
35+
actions.close(prompt_bufnr)
36+
local selection = astate.get_selected_entry()
37+
ws.open(selection.value)
38+
end)
39+
return true
40+
end,
41+
theme = "dropdown",
42+
layout_config = {
43+
width = 0.5,
44+
height = 0.4,
45+
},
46+
})
47+
:find()
48+
end
Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
1-
return function() end
1+
local action_state = require("telescope.actions.state")
2+
local actions = require("telescope.actions")
3+
local finders = require("telescope.finders")
4+
local pickers = require("telescope.pickers")
5+
local previewers = require("telescope.previewers")
6+
local sorters = require("telescope.sorters")
7+
local telescope = require("telescope")
8+
9+
local function get_backlinks_to_file(file_path)
10+
function find_backlinks(notes, target_note)
11+
local backlinks = {}
12+
for note, content in pairs(notes) do
13+
if content:find(target_note) then
14+
table.insert(backlinks, note)
15+
end
16+
end
17+
return backlinks
18+
end
19+
20+
local backlinks = {}
21+
-- Assuming a function `find_backlinks` that returns a list of files linking to `file_path`
22+
backlinks = find_backlinks(file_path)
23+
return backlinks
24+
end
25+
26+
local function find_backlinks(file_path)
27+
-- This function should be implemented to search through the workspace
28+
-- and return a list of files that contain links to `file_path`.
29+
-- For now, it returns a dummy list for demonstration purposes.
30+
return {
31+
"file1.md",
32+
"file2.md",
33+
"file3.md",
34+
}
35+
end
36+
37+
local function backlinks_picker(opts)
38+
opts = opts or {}
39+
local file_path = vim.fn.expand("%:p") -- Get the current file path
40+
local backlinks = get_backlinks_to_file(file_path)
41+
42+
pickers
43+
.new(opts, {
44+
prompt_title = "Backlinks to " .. file_path,
45+
finder = finders.new_table({
46+
results = backlinks,
47+
}),
48+
sorter = sorters.get_generic_fuzzy_sorter(),
49+
previewer = previewers.vim_buffer_cat.new(opts),
50+
attach_mappings = function(prompt_bufnr, map)
51+
actions.select_default:replace(function()
52+
actions.close(prompt_bufnr)
53+
local selection = action_state.get_selected_entry()
54+
if selection then
55+
vim.cmd("edit " .. selection[1])
56+
end
57+
end)
58+
return true
59+
end,
60+
})
61+
:find()
62+
end
63+
64+
-- To use the picker, you can call `backlinks_picker()` in your Neovim command line
65+
-- or map it to a keybinding in your Neovim configuration.
Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
return function() end
1+
local action_state = require("telescope.actions.state")
2+
local actions = require("telescope.actions")
3+
local finders = require("telescope.finders")
4+
local pickers = require("telescope.pickers")
5+
local previewers = require("telescope.previewers")
6+
local telescope = require("telescope")
7+
local conf = require("telescope.config").values
8+
9+
local function parse_links(content, link_type)
10+
local links = {}
11+
local pattern
12+
13+
if link_type == "hyperlinks" then
14+
pattern = "%b<>"
15+
elseif link_type == "file_links" then
16+
pattern = "%[.-%]%((.-)%)"
17+
elseif link_type == "emails" then
18+
pattern = "%S+@%S+"
19+
else
20+
return links
21+
end
22+
23+
for link in content:gmatch(pattern) do
24+
table.insert(links, link)
25+
end
26+
27+
return links
28+
end
29+
30+
local function markdown_links_picker(opts)
31+
opts = opts or {}
32+
local link_type = opts.link_type or "hyperlinks"
33+
34+
local content = vim.fn.join(vim.fn.getline(1, "$"), "\n")
35+
local links = parse_links(content, link_type)
36+
37+
pickers
38+
.new(opts, {
39+
prompt_title = "Markdown Links",
40+
finder = finders.new_table({
41+
results = links,
42+
}),
43+
sorter = conf.generic_sorter(opts),
44+
previewer = previewers.new_buffer_previewer({
45+
define_preview = function(self, entry, status)
46+
local bufnr = self.state.bufnr
47+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { entry.value })
48+
end,
49+
}),
50+
attach_mappings = function(prompt_bufnr, map)
51+
actions.select_default:replace(function()
52+
actions.close(prompt_bufnr)
53+
local selection = action_state.get_selected_entry()
54+
print("Selected link: " .. selection.value)
55+
end)
56+
return true
57+
end,
58+
})
59+
:find()
60+
end
61+
62+
telescope.register_extension({
63+
exports = {
64+
markdown_links = markdown_links_picker,
65+
},
66+
})
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
return function() end
1+
local lsp = require("down.mod.lsp")
2+
local ws = require("down.mod.workspace")
3+
4+
local actions = require("telescope.actions")
5+
local astate = require("telescope.actions.state")
6+
local autil = require("telescope.actions.utils")
7+
local entry = require("telescope.from_entry")
8+
local finders = require("telescope.finders")
9+
local gen = require("telescope.actions.generate")
10+
local hist = require("telescope.actions.history")
11+
local mt = require("telescope.actions.mt")
12+
local pick = require("telescope.pickers")
13+
local preview = require("telescope.previewers")
14+
local set = require("telescope.actions.set")
15+
local sorters = require("telescope.sorters")
16+
local ws = require("down.mod.workspace")
17+
18+
---@param o table
19+
return function(o)
20+
pick
21+
.new(o or {}, {
22+
prompt_title = "Down workspaces",
23+
prompt_prefix = "",
24+
results_title = "Workspaces",
25+
sorter = sorters.get_generic_fuzzy_sorter({}),
26+
finder = finders.new_table({
27+
results = ws.as_lsp_workspaces(),
28+
}),
29+
previewer = preview.vim_buffer_vimgrep.new({}),
30+
result_display = function(entry)
31+
return entry.value
32+
end,
33+
attach_mappings = function(prompt_bufnr, map)
34+
actions.select_default:replace(function()
35+
actions.close(prompt_bufnr)
36+
local selection = astate.get_selected_entry()
37+
ws.open(selection.value)
38+
end)
39+
return true
40+
end,
41+
theme = "dropdown",
42+
layout_config = {
43+
width = 0.5,
44+
height = 0.4,
45+
},
46+
})
47+
:find()
48+
end

0 commit comments

Comments
 (0)