Requiring luasnip to neovim causes errors - lua

I am trying to use luasnip.
The configuration I am using is AstroNvim.
The modifications I made to this configuration are :
require("luasnip.loaders.from_snipmate").lazy_load() at the end of init.lua
Made a directory snippets in the same directory of init.lua, and added some basic snipmate styled snippets inside.
Commands Like :LuaSnipListAvailable works well, and snippets from friendly-snippets works well too.
However the following error message pops up when opening neovim.
Error detected while processing /Users/myusername/.config/nvim/init.lua:
E5113: Error while calling lua chunk: ...m/site/pack/packer/start/packer.nvim/lua/packer/load.lua:171: Vim(echomsg):E121: Undefined variable: Error
stack traceback:
[C]: in function 'cmd'
...m/site/pack/packer/start/packer.nvim/lua/packer/load.lua:171: in function <...m/site/pack/packer/start/packer.nvim/lua/packer/load.lua:16
7>
/Users/myusername/.local/share/nvim/packer_compiled.lua:496: in function </Users/myusername/.local/share/nvim/packer_compiled.lua:485>
[C]: in function 'require'
/Users/myusername/.config/nvim/init.lua:23: in main chunk
I tried other methods of adding snippets, but any sort of require("luasnip") seems to break all.

Related

using a batch file to open lua love is giving error

I just started learning love and I've tried opening love with a batch file, while it does open it gives this error
Error
[love "boot.lua"]:321: No code to run
Your game might be packaged incorrectly.
Make sure main.lua is at the top level of the zip.
Traceback
[love "callbacks.lua"]:228: in function 'handler'
[C]: in function 'error'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
however when dragging the whole folder inside of love it works perfectly, does somebody know how can I fix this?
Error message
Error message indicates, that you did not set correct path to your code, so the is no code to run. It should be folder containing main.lua
Simple batch file
Place the following code to file start.bat:
#ECHO OFF
"C:\Program Files\LOVE\love.exe" "C:\path\to\game\folder"
(replace C:\path\to\game\folder with actual path to folder containing main.lua and if you have love.exe elsewhere, also replace it to the correct path)
And then, you can run your code by double-clicking the start.bat file
Some helpful links:
How to run your game
Batch file example

NeoVim - Check if a Vim function exists from Lua

I plan on using vim-plug with NeoVim. So, my init.lua file will have function calls such as
vim.fn['plug#begin'](vim.fn.stdpath('data') .. '/plugged')
vim.fn['plug#']('hoob3rt/lualine.nvim')
However, I don't want to assume vim-plug is definitely installed. I want my init.lua file to degrade gracefully if vim-plug is not installed, rather than throwing an error
E5113: Error while calling lua chunk: Vim:E117: Unknown function: plug#begin
stack traceback:
[C]: in function 'plug#begin'
/Users/andy/.config/nvim/init.lua:8: in main chunk
How can I check if the vim-plug functions exist before attempting to call them?
I tried print(vim.fn['plug#begin']) but that for some reason prints a non-null value: function: 0x0104ba36f0, even though the function doesn't exist.
I tried print(vim.fn['plug#begin']) but that for some reason prints a non-null value: function: 0x0104ba36f0, even though the function doesn't exist.
Presumably it's returning a function that throws the error you are getting. I would thus recommend using pcall:
local success, error = pcall(vim.fn['plug#begin'], vim.fn.stdpath('data') .. '/plugged')
if not success then --[[fail gracefully]] end
caveat: this will catch any error, so you'll probably want to perform some check like if error:find"Unknown function" then ... end to only catch this specific error.

neovim init.lua module "lua.plguins" not found

I'm in the configuration neovim
I added the configuration file in C:\Users\wupan\AppData\Local\nvim\init.lua
require("lua.plugins")
and C:\Users\wupan\AppData\Local\nvim\lua\plugins.lua
return require('packer').startup(function()
use "wbthomason/packer.nvim"
end)
After the start-up neovim
Error detected while processing C:\Users\wupan\AppData\Local\nvim\init.lua:
E5113: Error while calling lua chunk: C:\Users\wupan\AppData\Local\nvim\init.lua:1: module 'lua.plugins' not found:
no field package.preload['lua.plugins']
no file '.\lua\plugins.lua'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua\lua\plugins.lua'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua\lua\plugins\init.lua'
no file '.\lua\plugins.dll'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua\plugins.dll'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\loadall.dll'
no file '.\lua.dll'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua.dll'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\loadall.dll'
stack traceback:
[C]: in function 'require'
C:\Users\wupan\AppData\Local\nvim\init.lua:1: in main chunk
Press ENTER or type command to continue
May I ask why
I tried to install Lua test the require
scoop install lua
C:\Users\wupan\code\lua\test.lua
require("hello")
C:\Users\wupan\code\lua\hello.lua
print("hello world")
There are also problems
C:\Users\wupan\scoop\apps\lua\current\lua54.exe: .\test.lua:1: module 'hello' not found:
no field package.preload['hello']
no file 'C:\Users\wupan\scoop\apps\lua\current'
no file 'C:\Users\wupan\scoop\apps\lua\current'
stack traceback:
[C]: in function 'require'
.\test.lua:1: in main chunk
[C]: in ?
Problem analysis
Let's use the following example which you gave us:
C:\Users\wupan\AppData\Local\nvim\init.lua
require("lua.plugins")
C:\Users\wupan\AppData\Local\nvim\lua\plugins.lua
return require('packer').startup(function()
use "wbthomason/packer.nvim"
end)
And now let's take a look into your error message:
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua\lua\plugins.lua'
The important part is the bin\lua\lua thing here. It's because your
require("lua.plugins") starts to look up a directory in one of your runtime
path (see :h rtp for more information).
Solution
You just need to fix your content in
C:\Users\wupan\AppData\Local\nvim\init.lua to:
require("plugins")
because then neovim will look up a directory which is named plugins in
C:\Users\wupan\scoop\apps\neovim\current\bin\lua for example, since its in
your runtime path.
Little note
You can change your content from
C:\Users\wupan\AppData\Local\nvim\lua\plugins.lua to:
require('packer').startup(function()
use "wbthomason/packer.nvim"
end)
or how I did it:
local packer = require('packer')
packer.startup(function(use)
use 'wbthomason/packer.nvim'
end)
That makes it a little bit more readable in my opinion :)

Basic Quit Function in LOVE2D

I started using LOVE yesterday and I'm trying to code a basic quit function with LUA.
Here's my code
if function love.keyboard.getKey("q")
function love.event.quit()
end
I've tried it with and without the functions.
When I run it, it gives me this error
Error
Syntax error: main.lua:1: '(' expected near 'love'
Traceback
[C]: at 0x7ff9037828f0
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
if function love.keyboard.getKey("q")
function love.event.quit()
end
Is invalid Lua syntax.
function is a keyword that is used to define function value. It is not part of the if statement and not used in function calls.
an if statement looks like
if condition then
-- block
end
love.keyboard.getKey("q") is not part of the love2d API.
What you want to do would probably be achieved by implementing a keypressed event handler.
Computer programs are not written by guessing some syntax and then asking for help.
Do a tutorial and read the Lua manual if you want to do anything useful with Lua.

Torch 7/Lua cannot require a .so file, but package.loadlib() can

I get a problem when playing Torch 7 code. I have a shared library libhashnn.so file, and I want to load the functions in Torch 7 script, so I use this expression: require 'libhashnn', but the trepl gives an error.
However, if I use package.loadlibfunction, it does work. Here are the results, but I don't know why I can't use require, how can I use require to load lib successfully?
require'libhashnn'
/home/dazhen/torch/install/share/lua/5.1/trepl/init.lua:383: bad argument #1 to '?' (table expected, got string)
stack traceback:
[C]: in function 'error'
/home/dazhen/torch/install/share/lua/5.1/trepl/init.lua:383: in function 'require'
[string "_RESULT={require'libhashnn'}"]:1: in main chunk
[C]: in function 'xpcall'
/home/dazhen/torch/install/share/lua/5.1/trepl/init.lua:650: in function 'repl'
...zhen/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
and
f=package.loadlib('libhashnn.so' ,'luaopen_libhashnn')
[0.0195s]
th> f
function: 0x41630f38
Try calling the function that package.loadlib gives you. I bet you will then get the same error that you get when you use require.
To understand why: require does the equivalent of package.loadlib and then calls that function so that the module that you are loading can initialize itself. With just package.loadlib, this initialization is not done and so the error that occurs during initialization does not show up.
I don't know what hashnn is and so I cannot tell you why it is broken, but looking at /home/dazhen/torch/install/share/lua/5.1/trepl/init.lua line 383 might be a good start to figure out what is going on.

Resources