Skip to content

Commit

Permalink
Streamline things
Browse files Browse the repository at this point in the history
  • Loading branch information
KillTheMule committed Jul 21, 2018
1 parent 92cd193 commit a94fe94
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 91 deletions.
10 changes: 5 additions & 5 deletions runtime/lua/testplugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local tycho = require('tycho')
local nvim = vim.api


local this = tycho.new_plugin("testplugin")
local plugin = tycho.new_plugin("testplugin")

local function stuff()
nvim.nvim_input("iTestplugin")
Expand All @@ -13,12 +13,12 @@ local function stuff2()
end

local function init()
this:thismap(stuff, "<F2>")
this:thismap(stuff2, "<")
plugin:map("<F2>", stuff)
plugin:map("<", stuff2)
end

return {
stuff = stuff,
stuff2 = stuff2,
--stuff = stuff,
--stuff2 = stuff2,
init = init
}
106 changes: 42 additions & 64 deletions runtime/lua/tycho/init.lua
Original file line number Diff line number Diff line change
@@ -1,81 +1,59 @@
-- luacheck: globals unpack vim.api
local nvim = vim.api
local tycho = {}
local utils = require("tycho.utils")
local messages = {}

tycho.as_nvim_command = function(obj)
assert(type(obj.ns) == "number", "Must supply a number namespace")
assert(type(obj.keys) == "string", "Must supply keys")
--assert(type(obj.fn) == "function", "Must supply the name of the function")
--
local keys = obj.keys:gsub("<", "<lt>")

local command = "lua require('tycho').functions['"..tostring(obj.ns).."']['"..keys.."']("
if obj.args ~= nil and #obj.args >= 0 then
for _, i in ipairs(obj.args) do
if type(i) == "string" then
command = command .. ", '" .. i .. "'"
else
command = command .. ", " .. i
end
end
end
command = command .. ")"
local command = nvim.nvim_command

return command
local messages = {}
local functions = {}

local function map_to_ns(keys, ns)
local keys_rhs = keys:gsub("<", "<lt>")
local rhs = "lua require('tycho').functions["..
tostring(ns).."]['"..keys_rhs.."']()"
table.insert(messages, { rhs = rhs })
command("map "..keys.." <Cmd>"..rhs.."<CR>")
end

--[[
tycho.map{
ns = ...,
fn = "myfn",
args = {"arg", 123}, -- optional
keys = "<leader>p"
}
produces: `map <leader>p <Cmd>lua require("myns").myfn("arg", 123)<CR>`
--]]
tycho.map = function(obj)
assert(obj.keys ~= nil and type(obj.keys) == "string", "Must supply keys to be mapped")

local command = tycho.as_nvim_command(obj)
table.insert(messages, {command = command})
nvim.nvim_command("map " .. obj.keys .. " <Cmd>" .. command .. "<CR>")
local function debug()
nvim.nvim_set_var("tycho_msgs", require('inspect')(messages))
nvim.nvim_set_var("tycho_fns", require('inspect')(functions))
command("put =tycho_msgs")
command("put =tycho_fns")
end

tycho.functions = {}
tycho.debug = function()
messages.dummy = "dummytext"
--nvim.nvim_command("let @a = '"..require('helpers').inspect(messages).."'")
nvim.nvim_command("let @a = '"..require('inspect')(tycho.functions).."'")
nvim.nvim_input('"ap')
--[[
nvim.nvim_input("\nKeys:\n")
for i, t in pairs(tycho.functions) do
for k, _ in pairs(t) do
nvim.nvim_input(i.." - "..k.."\n")
end
end
--]]
end

local function map(self, keys, fn)
assert(type(keys) == "string", "Must pass a string as 2nd argument to map")
assert(type(fn) == "function", "Must pass a function as 2nd argument to map")

-- we need that table[ns] is the same as luaeval "table["..tostring(ns).."]"
-- Not sure how to ascertain that, so let's just throw out an error if the
-- returned type of nvim_create_namespace changes
assert(type(self.ns) == "number", "Namespace must be a number")

local function thismap(self, fn, keys)
local ns = self.ns
local name = self.name
if not tycho.functions[tostring(ns)] then
tycho.functions[tostring(ns)] = {}
end
tycho.functions[tostring(ns)][keys] = fn

tycho.map{ns = ns, keys = keys}
assert(functions[ns][keys] == nil, "Keys already mapped from tycho")
functions[ns][keys] = fn

map_to_ns(keys, ns)
end

tycho.new_plugin = function(name)
local ns = nvim.nvim_create_namespace("name")
return { ns = ns, name = name, thismap = thismap}
local function new_plugin(name)
-- TODO(KillTheMule): This code assumes that subsequent calls with the same
-- name return the same ns. Make sure of that, or adapt
local ns = nvim.nvim_create_namespace(name)

-- Should be redundant after the comment above has ben ascertained
assert(functions[ns] == nil, "Namspace already exists in tycho")

functions[ns] = {}
return { ns = ns, name = name, map = map }
end

_G.tycho = tycho -- HACK: For manual testing. Needs to be removed.
return tycho
return {
debug = debug,
new_plugin = new_plugin,
functions = functions,
}
22 changes: 0 additions & 22 deletions runtime/lua/tycho/utils.lua

This file was deleted.

0 comments on commit a94fe94

Please sign in to comment.