From c5579dfc2fb36119dfe3703945096521a260236f Mon Sep 17 00:00:00 2001 From: Tyler Jang Date: Wed, 18 Oct 2023 14:39:18 -0700 Subject: [PATCH 1/4] fix windows format on save and log file --- .trunk/.gitignore | 16 ++++++++-------- .trunk/trunk.yaml | 2 +- lua/log.lua | 2 +- lua/trunk.lua | 37 ++++++++++++++++++++++++++++++------- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/.trunk/.gitignore b/.trunk/.gitignore index 1e24652..fc71dfe 100644 --- a/.trunk/.gitignore +++ b/.trunk/.gitignore @@ -1,8 +1,8 @@ -*out -*logs -*actions -*notifications -*tools -plugins -user_trunk.yaml -user.yaml +*out +*logs +*actions +*notifications +*tools +plugins +user_trunk.yaml +user.yaml 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/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..b3f6bd9 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, From 1c3b800ea505e5c6ad9ef7d464cbf8a10651a05b Mon Sep 17 00:00:00 2001 From: Tyler Jang Date: Wed, 18 Oct 2023 14:41:40 -0700 Subject: [PATCH 2/4] unix style gitignore --- .trunk/.gitignore | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.trunk/.gitignore b/.trunk/.gitignore index fc71dfe..1e24652 100644 --- a/.trunk/.gitignore +++ b/.trunk/.gitignore @@ -1,8 +1,8 @@ -*out -*logs -*actions -*notifications -*tools -plugins -user_trunk.yaml -user.yaml +*out +*logs +*actions +*notifications +*tools +plugins +user_trunk.yaml +user.yaml From 87624d1cdd5d77ee2e34714293bf2ff84c0fa821 Mon Sep 17 00:00:00 2001 From: Tyler Jang Date: Wed, 18 Oct 2023 14:42:20 -0700 Subject: [PATCH 3/4] lint --- lua/trunk.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/trunk.lua b/lua/trunk.lua index b3f6bd9..41fa19a 100644 --- a/lua/trunk.lua +++ b/lua/trunk.lua @@ -278,7 +278,7 @@ local function start() -- TODO(Tyler): Handle carriage returns correctly here. -- NOTE(Tyler): Powershell does not have && and || so we must use cmd /c formatCommand = ( - ":% ! cmd /c \"tee " + ':% ! cmd /c "tee ' .. tmpFile .. " | " .. table.concat(executionTrunkPath(), " ") @@ -288,7 +288,7 @@ local function start() .. tmpFormattedFile .. " || cat " .. tmpFile - .. "\"" + .. '"' ) elseif timeoutResult:len() == 0 then logger.debug("Formatting without timeout") From ae4b5431a49782b16bdda69cf975050b648cc1a7 Mon Sep 17 00:00:00 2001 From: Tyler Jang Date: Wed, 18 Oct 2023 14:44:44 -0700 Subject: [PATCH 4/4] fix docs --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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.