diff --git a/lua/jazz/interrupt.lua b/lua/jazz/interrupt.lua new file mode 100644 index 0000000..76bd4fd --- /dev/null +++ b/lua/jazz/interrupt.lua @@ -0,0 +1,50 @@ +-- luacheck: globals vim +local acid = require("acid") +local utils = require("jazz.utils") +local log = require("jazz.log").msg +local interrupt_op = require("acid.ops").interrupt +local connections = require("acid.connections") +local sessions = require("acid.sessions") +local impromptu = require("impromptu") + +local interrupt = {} + + +interrupt.select_session = function() + local connection_ix = connections.peek() + local session_ids = sessions.store[connection_ix] + if session_ids ~= nil then + session_ids = session_ids.list + else + return + end + + local response = function(data) + if utils.contains('interrupted', data.status) then + log"Successfully interrupted session" + elseif utils.contains('session-idle', data.status) then + log"Nothing to interrupt" + else + log"shrug" -- TODO deal with other two cases + end + end + + if #session_ids > 1 then + + impromptu.filter{ + title = "🎵 Select session for interrupting", + options = utils.map(function(id) + return {description = id} + end, session_ids), + handler = function(_, selected) + acid.run(interrupt_op{session = selected.description}:with_handler(response)) + return true + end + } + else + acid.run(interrupt_op{session = session_ids[1]}:with_handler(response)) + end + +end + +return interrupt diff --git a/lua/jazz/navigation.lua b/lua/jazz/navigation.lua index 2adb519..b9a00b3 100644 --- a/lua/jazz/navigation.lua +++ b/lua/jazz/navigation.lua @@ -1,5 +1,6 @@ -- luacheck: globals vim local acid = require("acid") +local go_to = require("acid.features").go_to local ops = require("acid.ops") local impromptu = require("impromptu") local log = require("jazz.log").msg @@ -29,14 +30,7 @@ navigation.symbols = function(ns) title = "🎵 Navigate to symbols", options = {}, handler = function(_, selected) - acid.run(ops["info"]{ns = selected.ns, symbol = selected.var}:with_handler(function(ret) - - local fpath = vim.api.nvim_call_function("AcidFindFileInPath", {ret.file, ret.resource}) - vim.api.nvim_command("edit " .. fpath) - - vim.api.nvim_win_set_cursor(window, {ret.line, ret.column}) - - end)) + go_to(selected.var, selected.ns) return true end } diff --git a/lua/jazz/nrepl.lua b/lua/jazz/nrepl.lua index 8b150e5..466dc40 100644 --- a/lua/jazz/nrepl.lua +++ b/lua/jazz/nrepl.lua @@ -3,6 +3,8 @@ local impromptu = require("impromptu") local connections = require('acid.connections') local nrepl = require('acid.nrepl') local acid_utils = require('acid.utils') +local acid = require("acid") +local eval = require("acid.ops").eval local find_value = function(tbl, val) for ix, v in ipairs(tbl) do @@ -17,11 +19,11 @@ local jazz_nrepl = {} local select_portno = function(handler) return impromptu.new.form{ - title = "🎵 Select port number:", - questions = {portno = {description = "Port number"}}, - handler = function(session, result) - return handler(session, result) - end + title = "🎵 Select port number:", + questions = {portno = {description = "Port number"}}, + handler = function(session, result) + return handler(session, result) + end } end @@ -33,12 +35,67 @@ local existing = function(obj) end) end +local run_alias = function(obj) + local options = { + start = { + description = "Start", + hl = "Function" + } + } + + local spawn_options = { + pwd = obj.pwd, + alias = {} + } + + local new_session = obj.session:stack(impromptu.new.ask{ + title = "🎵 From deps.edn", + options = options, + handler = function(session, selected) + if selected.tp == "alias" then + table.insert(spawn_options.alias, selected.description) + session.lines[selected.description].hl = "String" + else + nrepl.start(spawn_options) + return true + end + end + }) + local admin = acid.admin_session() + acid.run(eval{ + code = '(map println (keys (:aliases (clojure.edn/read-string (slurp "' .. + obj.pwd .. "/" ..obj.file .. + '")))))'}:with_handler(function(data) + if data.status or data.value then + return + end + + for _, v in ipairs(acid_utils.split_lines(data.out)) do + options[v] = { + description = v, + tp = "alias", + hl = "Comment" + } + end + + new_session:render() + end), + admin) + + return false +end + local toolsdeps = function(obj) local files = vim.api.nvim_call_function("expand", {"**/*.edn", true, true}) if #files == 1 and files[1] == "deps.edn" then - nrepl.start{pwd = obj.pwd} - return true + local admin = acid.admin_session() + if admin == nil then + nrepl.start{pwd = obj.pwd} + return true + else + return run_alias{file = files[1], session = obj.session, pwd = obj.pwd} + end end local options = {} @@ -53,9 +110,8 @@ local toolsdeps = function(obj) obj.session:stack(impromptu.new.ask{ title = "🎵 Select deps.edn file", options = options, - handler = function(_, selected) - nrepl.start{pwd = obj.pwd, deps_file = selected.description, alias = "-R:nrepl"} - return true + handler = function(session, selected) + return run_alias{file = selected.description, session = session, pwd = obj.pwd} end }) @@ -64,28 +120,28 @@ end local connect_nrepl = function(obj) return impromptu.new.form{ - title = "🎵 Connect nrepl to:", - questions = { - portno = {description = "Port number"}, - host = {description = "Host address"} - }, - handler = function(session, result) - obj.port = tonumber(result.portno) - obj.host = result.host - obj.connect = true - - session:pop() - - session.lines.connect.description = "Connect to remote nrepl? (true)" - session.lines.connect.hl = "String" - - session.lines.host.description = "Connect to address(" .. result.host .. ")" - session.lines.host.hl = "String" - - session.lines.port.description = "Port number (" .. result.portno .. ")" - session.lines.port.hl = "String" - return false - end + title = "🎵 Connect nrepl to:", + questions = { + portno = {description = "Port number"}, + host = {description = "Host address"} + }, + handler = function(session, result) + obj.port = tonumber(result.portno) + obj.host = result.host + obj.connect = true + + session:pop() + + session.lines.connect.description = "Connect to remote nrepl? (true)" + session.lines.connect.hl = "String" + + session.lines.host.description = "Connect to address(" .. result.host .. ")" + session.lines.host.hl = "String" + + session.lines.port.description = "Port number (" .. result.portno .. ")" + session.lines.port.hl = "String" + return false + end } end @@ -110,23 +166,23 @@ local custom_nrepl = function(obj) } --opts.bind = { - --description = "Bind to address (127.0.0.1)", - --hl = "Comment" + --description = "Bind to address (127.0.0.1)", + --hl = "Comment" --} --opts.host = { - --description = "Connect to address (127.0.0.1)", - --hl = "Comment" + --description = "Connect to address (127.0.0.1)", + --hl = "Comment" --} --opts.connect = { - --description = "Connect to remote nrepl? (false)", - --hl = "Comment" + --description = "Connect to remote nrepl? (false)", + --hl = "Comment" --} --opts.pwd = { - --description = "Directory (" .. obj.pwd .. ")", - --hl = "Comment" + --description = "Directory (" .. obj.pwd .. ")", + --hl = "Comment" --} opts.start = {description = "Start with custom configuration"} @@ -169,6 +225,7 @@ local custom_nrepl = function(obj) } end + jazz_nrepl.nrepl_menu = function(pwd) pwd = pwd or vim.api.nvim_call_function("getcwd", {}) if not acid_utils.ends_with(pwd, "/") then diff --git a/lua/jazz/usages.lua b/lua/jazz/usages.lua index 2205792..4f113a1 100644 --- a/lua/jazz/usages.lua +++ b/lua/jazz/usages.lua @@ -1,5 +1,6 @@ -- luacheck: globals unpack vim local acid = require('acid') +local go_to = require("acid.features").go_to local forms = require('acid.forms') local commands = require('acid.commands') local impromptu = require('impromptu') @@ -23,8 +24,8 @@ usages.find_all = function(symbol, ns) local ui = impromptu.filter{ title = "🎵 Finding usages of [" .. ns .. "/" .. symbol .. "]", options = {}, - handler = function(_, obj) - local data = obj.data.occurrence + handler = function(_, selected) + local data = selected.data.occurrence local fpath = data.file local col = math.floor(data['col-beg'] or 1) @@ -35,15 +36,23 @@ usages.find_all = function(symbol, ns) else vim.api.nvim_command(winnr .. "wincmd w | edit +" .. ln .. " " .. fpath) end - return true end } local acid_handler = function(data) - if data.occurrence ~= nil then + if data.err ~= nil then + vim.api.nvim_err_writeln(data.ex) + vim.api.nvim_err_writeln(data.err) + ui:handle("__quit") + return + elseif data.occurrence ~= nil then +--[[ +2019-12-06 16:02:39,935 - [acid :INFO] - {'col-beg': 1, 'col-end': 5, 'file': '/opt/code/klarna/exam.ple/src/exam/ple.clj', 'line-beg': 5, 'line-end': 6, 'match': '(defn my-function []\n 1)', 'name': 'exam.ple/my-function'} +--]] + local occr_ns = vim.fn.AcidGetNs(data.occurrence.file) local descr = ( - data.occurrence.match .. " @ " .. data.occurrence.file .. ":" .. math.floor(data.occurrence['line-beg']) + data.occurrence.match .. " @ " .. occr_ns .. " [" .. math.floor(data.occurrence['line-beg']) .. ":" .. math.floor(data.occurrence['col-beg']) .. ']' ):gsub("\n", "\\n") ui:update{description = descr, data = data} diff --git a/lua/jazz/utils.lua b/lua/jazz/utils.lua index 70a72b6..5545730 100644 --- a/lua/jazz/utils.lua +++ b/lua/jazz/utils.lua @@ -14,4 +14,20 @@ utils.map = function(fn, tbl) return new end +utils.iter_map = function(fn, iter, c, zero) + return function(inv, cc) + local ix, value = iter(inv, cc) + return ix, fn(value) + end, c, zero +end + +utils.contains = function(item, iter) + for _, v in ipairs(iter) do + if v == item then + return true + end + end + return false +end + return utils diff --git a/plugin/jazz.vim b/plugin/jazz.vim index 2bcace4..fe2db2c 100644 --- a/plugin/jazz.vim +++ b/plugin/jazz.vim @@ -1,12 +1,13 @@ command! -nargs=0 JazzNrepl lua require('jazz.nrepl').nrepl_menu() command! -nargs=? JazzFindUsages lua require('jazz.usages').find_all() -command! -nargs=0 JazzNavigateSymbols lua require('jazz.navigation').symbols() +command! -nargs=? JazzFindSymbols lua require('jazz.navigation').symbols() nmap n JazzNrepl augroup Jazz au FileType clojure nmap u JazzFindUsages - au FileType clojure nmap s JazzNavigateSymbols + au FileType clojure nmap s lua require('jazz.navigation').symbols() au FileType clojure nmap a lua require("jazz.files").alternate() au FileType clojure nmap f lua require("jazz.files").new() + au FileType clojure nmap c lua require("jazz.interrupt").select_session() augroup END