Skip to content
This repository has been archived by the owner on Jun 1, 2020. It is now read-only.

Simplified API #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 11 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,24 @@ Or maybe you're interested in using your function as a handler for asynchronous

Tycho exists to solve that gap.

## How?
Also, take a look at [this issue](https://github.com/Vigemus/tycho.nvim/issues/2) so you can understand a little better.

Tycho has three APIs:
## Who is this plugin for?

### The formal API (aka. core)
This is a resource for plugin writers that want o use the lua API.
It allows a one to write plugins in lua purely, without needing to think about the interop or viml semantics.

This is the actual implementation of how tycho talks to neovim.
All the other implementations are a matter of taste and were build on top of this one:
```lua
local tycho = require("tycho")

-- Register your function
tycho.core.register("my_awesome_function", function()
print("Hello from Tycho!")
end)

-- Add the mapping in neovim
tycho.core.map("my_awesome_function", "<Space>!")

-- With arguments
tycho.core.register("another_function", function(t, msg)
for i = t, 0, -1 do
print(msg)
end
end)

tycho.core.map("another_function", "<Space><CR>", 10, 'Mapped with arguments")
## How do I use it?

```

### The sugared version

This is an attempt to make the api fluid and hide the integration bits.
```lua
local tycho = require("tycho")

-- Directly assign to tycho table to register the function
tycho.my_awesome_function = function()
print("Hello from Tycho!")
end

-- A more complex example
tycho.another_function = function(t, msg)
for i = t, 0, -1 do
print(msg)
end
end

-- You map passing the assigned object
tycho.api.map("<Space>!", tycho.my_awesome_function)

-- You can also map anonymous functions
tycho.api.map("<Space>!", function()
print("Hello from Tycho!")
end)

local my_fn = function(a, b)
print(a + b)
end

-- But you can't pass arguments to it
tycho.api.map("<Space><CR>", function()
my_fn(12, 30)
end)
-- Mapping a lua function to a key sequence:
tycho.map{
ns = ..., -- (unique name of current module)
fn = "my_function",
keys = "<leader>q"
}
```


## What comes next

- [ ] Support for jobs
Expand Down
32 changes: 0 additions & 32 deletions lua/tycho/core.lua

This file was deleted.

74 changes: 32 additions & 42 deletions lua/tycho/init.lua
Original file line number Diff line number Diff line change
@@ -1,53 +1,43 @@
-- luacheck: globals unpack vim.api
local nvim = vim.api
local impl = require("tycho.core")
local tycho = {
api = {},
core = impl.core,
cache = impl.cache
}

local sugar = {
__newindex = function(_, key, value)
tycho.core.register(key, value)
end,

__index = function(table, key)
local real = rawget(table, key)
if real ~= nil then
return real
local tycho = {}
local utils = require("tycho.utils")

tycho.as_nvim_command = function(obj)
assert(obj.ns ~= nil and type(obj.ns) == "string", "Must supply a string namespace")
assert(obj.fn ~= nil and type(obj.fn) == "string", "Must supply the name of the function")
assert(utils.get_qualified(require(obj.ns), obj.fn) ~= nil, "Unable to find function")

local command = "lua require('" .. obj.ns .. "')." .. obj.fn .. "("
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

local cache = rawget(table, "cache")
local ret = {
ns = key,
fn = cache[key]
}
return ret
end
}
command = command .. ")"

tycho.api.map = function(keymap, fn)
local func
local ns
return command
end

if type(fn) == "table" then
ns = fn.ns
func = fn.fn
else
ns = tostring(math.random(1, 9999999))
func = fn
end
--[[
tycho.map{
ns = ...,
fn = "myfn",
args = {"arg", 123}, -- optional
keys = "<leader>p"
}

tycho.core.register(ns, func)
tycho.core.map(ns, keymap)
end
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")

tycho.api.debug = function()
print(require("inspect")(tycho))
nvim.nvim_command("map " .. obj.keys .. " <Cmd>" .. command .. "<CR>")
end

setmetatable(tycho, sugar)

_G.tycho = tycho
_G.tycho = tycho -- HACK: For manual testing. Needs to be removed.
return tycho
22 changes: 22 additions & 0 deletions lua/tycho/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local utils = {}

utils.get = function(d, k)
return d and d[k]
end

utils.get_in = function(d, k)
local p = d
for _, i in ipairs(k) do
p = utils.get(p, i)
end

return p
end

utils.get_qualified = function(d, p)
local buff = {}
p:gsub("([^.]+)", function(i) table.insert(buff, i) end)
return utils.get_in(d, buff)
end

return utils
7 changes: 0 additions & 7 deletions plugin/tycho.vim

This file was deleted.