Skip to content

Commit

Permalink
WIP CR Processing
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayerOfTheBad committed Aug 14, 2024
1 parent 8e1369c commit 70c3d46
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 174 deletions.
2 changes: 1 addition & 1 deletion lua/orgmode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local auto_instance_keys = {
---@field capture OrgCapture
---@field clock OrgClock
---@field completion OrgCompletion
---@field links OrgLinkHandlerRegistry
---@field links OrgLinks
---@field org_mappings OrgMappings
---@field notifications OrgNotifications
local Org = {}
Expand Down
4 changes: 2 additions & 2 deletions lua/orgmode/org/links/handlers/_internal/custom_id.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function CustomId:follow()
return utils.echo_warning(('Could not find custom ID "%s".'):format(self.custom_id))
end

self.goto_oneof(headlines)
utils.goto_oneof(headlines)
end

function CustomId:insert_description()
Expand All @@ -44,7 +44,7 @@ function CustomId:complete(lead, context)
local completions = {}
for _, headline in pairs(headlines) do
local id = headline:get_property('CUSTOM_ID')
table.insert(completions, self:new(id):__tostring())
table.insert(completions, tostring(self:new(id)))
end

return completions
Expand Down
19 changes: 2 additions & 17 deletions lua/orgmode/org/links/handlers/_internal/headline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,7 @@ function Headline:follow()
return utils.echo_warning(('Could not find headline "%s".'):format(self.headline))
end

self.goto_oneof(headlines)
end

function Headline:resolve()
local headlines = Org.files:get_current_file():find_headlines_by_title(self.headline)

if #headlines == 0 then
return self
end

local id = headlines[1]:get_property('id')
if not id then
return self
end

return Id:new(id):resolve()
utils.goto_oneof(headlines)
end

function Headline:insert_description()
Expand All @@ -59,7 +44,7 @@ function Headline:complete(lead, context)

local completions = {}
for _, headline in pairs(headlines) do
table.insert(completions, Headline:new(headline:get_title()):__tostring())
table.insert(completions, tostring(Headline:new(headline:get_title())))
end

return completions
Expand Down
4 changes: 2 additions & 2 deletions lua/orgmode/org/links/handlers/_internal/plain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ function Plain:complete(lead, context)

local anchors = file.content:gmatch(('<<<?%s[^>]*>>>?'):format(lead))
for anchor in anchors do
table.insert(completions, Plain:new(anchor):__tostring())
table.insert(completions, tostring(Plain:new(anchor)))
end

local headlines = file:find_headlines_by_title(lead)
for _, headline in pairs(headlines) do
table.insert(completions, Plain:new(headline:get_title()):__tostring())
table.insert(completions, tostring(Plain:new(headline:get_title())))
end

return completions
Expand Down
89 changes: 43 additions & 46 deletions lua/orgmode/org/links/handlers/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,25 @@ local Id = require('orgmode.org.links.handlers.id')
local Internal = require('orgmode.org.links.handlers.internal')

---@class OrgLinkHandlerFile:OrgLinkHandler
---@field new fun(self: OrgLinkHandlerFile, path: string, target: OrgLinkHandlerInternal | nil, prefix: boolean | nil): OrgLinkHandlerFile
---@field new fun(self: OrgLinkHandlerFile, path: string, target: OrgLinkHandlerInternal | nil, prefix: boolean | nil, files: OrgFile[]): OrgLinkHandlerFile
---@field parse fun(link: string, prefix: boolean | nil): OrgLinkHandlerFile | nil
---@field path string
---@field skip_prefix boolean
---@field target OrgLinkHandlerInternal | nil
local File = Link:new('file')

function File:new(path, target, skip_prefix)
function File:new(path, target, skip_prefix, files)
---@class OrgLinkHandlerFile
local this = Link:new()
this.skip_prefix = skip_prefix or false
this.path = path
this.target = target
this.files = files
setmetatable(this, self)
self.__index = self
return this
end

function File.parse(input, skip_prefix)
if input == nil or #input == 0 then
return nil
end
local deliniator_start, deliniator_stop = input:find('::')

---@type OrgLinkHandlerInternal | nil
local target = nil
local path = input

if deliniator_start then
---@class OrgLinkHandlerInternal | nil
target = Internal.parse(input:sub(deliniator_stop + 1), true)
path = input:sub(0, deliniator_start - 1)
end

return File:new(path, target, skip_prefix)
end

-- TODO make protocol prefix optional. Based on what?
function File:__tostring()
local v = ''
Expand All @@ -65,9 +47,8 @@ function File:follow()
end
end

local function autocompletions_filenames(lead)
local Org = require('orgmode')
local filenames = Org.files:filenames()
function File:_autocompletions_filenames(lead)
local filenames = self.files:filenames()

local matches = {}
for _, f in ipairs(filenames) do
Expand All @@ -82,24 +63,6 @@ local function autocompletions_filenames(lead)
return matches
end

function File:resolve()
local Org = require('orgmode')
local path = fs.get_real_path(self.path)
if not path then
return self
end
local file = Org.files:get(path)
if not file then
return self
end
local id = file:get_property('id')
if not id then
return self
end

return Id:new(id, self.target):resolve()
end

function File:insert_description()
local Org = require('orgmode')
if self.target then
Expand Down Expand Up @@ -133,14 +96,14 @@ end

function File:_complete(lead, context)
return vim.tbl_map(function(f)
return self:new(f, nil, context.skip_prefix):__tostring()
end, autocompletions_filenames(lead))
return tostring(self:new(f, nil, context.skip_prefix))
end, self:_autocompletions_filenames(lead))
end

function File:_complete_targets(path, target_lead, context)
return vim.tbl_map(
function(t)
return self:new(path, t, context.skip_prefix):__tostring()
return tostring(self:new(path, t, context.skip_prefix))
end,
Internal:complete(target_lead, {
filename = fs.get_real_path(path),
Expand All @@ -149,4 +112,38 @@ function File:_complete_targets(path, target_lead, context)
)
end

return File
local FileFactory = {}

function FileFactory:init(files)
---@class OrgLinkHandlerFile
local this = {}
this.files = files
setmetatable(this, self)
self.__index = self
return this
end

function FileFactory:new(path, target, skip_prefix)
return File:new(path, target, skip_prefix, self.files)
end

function FileFactory:parse(input, skip_prefix)
if input == nil or #input == 0 then
return nil
end
local deliniator_start, deliniator_stop = input:find('::')

---@type OrgLinkHandlerInternal | nil
local target = nil
local path = input

if deliniator_start then
---@class OrgLinkHandlerInternal | nil
target = Internal.parse(input:sub(deliniator_stop + 1), true)
path = input:sub(0, deliniator_start - 1)
end

self:new(path, target, skip_prefix)
end

return FileFactory
24 changes: 17 additions & 7 deletions lua/orgmode/org/links/handlers/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,33 @@ function Http:new(url)
return this
end

---@param input string
function Http.parse(input)
return Http:new(input:gsub('^/*', ''))
end

function Http:__tostring()
return string.format('%s://%s', self.protocol, self.url)
end

function Http:follow()
if vim.ui['open'] then
return vim.ui.open(self:__tostring())
return vim.ui.open(tostring(self))
end
if not vim.g.loaded_netrwPlugin then
return utils.echo_warning('Netrw plugin must be loaded in order to open urls.')
end
return vim.fn['netrw#BrowseX'](self:__tostring(), vim.fn['netrw#CheckIfRemote']())
return vim.fn['netrw#BrowseX'](tostring(self), vim.fn['netrw#CheckIfRemote']())
end

local HttpFactory = {}

function HttpFactory:new()
---@class OrgLinkHandlerId
local this = {}
setmetatable(this, self)
self.__index = self
return this
end

---@param input string
function HttpFactory.parse(input)
return HttpFactory:new(input:gsub('^/*', ''))
end

return Http
26 changes: 18 additions & 8 deletions lua/orgmode/org/links/handlers/https.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,33 @@ function Https:new(url)
return this
end

---@param input string
function Https.parse(input)
return Https:new(input:gsub('^/*', ''))
end

function Https:__tostring()
return string.format('%s://%s', self.protocol, self.url)
end

function Https:follow()
if vim.ui['open'] then
return vim.ui.open(self:__tostring())
return vim.ui.open(tostring(self))
end
if not vim.g.loaded_netrwPlugin then
return utils.echo_warning('Netrw plugin must be loaded in order to open urls.')
end
return vim.fn['netrw#BrowseX'](self:__tostring(), vim.fn['netrw#CheckIfRemote']())
return vim.fn['netrw#BrowseX'](tostring(self), vim.fn['netrw#CheckIfRemote']())
end

local HttpsFactory = {}

function HttpsFactory:new()
---@class OrgLinkHandlerId
local this = {}
setmetatable(this, self)
self.__index = self
return this
end

---@param input string
function HttpsFactory.parse(input)
return HttpsFactory:new(input:gsub('^/*', ''))
end

return Https
return HttpsFactory
Loading

0 comments on commit 70c3d46

Please sign in to comment.