Skip to content

Commit

Permalink
Add distant connect (#55)
Browse files Browse the repository at this point in the history
* Add `DistantConnect` command as alternative to `DistantLaunch`
* Fix docker container by switching to a locked version of neovim 0.5.0
  • Loading branch information
chipsenkbeil authored Nov 2, 2021
1 parent 9f8a802 commit 06de9e1
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 7 deletions.
6 changes: 1 addition & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=linux/amd64 alpine:3.14
FROM --platform=linux/amd64 anatolelucet/neovim:0.5.0

# Install all of the packages we need
#
Expand Down Expand Up @@ -38,10 +38,6 @@ RUN rustup-init -y \
&& rustup component add rls \
&& sudo ln -s $cargo_bin_dir/rls /usr/bin/rls

# Install neovim 0.5 binary (from edge)
RUN sudo apk add neovim \
--repository=http://dl-cdn.alpinelinux.org/alpine/edge/community/

# Install and configure sshd with key using empty password
#
# 1. Support openrc not being properly ready (touch softlevel)
Expand Down
23 changes: 21 additions & 2 deletions doc/distant.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
\▓▓▓▓▓▓▓ \▓▓▓▓▓▓ \▓▓▓▓▓▓ \▓▓ \▓▓ \▓▓\▓▓ \▓▓ \▓▓


Version: 0.1.0

==============================================================================
*distant.nvim*

Expand Down Expand Up @@ -237,6 +235,26 @@ DistantLaunch {host} {opts} *DistantLaunch*
:DistantLaunch example.com ssh.port=2222 distant.bin=/path/to/distant
<

DistantConnect {host} {port} {opts} *DistantConnect*

Attempts to connect to a running distant server at the specified
host and port.

Will prompt for the secret key used to communicate with the server.

Options: ~
{timeout} (number) maximum time in milliseconds before failing
(default: settings.max_timeout)
{interval} (number) approximate time in milliseconds between
each check to see if the operation completed
(default: settings.timeout_interval)

Examples: ~
>
:DistantConnect example.com 2222
:DistantConnect example.com 2222 timeout=5000
<

DistantInstall [reload] *DistantInstall*

Triggers installation process for the C library.
Expand Down Expand Up @@ -1274,6 +1292,7 @@ Latest ~

* Add logging initialization for distant lua shared library
* Fix `poll_interval` to be properly applied when polling futures
* Add |DistantConnect| as new command alternative to |DistantLaunch|

v0.1.1 ~

Expand Down
28 changes: 28 additions & 0 deletions lua/distant/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ command.launch = function(input)
end)
end

--- DistantConnect host port [opt1=..., opt2=...]
command.connect = function(input)
input = command.parse_input(input)
paths_to_number(input.opts, {'timeout', 'interval'})

if #input.args == 0 then
vim.api.nvim_err_writeln('Missing host and port')
return
elseif #input.args == 1 then
vim.api.nvim_err_writeln('Missing port')
return
end

local host = input.args[1]
input.opts.host = host

local port = tonumber(input.args[2])
input.opts.port = port

editor.connect(input.opts, function(success, msg)
if success then
print('Connected to ' .. host .. ':' .. tostring(port))
else
vim.api.nvim_err_writeln(tostring(msg) or 'Connect failed without cause')
end
end)
end

--- DistantInstall [reload]
command.install = function(input)
input = command.parse_input(input)
Expand Down
1 change: 1 addition & 0 deletions lua/distant/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
local editor = {}

editor.launch = require('distant.editor.launch')
editor.connect = require('distant.editor.connect')
editor.open = require('distant.editor.open')
editor.write = require('distant.editor.write')

Expand Down
64 changes: 64 additions & 0 deletions lua/distant/editor/connect.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local lib = require('distant.lib')
local log = require('distant.log')
local state = require('distant.state')

--- Connects to a running distance binary on the remote machine
return function(opts, cb)
opts = opts or {}
cb = cb or function() end
vim.validate({opts = {opts, 'table'}, cb = {cb, 'function'}})
log.fmt_trace('editor.connect(%s)', opts)

-- Verify that we were provided a host
local host_type = type(opts.host)
if host_type ~= 'string' then
error('opts.host should be string, but got ' .. host_type)
end

-- Verify that we were provided a port
local port_type = type(opts.port)
if port_type ~= 'number' then
error('opts.port should be number, but got ' .. port_type)
end

local key = vim.fn.inputsecret('Enter distant key: ')
if #key == 0 then
error('key cannot be empty')
end
opts.key = key

-- Load settings for the particular host
state.load_settings(opts.host)
opts = vim.tbl_deep_extend('keep', opts, state.settings or {})

-- Clear any pre-existing session
state.session = nil

local first_time = not lib.is_loaded()
lib.load(function(success, res)
if not success then
local msg = tostring(res)
vim.api.nvim_err_writeln(msg)
cb(false, msg)
return
end

-- Initialize logging of rust module
if first_time then
log.init_lib(res)
end

local session
success, session = pcall(res.session.connect, opts)
if not success then
local msg = tostring(session)
vim.api.nvim_err_writeln(msg)
cb(false, msg)
return
end

state.session = session
state.sessions[opts.host] = session
cb(true)
end)
end
4 changes: 4 additions & 0 deletions lua/distant/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ end
lsp.connect = function(buf)
log.fmt_trace('distant.lsp.connect(%s)', buf)
local path = v.buf.remote_path(buf)
log.fmt_trace('buf(%s) remote path = %s', buf, path)

-- Only perform a connection if we have connected
-- and have a remote path
Expand All @@ -275,7 +276,10 @@ lsp.connect = function(buf)
-- start an LSP client once per session as well as
-- attach it to a buffer only once (not on enter)
if path ~= nil then
log.fmt_trace('lsp settings: %s', state.settings.lsp)
for label, config in pairs(state.settings.lsp) do
log.fmt_trace('Checking if %s is in %s', path, config.root_dir)

-- Only apply clients with a root directory that contains this file
if vim.startswith(path, config.root_dir) then
log.fmt_trace('File %s is within %s of %s', path, config.root_dir, label)
Expand Down
2 changes: 2 additions & 0 deletions plugin/distant.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ command! -nargs=* DistantOpen
\ lua require('distant.command').open(vim.fn.expand('<args>'))
command! -nargs=* DistantLaunch
\ lua require('distant.command').launch(vim.fn.expand('<args>'))
command! -nargs=* DistantConnect
\ lua require('distant.command').connect(vim.fn.expand('<args>'))
command! -nargs=* DistantMetadata
\ lua require('distant.command').metadata(vim.fn.expand('<args>'))
command! -nargs=* DistantInstall
Expand Down

0 comments on commit 06de9e1

Please sign in to comment.