I'm using Visual Studio 2013 Ultimate and it automatically adds a closing brace when I type an opening brace. However, it can be quite annoying at times e.g.
if (bFlag)
Console.Output("Hello World");
Now if I wanted to enclose Console.Output code in braces, and enter an opening brace after the if expression, VS automatically places a closing brace there as well e.g.
if (bFlag)
{}
Console.Output("Hello World");
This is annoying in that first I have to delete the closing brace and then manually type in closing brace at the end. Is there an automagical way of doing this? I don't necessarily want to disable the auto-close brace feature.
Edit: I'm hoping for a more intelligent solution that automatically adds parenthesis at the end of the expected code block (read: it automatically detects code block). In the example I gave earlier, when I type opening brace after 'if' clause, it should automatically put a closing brace before the 'else' clause etc. If I have to first select all the code and then press a hot-key combo (typically twice), I might as well just manually put the opening/closing braces.
Related
We are currently using C# and want to know if C# bracket placements can change the results.
In Javascript, it matters as results vary based on the curly brace placement .
Why do results vary based on curly brace placement?
In JS they should be kept on the same line, if there are problems with browsers incorrectly interpretting it.
if (x == a)
{
...
}
if (x == a) {
...
Does bracket placement matter for C#?
No, they don't.
In JavaScript, you can write code without ending your lines of code with a semicolon, and JavaScript will automatically fill in the missing semicolons when it interprets your code. That's what this answer to the question you linked is essentially stating. That is to say: the brace placement isn't the real issue in JS; it's the ability to write code with/without semicolons and have JS automatically fill these in for you. The brace placement issue is more of a side effect of this functionality.
In C#, a "line" doesn't end until the semicolon is reached (even if that "line" spans multiple physical lines), and writing code without semicolons isn't something that is automagically taken care of for you by the compiler; it will simply fail to compile. The brace placement in C# therefore is unimportant.
I might have some degree of OCD, maybe that's why it bothers me when I close a line with ';' below a while{} for{} or other code block and resharper would automatically insert a line break above my line.
I DO NOT want an extra line.
I looked through the Editing option page under braces and new lines, but nothing seems to jump out.
sample:
if (true){
GC.Collect();
}
string s = "Any statement here, ending the line with ';' causes newline above";
Visual Studio (2019) has these formatting settings. I'm assuming you're using VS and resharper isn't doing the formatting.
Tools > Options > Text Editor > C# > Code Style > Formatting > New Line
Find "Place open brace on new line for control blocks". There are a bunch others there as well.
In ReSharper options: Code Editing->C#->Formating Style->Blank Lines - set After statements with child blocks to 0.
Visual Studio 2015 with Resharper 10 here. And a very annoying behaviour I have no idea how to get rid of:
If I type this for example
Thread.Sleep(
I get the intellisense list as expected. I then move the selection bar down to TimeSpan.FromMinutes with the cursor down key and hit enter.
Result:
Thread.Sleep( TimeSpan.FromMinutes);
Question: what can I do to not get the ); inserted automatically?
Options that I turned off already:
in VS:
Automatic brace completion
in Resharper:
Auto-insert pair brackets, parentheses and quotes
Auto-insert closing brace
Automatically insert parentheses after completion
When typing a function like the following:
public bool doSomething()
With the cursor at the end of the line, if I insert a { character, Visual Studio types {, inserting an extra space before the brace. I don't want the extra space. This does not appear to be an option in Text Editor -> C# -> Formatting -> Spacing. If I Ctrl+z it removes the space, so it knows it's a separate operation. I've turned off all auto-format options and it's still auto-formatting. How do I make it stop trying to be smarter than me?
Edit: I've also just discovered that when I use the keyboard shortcut to uncomment a block of code, it adds that space back in. This is also a problem.
We're writing a code generator with roslyn. Among other things the user should be able to specify the single statements of a method body or the body of a getter/setter of a property. Therefore he passes a list of strings to a translation method. When passing single curly braces as statements the closing brace gets swallowed somehow.
The method:
internal static SyntaxList<StatementSyntax> GetSyntaxListOfStatementSyntaxs(IEnumerable<string> statements)
{
if (statements.Any())
{
var statementSyntaxs = statements.Select(s => Syntax.ParseStatement(s));
return Syntax.List(statementSyntaxs);
}
return Syntax.List<StatementSyntax>();
}
The input:
var list = new List<string>
{
"if (this.field != null)",
"{",
"this.field = new MyType();",
"}",
"return this.field;"
};
The SyntaxList would be used in a new method declaration (last parameter):
var methodDeclarationSyntax = Syntax.MethodDeclaration(
Syntax.List<AttributeDeclarationSyntax>(),
Syntax.TokenList(),
Syntax.IdentifierName("MyType"),
null,
Syntax.Identifier("MethodIdentifier"),
null,
Syntax.ParameterList(),
Syntax.List<TypeParameterConstraintClauseSyntax>(),
Syntax.Block(statementSyntaxList));
I also tried to process the single closing brace separately but I didn't manage to create a statement with only one closing brace.
The weird thing is the single opening brace gets parsed as a syntax block (correctly or not) but it seems impossible to create that syntax block manually. Neither for the opening nor for the closing brace.
I don't want to add custom parsing of these statements because we decided for Roslyn to be free of parsing tasks. Does someone know how to deal with these special statements? Or maybe somebody can come with another way to treat this issue. Any help appreciated. Thanks in advance.
The problem is that neither an opening brace nor a closing brace are statements, so you can't parse them as such.
Roslyn tries to parse even invalid code, which is why you're getting a BlockSyntax when you parse {. But it's an incomplete block, with the closing brace missing.
I think you should parse the whole method body at once. You could do that by joining the lines together into one string and adding an opening and closing brace.
So, the string that you would actually parse as a statement would look like:
{
if (this.field != null)
{
this.field = new MyType();
}
return this.field;
}