Skip to content

Commit

Permalink
Add restart command
Browse files Browse the repository at this point in the history
  • Loading branch information
monkoose committed Apr 4, 2024
1 parent 2704dd7 commit c103810
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ NeoCodeium provides `:NeoCodeium` user command, which has some useful actions:
- `:NeoCodeium toggle` - toggles NeoCodeium completion.
- `:NeoCodeium disable_buffer` - disables NeoCodeium completion in the current buffer.
- `:NeoCodeium enable_buffer` - enables NeoCodeium completion in the current buffer.
- `:NeoCodeium restart` - restarts Codeium server (useful when server stop responding for any reason).

#### 🎨 Color groups

Expand Down
5 changes: 5 additions & 0 deletions lua/neocodeium/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local conf = require("neocodeium.utils.conf")
local log = require("neocodeium.log")
local options = require("neocodeium.options").options
local api_key = require("neocodeium.api_key")
local server = require("neocodeium.server")

local vf = vim.fn
local json = vim.json
Expand Down Expand Up @@ -160,6 +161,10 @@ function commands.enable_buffer()
vim.b.neocodeium_enabled = true
end

function commands.restart()
server:restart()
end

function commands.toggle()
options.enabled = not options.enabled
end
Expand Down
35 changes: 29 additions & 6 deletions lua/neocodeium/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local log = require("neocodeium.log")
local api_key = require("neocodeium.api_key")
local options = require("neocodeium.options").options
local stdio = require("neocodeium.utils.stdio")
local echo = require("neocodeium.utils.echo")
local utils = require("neocodeium.utils")
local Bin = require("neocodeium.binary")

Expand All @@ -17,9 +18,12 @@ local json = vim.json
---@class Server
---@field bin Binary
---@field port? string
---@field handle? uv.uv_handle_t
---@field handle? uv.uv_process_t
---@field pid? integer
---@field is_restart boolean
local Server = {
bin = Bin.new(),
is_restart = false,
}

-- Auxiliary functions ------------------------------------- {{{1
Expand Down Expand Up @@ -66,7 +70,7 @@ function Server:start()
local stderr = assert(uv.new_pipe())

---@diagnostic disable-next-line: missing-fields
self.handle = uv.spawn(self.bin.path, {
self.handle, self.pid = uv.spawn(self.bin.path, {
args = args,
stdio = { stdin, stdout, stderr },
}, function(_, _)
Expand All @@ -77,6 +81,14 @@ function Server:start()
uv.close(stderr)
if self.handle then
uv.close(self.handle)
self.handle = nil
end
self.port = nil
if self.is_restart then
self.is_restart = false
vim.schedule(function()
self:run()
end)
end
end)

Expand Down Expand Up @@ -110,12 +122,23 @@ function Server:run()
end
end

---Properly stops the server and closes the handle
---Properly stops the server, executing self.handle on_exit callback.
function Server:stop()
if self.handle then
uv.close(self.handle)
self.handle = nil
if self.pid then
uv.kill(self.pid, "sigint")
end
end

---Restarts the server
function Server:restart()
if self.handle and not uv.is_closing(self.handle) then
self.is_restart = true
self:stop()
else
self:run()
end
log.info("Server restarted")
echo.info("server restarted")
end

---Sends request to the server
Expand Down

0 comments on commit c103810

Please sign in to comment.