LSP server on_init per project configuration - lua

I'm noob in Lua and after many hours of searching, it wasn't successful.
I want to load some EFM language server configurations depends on a project setup. So, I found this: https://github.com/neovim/nvim-lspconfig/wiki/Project-local-settings
For example: in A project I have a "black" code formatter, in B project I have "yapf" code formatter.
After that I wrote custom on_init handler:
...
on_init = function(client)
-- We need to register only available linters and formatters, because it improves perfomance↴
local result = vim.fn.systemlist(
[[ poetry run pip list --disable-pip-version-check | grep -w -o '^black \|^yapf \|^flake8 \|^isort \|^mypy ' ]]
for _, package_name in pairs(result) do
local package_config = M[string.gsub(package_name, "%s+", "")]
if package_config then
table.insert(client.config.settings.languages.python, package_config)
end
end
client.notify("workspace/didChangeConfiguration")
return true
end,
...
Well, it works, but i have a problem with a custom command, it's sync and freezes UI on first LSP load. Is it possible to load on_init async or execute cmd async?

Related

Use fdfind and fzf to open files in mpv

kdialog-open-files.lua and zenity-open-files.lua are two projects that use KDE KDialog and Gnome's zenity respectively to open files in mpv. I wish to do the same using fdfind and fzf.
As illustrated in this stackoverflow answer the following works and will be our starting point:
fdfind . /path/to/Music | fzf -m | xargs -d "\n" mpv
We can use the above command to select some files and play them using mpv. However we want a dialog to be created for our file selection. To do this, we create a bash script menu_fzf.sh with the following contents:
#!/bin/bash
urxvt -name fzf_menu -e bash -c "fzf -m $* < /proc/$$/fd/0 > /proc/$$/fd/1"
(urxvt is only an example and any other suitable terminal emulator can be used in the above).
Now if we run:
fdfind . /path/to/Music | menu_fzf.sh
then a new terminal opens that lets you pick multiple files from /path/to/Music with fzf and will return the selection as output to the calling terminal. So in fact right now we could run the following
fdfind . /path/to/Music | menu_fzf.sh | xargs -d "\n" mpv
and it will play the selected files in mpv. So far so good!
The tricky part now is how to do all the above using lua scripts from within mpv. By looking at the kdialog-open-files.lua or zenity-open-files.lua it seems I need to write something like this:
utils = require 'mp.utils'
-- TODO: make the following work so that file_select correctly stores
-- the files selected using menu_fzf.sh:
file_select = utils.subprocess({
args = {'command', 'argument1', 'argument2', 'and so on'},
cancellable = false,
})
-- and then the rest of the code from kdialog-open-files.lua
However I am finding the official manual for utils.subprocess to be terse and inadequate. I have creatively tried picking command, argument1, argument2, and so on in the above, but nothing seems to work!
So I have two questions:
Can you (please) complete the above code template into a working solution?
Do you know where I can find more information on utils.subprocess? Is there a more detailed manual somewhere? Where is the source-code?
Remark: As I have never programmed in lua before I am struggling with solving question-1 above and will probably continue to struggle even if I had the source code for utils.subprocess. So really help with question-1 is of higher priority. But any help with question-2 is also great!
My failed attempts so far:
The following will append all files in current directory to playlist:
file_select = utils.subprocess({
args = {'fdfind', '.', directory},
cancellable = false,
})
The following fails:
file_select = utils.subprocess({
args = {'fdfind', '.', directory, '|', 'fzf'},
cancellable = false,
})
with error message:
[open_files] [fd error]: Search path '|' is not a directory.
[open_files] [fd error]: Search path 'fzf' is not a directory.
The following allows selecting multiple files from $HOME directory but (as expected) does not return the selection:
file_select = utils.subprocess({
args = {'urxvt','-e','fzf', '-m'},
cancellable = false,
})
The following doesn't work:
file_select = utils.subprocess({
args = {'urxvt','-e','fzf', '-m', '>', '/proc/$$/fd/1'},
cancellable = false,
})
The following opens up a new dialog with fzf but has nothing to select from:
file_select = utils.subprocess({
args = {'menu_fzf.sh'},
cancellable = false,
})
Create a bash script mpv_fzf_menu.sh with the following content:
#!/bin/bash
urxvt -name fzf_menu -e bash -c "fdfind . "$1" | fzf -m > /proc/$$/fd/1"
Create a lua file open-files.lua with the following content:
utils = require 'mp.utils'
function select_files_dmenu_fzf()
if mp.get_property("path") == nill then
directory = ""
else
directory = utils.split_path(utils.join_path(mp.get_property("working-directory"), mp.get_property("path")))
end
file_select = utils.subprocess({
args = {'mpv_fzf_menu.sh', directory},
cancellable = false,
})
end
function add_files()
select_files_dmenu_fzf()
if (file_select.status ~= 0) then return end
local first_file = true
for filename in string.gmatch(file_select.stdout, '[^\n]+') do
mp.commandv('loadfile', filename, first_file and 'replace' or 'append')
first_file = false
end
end
mp.add_key_binding("Ctrl+f", "add-files-kdialog", add_files)
(I am assuming you know what to do next: make the bash script executable and put it somewhere where bash finds it in its $PATH. Put the lua script in .config/mpv/scripts and everything should work, i.e. - when you press Ctrl+f within mpv a dialog should open to let you select files).
Remark: All the functionalities of kdialog-open-files.lua have not been implemented. But the above lua script can be easily extended. You can further make things fancy by filtering for files with the specified extension with fdfind. And you can also use the bash script independent of the lua script. If you configure the --preview option of fzf then you can get a file preview before making selection.

Lua: forward output from Minicom

I have a Lua script and in there I open a minicom session which executes a script (with the -S" parameter).
local myFile = assert(io.popen('minicom -S myScript.sh ' myDevice ' -C myLogFile.log'))
local myFileOutput = myFile:read('*all')
myFile:close()
This works really fine.
But I would like to get the same output as if I execute the minicom command itself:
minicom -S myScript.sh ' myDevice ' -C myLogFile.log
Right now I don't get any output at all (I know that that's somehow obvious).
I would that the output should also occur at (at least nearly) the same time as with the minicom command itself. Not one big chuck of data at the end.
Does anyone know how to achieve that?
If I understand you correctly, you need something like
local myFile = assert(io.popen('minicom ...'))
for line in myFile:lines('l') do
print(line)
end
myFile:close()

How do I run a beam file compiled by Elixir or Erlang?

I have installed Erlang/OTP and Elixir, and compiled the HelloWorld program into a BEAM using the command:
elixirc test.ex
Which produced a file named Elixir.Hello.beam
How do I run this file?
Short answer: no way to know for sure without also knowing the contents of your source file :)
There are a few ways to run Elixir code. This answer will be an overview of various workflows that can be used with Elixir.
When you are just getting started and want to try things out, launching iex and evaluating expressions one at a time is the way to go.
iex(5)> Enum.reverse [1,2,3,4]
[4, 3, 2, 1]
You can also get help on Elixir modules and functions in iex. Most of the functions have examples in their docs.
iex(6)> h Enum.reverse
def reverse(collection)
Reverses the collection.
[...]
When you want to put some code into a file to reuse it later, the recommended (and de facto standard) way is to create a mix project and start adding modules to it. But perhaps, you would like to know what's going on under the covers before relying on mix to perform common tasks like compiling code, starting applications, and so on. Let me explain that.
The simplest way to put some expressions into a file and run it would be to use the elixir command.
x = :math.sqrt(1234)
IO.puts "Your square root is #{x}"
Put the above fragment of code into a file named simple.exs and run it with elixir simple.exs. The .exs extension is just a convention to indicate that the file is meant to be evaluated (and that is what we did).
This works up until the point you want to start building a project. Then you will need to organize your code into modules. Each module is a collection of functions. It is also the minimal compilation unit: each module is compiled into a .beam file. Usually people have one module per source file, but it is also fine to define more than one. Regardless of the number of modules in a single source file, each module will end up in its own .beam file when compiled.
defmodule M do
def hi(name) do
IO.puts "Hello, #{name}"
end
end
We have defined a module with a single function. Save it to a file named mymod.ex. We can use it in multiple ways:
launch iex and evaluate the code in the spawned shell session:
$ iex mymod.ex
iex> M.hi "Alex"
Hello, Alex
:ok
evaluate it before running some other code. For example, to evaluate a single expression on the command line, use elixir -e <expr>. You can "require" (basically, evaluate and load) one or more files before it:
$ elixir -r mymod.ex -e 'M.hi "Alex"'
Hello, Alex
compile it and let the code loading facility of the VM find it
$ elixirc mymod.ex
$ iex
iex> M.hi "Alex"
Hello, Alex
:ok
In that last example we compiled the module which produced a file named Elixir.M.beam in the current directory. When you then run iex in the same directory, the module will be loaded the first time a function from it is called. You could also use other ways to evaluate code, like elixir -e 'M.hi "..."'. As long as the .beam file can be found by the code loader, the module will be loaded and the appropriate function in it will be executed.
However, this was all about trying to play with some code examples. When you are ready to build a project in Elixir, you will need to use mix. The workflow with mix is more or less as follows:
$ mix new myproj
* creating README.md
* creating .gitignore
* creating mix.exs
[...]
$ cd myproj
# 'mix new' has generated a dummy test for you
# see test/myproj_test.exs
$ mix test
Add new modules in the lib/ directory. It is customary to prefix all module names with your project name. So if you take the M module we defined above and put it into the file lib/m.ex, it'll look like this:
defmodule Myproj.M do
def hi(name) do
IO.puts "Hello, #{name}"
end
end
Now you can start a shell with the Mix project loaded in it.
$ iex -S mix
Running the above will compile all your source file and will put them under the _build directory. Mix will also set up the code path for you so that the code loader can locate .beam files in that directory.
Evaluating expressions in the context of a mix project looks like this:
$ mix run -e 'Myproj.M.hi "..."'
Again, no need to compile anything. Most mix tasks will recompile any changed files, so you can safely assume that any modules you have defined are available when you call functions from them.
Run mix help to see all available tasks and mix help <task> to get a detailed description of a particular task.
To specifically address the question:
$ elixirc test.ex
will produce a file named Elixir.Hello.beam, if the file defines a Hello module.
If you run elixir or iex from the directory containing this file, the module will be available. So:
$ elixir -e Hello.some_function
or
$ iex
iex(1)> Hello.some_function
Assume that I write an Elixir program like this:
defmodule PascalTriangle do
defp next_row(m), do: for(x <- (-1..Map.size(m)-1), do: { (x+1), Map.get(m, x, 0) + Map.get(m, x+1, 0) } ) |> Map.new
def draw(1), do: (IO.puts(1); %{ 0 => 1})
def draw(n) do
(new_map = draw(n - 1) |> next_row ) |> Map.values |> Enum.join(" ") |> IO.puts
new_map
end
end
The module PascalTriangle can be used like this: PascalTriangle.draw(8)
When you use elixirc to compile the ex file, it will create a file called Elixir.PascalTriangle.beam.
From command line, you can execute the beam file like this:
elixir -e "PascalTriangle.draw(8)"
You can see the output similar to the photo:

error while executing lua script for redis server

I was following this simple tutorial to try out a simple lua script
http://www.redisgreen.net/blog/2013/03/18/intro-to-lua-for-redis-programmers/
I created a simple hello.lua file with these lines
local msg = "Hello, world!"
return msg
And i tried running simple command
EVAL "$(cat /Users/rsingh/Downloads/hello.lua)" 0
And i am getting this error
(error) ERR Error compiling script (new function): user_script:1: unexpected symbol near '$'
I can't find what is wrong here and i haven't been able to find someone who has come across this.
Any help would be deeply appreciated.
Your problem comes from the fact you are executing this command from an interactive Redis session:
$ redis-cli
127.0.0.1:6379> EVAL "$(cat /path/to/hello.lua)" 0
(error) ERR Error compiling script (new function): user_script:1: unexpected symbol near '$'
Within such a session you cannot use common command-line tools like cat et al. (here cat is used as a convenient way to get the content of your script in-place). In other words: you send "$(cat /path/to/hello.lua)" as a plain string to Redis, which is not Lua code (of course), and Redis complains.
To execute this sample you must stay in the shell:
$ redis-cli EVAL "$(cat /path/to/hello.lua)" 0
"Hello, world!"
If you are coming from windows and trying to run a lua script you should use this format:
redis-cli --eval script.lua
Run this from the folder where your script is located and it will load a multi line file and execute it.
On the off chance that anyone's come to this from Windows instead, I found I had to do a lot of juggling to achieve the same effect. I had to do this:
echo “local msg = 'Hello, world!'; return msg” > hello.lua
for /F "delims=" %i in ('type hello.lua') do #set cmd=%i
redis-cli eval "%cmd%" 0
.. if you want it saved as a file, although you'll have to have all the content on one line. If you don’t just roll the content into a set command
set cmd=“local msg = 'Hello, world!'; return msg”
redis-cli eval "%cmd%" 0

jenkins plugin for triggering build whenever any file changed in a given directory

I am looking for functionality where we have a directory with some files in it.
Whenever any one makes a change in any of the files in the directory, jenkins shoukd trigger a build.
Is there any plugin or mathod for this functionality. Please advise.
Thanks in advance.
I have not tried it myself, but The FSTrigger plugin seems to do what you want:
FSTrigger provides polling mechanisms to monitor a file system and
trigger a build if a file or a set of files have changed.
If you can monitor the directory with a script, you can trigger the build with a HTTP GET, for example with wget or curl:
wget -O- $JENKINS_URL/job/JOBNAME/build
Although slightly related.. it seems like this issue was about monitoring static files on system.. however there are many version control systems for just this purpose.
I answered this in another post if you're using git to track changes on the files themselves:
#!/bin/bash
set -e
job_name="whatever"
JOB_URL="http://myserver:8080/job/${job_name}/"
FILTER_PATH="path/to/folder/to/monitor"
python_func="import json, sys
obj = json.loads(sys.stdin.read())
ch_list = obj['changeSet']['items']
_list = [ j['affectedPaths'] for j in ch_list ]
for outer in _list:
for inner in outer:
print inner
"
_affected_files=`curl --silent ${JOB_URL}${BUILD_NUMBER}'/api/json' | python -c "$python_func"`
if [ -z "`echo \"$_affected_files\" | grep \"${FILTER_PATH}\"`" ]; then
echo "[INFO] no changes detected in ${FILTER_PATH}"
exit 0
else
echo "[INFO] changed files detected: "
for a_file in `echo "$_affected_files" | grep "${FILTER_PATH}"`; do
echo " $a_file"
done;
fi;
You can add the check directly to the top of the job's exec shell, and it will exit 0 if no changes detected.. Hence, you can always poll the top level of the repo for check-in's to trigger a build. And only complete a build if the files in question change.

Resources