VIM highlight current scope - editor

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>)

Related

Trouble setting up Neovim to autocomplete with neovim/nvim-lspconfig

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

I'm trying to code a script that tells me when the mouse enters and leaves a ClickDetector in Roblox Studio

Here's the code.
local detector = script.Parent.ClickDetector -- setting a click detector variable to save time
function mousein() -- mousein function
print("Mouse has entered!") -- print "Mouse has entered!" in the output
end -- end of the function
function mouseout()-- mouseout function
print("Mouse has left!") -- print "Mouse has left!" in the output
end -- end of the function
detector.MouseHoverEnter:Connect(mousein) -- run mousein when my mouse hovers over the clickdetector
detector.MouseHoverLeave:Connect(mouseout)-- run mouseout when my cursor leaves the clickdetector
I labeled the code to make this easier to read.
Please put the working code in the answers! This is my first question on StackOverflow. I am new to coding.
You need to create the ClickDetector instance.
local detector = Instance.new("ClickDetector")
detector.Parent = thing --whatever object you want to detect mouse stuff on
detector.MaxActivationDistance = 10
function mousein()
print("Mouse has entered!")
end
function mouseout()
print("Mouse has left!")
end
detector.MouseHoverEnter:Connect(mousein)
detector.MouseHoverLeave:Connect(mouseout)
Or alternatively, as stated in the API documantion, In order for script.Parent.ClickDetector to work as expected, it must be placed in a Script or LocalScript whose parent is the ClickDetector
APIREFERENCE > INPUT > CLICKDETECTOR > MouseHoverEnter

How do I do "io.read" twice on one variable after inputting the wrong type?

Heyo. I'm pretty new to Lua (although I do code with Java), so I don't really know anything on this. I'm basically trying to get a user's input, and if it's not the right type, then restart. Now, I'm not sure if it's just Lua or my IDE (I'm using ZeroBrane Studio if that helps), but it won't reinput for whatever reason. (it just loops, meaning it skips the io.read line)
::restart::
...
a = io.read("*number")
if unit == nil then
print("Error! Incorrect Input!\nRestarting...")
goto restart
end
Oh, and yes, I'm using goto commands for restart. I thought that might be what's causing the issue, but I also tried this:
a = io.read("*number") --input non-number
print(a) --prints
a = io.read("*number") --skips
print(a) --prints
When you input a number, it doesn't skip.
Any help would be nice. Thanks in advance.
nvm i solved it myself
local a
repeat
a = io.read(); a = tonumber(a)
if not a then
print("Incorrect Input!\n(Try using only numbers)")
end
until a
::restart::
local a = io.read("*n", "*l")
if a == nil then
io.read("*l") -- skip the erroneous input line
print("Error! Incorrect Input!\nRestarting...")
goto restart
end
P.S.
Feel free to use goto whenever it makes your code more understandable.
For example, using while of repeat-until loop in this code wouldn't make it better (you would need either additional local variable or break statement).
Instead of using the built-in filter of io.read() (which I do think is bugged sometimes) you should consider using an own little function to ensure that the correct data will be give by the user.
This is such a function:
function --[[ any ]] GetUserInput(--[[ string ]] expectedType, --[[ string ]] errorText)
local --[[ bool ]] needInput = true
local --[[ any ]] input = nil
while needInput do
input = GetData()
if ( type(input) == expectedType ) then
needInput = false
else
print(errorText)
end
end
return input
end
You can then call it with:
local userInput = GetUserInput("number", "Error: Incorrect Input! Please give a number.")
Oh and on a sidenote: Goto is considered bad practice.

Changing contents of global variables in a Lua script for Awesome Window Manager?

So I've been trying to configure my Awesome WM config (rc.lua) to detect if my IBM model M13 is connected to my laptop upon login/reset. This is to change what the modkey should be since the M13 doesn't have a super key.
The following code makes sense to me and changes modkey within the function being made for the awful.spawn.easy_async function, but after finishing the modkey changes back to Mod4.
modkey = "Mod4"
awful.spawn.easy_async(
"xinput list",
function(stdout, stderr, reason, code)
local msg = "Regular keyboard Modkey = Super"
-- Debug notification that shows that the modkey is
-- at its default for the superkey Mod4
naughty.notify({
text = modkey,
timeout =7
})
if code ~= 0 then
msg = "Missing xinput to see devices\nModkey = Super"
elseif stdout:match("CHESEN") == "CHESEN" then
-- CHESEN is my PS/2 to USB adapter
msg = "IBM M13 detected\nModkey = Alt"
modkey = "Mod1" -- Sets new modkey to Alt
end
-- Notification message
naughty.notify({
text = msg,
timeout =7
})
end
)
-- Debug notification to verify key but key goes back to Mod4
naughty.notify({
text = modkey,
timeout =7
})
The output can be seen here. It doesn't print the notifications in order but the prints of Mod 4 are both of the debug prints.
Notification Output
I don't use Lua much aside from changing my configs from time to time so I'm having difficulty understanding how my global variable modkey can be changed with out it resetting. Other methods I tried was to have the function defined as a function I called setModKey to be passed as a parameter to easy_async and I tried setting modkey using _G to set it as _G.modkey, but I end up getting the same result.
Am I missing something fundamental to Lua or is this affected by how Awesome WM utilizes Lua? Any help will be very appreciated.
Use io.popen instead of awful.spawn.easy_async. Yes, normally using io.popen is really not recommended, but here the following happens:
Awesome starts
You call easy_async to capture the output of xinput list
Since it is async, your config continues to be loaded, so e.g. all your keybindings are set
easy_async does its job and you set modkey to something else.
This means that any keybinding which will be defined from now on use the new modkey, but all already-existing keybindings are not modified by this. So, basically nothing happens.
And for your debugging calls to naughty.notify: The one after the function is triggered first and only then, later, the inner one triggers. So it does not go back, but instead you first show the old value and only later the new one.

changing a variable using gets.chomp()

im trying to write to a file using this code:
puts "-------------------- TEXT-EDITOR --------------------"
def tor(old_text)
old_text = gets.chomp #
end
$epic=""
def torr(input)
tore= $epic += input + ", "
File.open("tor.txt", "w") do |write|
write.puts tore
end
end
loop do
output = tor(output)
torr(output)
end
i have read the ultimate guide to ruby programming
and it says if i want to make a new line using in the file im writing to using File.open
i must use "line one", "line two
how can i make this happend using gets.chomp()? try my code and you will see what i mean
thank you.
The gets method will bring in any amount of text but it will terminate when you hit 'Enter' (or once the STDIN receives \n). This input record separator is stored in the global variable $/. If you change the input separator in your script, the gets method will actually trade the 'Enter' key for whatever you changed the global variable to.
$/ = 'EOF' # Or any other string
lines = gets.chomp
> This is
> multilined
> textEOF
lines #=> 'This is\nmultilined\ntext'
Enter whatever you want and then type 'EOF' at the end. Once it 'sees' EOF, it'll terminate the gets method. The chomp method will actually strip off the string 'EOF' from the end.
Then write this to your text file and the \n will translate into new lines.
File.open('newlines.txt', 'w') {|f| f.puts lines}
newlines.txt:
This is
multilined
text
If you dont use .chomp() the \n character will be added whenever you write a new line, if you save this to the file it also will have a new line. .chomp() removes those escape characters from the end of the input.
If this doesnt answer your question, i am sorry i dont understand it.

Resources