diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index 307b1d0..5550d60 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -1,6 +1,6 @@ version: 0.1 cli: - version: 1.17.0 + version: 1.17.1 options: - commands: [upgrade] args: -y --no-progress diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c3628ce..7c6fc9f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/lua/log.lua b/lua/log.lua index d31806b..70cb338 100644 --- a/lua/log.lua +++ b/lua/log.lua @@ -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", diff --git a/lua/trunk.lua b/lua/trunk.lua index 262ba28..41fa19a 100644 --- a/lua/trunk.lua +++ b/lua/trunk.lua @@ -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 " @@ -278,9 +299,9 @@ local function start() .. table.concat(executionTrunkPath(), " ") .. " format-stdin %:p >" .. tmpFormattedFile - .. "&& cat " + .. " && cat " .. tmpFormattedFile - .. "|| cat " + .. " || cat " .. tmpFile ) else @@ -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,