Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Mar 2, 2024
1 parent 94f22ac commit 871975b
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 6 deletions.
18 changes: 18 additions & 0 deletions lua/dotvim/extra/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---@class dotvim.extra
local M = {}

---@type dora.lib
local lib = require("dora.lib")

---@async
---@param cmd string[]
---@param opts? vim.SystemOpts
---@return vim.SystemCompleted
function M.system(cmd, opts)
return lib.async.wrap(vim.system)(cmd, opts)
end

---@type dotvim.extra.obsidian
M.obsidian = require("dotvim.extra.obsidian")

return M
169 changes: 169 additions & 0 deletions lua/dotvim/extra/obsidian/graphql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
---@class dotvim.extra.obsidian.graphql
local M = {}

---@type dora.lib
local lib = require("dora.lib")

--[[
Expect https://github.com/TwIStOy/obsidian-local-graphql
]]

local graphql_url = "http://localhost:28123"

local all_markdown_files = [[
query {
vault {
allMarkdownFiles {
path
basename
cachedMetadata {
tags {
tag
}
frontmatter {
key
value
}
links {
link
linkPath {
path
}
}
}
}
}
}
]]

---@async
---@return table?
local function request_obsidian(url, query)
local cmd = {
"curl",
"--location",
"--request",
"POST",
url,
"--header",
"Content-Type: application/json",
"--data-raw",
vim.fn.json_encode { query = query },
}
---@type vim.SystemCompleted
local ret =
lib.async.wrap(vim.system)(cmd, { text = true, timeout = 10 * 1000 })
if ret.code == 0 then
local ok, data = pcall(vim.fn.json_decode, ret.stdout)
if ok then
return data
end
end
end

---@class dotvim.extra.obsidian.ObsidianNoteCachedMetadata
---@field frontmatter table<string, any>
---@field links dotvim.extra.obsidian.ObsidianNoteCachedMetadataLink[]
---@field tags string[]
---@field backlinks_count number

---@class dotvim.extra.obsidian.ObsidianNoteCachedMetadataLink
---@field link string
---@field path string?

---@class dotvim.extra.obsidian.ObsidianNote
---@field path string
---@field basename string
---@field cachedMetadata dotvim.extra.obsidian.ObsidianNoteCachedMetadata
local ObsidianNote = {}

---@return string
function ObsidianNote:id()
return vim.F.if_nil(self.cachedMetadata.frontmatter.id, "")
end

---@return string[]
function ObsidianNote:aliases()
return vim.F.if_nil(self.cachedMetadata.frontmatter.aliases, {})
end

---@return string[]
function ObsidianNote:tags()
local ret = {}
for _, tag in ipairs(vim.F.if_nil(self.cachedMetadata.tags, {})) do
ret[#ret + 1] = tag
end
for _, tag in ipairs(vim.F.if_nil(self.cachedMetadata.frontmatter.tags, {})) do
ret[#ret + 1] = "#" .. tag
end
return ret
end

---@return string
function ObsidianNote:link_count()
return ("L%d B%d"):format(
#self.cachedMetadata.links,
self.cachedMetadata.backlinks_count
)
end

---@return string
function ObsidianNote:title()
return vim.F.if_nil(
self:aliases()[1],
self.cachedMetadata.frontmatter.id,
self.basename
)
end

local function is_nil(v)
return v == nil or v == vim.NIL
end

---@async
---@return dotvim.extra.obsidian.ObsidianNote[]
function M.get_all_markdown_files()
local response = request_obsidian(graphql_url, all_markdown_files)
if response == nil then
return {}
end
local data = vim.fn.json_decode(response.body)
local files = data.data.vault.allMarkdownFiles
local ret = {}
local backlinks_count = {}
for _, file in ipairs(files) do
local note = {}
note.path = file.path
note.cachedMetadata = {}
note.cachedMetadata.frontmatter = {}
note.cachedMetadata.links = {}
note.cachedMetadata.tags = {}
for _, tag in ipairs(file.cachedMetadata.tags) do
table.insert(note.cachedMetadata.tags, tag.tag)
end
for _, link in ipairs(file.cachedMetadata.links) do
if is_nil(link.linkPath) then
link.linkPath = {}
end
table.insert(note.cachedMetadata.links, {
link = link.link,
path = link.linkPath.path,
})
if not is_nil(link.linkPath.path) then
backlinks_count[link.linkPath.path] = (
backlinks_count[link.linkPath.path] or 0
) + 1
end
end
for _, frontmatter in ipairs(file.cachedMetadata.frontmatter) do
note.cachedMetadata.frontmatter[frontmatter.key] = frontmatter.value
end
ret[#ret + 1] = setmetatable(note, { __index = ObsidianNote })
end
for _, note in ipairs(ret) do
note.cachedMetadata.backlinks_count = backlinks_count[note.path] or 0
end
return ret
end

return M
6 changes: 6 additions & 0 deletions lua/dotvim/extra/obsidian/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---@class dotvim.extra.obsidian
local M = {}

M.graphql = require("dotvim.extra.obsidian.graphql")

return M
4 changes: 3 additions & 1 deletion lua/dotvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ function M.setup(opts)
"dora.packages.extra.lang.typescript",
"dora.packages.extra.lang.nix",
"dora.packages.extra.misc.competitive-programming",
"dora.packages.extra.obsidian",
"dora.packages.extra.misc.copilot",
"dora.packages.extra.misc.wakatime",
"dora.packages.extra.misc.rime",
"dora.packages.extra.obsidian",

"dotvim.packages._",
"dotvim.packages.obsidian",
"dotvim.packages.lsp",
}

if vim.uv.os_uname() == "Darwin" then
table.insert(opts.packages, "dora.packages.extra.lang.swift")
table.insert(opts.packages, "dora.packages.extra.misc.darwin")
end

opts.lazy = function(lazy_opts)
Expand Down
16 changes: 16 additions & 0 deletions lua/dotvim/packages/_.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---@type dora.core.package.PackageOption
return {
name = "dotvim.packages._",
deps = {},
plugins = {},
setup = function()
vim.api.nvim_create_autocmd("FileType", {
pattern = "*",
callback = function()
vim.bo.formatoptions = vim.bo.formatoptions:gsub("c", "")
vim.bo.formatoptions = vim.bo.formatoptions:gsub("r", "")
vim.bo.formatoptions = vim.bo.formatoptions:gsub("o", "")
end,
})
end,
}
22 changes: 17 additions & 5 deletions lua/dotvim/packages/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ return {
local function post_parse_symbol(_, _item, ctx)
local function merge_nested_namespaces(item)
if
item.kind == "Namespace"
and #item.children == 1
and item.children[1].kind == "Namespace"
and item.lnum == item.children[1].lnum
and item.end_lnum == item.children[1].end_lnum
item.kind == "Namespace"
and #item.children == 1
and item.children[1].kind == "Namespace"
and item.lnum == item.children[1].lnum
and item.end_lnum == item.children[1].end_lnum
then
item.name = item.name .. "::" .. item.children[1].name
item.children = item.children[1].children
Expand All @@ -35,5 +35,17 @@ return {
opts.post_parse_symbol = post_parse_symbol
end,
},
{
"nvim-lspconfig",
opts = function(_, opts)
-- add dora.nvim into
if opts.servers.opts.lua_ls ~= nil then
table.insert(
opts.servers.opts.lua_ls.settings.Lua.workspace.library,
vim.fn.stdpath("data") .. "/lazy/dora.nvim/lua"
)
end
end,
},
},
}

0 comments on commit 871975b

Please sign in to comment.