Skip to content

Commit

Permalink
fix: added support for module names with forward slashes instead of dots
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 2, 2024
1 parent 6aa003e commit 3c9423a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
13 changes: 7 additions & 6 deletions lua/lazydev/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local Pkg = require("lazydev.pkg")
local Source = {}

function Source:get_trigger_characters()
return { '"', "'", "." }
return { '"', "'", ".", "/" }
end

---@param params cmp.SourceCompletionApiParams
Expand All @@ -13,7 +13,7 @@ function Source:complete(params, callback)
local cmp = require("cmp")
local before = params.context.cursor_before_line
---@type string?
local req = Pkg.get_module(before, { before = true })
local req, forward_slash = Pkg.get_module(before, { before = true })
if not req then
return callback({})
end
Expand All @@ -22,10 +22,11 @@ function Source:complete(params, callback)
---@param modname string
---@param modpath string
local function add(modname, modpath)
items[modname] = items[modname] or {
label = modname,
kind = cmp.lsp.CompletionItemKind.Module,
}
items[modname] = items[modname]
or {
label = forward_slash and modname:gsub("%.", "/") or modname,
kind = cmp.lsp.CompletionItemKind.Module,
}
local item = items[modname]

local plugin = Pkg.get_plugin_name(modpath)
Expand Down
8 changes: 4 additions & 4 deletions lua/lazydev/pkg.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---@class lazydev.Pkg
local M = {}

M.PAT_MODULE_BASE = "%-%-%-%s*@module%s*[\"']([%w%.%-_]+)"
M.PAT_REQUIRE_BASE = "require%s*%(?%s*['\"]([%w%.%-_]+)"
M.PAT_MODULE_BASE = "%-%-%-%s*@module%s*[\"']([%w%.%-_/]+)"
M.PAT_REQUIRE_BASE = "require%s*%(?%s*['\"]([%w%.%-_/]+)"
M.PAT_MODULE_BEFORE = M.PAT_MODULE_BASE .. "$"
M.PAT_REQUIRE_BEFORE = M.PAT_REQUIRE_BASE .. "$"
M.PAT_MODULE = M.PAT_MODULE_BASE .. "[\"']"
Expand Down Expand Up @@ -80,7 +80,7 @@ end
--- either `---@module "modname"` or `require "modname"`
---@param line string
---@param opts? {before?:boolean}
---@return string?
---@return string?, boolean? forward_slash
function M.get_module(line, opts)
local patterns = opts and opts.before and {
M.PAT_MODULE_BEFORE,
Expand All @@ -92,7 +92,7 @@ function M.get_module(line, opts)
for _, pat in ipairs(patterns) do
local match = line:match(pat)
if match then
return match
return match:gsub("/", "."), match:find("/", 1, true)
end
end
end
Expand Down

0 comments on commit 3c9423a

Please sign in to comment.