Skip to content
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: 10 additions & 8 deletions lua/roslyn/sln/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ function M.find_files_with_extensions(dir, extensions)
end

---@param targets string[]
---@param csproj? string
---@param csproj_files string[]
---@return string[]
local function filter_targets(targets, csproj)
local function filter_targets(targets, csproj_files)
local config = require("roslyn.config").get()
return vim.iter(targets)
:filter(function(target)
if config.ignore_target and config.ignore_target(target) then
return false
end

return not csproj or sln_api.exists_in_target(target, csproj)
return vim.iter(csproj_files):any(function(csproj_path)
return sln_api.exists_in_target(target, csproj_path)
end)
end)
:totable()
end
Expand Down Expand Up @@ -116,11 +118,11 @@ function M.find_solutions_broad(bufnr)
end

---@param bufnr number
---@return string?
---@return string[]
local function find_csproj_file(bufnr)
return vim.fs.find(function(name)
return name:match("%.csproj$") ~= nil
end, { upward = true, path = vim.api.nvim_buf_get_name(bufnr) })[1]
end, { upward = true, path = vim.api.nvim_buf_get_name(bufnr), limit = math.huge })
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause it to search a bit more, but I don't believe this is something that will be noticed in the performance since it is searching upward

end

---@param bufnr number
Expand All @@ -133,9 +135,9 @@ function M.root_dir(bufnr)
return vim.fs.dirname(solutions[1])
end

local csproj = find_csproj_file(bufnr)
local csproj_files = find_csproj_file(bufnr)

local filtered_targets = filter_targets(solutions, csproj)
local filtered_targets = filter_targets(solutions, csproj_files)
if #filtered_targets > 1 then
local chosen = config.choose_target and config.choose_target(filtered_targets)
if chosen then
Expand Down Expand Up @@ -169,7 +171,7 @@ function M.root_dir(bufnr)
local selected_solution = vim.g.roslyn_nvim_selected_solution
return vim.fs.dirname(filtered_targets[1])
or selected_solution and vim.fs.dirname(selected_solution)
or csproj and vim.fs.dirname(csproj)
or csproj_files[1] and vim.fs.dirname(csproj_files[1])
end

---@param bufnr number
Expand Down
20 changes: 20 additions & 0 deletions test/lsp_integration_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ describe("LSP integration with mock server", function()
assert.are_equal("Locked.sln", selected)
end)

it("handles multiple csproj files when only one exist in sln", function()
create_sln_file("Foo.sln", { { name = "Bar", path = "Foo/Foo.csproj" } })
create_sln_file("Baz.sln", { { name = "Bar", path = "Bar/Bar.csproj" } })

create_file("Foo/Foo.csproj")
create_file("Foo/Test/Program.cs")

-- csproj file that doesn't belong to any solution should be ignored
create_file("Foo/Test/Foo.csproj")

create_file("Bar/Bar.csproj")
create_file("Bar/Program.cs")

command("edit " .. vim.fs.joinpath(helpers.scratch, "Foo", "Test", "Program.cs"))

local clients = get_lsp_clients()
assert.are_equal(1, #clients)
assert.are_equal(vim.fs.joinpath(scratch), clients[1].root_dir)
end)

it("finds solution with broad_search enabled", function()
helpers.exec_lua(function()
require("roslyn.config").setup({ broad_search = true })
Expand Down
Loading