forked from neovim/neovim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92cd193
commit a94fe94
Showing
3 changed files
with
47 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file was deleted.
Oops, something went wrong.