How to make ReSharper stop unwrapping my lambdas in cshtml? - resharper

I have the following code in a .cshtml file:
#(Html.Kendo().DropDownListFor(m => m.TypeFk)
.DataSource(ds =>
{
ds.Read(r =>
{
r.Action(nameof(TaskController.GetTypes), "Task").Data("getTmNum");
}).ServerFiltering(true);
}))
When I run ReSharper's Cleanup Code function on the file, it changes it to:
#(Html.Kendo().DropDownListFor(m => m.TypeFk)
.DataSource(ds => { ds.Read(r => { r.Action(nameof(TaskController.GetTypes), "Task").Data("getTmNum"); }).ServerFiltering(true); }))
I've tried to find the option to configure this under ReSharper | Options | C# | Formatting Style | Line Breaks and Wrapping. But I can't seem to find it.
How do I make this stop?

You need to turn off an option called "Place simple anonymous method on single line".

Related

Pass a static string to a macro_rule in rust

I wrote the following code :
macro_rules! my_macro{
("A") => {
println!("Macro called !")
}
}
fn main(){
static test: &'static str = "A";
my_macro!(test);
}
but I have the following error :
error: no rules expected the token `test`
--> test.rt:9:19
|
1 | macro_rules! my_macro{
| --------------------- when calling this macro
...
9 | my_macro!(test);
| ^^^^ no rules expected this token in macro call
error: aborting due to previous error
However, it works fine if I directly call my_macro("A"). Is it possible to fix this ?
Is it possible to fix this ?
No. Macros are expanded at compile time before item names are resolved, therefore your macro has no idea what the value of test is (and would have no idea even if it were a const rather than a static).
so the first problem here is that you macro expects a pattern of "A" not a variable that contains "A"
when you create macros you define certain patterns and follow those patterns in your case your macro must always have "A" in it but it is not a string a it is a pattern of double quote followed by capital a followed by another double quote
If you want to pass a value you should use variable syntax and define what it should expect such as ($a:expr)=>{...}
here you can see all magic tokens possible just scroll down a bit on that docs there are a lot of great examples
PS. here is a macro I use for responding from my endpoints
macro_rules! resp {
(ok) => {
|_| actix_web::HttpResponse::Ok().body(r#"{"success":true}"#)
};
(ok,$data:expr) => {
|_| actix_web::HttpResponse::Ok().json(serde_json::json!({"success":true,"data":$data}))
};
(ok,) => {
|d| actix_web::HttpResponse::Ok().json(serde_json::json!({"success":true,"data":d}))
};
}

logstash prune plugin causes json to become text (/ruby objects)

I'm using logstash that outputs to coralogix. It works fine, unless i use the prune plugin to whitelist fields. once i use the prune plugin, it outputs text/string instead of a json object.
the used code:
input {
beats {
port => 5000
}
}
filter {
if [should_prune] {
prune {
whitelist_names => [
"^test$",
"^#timestamp$",
"^tags$"
]
}
add_tag => [ "pruned" ]
}
}
output {
coralogix_logger {
config_params => {
"PRIVATE_KEY" => "********"
"APP_NAME" => "myawesomeapp"
"SUB_SYSTEM" => "subapp"
}
is_json => true
}
}
example of output is:
{"#timestamp"=>2019-07-15T06:47:57.364Z, "tags"=>["pruned"], "test"=>"ok"}
instead of:
{"#timestamp":2019-07-15T06:47:57.364Z, "tags":["pruned"], "test":"ok"}
any logs that are not pruned (in this scenario, doesn't contain the should_prune field), are passed just fine.
any ideas?
thanks!
Apparently my issues was not related to the prune plugin, it was related to the plugin I used to output my logs (in my case - coralogix). had to update my output plugin (https://github.com/coralogix/logstash-output-coralogix).
if you encounter this issue and not using coralogix, another method to resolve this can be using the "json_encode" plugin https://www.elastic.co/guide/en/logstash/current/plugins-filters-json_encode.html
generally speaking, it should work out of the box - just like the configuration i used in the initial question.

Selecting (and deleting) entire function with definition (INCLUDING whitespace) in Vim

I recently switched to using Vim (with VSCode) as my editor.
I'm trying to delete a function with it's definition in JavaScript. I looked on google and here on StackOverflow and found this question. Unfortunately the answers for this question only work for functions without white space.
Here is how my function looks:
const useBattery = () => {
const [battery, setBattery] = useState({ level: 0, charging: false });
const handleChange = ({ target: { level, charging } }) => setBattery({ level, charging });
useEffect(() => {
let battery;
navigator.getBattery().then(bat => {
battery = bat;
battery.addEventListener("levelchange", handleChange);
battery.addEventListener("chargingchange", handleChange);
handleChange({ target: battery });
});
return () => {
battery.removeEventListener("levelchange", handleChange);
battery.removeEventListener("chargingchange", handleChange);
};
}, []);
return battery;
};
I tried several approaches, the best one was da{ when my cursor is within the function. This motion will delete the function body, but not the definition.
Is there any way to delete the function and the definition in one motion using Vim, if there is white space in the function?
From inside the function, as you say da{ deletes only the braces and its content, without the preceding declaration or the following semicolon. However... if we switch to linewise...?
There is a semi-hidden section a bit under :help exclusive-linewise with bold heading but no tag to jump to: "FORCING A MOTION TO BE LINEWISE, CHARACTERWISE OR BLOCKWISE", saying that we can switch to a non-default selection by using v (characterwise), V (linewise) or Ctrl-V (blockwise) immediately after the operator. So...
dVa{
As mentioned in the post you linked to, d]] when the cursor is placed at the beginning of the function definition will delete the whole function.

Why does a match never use the second match arm?

I am trying to make reimplementation of first 2 Final Fantasy games using original data from various platforms. I want to get 2 program arguments using the getopts crate and handle both of them by using match but it just executes the first match element. I think I screwed up something with types.
Maybe there is another way to do it? I am lost using the official Rust docs and any tutorials on internet are not really noob-friendly.
Here is the code:
let args: Vec<String> = env::args().map(|x| x.to_string()).collect();
if(args.len() < 3) {
println!("=====ERROR=====\nInvalid number of parameters\nExpected: <gamename> <gamerom>\nType in: 'help me' to get some help.");
process::exit(1);
}
let ref game = args[1];
let ref rom = args[2];
match game {
help => {
println!("=====HELP======");
match rom {
list => println!("Available games: ff1, ff2\nAvailable roms: ff1_j_msx, ff1_j_nes, ff1_u, ff1and2, ff2_j, ff2_u_proto"),
me => println!("Available help commands:\nlist -> List of available games and roms.\nme -> This help"),
_ => println!("=====ERROR=====\nInvalid help command.")
}
},
_ => println!("=====ERROR=====\nInvalid game, type in 'help me' to get some help.")
}
You really need to read the compilers error and warning messages. This code has seven warnings. If you had addressed any of them you'd be a lot closer to fixing the problem yourself. If you'd fixed all of them, your problem would be gone.
Here's a representative warning where the compiler tells you exactly what the problem is:
warning: unreachable pattern
--> src/main.rs:24:5
|
24 | _ => println!("=====ERROR=====\nInvalid game, type in 'help me' to get some help.")
| ^ this is an unreachable pattern
|
= note: #[warn(unreachable_patterns)] on by default
note: this pattern matches any value
--> src/main.rs:15:5
|
15 | help => {
| ^^^^
When you use just help, that creates a new variable with the value you are matching on. In this case, it matches everything, so the subsequent arms can never match.
Instead, you need to match against a string literal:
match game.as_str() {
"help" => {
match rom.as_str() {
"list" => /* ... */,
"me" => /* ... */,
_ => /* ... */,
}
},
_ => /* ... */,
}
I'd strongly encourage you to go back and re-read The Rust Programming Language. It's where a lot of the beginner documentation is kept. Specifically, you should read from the beginning and then up through the chapter on match and the chapter on patterns.

set selected value for the kendo-combobox when data bound with async pipe

I have a sample code like this
<kendo-combobox [allowCustom]="false"
[suggest]="true"
[data]="dropDownButtonItemsObs | async"
[(ngModel)]="selectedComboBoxItem"
[textField] = "'text'" [valueField] = "'value'">
</kendo-combobox>
The dropDownButtonItemsObs is a Observable, data retrieved from the web api.
With my example above - using async pipe, can you guide an example how to programmatic set the selected item for the combo box?
figured out how to have it set up, however I feel my code is not the fully exactly the option using async pipe as it is in the document http://www.telerik.com/kendo-angular-ui/components/dropdowns/combobox/data-binding/#toc-async-pipe
this.dropDownButtonItemsObs = this.dashboardService.getDropdownItems();
this.dropDownButtonItemsObs.subscribe(dropdownItems => {
this.dashboardService.getDropdownSelectedValue().subscribe(data => {
this.selectedComboBoxItem = dropdownItems.find(item => { return item.value === 2 });
});
});

Resources