Skip to content

Commit

Permalink
Unify formatting (#44)
Browse files Browse the repository at this point in the history
* Add .editorconfig and stylua.toml

* Add stylua check to CI pipeline

* Reformat with stylua

---------

Co-authored-by: Sebastian Flügge <[email protected]>
  • Loading branch information
seflue and Sebastian Flügge authored May 31, 2024
1 parent 295fa65 commit ae95490
Show file tree
Hide file tree
Showing 66 changed files with 1,476 additions and 1,352 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# see https://github.com/CppCXY/EmmyLuaCodeStyle
[*.yml]
quote_style = double

[*.lua]

indent_style = space
indent_size = 4
quote_style = double

continuation_indent = 4
max_line_length = 120

end_of_line = lf

9 changes: 9 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ env:
ROAM_WAIT_TIME: 500

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: JohnnyMorganz/stylua-action@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --check lua/ spec/
tests:
strategy:
fail-fast: false
Expand Down
28 changes: 14 additions & 14 deletions lua/org-roam/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
---@param roam OrgRoam
---@return org-roam.Api
return function(roam)
local AliasApi = require("org-roam.api.alias")(roam)
local AliasApi = require("org-roam.api.alias")(roam)
local CompletionApi = require("org-roam.api.completion")(roam)
local NodeApi = require("org-roam.api.node")(roam)
local OriginApi = require("org-roam.api.origin")(roam)
local NodeApi = require("org-roam.api.node")(roam)
local OriginApi = require("org-roam.api.origin")(roam)

---@class org-roam.Api
local M = {}
local M = {}

M.add_alias = AliasApi.add_alias
M.add_origin = OriginApi.add_origin
M.capture_node = NodeApi.capture
M.complete_node = CompletionApi.complete_node_under_cursor
M.find_node = NodeApi.find
M.goto_next_node = OriginApi.goto_next_node
M.goto_prev_node = OriginApi.goto_prev_node
M.insert_node = NodeApi.insert
M.remove_alias = AliasApi.remove_alias
M.remove_origin = OriginApi.remove_origin
M.add_alias = AliasApi.add_alias
M.add_origin = OriginApi.add_origin
M.capture_node = NodeApi.capture
M.complete_node = CompletionApi.complete_node_under_cursor
M.find_node = NodeApi.find
M.goto_next_node = OriginApi.goto_next_node
M.goto_prev_node = OriginApi.goto_prev_node
M.insert_node = NodeApi.insert
M.remove_alias = AliasApi.remove_alias
M.remove_origin = OriginApi.remove_origin

return M
end
207 changes: 110 additions & 97 deletions lua/org-roam/api/alias.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,57 @@ local function roam_add_alias(roam, opts)
return Promise.new(function(resolve, reject)
utils.node_under_cursor(function(node)
-- Mark unsuccessful and exit
if not node then return resolve(false) end
if not node then
return resolve(false)
end

roam.database:load_file({ path = node.file }):next(function(results)
-- Get the OrgFile instance
local file = results.file
roam.database
:load_file({ path = node.file })
:next(function(results)
-- Get the OrgFile instance
local file = results.file

-- Look for a file or headline that matches our node
local entry = utils.find_id_match(file, node.id)
-- Look for a file or headline that matches our node
local entry = utils.find_id_match(file, node.id)

if entry then
-- Get list of aliases that already exist
local aliases = entry:get_property(ALIASES_PROP_NAME) or ""
if entry then
-- Get list of aliases that already exist
local aliases = entry:get_property(ALIASES_PROP_NAME) or ""

local alias = vim.trim(opts.alias or vim.fn.input({
prompt = "Alias: ",
}))
local alias = vim.trim(opts.alias or vim.fn.input({
prompt = "Alias: ",
}))

-- Skip if not given a non-empty alias
if alias == "" then
notify.echo_info("canceled adding alias")
-- Skip if not given a non-empty alias
if alias == "" then
notify.echo_info("canceled adding alias")

-- Mark unsuccessful
resolve(false)
-- Mark unsuccessful
resolve(false)

return file
end
return file
end

-- Escape double quotes and backslashes within alias as
-- we're going to wrap it
alias = utils.wrap_prop_value(alias)
-- Escape double quotes and backslashes within alias as
-- we're going to wrap it
alias = utils.wrap_prop_value(alias)

-- Append our new alias to the end
aliases = vim.trim(string.format("%s \"%s\"", aliases, alias))
-- Append our new alias to the end
aliases = vim.trim(string.format('%s "%s"', aliases, alias))

-- Update the entry
entry:set_property(ALIASES_PROP_NAME, aliases)
-- Update the entry
entry:set_property(ALIASES_PROP_NAME, aliases)

-- Mark successful
resolve(true)
else
-- Mark unsuccessful
resolve(false)
end
-- Mark successful
resolve(true)
else
-- Mark unsuccessful
resolve(false)
end

return file
end):catch(reject)
return file
end)
:catch(reject)
end, { win = opts.win })
end)
end
Expand All @@ -78,83 +83,91 @@ local function roam_remove_alias(roam, opts)
return Promise.new(function(resolve, reject)
utils.node_under_cursor(function(node)
-- Mark unsuccessful and exit
if not node then return resolve(false) end
if not node then
return resolve(false)
end

roam.database:load_file({ path = node.file }):next(function(results)
-- Get the OrgFile instance
local file = results.file
roam.database
:load_file({ path = node.file })
:next(function(results)
-- Get the OrgFile instance
local file = results.file

-- Look for a file or headline that matches our node
local entry = utils.find_id_match(file, node.id)
-- Look for a file or headline that matches our node
local entry = utils.find_id_match(file, node.id)

if entry and opts.all then
entry:set_property(ALIASES_PROP_NAME, nil)
if entry and opts.all then
entry:set_property(ALIASES_PROP_NAME, nil)

-- Mark successful
resolve(true)
elseif entry then
local aliases = entry:get_property(ALIASES_PROP_NAME) or ""

-- If we have nothing to remove, exit successfully
if vim.trim(aliases) == "" then
-- Mark successful
resolve(true)
return file
end
elseif entry then
local aliases = entry:get_property(ALIASES_PROP_NAME) or ""

local function on_cancel()
notify.echo_info("canceled removing alias")

-- Mark unsuccessful
resolve(false)
end
-- If we have nothing to remove, exit successfully
if vim.trim(aliases) == "" then
resolve(true)
return file
end

---@param alias string
local function on_choice(alias)
local remaining = vim.tbl_filter(function(item)
return item ~= "" and item ~= alias
end, utils.parse_prop_value(aliases))
local function on_cancel()
notify.echo_info("canceled removing alias")

-- Break up our alias into pieces, filter out the specified alias,
-- and then reconstruct back (wrapping in quotes) into aliases string
aliases = vim.trim(table.concat(vim.tbl_map(function(item)
return "\"" .. utils.wrap_prop_value(item) .. "\""
end, remaining), " "))
-- Mark unsuccessful
resolve(false)
end

-- Update the entry
if aliases == "" then
entry:set_property(ALIASES_PROP_NAME, nil)
else
entry:set_property(ALIASES_PROP_NAME, aliases)
---@param alias string
local function on_choice(alias)
local remaining = vim.tbl_filter(function(item)
return item ~= "" and item ~= alias
end, utils.parse_prop_value(aliases))

-- Break up our alias into pieces, filter out the specified alias,
-- and then reconstruct back (wrapping in quotes) into aliases string
aliases = vim.trim(table.concat(
vim.tbl_map(function(item)
return '"' .. utils.wrap_prop_value(item) .. '"'
end, remaining),
" "
))

-- Update the entry
if aliases == "" then
entry:set_property(ALIASES_PROP_NAME, nil)
else
entry:set_property(ALIASES_PROP_NAME, aliases)
end

-- Mark successful
resolve(true)
end

-- Mark successful
resolve(true)
end
-- Build our prompt, updating it to a left-hand side
-- style if we have neovim 0.10+ which supports inlining
local prompt = "(alias {sel}/{cnt})"
if vim.fn.has("nvim-0.10") == 1 then
prompt = "{sel}/{cnt} alias> "
end

-- Build our prompt, updating it to a left-hand side
-- style if we have neovim 0.10+ which supports inlining
local prompt = "(alias {sel}/{cnt})"
if vim.fn.has("nvim-0.10") == 1 then
prompt = "{sel}/{cnt} alias> "
-- Open a selection dialog for the alias to remove
Select:new({
auto_select = true,
init_input = opts.alias,
items = utils.parse_prop_value(aliases),
prompt = prompt,
})
:on_cancel(on_cancel)
:on_choice(on_choice)
:open()
else
-- Mark unsuccessful
resolve(false)
end

-- Open a selection dialog for the alias to remove
Select:new({
auto_select = true,
init_input = opts.alias,
items = utils.parse_prop_value(aliases),
prompt = prompt,
})
:on_cancel(on_cancel)
:on_choice(on_choice)
:open()
else
-- Mark unsuccessful
resolve(false)
end

return file
end):catch(reject)
return file
end)
:catch(reject)
end, { win = opts.win })
end)
end
Expand Down
19 changes: 10 additions & 9 deletions lua/org-roam/api/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ local function roam_complete_node_under_cursor(roam, opts)
local row = cursor[1] - 1 -- make zero-indexed
local line = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, true)[1]

selection = string.sub(
line,
link.range.start.column + 1,
link.range.end_.column + 1
)
selection = string.sub(line, link.range.start.column + 1, link.range.end_.column + 1)

-- Set initial input to be the link's existing path,
-- stripping id: if already starting with that
Expand All @@ -47,7 +43,8 @@ local function roam_complete_node_under_cursor(roam, opts)
end

return Promise.new(function(resolve)
roam.ui.select_node({ auto_select = true, init_input = input })
roam.ui
.select_node({ auto_select = true, init_input = input })
:on_choice(function(choice)
local node = roam.database:get_sync(choice.id)
if not node then
Expand All @@ -70,7 +67,9 @@ local function roam_complete_node_under_cursor(roam, opts)
local i = 0
while i < string.len(line) do
i = string.find(line, selection, i + 1, true)
if i == nil then break end
if i == nil then
break
end

-- Check if the current match contains the cursor column
if i - 1 <= col and i - 1 + #selection > col then
Expand All @@ -87,7 +86,7 @@ local function roam_complete_node_under_cursor(roam, opts)

-- Replace the text (this will place us into insert mode)
vim.api.nvim_buf_set_text(bufnr, row, col, row, col + #selection, {
string.format("[[id:%s][%s]]", node.id, choice.label)
string.format("[[id:%s][%s]]", node.id, choice.label),
})

-- Force ourselves back into normal mode
Expand All @@ -96,7 +95,9 @@ local function roam_complete_node_under_cursor(roam, opts)
-- Mark as successful
resolve(true)
end)
:on_cancel(function() resolve(false) end)
:on_cancel(function()
resolve(false)
end)
:open()
end)
end
Expand Down
Loading

0 comments on commit ae95490

Please sign in to comment.