Skip to content

Commit 6b5ca2b

Browse files
WIP CR Processing
1 parent e7da5f8 commit 6b5ca2b

File tree

13 files changed

+191
-180
lines changed

13 files changed

+191
-180
lines changed

lua/orgmode/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local auto_instance_keys = {
2020
---@field capture OrgCapture
2121
---@field clock OrgClock
2222
---@field completion OrgCompletion
23-
---@field links OrgLinkHandlerRegistry
23+
---@field links OrgLinks
2424
---@field org_mappings OrgMappings
2525
---@field notifications OrgNotifications
2626
local Org = {}

lua/orgmode/org/links/handlers/_internal/custom_id.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function CustomId:follow()
3030
return utils.echo_warning(('Could not find custom ID "%s".'):format(self.custom_id))
3131
end
3232

33-
self.goto_oneof(headlines)
33+
utils.goto_oneof(headlines)
3434
end
3535

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

5050
return completions

lua/orgmode/org/links/handlers/_internal/headline.lua

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,7 @@ function Headline:follow()
3131
return utils.echo_warning(('Could not find headline "%s".'):format(self.headline))
3232
end
3333

34-
self.goto_oneof(headlines)
35-
end
36-
37-
function Headline:resolve()
38-
local headlines = Org.files:get_current_file():find_headlines_by_title(self.headline)
39-
40-
if #headlines == 0 then
41-
return self
42-
end
43-
44-
local id = headlines[1]:get_property('id')
45-
if not id then
46-
return self
47-
end
48-
49-
return Id:new(id):resolve()
34+
utils.goto_oneof(headlines)
5035
end
5136

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

6045
local completions = {}
6146
for _, headline in pairs(headlines) do
62-
table.insert(completions, Headline:new(headline:get_title()):__tostring())
47+
table.insert(completions, tostring(Headline:new(headline:get_title())))
6348
end
6449

6550
return completions

lua/orgmode/org/links/handlers/_internal/plain.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ function Plain:complete(lead, context)
5555

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

6161
local headlines = file:find_headlines_by_title(lead)
6262
for _, headline in pairs(headlines) do
63-
table.insert(completions, Plain:new(headline:get_title()):__tostring())
63+
table.insert(completions, tostring(Plain:new(headline:get_title())))
6464
end
6565

6666
return completions

lua/orgmode/org/links/handlers/file.lua

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,25 @@ local Id = require('orgmode.org.links.handlers.id')
44
local Internal = require('orgmode.org.links.handlers.internal')
55

66
---@class OrgLinkHandlerFile:OrgLinkHandler
7-
---@field new fun(self: OrgLinkHandlerFile, path: string, target: OrgLinkHandlerInternal | nil, prefix: boolean | nil): OrgLinkHandlerFile
7+
---@field new fun(self: OrgLinkHandlerFile, path: string, target: OrgLinkHandlerInternal | nil, prefix: boolean | nil, files: OrgFile[]): OrgLinkHandlerFile
88
---@field parse fun(link: string, prefix: boolean | nil): OrgLinkHandlerFile | nil
99
---@field path string
1010
---@field skip_prefix boolean
1111
---@field target OrgLinkHandlerInternal | nil
1212
local File = Link:new('file')
1313

14-
function File:new(path, target, skip_prefix)
14+
function File:new(path, target, skip_prefix, files)
1515
---@class OrgLinkHandlerFile
1616
local this = Link:new()
1717
this.skip_prefix = skip_prefix or false
1818
this.path = path
1919
this.target = target
20+
this.files = files
2021
setmetatable(this, self)
2122
self.__index = self
2223
return this
2324
end
2425

25-
function File.parse(input, skip_prefix)
26-
if input == nil or #input == 0 then
27-
return nil
28-
end
29-
local deliniator_start, deliniator_stop = input:find('::')
30-
31-
---@type OrgLinkHandlerInternal | nil
32-
local target = nil
33-
local path = input
34-
35-
if deliniator_start then
36-
---@class OrgLinkHandlerInternal | nil
37-
target = Internal.parse(input:sub(deliniator_stop + 1), true)
38-
path = input:sub(0, deliniator_start - 1)
39-
end
40-
41-
return File:new(path, target, skip_prefix)
42-
end
43-
4426
-- TODO make protocol prefix optional. Based on what?
4527
function File:__tostring()
4628
local v = ''
@@ -65,9 +47,8 @@ function File:follow()
6547
end
6648
end
6749

68-
local function autocompletions_filenames(lead)
69-
local Org = require('orgmode')
70-
local filenames = Org.files:filenames()
50+
function File:_autocompletions_filenames(lead)
51+
local filenames = self.files:filenames()
7152

7253
local matches = {}
7354
for _, f in ipairs(filenames) do
@@ -82,24 +63,6 @@ local function autocompletions_filenames(lead)
8263
return matches
8364
end
8465

85-
function File:resolve()
86-
local Org = require('orgmode')
87-
local path = fs.get_real_path(self.path)
88-
if not path then
89-
return self
90-
end
91-
local file = Org.files:get(path)
92-
if not file then
93-
return self
94-
end
95-
local id = file:get_property('id')
96-
if not id then
97-
return self
98-
end
99-
100-
return Id:new(id, self.target):resolve()
101-
end
102-
10366
function File:insert_description()
10467
local Org = require('orgmode')
10568
if self.target then
@@ -118,7 +81,36 @@ function File:insert_description()
11881
return file:get_title()
11982
end
12083

121-
function File:complete(lead, context)
84+
local FileFactory = {}
85+
86+
function FileFactory:init(files)
87+
self.files = files
88+
end
89+
90+
function FileFactory:new(path, target, skip_prefix)
91+
return File:new(path, target, skip_prefix, self.files)
92+
end
93+
94+
function FileFactory:parse(input, skip_prefix)
95+
if input == nil or #input == 0 then
96+
return nil
97+
end
98+
local deliniator_start, deliniator_stop = input:find('::')
99+
100+
---@type OrgLinkHandlerInternal | nil
101+
local target = nil
102+
local path = input
103+
104+
if deliniator_start then
105+
---@class OrgLinkHandlerInternal | nil
106+
target = Internal.parse(input:sub(deliniator_stop + 1), true)
107+
path = input:sub(0, deliniator_start - 1)
108+
end
109+
110+
self:new(path, target, skip_prefix)
111+
end
112+
113+
function FileFactory:complete(lead, context)
122114
context = context or {}
123115
local deliniator_start, deliniator_stop = lead:find('::')
124116

@@ -131,16 +123,16 @@ function File:complete(lead, context)
131123
end
132124
end
133125

134-
function File:_complete(lead, context)
126+
function FileFactory:_complete(lead, context)
135127
return vim.tbl_map(function(f)
136-
return self:new(f, nil, context.skip_prefix):__tostring()
137-
end, autocompletions_filenames(lead))
128+
return tostring(self:new(f, nil, context.skip_prefix))
129+
end, self:_autocompletions_filenames(lead))
138130
end
139131

140-
function File:_complete_targets(path, target_lead, context)
132+
function FileFactory:_complete_targets(path, target_lead, context)
141133
return vim.tbl_map(
142134
function(t)
143-
return self:new(path, t, context.skip_prefix):__tostring()
135+
return tostring(self:new(path, t, context.skip_prefix))
144136
end,
145137
Internal:complete(target_lead, {
146138
filename = fs.get_real_path(path),
@@ -149,4 +141,4 @@ function File:_complete_targets(path, target_lead, context)
149141
)
150142
end
151143

152-
return File
144+
return FileFactory

lua/orgmode/org/links/handlers/http.lua

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,33 @@ function Http:new(url)
1313
return this
1414
end
1515

16-
---@param input string
17-
function Http.parse(input)
18-
return Http:new(input:gsub('^/*', ''))
19-
end
20-
2116
function Http:__tostring()
2217
return string.format('%s://%s', self.protocol, self.url)
2318
end
2419

2520
function Http:follow()
2621
if vim.ui['open'] then
27-
return vim.ui.open(self:__tostring())
22+
return vim.ui.open(tostring(self))
2823
end
2924
if not vim.g.loaded_netrwPlugin then
3025
return utils.echo_warning('Netrw plugin must be loaded in order to open urls.')
3126
end
32-
return vim.fn['netrw#BrowseX'](self:__tostring(), vim.fn['netrw#CheckIfRemote']())
27+
return vim.fn['netrw#BrowseX'](tostring(self), vim.fn['netrw#CheckIfRemote']())
28+
end
29+
30+
local HttpFactory = {}
31+
32+
function HttpFactory:new()
33+
---@class OrgLinkHandlerId
34+
local this = {}
35+
setmetatable(this, self)
36+
self.__index = self
37+
return this
38+
end
39+
40+
---@param input string
41+
function HttpFactory.parse(input)
42+
return HttpFactory:new(input:gsub('^/*', ''))
3343
end
3444

3545
return Http

lua/orgmode/org/links/handlers/https.lua

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,33 @@ function Https:new(url)
1313
return this
1414
end
1515

16-
---@param input string
17-
function Https.parse(input)
18-
return Https:new(input:gsub('^/*', ''))
19-
end
20-
2116
function Https:__tostring()
2217
return string.format('%s://%s', self.protocol, self.url)
2318
end
2419

2520
function Https:follow()
2621
if vim.ui['open'] then
27-
return vim.ui.open(self:__tostring())
22+
return vim.ui.open(tostring(self))
2823
end
2924
if not vim.g.loaded_netrwPlugin then
3025
return utils.echo_warning('Netrw plugin must be loaded in order to open urls.')
3126
end
32-
return vim.fn['netrw#BrowseX'](self:__tostring(), vim.fn['netrw#CheckIfRemote']())
27+
return vim.fn['netrw#BrowseX'](tostring(self), vim.fn['netrw#CheckIfRemote']())
28+
end
29+
30+
local HttpsFactory = {}
31+
32+
function HttpsFactory:new()
33+
---@class OrgLinkHandlerId
34+
local this = {}
35+
setmetatable(this, self)
36+
self.__index = self
37+
return this
38+
end
39+
40+
---@param input string
41+
function HttpsFactory.parse(input)
42+
return HttpsFactory:new(input:gsub('^/*', ''))
3343
end
3444

35-
return Https
45+
return HttpsFactory

0 commit comments

Comments
 (0)