Skip to content
Merged
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
24 changes: 15 additions & 9 deletions lua/swift/features/formatter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ function M.format_with_swift_format(bufnr)

table.insert(cmd, "-")

local result = vim.fn.system(cmd, content)
local exit_code = vim.v.shell_error
local obj = vim.system(cmd, { text = true, stdin = content, timeout = 5000 }):wait()
local result = obj.stdout or ""
local exit_code = obj.code

if exit_code ~= 0 then
vim.notify("swift-format failed: " .. result, vim.log.levels.ERROR, { title = "swift.nvim" })
local err_msg = obj.stderr or result
vim.notify("swift-format failed: " .. err_msg, vim.log.levels.ERROR, { title = "swift.nvim" })
return false
end

Expand Down Expand Up @@ -179,11 +181,13 @@ function M.format_with_swiftformat(bufnr)
table.insert(cmd, config_file)
end

local result = vim.fn.system(cmd)
local exit_code = vim.v.shell_error
local obj = vim.system(cmd, { text = true, timeout = 5000 }):wait()
local result = obj.stdout or ""
local exit_code = obj.code

if exit_code ~= 0 then
vim.notify("swiftformat failed: " .. result, vim.log.levels.ERROR, { title = "swift.nvim" })
local err_msg = obj.stderr or result
vim.notify("swiftformat failed: " .. err_msg, vim.log.levels.ERROR, { title = "swift.nvim" })
return false
end

Expand Down Expand Up @@ -274,11 +278,13 @@ function M.format_selection()
table.insert(cmd, start_offset .. ":" .. end_offset)
table.insert(cmd, "-")

local result = vim.fn.system(cmd, content)
local exit_code = vim.v.shell_error
local obj = vim.system(cmd, { text = true, stdin = content, timeout = 5000 }):wait()
local result = obj.stdout or ""
local exit_code = obj.code

if exit_code ~= 0 then
vim.notify("swift-format failed: " .. result, vim.log.levels.ERROR, { title = "swift.nvim" })
local err_msg = obj.stderr or result
vim.notify("swift-format failed: " .. err_msg, vim.log.levels.ERROR, { title = "swift.nvim" })
return
end

Expand Down
9 changes: 5 additions & 4 deletions lua/swift/features/linter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ function M.lint_file(filename)
table.insert(cmd, config_file)
end

local result = vim.fn.system(cmd)
return M.parse_swiftlint_output(result)
local obj = vim.system(cmd, { text = true, timeout = 5000 }):wait()
return M.parse_swiftlint_output(obj.stdout or "")
end

-- Lint current buffer
Expand Down Expand Up @@ -201,8 +201,9 @@ function M.fix(filename)
table.insert(cmd, config_file)
end

local result = vim.fn.system(cmd)
local exit_code = vim.v.shell_error
local obj = vim.system(cmd, { text = true, timeout = 5000 }):wait()
local result = obj.stdout or ""
local exit_code = obj.code

-- Reload buffer to see changes
vim.cmd("silent! edit!")
Expand Down
3 changes: 2 additions & 1 deletion lua/swift/features/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function M.find_sourcekit_lsp()

-- Try Xcode toolchain
if vim.fn.has("mac") == 1 then
local xcrun_path = vim.fn.system("xcrun --find sourcekit-lsp 2>/dev/null"):gsub("\n", "")
local xcrun_path = vim.system({ "xcrun", "--find", "sourcekit-lsp" }, { text = true }):wait().stdout or ""
xcrun_path = xcrun_path:gsub("\n", "")
if xcrun_path ~= "" and vim.fn.executable(xcrun_path) == 1 then
table.insert(possible_paths, 1, xcrun_path)
end
Expand Down
14 changes: 8 additions & 6 deletions lua/swift/features/target_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ function M.parse_spm_targets()
end

-- First, try using swift package dump-package (most reliable)
local cmd = string.format('cd "%s" && swift package dump-package 2>/dev/null', project_info.root)
local output = vim.fn.system(cmd)
local sh_cmd = { "sh", "-c", string.format('cd "%s" && swift package dump-package 2>/dev/null', project_info.root) }
local obj = vim.system(sh_cmd, { text = true, timeout = 5000 }):wait()
local output = obj.stdout or ""

if vim.v.shell_error == 0 and output ~= "" then
if obj.code == 0 and output ~= "" then
-- Parse JSON output
local ok, package_data = pcall(vim.json.decode, output)
if ok and package_data and package_data.targets then
Expand Down Expand Up @@ -139,10 +140,11 @@ function M.parse_xcode_targets()
end

-- Use xcodebuild to list schemes (which correspond to targets)
local cmd = string.format('cd "%s" && xcodebuild -list -json 2>/dev/null', vim.fn.fnamemodify(project_file, ":h"))
local output = vim.fn.system(cmd)
local sh_cmd = { "sh", "-c", string.format('cd "%s" && xcodebuild -list -json 2>/dev/null', vim.fn.fnamemodify(project_file, ":h")) }
local obj = vim.system(sh_cmd, { text = true, timeout = 5000 }):wait()
local output = obj.stdout or ""

if vim.v.shell_error ~= 0 then
if obj.code ~= 0 then
return nil
end

Expand Down
18 changes: 11 additions & 7 deletions lua/swift/features/xcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ function M.list_schemes()
project_file = info.project
end

local cmd = string.format(
"xcodebuild -list -workspace %s 2>/dev/null || xcodebuild -list -project %s 2>/dev/null",
vim.fn.shellescape(project_file),
vim.fn.shellescape(project_file)
)
local sh_cmd = {
"sh", "-c",
string.format(
"xcodebuild -list -workspace %s 2>/dev/null || xcodebuild -list -project %s 2>/dev/null",
vim.fn.shellescape(project_file),
vim.fn.shellescape(project_file)
)
}

local output = vim.fn.system(cmd)
local obj = vim.system(sh_cmd, { text = true, timeout = 5000 }):wait()
local output = obj.stdout or ""
local schemes = {}
local in_schemes = false

Expand Down Expand Up @@ -272,7 +276,7 @@ function M.open_in_xcode()
file_to_open = info.project
end

vim.fn.system("open " .. vim.fn.shellescape(file_to_open))
vim.system({ "open", file_to_open })
vim.notify("Opening in Xcode.app", vim.log.levels.INFO, { title = "swift.nvim" })
end

Expand Down
2 changes: 1 addition & 1 deletion lua/swift/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function M.check()
end
end
else
local version = vim.fn.system("swift --version 2>&1 | head -n 1")
local version = vim.system({ "sh", "-c", "swift --version 2>&1 | head -n 1" }, { text = true, timeout = 5000 }):wait().stdout or ""
health.info("Version: " .. vim.trim(version))
end
else
Expand Down
6 changes: 3 additions & 3 deletions lua/swift/version_validator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end

-- Get installed Swift version
function M.get_installed_swift_version()
local output = vim.fn.system("swift --version 2>&1")
local output = vim.system({ "sh", "-c", "swift --version 2>&1" }, { text = true, timeout = 5000 }):wait().stdout or ""

if vim.v.shell_error ~= 0 then
return nil
Expand Down Expand Up @@ -142,7 +142,7 @@ function M.list_swiftly_versions()
return nil
end

local output = vim.fn.system("swiftly list 2>/dev/null")
local output = vim.system({ "sh", "-c", "swiftly list 2>/dev/null" }, { text = true, timeout = 5000 }):wait().stdout or ""

if vim.v.shell_error ~= 0 then
return nil
Expand Down Expand Up @@ -198,7 +198,7 @@ function M.get_swift_format_version()
return nil
end

local output = vim.fn.system(swift_format .. " --version 2>&1")
local output = vim.system({ "sh", "-c", swift_format .. " --version 2>&1" }, { text = true, timeout = 5000 }):wait().stdout or ""

if vim.v.shell_error ~= 0 then
return nil
Expand Down
Loading