From f7277f09190c1834a677926747e8b990566b2f3e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 25 Jan 2026 13:33:06 +0000 Subject: [PATCH] feat: make logseq_dir optional and add .logseqfolder detection --- lua/logseq_mode/config.lua | 1 - lua/logseq_mode/formatter.lua | 4 +-- lua/logseq_mode/init.lua | 30 ++++++++++--------- lua/logseq_mode/utils.lua | 33 +++++++++++++++++++++ tests/test_detection.lua | 56 +++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 17 deletions(-) create mode 100644 lua/logseq_mode/utils.lua create mode 100644 tests/test_detection.lua diff --git a/lua/logseq_mode/config.lua b/lua/logseq_mode/config.lua index e366c72..cd9d1d6 100644 --- a/lua/logseq_mode/config.lua +++ b/lua/logseq_mode/config.lua @@ -1,7 +1,6 @@ local M = {} M.defaults = { - logseq_dir = vim.fn.expand("~/logseq-graph"), -- Optional: Used for unified search if you have an obsidian vault too obsidian_dir = vim.fn.expand("~/main-vault"), -- Amount of extra space between lines (only works in GUI clients) diff --git a/lua/logseq_mode/formatter.lua b/lua/logseq_mode/formatter.lua index 77db8ed..783212c 100644 --- a/lua/logseq_mode/formatter.lua +++ b/lua/logseq_mode/formatter.lua @@ -53,11 +53,11 @@ M.awk_script = [[ } ]] -function M.get_config(logseq_dir) +function M.get_config() return { condition = function(self, ctx) local bufname = vim.api.nvim_buf_get_name(ctx.buf) - return bufname:find(logseq_dir, 1, true) ~= nil + return require("logseq_mode.utils").get_logseq_root(bufname) ~= nil end, command = "awk", args = { M.awk_script }, diff --git a/lua/logseq_mode/init.lua b/lua/logseq_mode/init.lua index 35f128c..ca56955 100644 --- a/lua/logseq_mode/init.lua +++ b/lua/logseq_mode/init.lua @@ -1,5 +1,6 @@ local Config = require("logseq_mode.config") local Formatter = require("logseq_mode.formatter") +local Utils = require("logseq_mode.utils") local M = {} @@ -52,7 +53,18 @@ end function M.daily_note() local date = os.date("%Y_%m_%d") - local path = Config.options.logseq_dir .. "/journals/" .. date .. ".md" + local root = Config.options.logseq_dir + + if not root or root == "" then + root = Utils.get_logseq_root(vim.api.nvim_buf_get_name(0)) + end + + if not root then + vim.notify("Logseq directory not configured and not in a Logseq project", vim.log.levels.ERROR) + return + end + + local path = root .. "/journals/" .. date .. ".md" vim.cmd("edit " .. path) end @@ -87,7 +99,7 @@ function M.setup(opts) -- Register Formatter if Conform is loaded local has_conform, conform = pcall(require, "conform") if has_conform then - conform.formatters.logseq_fixer = Formatter.get_config(Config.options.logseq_dir) + conform.formatters.logseq_fixer = Formatter.get_config() end -- FileType Autocommand @@ -100,13 +112,8 @@ function M.setup(opts) end local bufname = ev.file - local logseq_dir = Config.options.logseq_dir - if not logseq_dir or type(logseq_dir) ~= "string" or not bufname or bufname == "" then - return - end - - if bufname:find(logseq_dir, 1, true) then + if Utils.get_logseq_root(bufname) then -- Set local options vim.opt_local.foldmethod = "indent" vim.opt_local.shiftwidth = 0 -- Use tabstop @@ -180,14 +187,9 @@ function M.setup(opts) end local bufname = ev.file - local logseq_dir = Config.options.logseq_dir - - if not logseq_dir or type(logseq_dir) ~= "string" or not bufname or bufname == "" then - return - end -- Check if current buffer is in logseq dir - if bufname:find(logseq_dir, 1, true) then + if Utils.get_logseq_root(bufname) then if Config.options.linespace and Config.options.linespace > 0 then vim.opt.linespace = Config.options.linespace end diff --git a/lua/logseq_mode/utils.lua b/lua/logseq_mode/utils.lua new file mode 100644 index 0000000..b10f708 --- /dev/null +++ b/lua/logseq_mode/utils.lua @@ -0,0 +1,33 @@ +local Config = require("logseq_mode.config") + +local M = {} + +---Returns the root directory of the Logseq graph for the given buffer path, or nil. +---@param bufpath string +---@return string|nil +function M.get_logseq_root(bufpath) + if not bufpath or bufpath == "" then + return nil + end + + -- 1. Check configured logseq_dir + if Config.options.logseq_dir and Config.options.logseq_dir ~= "" then + -- Use plain string search to avoid pattern issues with paths + if bufpath:find(Config.options.logseq_dir, 1, true) then + return Config.options.logseq_dir + end + end + + -- 2. Check for .logseqfolder marker + -- vim.fs.root returns the path to the directory containing the marker + if vim.fs and vim.fs.root then + local root = vim.fs.root(bufpath, ".logseqfolder") + if root then + return root + end + end + + return nil +end + +return M diff --git a/tests/test_detection.lua b/tests/test_detection.lua new file mode 100644 index 0000000..fe9e13e --- /dev/null +++ b/tests/test_detection.lua @@ -0,0 +1,56 @@ +-- Minimal mock framework for testing logic without full Neovim +_G.vim = { + fn = { + expand = function(path) return path:gsub("~", "/home/user") end + }, + fs = { + root = function(path, marker) + -- Simple mock: check if path starts with a known mock root containing the marker + if marker == ".logseqfolder" then + if path:find("/home/user/graph_with_marker") then + return "/home/user/graph_with_marker" + end + end + return nil + end + } +} + +-- Adjust package path to find the lua module from repo root +package.path = "lua/?.lua;" .. package.path + +local Utils = require("logseq_mode.utils") +local Config = require("logseq_mode.config") + +-- Helper to run test +local function assert_eq(a, b, msg) + if a ~= b then + error("FAIL: " .. msg .. " | Expected: " .. tostring(b) .. ", Got: " .. tostring(a)) + end +end + +print("Running detection tests...") + +-- Test 1: Configured logseq_dir +print("Test 1: Configured logseq_dir") +Config.options.logseq_dir = "/home/user/my_graph" +local res1 = Utils.get_logseq_root("/home/user/my_graph/pages/Page.md") +assert_eq(res1, "/home/user/my_graph", "Should match configured dir") + +-- Test 2: Configured logseq_dir (mismatch) +print("Test 2: Configured logseq_dir mismatch") +local res2 = Utils.get_logseq_root("/home/user/other/file.md") +assert_eq(res2, nil, "Should return nil for outside file") + +-- Test 3: Marker file (logseq_dir nil) +print("Test 3: Marker file detection") +Config.options.logseq_dir = nil +local res3 = Utils.get_logseq_root("/home/user/graph_with_marker/pages/A.md") +assert_eq(res3, "/home/user/graph_with_marker", "Should detect marker") + +-- Test 4: No config, no marker +print("Test 4: No config, no marker") +local res4 = Utils.get_logseq_root("/home/user/random/file.md") +assert_eq(res4, nil, "Should return nil") + +print("All tests passed!")