Resharper force space after curly bracket and before closing bracket - resharper

Resharper is giving me this:
new MyObject {Prop1 = prop1, Prop2 = prop2}
But I want:
new MyObject { Prop1 = prop1, Prop2 = prop2 }
Currently it's breaking my StyleCop rules.

I think this is the setting you are looking for.
http://screencast.com/t/nEXMHo6Ko Dead link
--Edit--
In case of a dead link, The setting is under C# -> Formatting Style -> Spaces -> Within single-line initializer braces. Check the box next to it and you'll be all set.

you can get resharper to match stylecop here by putting these lines in .editorconfig
resharper_space_within_empty_braces = true
resharper_space_within_single_line_array_initializer_braces = true

Related

How to check expression body result on debug

I have expression like this in Kotlin:
fun getSomething() = create()
How to check in IntelliJ IDEA (Android Studio) during debug what value does function return?
If I just put breakpoint it does not show expression result.
UPD. I understand that if I rewrite code using a temporary variable I could debug it:
fun getSomething() {
var tmp = create()
return tmp
}
But how to do it without rewriting code?
Put the breakpoint after the line which calls the function / assigns the return value like this:
fun getSomething(i: Int) = 5 * i
fun main(args: Array<String>) {
val x = getSomething(5) // (1)
println(x) // (2)
}
(1) If you put the breakpoint here, x won't be assigned yet
(2) if you want to know the value of x, put the breakpoint on a line after the assignment (println is just used as an example here)
If you want to test what getSomething returns for different values of i,
highlight it and right-click in the editor window when the debugger stops at the breakpoint and choose "Evaluate Expression" (or hit Alt+Shift+8 on Windows or Linux)
In the window that opens you can evaluate any code which is in scope.

ReSharper Formatting - put method closing parenthesis on new line

When I declare or call a C# method with multi-line parameters like this:
public void DoSomething(
p1,
p2,
p3);
Then I would like ReSharper to format that code to like this:
public void DoSomething(
p1,
p2,
p3
);
Is there a setting that enables this behavior?
This feature is only available beginning with ReSharper 2018.1.
Those settings are located here:
Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping
-> Arrangement of Method Signatures -> Prefer wrap before ")" in declaration
-> Arrangement of Invocations -> Prefer wrap before ")" in invocation

Using groovy map for DSL with invalid keys

I'm currently building a small DSL, which needs to specify a set of properties in key=value pairs, however the keys may contain dashes '-' or periods '.' and I can't seem to get it to work.
Boiled down I essentially try passing a Map as a delegate to a closure, but the syntax keeps alluring me.
As an example, consider this:
def map = [:]
map.with {
example1 = 123
//exam-ple2 = 123
//'exam-ple3' = 123
//(exam-ple4) = 123
exam.ple5 = 123
//'exam.ple6' = 123
}
Example 1 is fine, key equals value and easy readable. Examples 2 and 4 are according to the compiler a binary expression and won't compile. Examples 3 and 6 are constant expressions and won't compile. Example 5 will compile, but generate a NPE at runtime.
I can use workarounds like passing the Map as an argument to the closure, which gives me example 3 and 6, but the verbosity of it annoys me.
Does anybody have any ideas how to neatly DSL a property map?
BTW: I call the DSL from java not groovy, so tricks on the parsing side has to be java :)
UPDATE 1 : After the inital comments and answers..
So the script is evaluated by a GroovyShell as a DelegatingScript, where the delegate is a Java object. The closure contains properties from .properties files, that needs to be defined in different context, e.g.
env {
server-name=someHost1
database.name=someHost2
clientName=someHost3
}
The delegating (Java) object would read this block as
public void env(Closure closure) {
Map map = new HashMap();
closure.setDelegate(map);
closure.setResolveStrategy(Closure.DELEGATE_ONLY);
closure.call();
... do something with map...
}
Now the user (i.e. not me) will probably copy from the original property files into the script and change the names, hence I would rather they could do it without having to edit too much as it is bound to cause typos..
As I stated I has also example 3 and 6 covered as well, but yes, Tim, I forgot the implicit it :)
For now I have changed the format to a string, so the DSL writes something like
env '''
server-name=someHost1
database.name=someHost2
clientName=someHost3
'''
That is, using a multi-line string instead of a closure, and then read the string and using a standard java.util.Properties:
public void env(String envString) {
Properties properties = new Properties;
properties.load(new StringReader(envString))
....etc
}
And although this works, the mix of having closures and multi-line string is the only downside for now.
In a map declaration, Groovy parses identifiers such as example1 and example2 as string keys to a map:
[ example1: 1, example2: 2 ]
In the .with{} context, it probably uses a setProperty(property, value) mechanism.
But your case features expressions exam.ple and exam-ple. Those expressions have precedence, thus, Groovy will try to resolve them first (with probably something like exam.getProperty('ple') and exam.minus(ple), respectively).
You have some syntactic alternatives, but you will have to make it clear to Groovy what are supposed to be string keys and what are other expressions:
def map = [
'exam.ple4' : 4, // direct map declaration
example5 : 5 // unambiguous key declaration: no quotes needed
]
map.with {
example1 = 1
put 'exam.ple2', 2 // ambiguous, needs quotes
it.'exam-ple3' = 3 // as per #TimYates suggestion
}
assert map['exam-ple3'] == 3
assert map.'exam.ple2' == 2
assert map['exam.ple4'] == 4
assert map.example5 == 5 // again, no quotes needed for key

How to get text from a textView in Haskell

How do you get text from a textView in Haskell?
I am not sure I am doing it right. The only "get" function I could find in the TextView class is textViewGetBuffer which evaluates to an IO TextBuffer, not a string.
I am trying to get the string value of the IO TextBuffer using textBufferGetText :: self -> TextIter -> TextIter -> Bool -> String, but cannot figure out what to specify as the TextIter values.
The TextIter values specify the start and end of the range to get text, but when I looked up the TextIter datatype it did not specify any value constructors I could use.
name = textBufferGetText (textViewGetBuffer txtView) start end True
Any suggestions/help would be greatly appreciated.
(Disclaimer: I don't know how to use GTK in Haskell and I can't install it on this machine, so I can't test this)
Looking at the documentation, there are several methods to get a TextIter value from a TextBufferClass. The ones you're probably interested in are the textBufferGetStartIter and textBufferGetEndIter, which each take a TextBufferClass and return IO TextIter, so you could write a function something like
textViewGetValue :: TextViewClass self => self -> IO String
textViewGetValue tv = do
buf <- textViewGetBuffer tv
start <- textBufferGetStartIter buf
end <- textBufferGetEndIter buf
value <- textBufferGetText buf start end True
return value
which should work. Technically, the last two lines could just be textBufferGetText buf start end True, but I think this makes it a bit more clear what is happening.
EDIT: I should say that I found these methods in Graphics.UI.Gtk.Multiline.TextView, Graphics.UI.Gtk.Multiline.TextBuffer, and Graphics.UI.Gtk.Multiline.TextIter, so you may have to import those as well. But I don't know what all is already in your namespace.

Resharper settings for method chaining

Is it possible to configure resharper to chop all methods in a method chain
e.g.
var query = list.Where(x => true).Select(x => x);
becomes
var query = list
.Where(x => true)
.Select(x => x);
If not, then is it possible to configure resharper to ignore method chains when formatting? So I can chop the text manually without having to worry about resharper re-formatting it.
Unfortunately, there is no way to align .Where under list.
As for chopping, there is an option in ReSharper | Options -> Code Editing | C# | Formatting Style | Line Breaks and Wrapping -> Line Wrapping called Wrap chained method calls. If you set it to Chop always, it would chop, but it uses a slightly different formatting:
var query = list.Where(x => true)
.Select(x => x);
If you leave it to Chop if long (default value), then it wouldn't re-chop your code unless it would be very long (more than Right margin option, which is in the same option group mentioned above).

Resources