Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ccaglak committed Oct 17, 2024
1 parent 887b36e commit fdeeb88
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
10 changes: 8 additions & 2 deletions lua/namespace/composer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,21 @@ end

function N.get_insertion_point()
local content = api.nvim_buf_get_lines(0, 0, -1, false)
if #content == 0 then
return nil
end

local insertion_point = 2

for i, line in ipairs(content) do
if vim.fn.match(line, "^\\(declare\\)") >= 0 then
insertion_point = i
elseif vim.fn.match(line, "^\\(namespace\\)") >= 0 then
return i, vim.fn.match(line, "^\\(namespace\\)")
elseif vim.fn.match(line, "^\\(use\\|class\\|final\\|interface\\|abstract\\|trait\\|enum\\)") >= 0 then
return insertion_point
elseif vim.fn.match(line, "^\\(use\\)") >= 0 then
return insertion_point, nil
elseif vim.fn.match(line, "^\\(class\\|final\\|interface\\|abstract\\|trait\\|enum\\)") >= 0 then
return insertion_point, nil
end
end

Expand Down
16 changes: 8 additions & 8 deletions lua/namespace/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ local function transform_path(path, prefix_table, workspace_root, composer)
end

local relative_path = path:gsub(workspace_root, "")
relative_path = relative_path:gsub("^/", "") -- first slash
relative_path = relative_path:gsub("^/", "") -- first slash
relative_path = relative_path:gsub("^\\", "") -- first slash
relative_path = relative_path:gsub("\\\\", "\\")
relative_path = relative_path:gsub(sep, "\\")
Expand Down Expand Up @@ -233,12 +233,12 @@ local function get_insertion_point()
end

if
line:find("^class")
or line:find("^final")
or line:find("^interface")
or line:find("^abstract")
or line:find("^trait")
or line:find("^enum")
line:find("^class")
or line:find("^final")
or line:find("^interface")
or line:find("^abstract")
or line:find("^trait")
or line:find("^enum")
then
break
end
Expand Down Expand Up @@ -278,7 +278,7 @@ local function process_file_search(class_entry, prefix, workspace_root, current_
if files and #files == 1 then
matching_files = vim.tbl_filter(function(file)
return file:match(class_entry.name:gsub("\\", "/") .. ".php$")
and vim.fn.fnamemodify(file, ":h") ~= current_directory:sub(2)
and vim.fn.fnamemodify(file, ":h") ~= current_directory:sub(2)
end, files)
else
matching_files = files
Expand Down
20 changes: 1 addition & 19 deletions tests/namespace/composer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,6 @@ describe("composer", function()
vim.api.nvim_buf_get_lines = original_nvim_buf_get_lines
end)

it("should return insertion point before namespace", function()
vim.api.nvim_buf_get_lines.returns({
"<?php",
"",
"namespace App\\Test;",
"",
"class TestClass",
"{",
"}",
})

local line, col = namespace.get_insertion_point()

assert.are.equal(3, line)
assert.are.equal(1, col)
end)

it("should return insertion point before class when no namespace is present", function()
vim.api.nvim_buf_get_lines.returns({
"<?php",
Expand Down Expand Up @@ -266,10 +249,9 @@ describe("composer", function()
"}",
})

local line, col = namespace.get_insertion_point()
local line, _ = namespace.get_insertion_point()

assert.are.equal(5, line)
assert.are.equal(1, col)
end)

it("should return insertion point before interface", function()
Expand Down

0 comments on commit fdeeb88

Please sign in to comment.