I've been trying to set up autocomplete for languages with neovim/nvim-lspconfig
This is the code I have so far:
set cot=menuone,noinsert shm+=c
let g:completion_matching_strategy_list = ['exact', 'substring', 'fuzzy']
nnoremap <leader>vd :lua vim.lsp.buf.definition()<CR>
nnoremap <leader>vi :lua vim.lsp.buf.implementation()<CR>
nnoremap <leader>vsh :lua vim.lsp.buf.signature_help()<CR>
nnoremap <leader>vrr :lua vim.lsp.buf.references()<CR>
nnoremap <leader>vrn :lua vim.lsp.buf.rename()<CR>
nnoremap <leader>vh :lua vim.lsp.buf.hover()<CR>
nnoremap <leader>vca :lua vim.lsp.buf.code_action()<CR>
nnoremap <leader>vsd :lua vim.lsp.util.show_line_diagnostics(); vim.lsp.util.show_line_diagnostics()<CR>
command! -buffer -nargs=0 LspShowLineDiagnostics lua require'jumpLoc'.openLineDiagnostics()
nnoremap <buffer><silent> <C-h> <cmd>LspShowLineDiagnostics<CR>
:lua << EOF
local nvim_lsp = require('lspconfig')
local on_attach = function(_, bufnr)
require('completion').on_attach()
end
local servers = {'clangd', 'pyright', 'gopls'}
for _, lsp in ipairs(servers) do
nvim_lsp[lsp].setup {
on_attach = on_attach,
}
end
EOF
When I try to test them, I'm only able to get warning checks and syntax highlighting. I'm not able to get autocomplete to work. Does anyone know if I need to enable a setting or environment variable to enable this?
yeah. your problem is that you call on_attach instead of returning it
this is the problem
local on_attach = function(_, bufnr)
require('completion').on_attach()
end
change the above code to
local on_attach = function(_, bufnr)
return require('completion').on_attach
end
and here:
nvim_lsp[lsp].setup {
on_attach = on_attach,
}
you are parsing a on_attach function, you are not allowed to call it, you pass the function object
from the completion-nvim docs (https://github.com/nvim-lua/completion-nvim#setup):
lua require'lspconfig'.pyls.setup{on_attach=require'completion'.on_attach}
they pass the function, instead of calling it.
just try this example from readme, ok? and tell me the results
Related
Here I have a loadstring but attacker can just hook it and steal it's content like bellow. I want to make a anti hook so that it can be prevented.
-- Save copies of orignal functions
local o_load = _G["load"]
local o_loadstring = _G["loadstring"]
local o_unpack = _G["unpack"]
local o_pairs = _G["pairs"]
local o_writefile = _G["writefile"]
local o_print = _G["print"]
local o_tostring = _G["tostring"]
-- Dynamic function names
local load_name = tostring(_G["load"])
local loadstring_name = tostring(_G["loadstring"])
local tostring_name = tostring(_G["tostring"])
-- For multiple instances of loadstring or load being used
local files_loadstring = 1
local files_load = 1
-- Hide function names
_G["tostring"] = function(...)
local args = {...}
local arg = o_unpack(args)
if arg == _G["tostring"] then
return tostring_name
end
if arg == _G["loadstring"] then
return loadstring_name
end
if arg == _G["load"] then
return load_name
end
local ret = { o_tostring(o_unpack(args)) }
local value = o_unpack(ret)
return value
end
-- Hook loadstring
_G["loadstring"] = function(...)
local args = {...}
o_print("loadstring called")
local str = ""
for k, v in o_pairs(args) do
str = str .. o_tostring(v) .. " "
end
o_writefile("hook_loadstring"..o_tostring(files_loadstring)..".lua", str)
o_print("file written to hook_loadstring"..o_tostring(files_loadstring)..".lua")
files_loadstring = files_loadstring +1
local ret = { o_loadstring(o_unpack(args)) }
str = ""
for k, v in o_pairs(ret) do
str = str .. o_tostring(v) .. " "
end
return o_unpack(ret)
end
-- Hook load
_G["load"] = function(...)
local args = {...}
o_print("load called")
local str = ""
for k, v in o_pairs(args) do
str = str .. o_tostring(v) .. " "
end
o_writefile("hook_load"..o_tostring(files_load)..".lua", str)
o_print("file written to hook_load"..o_tostring(files_load)..".lua")
files_load = files_load +1
local ret = { o_load(o_unpack(args)) }
str = ""
for k, v in o_pairs(ret) do
str = str .. o_tostring(v) .. " "
end
return o_unpack(ret)
end
-- bellow is the loadstring function with a lua and all it's content is getting hooked and writen in a file
loadstring("local a = 1;")()`
I tried using tostring(loadstring) to compare the function name to check if it's real or fake but attacker can just hook tostring as well and give original function name so my anti hook won't work at all. Also I can't do writefile = nil or print = nil so attacker can't print or write the file since he saved a copy of these functions on top of the file so his code will work always. How can I prevent my loadstring from getting hooked like this. Please help.
I just want my content inside loadstring not to be stolen.
loadstring being hooked implies that the attacker's code runs before your code and in the same environment. This implies that all functions of the global environment may be hooked - from loadstring over tostring and including the debug library. Thus, the only thing you may rely on (and even that is uncertain, depending on which software is interpreting your Lua) are "hard-wired" (e.g. syntactical) Lua language features. You can take a look at the complete syntax of Lua. Notice that all these are basic constructs that won't help you at all; everything advanced in Lua uses function calls and the global environment, which may be hooked.
Whatever loads these scripts would need to be patched to either (1) load everything in a separate, clean environment (2) protect the global environment or at least a few selected functions, but even then the "loader" could be tampered with to remove these "protections".
In conclusion, you can't reliably prevent or detect loadstring being hooked. If you are providing someone with code for them to run, that code inevitably has to run at some point, and at that point they will be able to "steal" it. The "best" you can do is obfuscate your code so that there is little benefit from stealing it.
Bottom line: As long as your software runs "on-premise" (on their machines) rather than "as a service" (on your machines), they will always be able to steal and analyze your code; all you can do is make it harder e.g. through obfuscation or checks (which may be circumvented however).
In my init.lua I have:
require("base") -- general settings
require("highlights") -- colourscheme and other highlights
require("keymaps") -- keymaps
require("plugins") -- plugins
require("hop") -- hop
require("bootstrap") -- packer auto-installer
print("✔ nvim loaded")
print(vim.inspect(package.loaded['hop']))
In base.lua, I have:
local g = vim.g
local o = vim.o
local opt = vim.opt
-- Map <leader> to space
g.mapleader = ' '
g.maplocalleader = ' '
In keymaps.lua, I have:
map("n", "<leader>w", ":HopWord<cr>")
map("n", "<leader>W", ":HopWordMW<cr>")
map("n", "f", ":HopChar1<cr>")
map("n", "F", ":HopChar1CurrentLine<cr>")
In plugins.lua, I have:
-- ...packer setup
local status, packer = pcall(require, "packer")
if not status then
return
end
return packer.startup(function(use)
use {
'phaazon/hop.nvim',
branch = 'v2', -- optional but strongly recommended
-- config = [[require('hop')]],
-- config = function()
-- -- you can configure Hop the way you like here; see :h hop-config
-- require'hop'.setup {}
-- end
}
if packer_bootstrap then
require("packer").sync()
end
end)
And finally, in hop.lua I have:
local setup, hop = pcall(require, "hop")
if not setup then
return
end
hop.setup { keys = "etovxqpdygfblzhckisuran" }
When I load vscode, I see the following output:
✔ nvim loaded
true
nil
The true is from the init.lua and means that hop was loaded, however, when I then press <space>w, I get the error:
E492: Not an editor command: HopWord
which means the hop commands did not load? Please help. I would like an easymotion style navigation flow for vscode neovim where it annotates places to jump to after pressing the spacebar.
Look at my response this ticket. It seems to work native without much configuration other than binding keybinds as hop does not configure any by default.
It seems when I return a table from a function I lose they keys. Not sure if this is how Lua should be functioning.
For example
function main()
local someTable = {}
someTable["foo"] = "bar"
print(someTable["foo"])
return someTable
end
local test = main()
print(test["foo"])
for k, v in pairs(test) do
print(k, v)
end
bar
nil
1 bar
Your code is ok and shows the expected behaviour in a standard Lua environment like the Lua Online Demo.
bar
bar
foo
bar
So either there is an issue with the environment you're running that script in or there is a difference between the code you posted here and the code you're running ony our machine.
Is there a way (or plugin) in vim to highlight current scope.
Say, I am inside function. VIM should use a bright background for the function and dark background for everything else.
When I move inside a loop that is inside the function, only the loop should have the bright background and everything else becomes slightly dark.
junegunn has this very nice plugin that does exactly what you want
https://github.com/junegunn/limelight.vim
I customized some things to make it better behave the way I'd expect it to:
"--------------------------------------------------------------------------------
"MAPPINGS{{{
"--------------------------------------------------------------------------------
" limelight works on ranges. Declare limelight to bein on content of current line
nnoremap <space>lb :let g:limelight_bop='^'.getline('.').'$'<cr>
" limelight works on ranges. Declare limelight to end on contents of current line
nnoremap <space>le :let g:limelight_eop='^'.getline('.').'$'<cr>
"decrement
nnoremap <space>ld :call SetLimeLightIndent(g:limelightindent - 4)<cr>
"increment
nnoremap <space>li :call SetLimeLightIndent(g:limelightindent + 4)<cr>
"reset indent to default 4
nnoremap <space>lr :call SetLimeLightIndent(4)<cr>
" set limelight toggle
noremap <space>ls :call SetLimeLightIndent(8)
nnoremap <space>lt :Limelight!!<cr>
"-----------------------------------------------------------------------------}}}
"FUNCTIONS{{{
"--------------------------------------------------------------------------------
let g:limelightindent=4
function! LimeLightExtremeties()
let limelight_start_stop='^\s\{0,'.g:limelightindent.'\}\S'
let g:limelight_eop=limelight_start_stop
let g:limelight_bop=limelight_start_stop
Limelight!!
Limelight!!
echo 'limelightindent = '.g:limelightindent
endfunction
function! SetLimeLightIndent(count)
let g:limelightindent = a:count
if(g:limelightindent < 0)
g:limelightindent = 0
endif
call LimeLightExtremeties()
endfunction
"-----------------------------------------------------------------------------}}}
command! -nargs=* SetLimeLightIndent call SetLimeLightIndent(<args>)
So I use a program where I script mods in lua, the lua is in a sandbox state, meaning most functions are blocked like IO and OS, I can't even use REQUIRE to add libs.
I need to have a function that unzips files in one of my mods and I don't seem to find a way.
Is there anyway to do it?
If it's not possible in an easy way, is it possible to hack the program .exe or dlls to re-enable those functions in the lua?
Thank you in advance, Regards
There are decompression librarys in pure Lua, you should be able to embed these in any environment that allows loading Lua scripts: http://lua-users.org/wiki/CompressionAndArchiving
If you can't access any files at all, you could try a simple packer:
#!/usr/bin/env lua
local files = arg
local w = io.write
local function pack(...) return {...} end
w("files = {\n")
for i, filename in ipairs(arg) do
w('\t["' ..filename .. '"] = "')
local file = assert(io.open(filename, "r"), "Can't open file!")
local data = file:read("*a")
data = data:gsub("\a", "\\a")
:gsub("\\", "\\\\")
:gsub("\f", "\\f")
:gsub("\n", "\\n")
:gsub("\r", "\\r")
:gsub("\t", "\\t")
:gsub("\v", "\\v")
:gsub('"', '\\"')
:gsub("'", "\\'")
w(data, '",\n')
end
w("}\n")
w([[
function require(path)
local data = assert(files[path..".lua"], "file not found")
local func = assert(loadstring(data))
local _, ret = assert(pcall(func))
return ret
end
]])
w('require("', arg[1]:match("^(.-)%.lua$"),'")\n')
This should create a script like this:
$ ./packer.lua init.lua
files = {
["init.lua"] = "for k,v in pairs(arg) do\n\tprint(k,v)\nend\n",
}
function require(path)
local data = assert(files[path..".lua"], "file not found")
local func = assert(loadstring(data))
local _, ret = assert(pcall(func))
return ret
end
require("init")