Neovim Lua isdirectory vim function - lua

I am hoping to set up keymaps for when I am in a git directory and when I am not. Maybe now that I am using Lua that maybe I cannot do this like this. Any help would be appreciated.
if vim.fn.isdirectory('.git') then
map('n', '<leader>t', '<cmd>lua require(\'telescope.builtin\').git_files({hidden = true})<CR>', options)
else
map('n', '<leader>e', '<cmd>lua require(\'telescope.builtin\').find_files({hidden = true})<CR>', options)
end
Seems like if always hits and never the else.

:h isdirectory
isdirectory({directory})
isdirectory() The result is a Number,
which is non-zero when a directory with the name {directory} exists.
If {directory} doesn't exist, or isn't a directory, the result is
FALSE. {directory} is any expression, which is used as a String.
:h FALSE
For boolean operators Numbers are used. Zero is FALSE, non-zero is
TRUE. You can also use |v:false| and |v:true|. When TRUE is returned
from a function it is the Number one, FALSE is the number zero.
Make sure that FALSE is actually false. It is probably 0 which would be a true value in Lua.
In Lua any values but false or nil are true.
So ideally check if vim.fn.isdirectory('.git') ~= 0 then

Related

Can a boolean be included in an "if" statement?

I'm just starting out on learning Lua (for 4 days now) and when running this code, I get an error: input:2: 'then' expected near '='
Here's the code I am using:
local imagineVar = true
if imagineVar = true then
print("LOL")
end
How do I fix this?
if imagineVar then
print("LOL")
end
in lua, anything in if statment will be true except false and nil
The error you're getting is a syntax error because assignments (var = something) are statements rather than expressions in Lua - that means they don't evaluate to a value and thus can't be used in an if-condition (or anywhere else where an expression is expected).
As others have pointed out, you'd use the operator == for comparison. It is however more idiomatic to check for truthiness if your variable is a boolean: if imagineVar then ... end; the body of the if will run only if imagineVar is not nil or false.
Compare needs double '='
local imagineVar = true
if imagineVar == true then
print("LOL")
end

Why does clang-format wrap this long line the way it does?

In clang-format the following template function declaration gets wrapped to three lines. I think it should only be wrapped to two. Is the problem in my .clang-format file or a bug in the tool?
template <class VEC3_T, class FLOAT_T>
FLOAT_T functionNamedBlahBlahhh(const VEC3_T blabla, const VEC3_T bla, FLOAT_T blah1,
FLOAT_T blah2) // foo
{
}
I am using clang-format version 10.0.0.
My .clang-format file is:
BasedOnStyle: LLVM # (LLVM, Google, Chromium, Mozilla, WebKit)
AccessModifierOffset: -4
AlignEscapedNewlines: Left
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
BreakBeforeBraces: Custom # If this is "Custom" then use the BraceWrapping settings
BraceWrapping:
AfterClass: false
AfterStruct: false
AfterUnion: true
AfterEnum: true
AfterNamespace: false
AfterControlStatement: false
AfterFunction: true
AfterExternBlock: false
BeforeCatch: true
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakConstructorInitializers: AfterColon
ColumnLimit: 130 # Drawback: AllowShort*OnASingleLine kind of goes crazy with it. Is there a Penalty property to adjust this?
IncludeBlocks: Regroup
IndentCaseLabels: false
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
PointerAlignment: Left
SpaceBeforeCpp11BracedList: true
UseTab: Never
I see a few other cases where it wraps a long line to 3+ lines, as well. Here's one more:
OCLShaderEnv::OCLShaderEnv(OCLEnv* oclEnv, SVMStruct* svm, std::string prog, std::string kernelName, int width, int height,
std::string buildOptions, Camera* camera, void* otherStuff, std::array<float, 4> fillColor) :
m_device(oclEnv->getDevice()),
m_context(oclEnv->getContext()), m_cmdQ(oclEnv->getCmdQ()), m_prog(prog), m_kernelName(kernelName), m_width(width),
m_height(height), m_camera(camera), m_svmStructPtr(svm), m_oclEnv(oclEnv)
{
}
Note the three lines of initializers that could easily fit on two.
In your first example, there appears to be an interaction between 1) starting with just one of the parameters on a line, and 2) having a comment on that line. If you remove the comment, it formats to:
template <class VEC3_T, class FLOAT_T>
FLOAT_T functionNamedBlahBlahhh(const VEC3_T blabla, const VEC3_T bla, FLOAT_T blah1, FLOAT_T blah2)
{
}
as you would expect. If you leave the comment, but start with all parameters on one line, it leaves them all on one line, as you would expect.
It seems like a bug in clang-format to me. Possibly it is intended behavior, thinking that the comment must apply to just that parameter and so shouldn't be moved around? But it's hard to imagine that being very useful.
Workaround: Remove the comment, or manually put all parameter together on one line.
In your second example, this seems to be an interaction between 1) having a constructor with enough parameters that the parameters take multiple lines, 2) having multiple initializers for that constructor, and 3) setting BreakConstructorInitializers: AfterColon. In this case, the first initializer is put on its own line even if it could be combined with the subsequent initializers.
This also seems like a bug in clang-format to me.
Workaround: Reduce the parameters to the constructor so that they fit on one line - which I know you shouldn't have to do, but might make your code cleaner anyway. Or set BreakConstructorInitializers to something other than AfterColon. Or just live with the sub-optimal formatting, which is probably what I would do :(.

vlc.playlist.loop automation

Basically I have this .lua code in my vlc extension, now I am having problems in setting the VLC repeat automatically to loop all in a playlist.
I have tried setting it into "all", 0, 1, 2, true, "TRUE" but it does just not set the playlist's loop value into "all".
I also cannot get the playlist loop value. I tried object.playlist().loop, vlc.playlist.loop.
I read the VLC's Lua Script and Extensions page but I still cannot get what is supposed to be that
<status>
value or any of its acceptable strings.
function trigger()
vlc.playlist.stop()
vlc.playlist.sort("random")
vlc.playlist.goto(0)
--vlc.playlist.repeat_(<status>)
--vlc.playlist.loop(<status>)
vlc.playlist.play()
end
Solved it, thanks to Vyacheslav and Piglet
this would now set the vlc playlist loop to loop all, but I cannot print or vlc.msg.info the value of playlist.loop though. But it works in the end.
function trigger()
vlc.playlist.stop()
vlc.playlist.sort("random")
vlc.playlist.goto(0)
playlist = vlc.object.playlist();
if vlc.var.get(playlist,"loop") == false then vlc.playlist.loop() end
vlc.playlist.play()
end
I have not tried it but following the documentations logic status should be either nil, true or false. If nil (or no argument) it will toggle the current state. true will enable, false disable loop or repeat.
Not sure why you expect loop to be "all".
Did you remove the -- in front of the respective line?
I assume setting both doesn't make sense. And they will toggle each other internally?
In common case, if you do not know how it properly works, it is possible to search such code in github.com.
In your case, you can use this:
https://github.com/search?utf8=%E2%9C%93&q=vlc.playlist.loop+language%3ALua&type=Code&ref=searchresults
As you can see,
you can use such code:
elseif command == "pl_loop" then
vlc.playlist.loop()
elseif command == "pl_repeat" then
vlc.playlist.repeat_()
or
elseif c == 'loop' then
if vlc.playlist.loop(v) then
vlc.playlist.repeat_('off')
end
elseif c == 'repeat' then
if vlc.playlist.repeat_(v) then
vlc.playlist.loop('off')
end

what does ? ? mean in ruby [duplicate]

This question already has answers here:
How do I use the conditional operator (? :) in Ruby?
(7 answers)
Closed 7 years ago.
What does the line below checks and perform?
prefix = root_dir.nil? ? nil : File.join(root_dir, '/')
Here is the block that contains the line of code.
def some_name(root_dir = nil, environment = 'stage', branch)
prefix = root_dir.nil? ? nil : File.join(root_dir, '/')
.
.
.
i know that the '?' in ruby is something that checks the yes/no fulfillment. But I am not very clear on its usage/syntax in the above block of code.
Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false.
When you write a function that can only return true or false, you should end the function name with a question mark.
The example you gave shows a ternary statement, which is a one-line if-statement. .nil? is a boolean function that returns true if the value is nil and false if it is not. It first checks if the function is true, or false. Then performs an if/else to assign the value (if the .nil? function returns true, it gets nil as value, else it gets the File.join(root_dir, '/') as value.
It can be rewritten like so:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
This is called a ternary operator and is used as a type of shorthands for if/else statements. It follows the following format
statement_to_evaluate ? true_results_do_this : else_do_this
A lot of times this will be used for very short or simple if/else statements. You will see this type of syntax is a bunch of different languages that are based on C.
The code is the equivalent of:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
See a previous question

LUA: Seeking efficient and error-free means of assigning default arguments

Instead of using long lists of arguments in my function definitions, I prefer to pass a few fixed parameters and a table of 'additional params' like this:
function:doit( text, params )
end
This is nice as it allows me to add new named parameters later without breaking old calls.
The problem I am experiencing occurs when I try to force default values for some of the params:
function:doit( text, params )
local font = params.font or native.systemBold
local fontSize = params.fontSize or 24
local emboss = params.emboss or true
-- ...
end
The above code works fine in all cases, except where I have passed in 'false' for emboss:
doit( "Test text", { fontSize = 32, emboss = false } )
The above code will result in emboss being set to true when I really wanted false.
To be clear, what I want is for the first non-NIL value to be assigned to emboss, instead I'm getting a first non-false and non-NIL.
To combat this problem I wrote a small piece of code to find the first non-NIL value in a table and to return that:
function firstNotNil( ... )
for i = 1, #arg do
local theArg = arg[i]
if(theArg ~= nil) then return theArg end
end
return nil
end
Using this function I would re-write the emboss assignment as follows:
local emboss = firstNotNil(params.emboss, true)
Now, this certainly works, but it seems so inefficient and over the top. I am hoping there is a more compact way of doing this.
Please note: I found this ruby construct which looked promising and I am hoping lua has something like it:
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
Lua's relational operators evaluate to the value of one of the operands (i.e. the value is not coerced to boolean) so you can get the equivalent of C's ternary operator by saying a and b or c. In your case, you want to use a if it's not nil and b otherwise, so a == nil and b or a:
local emboss = (params.emboss == nil) and true or params.emboss
Not as pretty as before, but you'd only need to do it for boolean parameters.
[snip - Lua code]
Now, this certainly works, but it seems so inefficient and over the top.
Please note: I found this ruby construct which looked promising and I am hoping lua has
something like it:
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
Your Lua function is no more over-the-top or inefficient. The Ruby construct is more succinct, in terms of source text, but the semantics are not really different from firstNotNil(c,b,a). Both constructs end up creating a list object, initialize it with a set of values, running that through a function that searches the list linearly.
In Lua you could skip the creation of the list object by using vararg expression with select:
function firstNotNil(...)
for i = 1, select('#',...) do
local theArg = select(i,...)
if theArg ~= nil then return theArg end
end
return nil
end
I am hoping there is a more compact way of doing this.
About the only way to do that would be to shorten the function name. ;)
If you really want to do it in a single line, you'll need something like this for a default value of true:
local emboss = params.emboss or (params.emboss == nil)
It's not very readable, but it works. (params.emboss == nil) evaluates to true when params.emboss is not set (when you would need a default value), otherwise it's false. So when params.emboss is false, the statement is false, and when it's true, the statement is true (true or false = true).
For a default of false, what you tried originally would work:
local emboss = params.emboss or false

Resources