Skip to content
Open
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
7 changes: 5 additions & 2 deletions doc/gitlab.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ Here is the default setup function. All of these values are optional, and if
you call this function with no values the defaults will be used:
>lua
require("gitlab").setup({
port = nil, -- The port of the Go server, which runs in the background, if omitted or `nil` the port will be chosen automatically
server = {
binary = nil, -- The path to the server binary. If omitted or nil, the server will be built
port = nil, -- The port of the Go server, which runs in the background. If omitted or `nil` the port will be chosen automatically
},
log_path = vim.fn.stdpath("cache") .. "/gitlab.nvim.log", -- Log path for the Go server
config_path = nil, -- Custom path for `.gitlab.nvim` file, please read the "Connecting to Gitlab" section
debug = {
Expand Down Expand Up @@ -678,7 +681,7 @@ by a |motion|.
Either the operator or the motion can be preceded by a count, so that `3sj` is
equivalent to `s3j`, and they both create a comment for the current line and
three more lines downwards. Similarly, both `2s`|ap| and `s2`|ap| create a suggestion
for two "outer" paragraphs.
for two "outer" paragraphs.

The operators force |linewise| visual selection, so they work correctly even
if the motion itself works |characterwise| (e.g., |i(| for selecting the inner
Expand Down
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, flake-utils, nixpkgs }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; config.allowUnfree = true; };
gitlab-nvim-server = pkgs.buildGoModule {
pname = "gitlab.nvim-server";
version = "git";
src = ./.;
vendorHash = "sha256-OLAKTdzqynBDHqWV5RzIpfc3xZDm6uYyLD4rxbh0DMg=";
postInstall = ''
cp -r ${./cmd/config} $out/bin/config
mv $out/bin/cmd $out/bin/gitlab.nvim
'';
};
gitlab-nvim = pkgs.vimUtils.buildVimPlugin {
name = "gitlab.nvim";
src = ./.;
doCheck = false;
};
in
rec {
formatter = pkgs.nixpkgs-fmt;
packages.gitlab-nvim-server = gitlab-nvim-server;
packages.gitlab-nvim = gitlab-nvim;
packages.default = packages.gitlab-nvim;
}
);
}
2 changes: 1 addition & 1 deletion lua/gitlab/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ local function setup(args)
return
end

server.build() -- Builds the Go binary if it doesn't exist
state.merge_settings(args) -- Merges user settings with default settings
server.build() -- Builds the Go binary if it doesn't exist
state.set_global_keymaps() -- Sets keymaps that are not bound to a specific buffer
require("gitlab.colors") -- Sets colors
reviewer.init()
Expand Down
2 changes: 1 addition & 1 deletion lua/gitlab/job.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local M = {}

M.run_job = function(endpoint, method, body, callback)
local state = require("gitlab.state")
local args = { "-s", "-X", (method or "POST"), string.format("localhost:%s", state.settings.port) .. endpoint }
local args = { "-s", "-X", (method or "POST"), string.format("localhost:%s", state.settings.server.port) .. endpoint }

if body ~= nil then
local encoded_body = vim.json.encode(body)
Expand Down
61 changes: 50 additions & 11 deletions lua/gitlab/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end

-- Starts the Go server and call the callback provided
M.start = function(callback)
local port = tonumber(state.settings.port) or 0
local port = tonumber(state.settings.server.port) or 0
local parsed_port = nil
local callback_called = false

Expand All @@ -51,7 +51,7 @@ M.start = function(callback)
settings = settings:gsub('"', '\\"')
end

local command = string.format('"%s" "%s"', state.settings.bin, settings)
local command = string.format('"%s" "%s"', state.settings.server.binary, settings)

local job_id = vim.fn.jobstart(command, {
on_stdout = function(_, data)
Expand All @@ -61,7 +61,7 @@ M.start = function(callback)
port = line:match("Server started on port:%s+(%d+)")
if port ~= nil then
parsed_port = port
state.settings.port = port
state.settings.server.port = port
break
end
end
Expand Down Expand Up @@ -105,26 +105,59 @@ end
-- Builds the Go binary with the current Git tag.
M.build = function(override)
local file_path = u.current_file_path()
local parent_dir = vim.fn.fnamemodify(file_path, ":h:h:h:h")
state.settings.root_path = vim.fn.fnamemodify(file_path, ":h:h:h:h")

-- If the user provided a path to the server, don't build it.
if state.settings.server.binary ~= nil then
local binary_exists = vim.loop.fs_stat(state.settings.server.binary)
if binary_exists == nil then
u.notify(
string.format("The user-provided server path (%s) does not exist.", state.settings.server.binary),
vim.log.levels.ERROR
)
end
return
end

-- If the user did not provide a path, we build it and place it in either the data path, or the
-- first writable path we find in the runtime.
local datapath = vim.fn.stdpath("data")
local runtimepath = vim.api.nvim_list_runtime_paths()
table.insert(runtimepath, 1, datapath)

local bin_folder
for _, path in ipairs(runtimepath) do
local ok, err = vim.loop.fs_access(path, "w")
if err == nil and ok ~= nil and ok then
bin_folder = path .. u.path_separator .. "gitlab.nvim" .. u.path_separator .. "bin"
if vim.fn.mkdir(bin_folder, "p") == 1 then
state.settings.server.binary = bin_folder .. u.path_separator .. "server"
break
end
end
end

local bin_name = u.is_windows() and "bin.exe" or "bin"
state.settings.root_path = parent_dir
state.settings.bin = parent_dir .. u.path_separator .. "cmd" .. u.path_separator .. bin_name
if state.settings.server.binary == nil then
u.notify("Could not find a writable folder in the runtime path to save the server to.", vim.log.levels.ERROR)
return
end

if not override then
local binary_exists = vim.loop.fs_stat(state.settings.bin)
local binary_exists = vim.loop.fs_stat(state.settings.server.binary)
if binary_exists ~= nil then
return
end
end

local version_output = vim.system({ "git", "describe", "--tags", "--always" }, { cwd = parent_dir }):wait()
local version_output = vim
.system({ "git", "describe", "--tags", "--always" }, { cwd = state.settings.root_path })
:wait()
local version = version_output.code == 0 and vim.trim(version_output.stdout) or "unknown"

local ldflags = string.format("-X main.Version=%s", version)
local res = vim
.system(
{ "go", "build", "-ldflags", ldflags, "-o", bin_name },
{ "go", "build", "-buildvcs=false", "-ldflags", ldflags, "-o", state.settings.server.binary },
{ cwd = state.settings.root_path .. u.path_separator .. "cmd" }
)
:wait()
Expand All @@ -133,6 +166,12 @@ M.build = function(override)
u.notify(string.format("Failed to install with status code %d:\n%s", res.code, res.stderr), vim.log.levels.ERROR)
return false
end

local Path = require("plenary.path")
local src = Path:new(state.settings.root_path .. u.path_separator .. "cmd" .. u.path_separator .. "config")
local dest = Path:new(bin_folder .. u.path_separator .. "config")
src:copy({ destination = dest, recursive = true, override = true })

u.notify("Installed successfully!", vim.log.levels.INFO)
return true
end
Expand Down Expand Up @@ -185,7 +224,7 @@ M.get_version = function(callback)
local version_output = vim.system({ "git", "describe", "--tags", "--always" }, { cwd = parent_dir }):wait()
local plugin_version = version_output.code == 0 and vim.trim(version_output.stdout) or "unknown"

local args = { "-s", "-X", "GET", string.format("localhost:%s/version", state.settings.port) }
local args = { "-s", "-X", "GET", string.format("localhost:%s/version", state.settings.server.port) }

-- We call the "/version" endpoint here instead of through the regular jobs pattern because earlier versions of the plugin
-- may not have it. We handle a 404 as an "unknown" version error.
Expand Down
5 changes: 4 additions & 1 deletion lua/gitlab/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ end
M.settings = {
auth_provider = M.default_auth_provider,
file_separator = u.path_separator,
port = nil, -- choose random port
server = {
binary = nil,
port = nil,
},
debug = {
request = false,
response = false,
Expand Down