Skip to content

Commit

Permalink
Merge pull request #10 from trunk-io/tyler/fix-windows
Browse files Browse the repository at this point in the history
Fix windows format on save and log file
  • Loading branch information
TylerJang27 authored Oct 18, 2023
2 parents 654c2aa + ae4b543 commit 8e16085
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 0.1
cli:
version: 1.17.0
version: 1.17.1
options:
- commands: [upgrade]
args: -y --no-progress
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ Neovim plugins are setup as follows:
- [trunk.lua](lua/trunk.lua), which defines global state for the lifetime of the plugin, provides
functionality for each of the vimscript commands, and launches the Trunk LSP server.
- [log.lua](lua/log.lua), which manages logging, which is written to `.trunk/logs/neovim.log` and
is flushed periodically. When run from outside of a Trunk repo, this log is written to
`/tmp/neovim_trunk.log`.
is flushed periodically. When run from outside of a Trunk repo, this log is written to a
tempfile.

These files interface with the built-in [Neovim LSP framework](https://neovim.io/doc/user/lsp.html)
to provide inline diagnostics and other features.
2 changes: 1 addition & 1 deletion lua/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ local default_config = {
use_file = true,

-- Default file to write
out_file = findWorkspace() and findWorkspace() .. "/.trunk/logs/neovim.log" or "/tmp/neovim_trunk.log",
out_file = findWorkspace() and findWorkspace() .. "/.trunk/logs/neovim.log" or os.tmpname(),

-- Any messages above this level will be logged.
level = "info",
Expand Down
37 changes: 30 additions & 7 deletions lua/trunk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,40 @@ local function start()
local cursor = vim.api.nvim_win_get_cursor(0)
local filename = vim.api.nvim_buf_get_name(0)
local workspace = findWorkspace()
if is_win() then
workspace = workspace:gsub("/", "\\")
end
-- if filename doesn't start with workspace
if workspace == nil or filename:sub(1, #workspace) ~= workspace then
return
end

local bufname = vim.fs.basename(vim.api.nvim_buf_get_name(0))
local handle = io.popen("command -v timeout")
local timeoutResult = handle:read("*a")
handle:close()
-- Stores current buffer in a temporary file in case trunk fmt fails so we don't overwrite the original buffer with an error message.
local tmpFile = "/tmp/.trunk-format-" .. bufname
local tmpFormattedFile = "/tmp/.trunk-formatted-" .. bufname
local tmpFile = os.tmpname()
local tmpFormattedFile = os.tmpname()
local formatCommand = ""
if is_win() or timeoutResult:len() == 0 then
if is_win() then
logger.debug("Formatting on Windows")
-- TODO(Tyler): Handle carriage returns correctly here.
-- NOTE(Tyler): Powershell does not have && and || so we must use cmd /c
formatCommand = (
':% ! cmd /c "tee '
.. tmpFile
.. " | "
.. table.concat(executionTrunkPath(), " ")
.. " format-stdin %:p >"
.. tmpFormattedFile
.. " && cat "
.. tmpFormattedFile
.. " || cat "
.. tmpFile
.. '"'
)
elseif timeoutResult:len() == 0 then
logger.debug("Formatting without timeout")
formatCommand = (
":% !tee "
Expand All @@ -278,9 +299,9 @@ local function start()
.. table.concat(executionTrunkPath(), " ")
.. " format-stdin %:p >"
.. tmpFormattedFile
.. "&& cat "
.. " && cat "
.. tmpFormattedFile
.. "|| cat "
.. " || cat "
.. tmpFile
)
else
Expand All @@ -294,15 +315,17 @@ local function start()
.. table.concat(executionTrunkPath(), " ")
.. " format-stdin %:p >"
.. tmpFormattedFile
.. "&& cat "
.. " && cat "
.. tmpFormattedFile
.. "|| cat "
.. " || cat "
.. tmpFile
)
end
logger.debug("Format command: " .. formatCommand)
vim.cmd(formatCommand)
local line_count = vim.api.nvim_buf_line_count(0)
os.remove(tmpFile)
os.remove(tmpFormattedFile)
vim.api.nvim_win_set_cursor(0, { math.min(cursor[1], line_count), cursor[2] })
end
end,
Expand Down

0 comments on commit 8e16085

Please sign in to comment.