Skip to content

Commit

Permalink
Merge pull request #52 from ej-shafran/nightly
Browse files Browse the repository at this point in the history
Apply changes from nightly
  • Loading branch information
ej-shafran authored Jan 24, 2025
2 parents 4feef0b + 6e067d4 commit eda0d6f
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to

- The `CompileDebugError` command, which prints debug information about the error under the current line (along with a matching API function).

### Fixed

- The behavior of the compilation directory. The existing behavior was misleading and did not match the documentation or the Emacs plugin behavior. `:Compile` now uses the current working directory (`vim.fn.getcwd()`), unless the `vim.g.compilation_directory` global variable is set (in which case, `:Compile` unsets it once it's finished compiling); `:Recompile` now always uses the directory used by the last `:Compile`.

## [5.3.1] - 2024-10-30

### Added
Expand Down
10 changes: 9 additions & 1 deletion doc/compile-mode.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Table of Contents *compile-mode-table-of-contents*
- interrupt() |compile-mode.interrupt()|
7. Commands |compile-mode-commands|
- :Compile |:Compile|
- vim.g.compilation_directory |vim.g.compilation_directory|
- :Recompile |:Recompile|
- :FirstError |:FirstError|
- :CurrentError |:CurrentError|
Expand Down Expand Up @@ -489,7 +490,8 @@ interrupt() *compile-mode.interrupt()*

:Compile[!] *:Compile*
Runs a command and places its output in the compilation buffer. The
command is run from the current working directory. The compilation buffer
command is run from the current working directory, unless the
|vim.g.compilation_directory| variable is set. The compilation buffer
is opened in a new split if it isn’t already opened. If an argument is
present, it is used as the command. Otherwise, the user is prompted using
|vim.ui.input()|.
Expand All @@ -509,6 +511,12 @@ interrupt() *compile-mode.interrupt()*
Additionally, you can run the command with a |count| to set the size of
the opened window, like with |:split|.

*compile-compilation-directory* *vim.g.compilation_directory*
If the `vim.g.compilation_directory` global variable is set to anything
but Lua `nil`, it is used instead of the current working directory when
compiling. After `:Compile` is finished, `vim.g.compilation_directory` is
set to `nil`.

:Recompile[!] *:Recompile*
Reruns the last compiled command. If there isn’t one, the error is
reported using |vim.notify()|. The compilation buffer is opened in a new
Expand Down
2 changes: 1 addition & 1 deletion lua/compile-mode/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ if not ok then
end

if #config.health_info.unrecognized_keys > 0 then
log.warn("found unrecognized options: " .. vim.fn.join(config.health_info.unrecognized_keys, ", "))
log.fmt_warn("found unrecognized options: %s", config.health_info.unrecognized_keys)
end

if config.health_info.no_user_config then
Expand Down
15 changes: 6 additions & 9 deletions lua/compile-mode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ local error_cursor = 0

---The previous directory used for compilation.
---@type string|nil
vim.g.compilation_directory = nil
local compilation_directory = nil

---A table which keeps track of the changes in directory for the compilation buffer,
---based on "Entering directory" and "Leaving directory" messages.
Expand Down Expand Up @@ -69,7 +69,7 @@ end
---@return string
local function find_directory_for_line(linenum)
local latest_linenum = nil
local dir = vim.g.compilation_directory or vim.fn.getcwd()
local dir = compilation_directory or vim.fn.getcwd()
for old_linenum, old_dir in pairs(dir_changes) do
if old_linenum < linenum and (not latest_linenum or latest_linenum <= old_linenum) then
latest_linenum = old_linenum
Expand Down Expand Up @@ -150,7 +150,7 @@ local runjob = a.wrap(

log.debug("starting job...")
local job_id = vim.fn.jobstart(cmd, {
cwd = vim.g.compilation_directory,
cwd = compilation_directory,
on_stdout = on_either,
on_stderr = on_either,
on_exit = function(id, code)
Expand Down Expand Up @@ -267,7 +267,7 @@ local runcommand = a.void(
utils.wait()
errors.highlight(bufnr)

log.fmt_debug("running command: `%s`", string.gsub(command, "\\`", "\\`"))
log.fmt_debug("running command: %s", command)
local line_count, code, job_id = runjob(command, bufnr, param)
if job_id ~= vim.g.compile_job_id then
return
Expand Down Expand Up @@ -423,12 +423,9 @@ M.compile = a.void(
end

vim.g.compile_command = command
if not vim.g.compilation_directory then
vim.g.compilation_directory = vim.fn.getcwd()
end
compilation_directory = vim.g.compilation_directory or vim.fn.getcwd()

runcommand(command, param)

vim.g.compilation_directory = nil
end
)
Expand Down Expand Up @@ -573,7 +570,7 @@ M.goto_error = a.void(

local linenum = unpack(vim.api.nvim_win_get_cursor(0))
local error = errors.error_list[linenum]
log.fmt_debug("error = %s", vim.inspect(error))
log.fmt_debug("error = %s", error)

if not error then
if not param.smods or not param.smods.silent then
Expand Down
22 changes: 13 additions & 9 deletions lua/compile-mode/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,23 @@ log.new = function(config, standalone)

-- Output to console
if config.use_console then
local console_string = string.format("[%-6s%s] %s: %s", nameupper, os.date("%H:%M:%S"), lineinfo, msg)
local is_headless = #vim.api.nvim_list_uis() == 0

if config.highlights and level_config.hl then
vim.cmd(string.format("echohl %s", level_config.hl))
end
local console_string = string.format("[%-6s%s] %s: %s", nameupper, os.date("%H:%M:%S"), lineinfo, msg)

local split_console = vim.split(console_string, "\n")
for _, v in ipairs(split_console) do
vim.cmd(string.format([[echom "[%s] %s"]], config.plugin, vim.fn.escape(v, '"')))
end

if config.highlights and level_config.hl then
vim.cmd("echohl NONE")
local message = string.format("[%s] %s", config.plugin, v)
-- Use Lua print in headless mode, so STDERR is not mangled
if is_headless then
print(message)
else
local chunk = { message }
if config.highlights and level_config.hl then
table.insert(chunk, level_config.hl)
end
vim.api.nvim_echo({ chunk }, true, {})
end
end
end

Expand Down
58 changes: 58 additions & 0 deletions spec/compilation_directory_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
local helpers = require("spec.test_helpers")
local assert = require("luassert")

local pwd
if vim.o.shell:match("cmd.exe$") then
pwd = "echo %cd%"
elseif vim.o.shell:match("pwsh$") or vim.o.shell:match("powershell$") then
pwd = "$pwd"
else
pwd = "pwd"
end

describe(":Compile", function()
before_each(helpers.setup_tests)

it("should use the current working directory", function()
local cwd = vim.fn.getcwd()
helpers.compile({ args = pwd })
assert.are.same({ pwd, cwd }, helpers.get_output())
end)

it("should use vim.g.compilation_directory if set", function()
local dir = vim.fn.fnamemodify(vim.fn.getcwd(), ":h")
vim.g.compilation_directory = dir

helpers.compile({ args = pwd })
assert.are.same({ pwd, dir }, helpers.get_output())
end)

it("should unset vim.g.compilation_directory", function()
vim.g.compilation_directory = vim.fn.fnamemodify(vim.fn.getcwd(), ":h")

helpers.compile({ args = pwd })
assert.is_nil(vim.g.compilation_directory)
end)
end)

describe(":Recompile", function()
before_each(helpers.setup_tests)

it("should reuse the directory from last compilation", function()
local old_cwd = vim.fn.getcwd()
helpers.compile({ args = pwd })
assert.are.same({ pwd, old_cwd }, helpers.get_output())

-- Over the current working directory
helpers.change_vim_directory("..")
helpers.recompile({ args = pwd })
assert.are.same({ pwd, old_cwd }, helpers.get_output())
helpers.change_vim_directory(old_cwd)

-- Over vim.g.compilation_directory
vim.g.compilation_directory = vim.fn.fnamemodify(vim.fn.getcwd(), ":h")
helpers.recompile({ args = pwd })
assert.are.same({ pwd, old_cwd }, helpers.get_output())
vim.g.compilation_directory = nil
end)
end)
7 changes: 7 additions & 0 deletions spec/test_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ function M.setup_tests(opts)
package.loaded["compile-mode.config.internal"] = nil
end

---@param directory string
function M.change_vim_directory(directory)
vim.cmd(("cd %s"):format(directory))
M.wait()
end

---@param ms integer?
function M.wait(ms)
ms = ms or 100
local co = coroutine.running()
Expand Down

0 comments on commit eda0d6f

Please sign in to comment.