/neovim-luajls

Making luajs and its promises work in neovim

Primary LanguageLuaMIT LicenseMIT

neovim-luajs

Original project and documentation see https://github.com/javalikescript/luajls

Changes made: 1) move all files into ./lua/* so that runtimepath of neovim can be used to find / require the files 2) downgrade of lua 5.3 like features like >> | & // using bit.* and math.floor

Why - Promise async await for lua within Neovim

I wanted to use Promises and await like syntax in a NeoVim .lua script

Example

vim.cmd('VAMActivate luajls')

local event = require("jls/lang/event-luv-neovim")
local Promise = require("jls/lang/Promise")

local uv = vim.loop

local cb, promise = Promise.ensureCallback(callback)
uv.fs_statfs("/tmp/", cb)

promise:next( function (d) print(vim.inspect(d)) end)

local HttpClient = require('jls.net.http.HttpClient')

local stat_promise = function ()
  local cb, promise = Promise.ensureCallback(callback)
  uv.fs_statfs("/tmp/", cb)
  return promise
end

local stat_promise_await = function (await)
  local stat = Promise.promisify(uv.fs_statfs)
  return await(stat("/tmp/"))
end

local function asyncGetTitle(await, url)
  local client = await(HttpClient:new({ url = url }):connect())
  local response = await(client:sendReceive())
  local title = string.match(response:getBody(), '<%s*[tT][iI][tT][lL][eE]%s*>%s*([^<]*)%s*<%s*/%s*[tT][iI][tT][lL][eE]%s*>')
  return title
end

Promise.async(function(await)
  print(asyncGetTitle(await, 'http://www.lua.org'))
  print(asyncGetTitle(await, 'http://www.lua.org/about.html'))
  print(vim.inspect(await(stat_promise())))
  print(vim.inspect(await(stat_promise_await(await))))
end)