Skip to content

Cmp completion filename #854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lua/cmp_obsidian.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ source.complete = function(_, request, callback)
if option.label then
new_text = client:format_link(
note,
{ label = option.label, link_style = link_style, anchor = option.anchor, block = option.block }
{
label = option.label,
link_style = link_style,
anchor = option.anchor,
block = option.block,
linkContent = client.opts.preferred_link_content
}
)

final_label = assert(option.alt_label or option.label)
Expand Down Expand Up @@ -215,7 +221,7 @@ source.complete = function(_, request, callback)
new_text_to_option[new_text].sort_text = new_text_to_option[new_text].sort_text .. " " .. sort_text
else
new_text_to_option[new_text] =
{ label = final_label, new_text = new_text, sort_text = sort_text, documentation = documentation }
{ label = final_label, new_text = new_text, sort_text = sort_text, documentation = documentation }
end
end
end
Expand All @@ -231,16 +237,16 @@ source.complete = function(_, request, callback)
if note.title ~= nil then
table.insert(aliases, note.title)
end
if note.path.filename then
table.insert(aliases, note.path.filename:match("([^/]+)%.%w+$"))
end
end

for alias in iter(aliases) do
update_completion_options(alias)
local alias_case_matched = util.match_case(search, alias)

if
alias_case_matched ~= nil
and alias_case_matched ~= alias
and not util.tbl_contains(note.aliases, alias_case_matched)
if alias_case_matched ~= nil and alias_case_matched ~= alias and not util.tbl_contains(note.aliases, alias_case_matched)
then
update_completion_options(alias_case_matched)
end
Expand Down
44 changes: 26 additions & 18 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1183,15 +1183,15 @@ Client.find_tags_async = function(self, term, callback, opts)
search_terms[#search_terms + 1] = "#" .. search.Patterns.TagCharsOptional .. t .. search.Patterns.TagCharsOptional
-- frontmatter tag in multiline list
search_terms[#search_terms + 1] = "\\s*- "
.. search.Patterns.TagCharsOptional
.. t
.. search.Patterns.TagCharsOptional
.. "$"
.. search.Patterns.TagCharsOptional
.. t
.. search.Patterns.TagCharsOptional
.. "$"
-- frontmatter tag in inline list
search_terms[#search_terms + 1] = "tags: .*"
.. search.Patterns.TagCharsOptional
.. t
.. search.Patterns.TagCharsOptional
.. search.Patterns.TagCharsOptional
.. t
.. search.Patterns.TagCharsOptional
else
-- tag in the wild
search_terms[#search_terms + 1] = "#" .. search.Patterns.TagCharsRequired
Expand Down Expand Up @@ -1313,11 +1313,11 @@ Client.find_backlinks_async = function(self, note, callback, opts)
local note_path = Path.new(note.path)
for raw_ref in iter { tostring(note.id), note_path.name, note_path.stem, self:vault_relative_path(note.path) } do
for ref in
iter(util.tbl_unique {
raw_ref,
util.urlencode(tostring(raw_ref)),
util.urlencode(tostring(raw_ref), { keep_path_sep = true }),
})
iter(util.tbl_unique {
raw_ref,
util.urlencode(tostring(raw_ref)),
util.urlencode(tostring(raw_ref), { keep_path_sep = true }),
})
do
if ref ~= nil then
if anchor == nil and block == nil then
Expand Down Expand Up @@ -1708,11 +1708,11 @@ Client.parse_title_id_path = function(self, title, id, dir)
else
local bufpath = Path.buffer(0):resolve()
if
self.opts.new_notes_location == config.NewNotesLocation.current_dir
-- note is actually in the workspace.
and self.dir:is_parent_of(bufpath)
-- note is not in dailies folder
and (self.opts.daily_notes.folder == nil or not (self.dir / self.opts.daily_notes.folder):is_parent_of(bufpath))
self.opts.new_notes_location == config.NewNotesLocation.current_dir
-- note is actually in the workspace.
and self.dir:is_parent_of(bufpath)
-- note is not in dailies folder
and (self.opts.daily_notes.folder == nil or not (self.dir / self.opts.daily_notes.folder):is_parent_of(bufpath))
then
base_dir = self.buf_dir or assert(bufpath:parent())
else
Expand Down Expand Up @@ -2054,7 +2054,15 @@ Client.format_link = function(self, note, opts)
link_style = self.opts.preferred_link_style
end

local new_opts = { path = rel_path, label = label, id = note_id, anchor = opts.anchor, block = opts.block }
local new_opts = {
path = rel_path,
label = label,
id = note_id,
anchor = opts.anchor,
block = opts.block,
linkContent =
opts.linkContent
}

if link_style == config.LinkStyle.markdown then
return self.opts.markdown_link_func(new_opts)
Expand Down
18 changes: 13 additions & 5 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local config = {}
---@field wiki_link_func (fun(opts: {path: string, label: string, id: string|?}): string)
---@field markdown_link_func (fun(opts: {path: string, label: string, id: string|?}): string)
---@field preferred_link_style obsidian.config.LinkStyle
---@field preferred_link_content obsidian.config.LinkContent
---@field follow_url_func fun(url: string)|?
---@field follow_img_func fun(img: string)|?
---@field note_frontmatter_func (fun(note: obsidian.Note): table)|?
Expand Down Expand Up @@ -49,6 +50,7 @@ config.ClientOpts.default = function()
wiki_link_func = util.wiki_link_id_prefix,
markdown_link_func = util.markdown_link,
preferred_link_style = config.LinkStyle.wiki,
preferred_link_content = config.LinkContent.id,
follow_url_func = nil,
note_frontmatter_func = nil,
disable_frontmatter = false,
Expand Down Expand Up @@ -134,8 +136,8 @@ config.ClientOpts.normalize = function(opts, defaults)
if warn then
log.warn_once(
"The config options 'completion.prepend_note_id', 'completion.prepend_note_path', and 'completion.use_path_only' "
.. "are deprecated. Please use 'wiki_link_func' instead.\n"
.. "See https://github.com/epwalsh/obsidian.nvim/pull/406"
.. "are deprecated. Please use 'wiki_link_func' instead.\n"
.. "See https://github.com/epwalsh/obsidian.nvim/pull/406"
)
end
end
Expand All @@ -157,7 +159,7 @@ config.ClientOpts.normalize = function(opts, defaults)
opts.completion.preferred_link_style = nil
log.warn_once(
"The config option 'completion.preferred_link_style' is deprecated, please use the top-level "
.. "'preferred_link_style' instead."
.. "'preferred_link_style' instead."
)
end

Expand All @@ -166,15 +168,15 @@ config.ClientOpts.normalize = function(opts, defaults)
opts.completion.new_notes_location = nil
log.warn_once(
"The config option 'completion.new_notes_location' is deprecated, please use the top-level "
.. "'new_notes_location' instead."
.. "'new_notes_location' instead."
)
end

if opts.detect_cwd ~= nil then
opts.detect_cwd = nil
log.warn_once(
"The 'detect_cwd' field is deprecated and no longer has any affect.\n"
.. "See https://github.com/epwalsh/obsidian.nvim/pull/366 for more details."
.. "See https://github.com/epwalsh/obsidian.nvim/pull/366 for more details."
)
end

Expand Down Expand Up @@ -277,6 +279,12 @@ config.LinkStyle = {
markdown = "markdown",
}

---@enum obsidian.config.LinkContent
config.LinkContent = {
id = "id",
filename = "filename",
}

---@class obsidian.config.CompletionOpts
---
---@field nvim_cmp boolean
Expand Down
50 changes: 28 additions & 22 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ util.is_url = function(s)
local search = require "obsidian.search"

if
string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.NakedUrl] .. "$")
or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.FileUrl] .. "$")
or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.MailtoUrl] .. "$")
string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.NakedUrl] .. "$")
or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.FileUrl] .. "$")
or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.MailtoUrl] .. "$")
then
return true
else
Expand Down Expand Up @@ -596,11 +596,11 @@ util.cursor_on_markdown_link = function(line, col, include_naked_urls, include_f
cur_col = col or cur_col + 1 -- nvim_win_get_cursor returns 0-indexed column

for match in
iter(search.find_refs(current_line, {
include_naked_urls = include_naked_urls,
include_file_urls = include_file_urls,
include_block_ids = include_block_ids,
}))
iter(search.find_refs(current_line, {
include_naked_urls = include_naked_urls,
include_file_urls = include_file_urls,
include_block_ids = include_block_ids,
}))
do
local open, close, m_type = unpack(match)
if open <= cur_col and cur_col <= close then
Expand Down Expand Up @@ -669,11 +669,11 @@ util.parse_link = function(link, opts)
local link_type = opts.link_type
if link_type == nil then
for match in
iter(search.find_refs(link, {
include_naked_urls = opts.include_naked_urls,
include_file_urls = opts.include_file_urls,
include_block_ids = opts.include_block_ids,
}))
iter(search.find_refs(link, {
include_naked_urls = opts.include_naked_urls,
include_file_urls = opts.include_file_urls,
include_block_ids = opts.include_block_ids,
}))
do
local _, _, m_type = unpack(match)
if m_type then
Expand Down Expand Up @@ -954,10 +954,10 @@ util.get_visual_selection = function(opts)
selection = string.sub(lines[1], cscol) .. "\n" .. string.sub(lines[n], 1, cecol)
else
selection = string.sub(lines[1], cscol)
.. "\n"
.. table.concat(lines, "\n", 2, n - 1)
.. "\n"
.. string.sub(lines[n], 1, cecol)
.. "\n"
.. table.concat(lines, "\n", 2, n - 1)
.. "\n"
.. string.sub(lines[n], 1, cecol)
end

return {
Expand Down Expand Up @@ -1027,6 +1027,8 @@ end
util.wiki_link_id_prefix = function(opts)
local anchor = ""
local header = ""
local base = ""
local filepath = opts.path:match("([^/]+)%.%w+$")
if opts.anchor then
anchor = opts.anchor.anchor
header = util.format_anchor_label(opts.anchor)
Expand All @@ -1035,12 +1037,16 @@ util.wiki_link_id_prefix = function(opts)
header = "#" .. opts.block.id
end

if opts.id == nil then
return string.format("[[%s%s]]", opts.label, anchor)
elseif opts.label ~= opts.id then
return string.format("[[%s%s|%s%s]]", opts.id, anchor, opts.label, header)
if opts.linkContent == "filename" or opts.id == nil then
base = filepath
else
base = tostring(opts.id)
end

if base ~= opts.label then
return string.format("[[%s%s|%s%s]]", base, anchor, opts.label, header)
else
return string.format("[[%s%s]]", opts.id, anchor)
return string.format("[[%s%s]]", base, anchor)
end
end

Expand Down
Loading