How to keep empty line in Dart code inside curly braсets? - android-studio

How to keep empty line in dart code inside curly braсets? By default, Android studio automaticaly removes them. How to change that?

Don't fight the formatter. It will win.
The way to avoid a { and } being collapsed is to have something between them.
I'd recommend something like:
Future<void> displayRangePicker(BuildContext context) async {
// Intentionally left empty.
}
or even just:
Future<void> displayRangePicker(BuildContext context) async {
return;
}
(which is equivalent to what you wrote, just being explicit about the return).

Related

Enforce space character before function brackets with eslint

I'm used to PHP PSR-4 style and I setup my VS Code to auto indent function like that:
Incorrect:
public function foo() {
// here goes something
}
Correct:
public function foo ()
{
// here goes something
}
I would like to enfore a similar style with Eslint for JavaScript (of course), in this case, not exactly the same but like this one:
function bar () {
}
I just want that always before the brackets it enforces a space character, is there any rule to make it work?
Some rule like space-before-brackets

Haxe 4: Uncaught exception macro-in-macro

I had code like this working with Haxe 3:
macro public static function get(key:String)
{
return Context.makeExpr(Context.definedValue(key), Context.currentPos());
}
However, after migrating to Haxe 4 this fails to compile with error:
Uncaught exception macro-in-macro
How should I go about migrating this function to Haxe 4? Is there a better way to access build flags in order to avoid this issue?
As #Gama11 alluded to, there's not actually a problem with your macro function, there's a problem with where you're calling it from. (Haxe 4 may have gotten more strict with these checks.)
If you have:
Main.hx
class Main
{
public static function main()
{
// Can call get from here:
var cvar = MacroUtil.get('cvar');
MacroUtil.some_macro_function();
trace('Hello world! cvar=${ cvar }');
}
}
MacroUtil.hx
import haxe.macro.Context;
import haxe.macro.Expr;
class MacroUtil
{
macro public static function get(key:String):Expr
{
return Context.makeExpr(Context.definedValue(key), Context.currentPos());
}
macro public static function some_macro_function()
{
// Cannot call get from here:
var cvar:Expr = get('cvar');
trace('will trace at compile time, and cvar is ${ cvar }');
return macro trace('will trace at runtime');
}
}
And execute it with: haxe -x Main -D cvar=abc
That will generate the error you're experiencing. It's because in some_macro_function, you're already in the macro context, so you can't call the macro function get from there.
There are a couple ways of dealing with this.
One Approach
You can use #if macro / if !macro to detect the macro context and adjust accordingly. So as silly as this looks, it does indeed solve your particular problem:
class MacroUtil
{
#if !macro macro #end public static function get(key:String):Expr
{
This function signature says, if I'm already in the macro context, don't consider this function a macro function. It's just a static helper at that point. And remember that it returns an Expr, not a String like it does in the main context.
If you mix macro and non-macro functions in a single file, you may also find yourself needing to use #if macro to avoid this condition as well.
Another Approach
You can refactor your macro functions into macro functions and macro helpers. It's a little more verbose, but maybe a little more clear as to what's happening:
MacroUtil.hx
import haxe.macro.Context;
import haxe.macro.Expr;
class MacroUtil
{
macro public static function get(key:String):Expr
{
return Context.makeExpr(MacroHelpers.get_define(key), Context.currentPos());
}
macro public static function some_macro_function()
{
// Cannot call get from here:
var cvar:String = MacroHelpers.get_define('cvar');
trace('will trace at compile time, and cvar is ${ cvar }');
return macro trace('will trace at runtime');
}
}
class MacroHelpers
{
public static function get_define(key:String):String
{
return Context.definedValue(key);
}
}
If you do it this way, then your macro functions all call the MacroHelpers, and non-macro function call the MacroUtils. Notice the helper returns a String, and it's up to the call-site to then convert it to an expression, if that's what they want.
We ended up removing this whole get method and switching occurrences to use Compiler.getDefine() instead, which is supported both by Haxe 3 and 4.
I believe the problem we were facing was related with the fact that this static macro get was being called from our test runner script, so that probably was the place where a macro was calling another macro. Still, I tried to put the solution suggested by Jeff Ward in place but kept getting the same result.

Is there a simple way to convert lambda into a full expression in Kotlin with Android Studio 3.5.1?

The code in popup.setOnMenuItemClickListener is lambda expression, the function of the code is to show a popup menu.
It's hard to understand lambda expression sometimes.
Is there a simple way to convert a lambda into a full expression ? Is there some utilities to do that?
Code
private fun showPopup(v: View, mContext: Context) {
val popup = PopupMenu(mContext, v)
popup.inflate(R.menu.menu_popup_more)
popup.setOnMenuItemClickListener {
when (it.itemId) {
R.id.MenuMoreAbout->{
requireActivity().openActivity<UIAbout>()
}
}
true // Why do I need add 'true'
}
popup.show()
}
Is there a simple way to convert a lambda into a full expression ?
Yes there is! First position the cursor on the first curly brace
Position your cursor at this character
|
v
popup.setOnMenuItemClickListener {
when (it.itemId) {
R.id.MenuMoreAbout->{
requireActivity().openActivity<UIAbout>()
}
}
true // Why do I need add 'true'
}
then type Alt+Enter (Option+Enter for Mac) to bring up the Quick Fix menu. Select Convert to anonymous function. That will turn the code into the following:
popup.setOnMenuItemClickListener(fun(it: MenuItem): Boolean {
when (it.itemId) {
R.id.MenuMoreAbout -> {
requireActivity().openActivity<UIAbout>()
}
}
return true
})
which is clearer for someone that is new to Kotlin syntax.
As can be seen, your true expression was the return value of the lambda. In Kotlin, the return value of a lambda is the value of the last expression in the lambda. If you prefer, you can make this explicit by replacing
true // Why do I need add 'true'
with
return#setOnMenuItemClickListener true
in your original code. See official docs for Return at Labels for more information.
The reason a Boolean has to be returned is because the lambda is for the following Java interface which is the type of the argument to setOnMenuItemClickListener(OnMenuItemClickListener)
public interface OnMenuItemClickListener {
boolean onMenuItemClick(MenuItem item);
}
As you can see, onMenuItemClick(MenuItem) returns a boolean (which is converted to Boolean in Kotlin). So the lambda must also return a Boolean.

VIM wrap block in curly braces (CSS code)

kind of new to VIM so be kind please.
I want to do the following:
.css-class {
some: 'rule';
}
to:
.another-class {
.css-class {
some: 'rule';
}
}
I tried surround.vim with S{ in visual mode with the block selected. But this ends up here:
{ .css-class {
some: 'rule';
} }
which doesn't really help much. If it would add the line breaks it would help but still... What would be the best way to do it?
Use Visual-line mode, V, instead of v.
VipSB.another-class
You can also use the yS operator if you do not want to use visual mode:
ySipBi.another-class

Checking if control exists throws an error

Really what I am after is a way to check if the control exists without throwing an error.
The code should look something like this:
Control myControl = UIMap.MyMainWindow;
if (!myControl.Exists)
{
//Do something here
}
The problem is that the control throws an error because it is invalid if it doesn't exist, essentially making the exists property useless.
What is the solution?
In this case I am using the tryfind method.
Like this:
HtmlDiv list = new HtmlDiv(Window.GetWebtop());
list.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
list.SearchProperties.Add(HtmlDiv.PropertyNames.InnerText, "Processing search", PropertyExpressionOperator.Contains);
if (list.TryFind())
{
//DO Something
}
I am re-posting the comment kida gave as a answer, because I think its the best solution.
Control myControl = UIMap.MyMainWindow;
if (!myControl.FindMatchingControls().Count == 0)
{
//Do something here
}
The FindMatchingControls().Count is much faster then the Try Catch or the TryFind. Since it does not wait for SearchTimeoutto check if the element is now there. Default it waits 30 seconds for the element to not be there, but I like my tests to fail fast.
Alternatively its possible to lower the Playback.PlaybackSettings.SearchTimeout before the Catch or TryFind and restore it afterwards, but this is unnecessary code if you ask me.
You can do one of two things: Wrap your code in a try-catch block so the exception will be swallowed:
try
{
if (!myControl.Exists)
{
// Do something here.
}
}
catch (System.Exception ex)
{
}
Or, you could add more conditions:
if (!myControl.Exists)
{
// Do something here.
}
else if (myControlExists)
{
// Do something else.
}
else
{
// If the others don't qualify
// (for example, if the object is null), this will be executed.
}
Personally, I like the catch block, because if I expect the control to be there as part of my test, I can Assert.Fail(ex.ToString()); to stop the test right there and log the error message for use in bug reporting.
If you are sure that control will exist or enabled after some time you can use WaitForControlExist() or WaitForControlEnabled() methods with a default timeout or specified timeout.
I have a situation like this and I am looping until the control is available :
bool isSaveButtonExist = uISaveButton.WaitForControlEnabled();
while (!isSaveButtonExist )
{
try
{
uISaveButton.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
uISaveButton.SetFocus(); // setting focus for the save button if found
isSaveButtonExist = uISaveButton.WaitForControlExist(100);
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message); // exception for every set focus message if the control not exist
}
}
// do something with found save button
// Click 'Save' button
Mouse.Click(uISaveButton, new Point(31, 37));
please refer to this link for more about these Methods:
Make playback wait methods

Resources