Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Jan 14, 2024
0 parents commit 24446ea
Show file tree
Hide file tree
Showing 37 changed files with 1,372 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .cljstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{:rules {:indentation {:list-indent 1
:indents {s/def [[:inner 0]]
s/defn [[:inner 0]]
s/defschema [[:inner 0]]
s/defrecord [[:inner 0] [:inner 1]]}}

:blank-lines {:insert-padding? false}
:functions {:enabled? false}
:types {:enabled? false}
:namespaces {:indent-size 1}}
:files {:ignore #{"target"}}}
; vim:ft=clojure
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
VIM_AUTO_RTP
.DS_Store
*.bk
cov
.clj-kondo
.cache
.cpcache
.nrepl-port
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Masashi Iizuka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
6 changes: 6 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= vim-elin

[quote,Nahoko Uehashi,The Beast Player]
Joeun smiled. “Elin, the wild apple that grows in the mountains. What a nice name.”

Work in progress
28 changes: 28 additions & 0 deletions autoload/elin.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let g:elin#babashka = get(g:, 'elin#babashka', 'bb')

function! elin#notify(...) abort
let conn = elin#server#connection()
if conn is# v:null
echom 'not connected'
return
endif
return call(function('elin#compat#rpc#notify'), [conn] + a:000)
endfunction

function! elin#request(...) abort
let conn = elin#server#connection()
if conn is# v:null
echom 'not connected'
return
endif
return call(function('elin#compat#rpc#request'), [conn] + a:000)
endfunction

function! s:callback(...) abort
echom printf('FIXME callback %s', a:000)
endfunction

function! elin#test(method, params) abort
call elin#notify(a:method, a:params, funcref('s:callback'))
endfunction

24 changes: 24 additions & 0 deletions autoload/elin/callback.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let s:registry = {}

function! elin#callback#register(callback) abort
let id = sha256(string(a:callback))
let s:registry[id] = a:callback
return id
endfunction

function! elin#callback#unregister(id) abort
if !has_key(s:registry, a:id)
return
endif
silent unlet s:registry[a:id]
endfunction

function! elin#callback#call(id, ...) abort
echom printf('FIXME clalback#call kiteruyo: %s', a:id)
if !has_key(s:registry, a:id)
throw printf('Callback id does not exists: %s', a:id)
endif
let ret = call(s:registry[a:id], a:000)
call elin#callback#unregister(a:id)
return ret
endfunction
62 changes: 62 additions & 0 deletions autoload/elin/compat/job.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function! elin#compat#job#start(command, options) abort
return s:start(a:command, a:options)
endfunction

function! elin#compat#job#stop(job) abort
return s:stop(a:job)
endfunction

function! elin#compat#job#redir(command, callback) abort
let d = {'result': '', 'callback': a:callback}
call s:start(a:command, {
\ 'out_cb': funcref('s:on_redir_out', d),
\ 'close_cb': funcref('s:on_redir_close', d),
\ })
endfunction

function! s:on_redir_out(_, out) abort dict
for out in elin#util#ensure_array(a:out)
let self.result = self.result . out
endfor
endfunction

function! s:on_redir_close(_) abort dict
call self.callback(self.result)
endfunction

if has('nvim')
function! s:on_exit(callback_key, options, job, exit_code, event_type) abort
return a:options[a:callback_key](a:job)
endfunction

function! s:start(command, options) abort
let options = elin#util#select_keys(a:options, ['cwd'])
if has_key(a:options, 'out_cb')
let options['on_stdout'] = {j,d,e -> a:options['out_cb'](j, d)}
endif
if has_key(a:options, 'err_cb')
let options['on_stderr'] = {j,d,e -> a:options['err_cb'](j, d)}
endif
if has_key(a:options, 'close_cb')
let options['on_exit'] = funcref('s:on_exit', ['close_cb', a:options])
endif
if has_key(a:options, 'exit_cb')
let options['on_exit'] = funcref('s:on_exit', ['exit_cb', a:options])
endif
return jobstart(a:command, options)
endfunction

function! s:stop(job_id) abort
return jobstop(a:job_id)
endfunction

else
function! s:start(command, options) abort
let options = elin#util#select_keys(a:options, ['cwd', 'out_cb', 'err_cb', 'close_cb', 'exit_cb'])
return job_start(a:command, options)
endfunction

function! s:stop(job_id) abort
return job_stop(a:job_id)
endfunction
endif
94 changes: 94 additions & 0 deletions autoload/elin/compat/rpc.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
function! elin#compat#rpc#connect(addr, ...) abort
let options = get(a:, 1, {})
return s:connect(a:addr, options)
endfunction

function! elin#compat#rpc#disconnect(conn) abort
return s:disconnect(a:conn)
endfunction

function! elin#compat#rpc#request(conn, method, params) abort
return s:request(a:conn, a:method, [a:params])
endfunction

function! elin#compat#rpc#notify(conn, method, params, ...) abort
let Callback = get(a:, 1, {_ -> 0 })
let callback_id = elin#callback#register(Callback)
return s:notify(a:conn, a:method, [a:params] + [callback_id])
endfunction

if has('nvim')

function! s:connect(addr, options) abort
let options = extend({
\ 'on_close': { -> 0 },
\ }, get(a:, 1, {}))
let id = sockconnect('tcp', a:addr, {'rpc': v:true})
if id is# 0
throw printf('Failed to connect: %s', a:addr)
endif
let conn = {
\ 'id': id,
\ 'on_close': options.on_close,
\}
return conn
endfunction

function! s:disconnect(conn) abort
call sockclose(a:conn.id)
call a:conn.on_close(a:conn)
endfunction

function! s:request(conn, method, params) abort
return call('rpcrequest', [a:conn.id, a:method] + a:params)
endfunction

function! s:notify(conn, method, params) abort
return call('rpcnotify', [a:conn.id, a:method] + a:params)
endfunction

else

function! s:fixme(ch, msg) abort
echom printf('CHANNEL CALLBACK: %s', a:msg)
endfunction

function! s:connect(addr, options) abort
let options = extend({
\ 'on_close': { -> 0 },
\ }, get(a:, 1, {}))

let ch = ch_open(a:addr, {
\ 'mode': 'json',
\ 'callback': funcref('s:fixme'),
\ 'drop': 'auto',
\ 'noblock': 1,
\ 'timeout': 1000 * 60 * 60 * 24 * 7,
\ 'close_cb': options.on_close,
\})
if ch_status(ch) !=# 'open'
throw printf('Failed to connect: %s', a:addr)
endif
return ch
endfunction

function! s:disconnect(ch) abort
return ch_close(a:ch)
endfunction

function! s:request(ch, method, params) abort
echom printf("FIXME start to request: %s", [a:method] + a:params)
"let [result, error] = ch_evalexpr(a:ch, [a:method] + a:params)
let result = ch_evalexpr(a:ch, [a:method] + a:params)
echom printf("FIXME finish to request: %s", result )
" if error isnot# v:null
" throw error
" endif
return result
endfunction

function! s:notify(ch, method, params) abort
return ch_sendraw(a:ch, json_encode([0, [a:method] + a:params]) . "\n")
endfunction

endif
9 changes: 9 additions & 0 deletions autoload/elin/script.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function! elin#script#empty_port(callback) abort
let command = [
\ g:elin#babashka,
\ '--prn',
\ '--eval',
\ '(with-open [sock (java.net.ServerSocket. 0)] (.getLocalPort sock))',
\ ]
return elin#compat#job#redir(command, a:callback)
endfunction
53 changes: 53 additions & 0 deletions autoload/elin/server.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
let s:job = v:null
let s:port = v:null
let s:conn = v:null

function! elin#server#start() abort
call elin#script#empty_port(funcref('s:start'))
call timer_start(100, funcref('s:try_connecting'), {'repeat': -1})
endfunction

function! s:try_connecting(timer_id) abort
echom printf('try to connect: %s', s:port)
if s:port is# v:null
return
endif

try

let s:conn = elin#compat#rpc#connect(printf('localhost:%s', s:port))
echom 'connected'
return timer_stop(a:timer_id)
catch
echom printf('failed to connect: %s', v:exception)
return v:null
endtry
endfunction

function! elin#server#stop() abort
return s:stop()
endfunction

function! elin#server#connection() abort
return s:conn
endfunction

" if has('nvim')
function! s:start(port) abort
let s:port = a:port
let command = [g:elin#babashka, '-m', 'elin.core', has('nvim') ? 'nvim' : 'vim', a:port]
let options = {
\ 'cwd': g:elin_home,
\ }
let s:job = elin#compat#job#start(command, options)
endfunction

function! s:stop() abort
if s:job is# v:null
return
endif

call elin#compat#job#stop(s:job)
let s:job = v:null
endfunction
" endif
12 changes: 12 additions & 0 deletions autoload/elin/util.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function! elin#util#ensure_array(x) abort
return (type(a:x) == v:t_list ? a:x : [a:x])
endfunction

function! elin#util#select_keys(d, ks) abort
let ret = {}
for k in a:ks
if !has_key(a:d, k) | continue | endif
let ret[k] = a:d[k]
endfor
return ret
endfunction
14 changes: 14 additions & 0 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{:paths ["src"]
:deps {clojure-msgpack/clojure-msgpack {:mvn/version "1.2.1"}
com.github.liquidz/merr {:mvn/version "0.4.193"}
com.stuartsierra/component {:mvn/version "1.1.0"}
exoscale/interceptor {:mvn/version "0.1.16"}
metosin/malli {:mvn/version "0.13.0"}
prismatic/schema {:mvn/version "1.4.1"}}

:tasks
{start-server {:task (shell "bb -m elin.core")}
repl {:override-builtin true
:extra-paths ["dev"]
:requires ([elin.task.nrepl :as elin.t.nrepl])
:task (elin.t.nrepl/-main)}}}
Loading

0 comments on commit 24446ea

Please sign in to comment.