From 871975b3a0a1827f20455075a61bee4a492504a5 Mon Sep 17 00:00:00 2001 From: Hawtian Wang Date: Sat, 2 Mar 2024 23:50:10 +0800 Subject: [PATCH] ... --- lua/dotvim/extra/init.lua | 18 +++ lua/dotvim/extra/obsidian/graphql.lua | 169 ++++++++++++++++++++++++++ lua/dotvim/extra/obsidian/init.lua | 6 + lua/dotvim/init.lua | 4 +- lua/dotvim/packages/_.lua | 16 +++ lua/dotvim/packages/lsp.lua | 22 +++- 6 files changed, 229 insertions(+), 6 deletions(-) create mode 100644 lua/dotvim/extra/init.lua create mode 100644 lua/dotvim/extra/obsidian/graphql.lua create mode 100644 lua/dotvim/extra/obsidian/init.lua create mode 100644 lua/dotvim/packages/_.lua diff --git a/lua/dotvim/extra/init.lua b/lua/dotvim/extra/init.lua new file mode 100644 index 00000000..ecc701fa --- /dev/null +++ b/lua/dotvim/extra/init.lua @@ -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 diff --git a/lua/dotvim/extra/obsidian/graphql.lua b/lua/dotvim/extra/obsidian/graphql.lua new file mode 100644 index 00000000..2745699b --- /dev/null +++ b/lua/dotvim/extra/obsidian/graphql.lua @@ -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 +---@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 diff --git a/lua/dotvim/extra/obsidian/init.lua b/lua/dotvim/extra/obsidian/init.lua new file mode 100644 index 00000000..111be245 --- /dev/null +++ b/lua/dotvim/extra/obsidian/init.lua @@ -0,0 +1,6 @@ +---@class dotvim.extra.obsidian +local M = {} + +M.graphql = require("dotvim.extra.obsidian.graphql") + +return M diff --git a/lua/dotvim/init.lua b/lua/dotvim/init.lua index 1cf4a0d7..4e8c57c1 100644 --- a/lua/dotvim/init.lua +++ b/lua/dotvim/init.lua @@ -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) diff --git a/lua/dotvim/packages/_.lua b/lua/dotvim/packages/_.lua new file mode 100644 index 00000000..a554f82d --- /dev/null +++ b/lua/dotvim/packages/_.lua @@ -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, +} diff --git a/lua/dotvim/packages/lsp.lua b/lua/dotvim/packages/lsp.lua index 6d6a4179..1bc28a27 100644 --- a/lua/dotvim/packages/lsp.lua +++ b/lua/dotvim/packages/lsp.lua @@ -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 @@ -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, + }, }, }