I've always written simple one line if statements in the following style when they are concise and semantically clear (contrived example):
foreach (var item in list)
{
if (item == null) continue;
// ...
}
However as of some version of VS2017, it is now insisting that the above should become:
foreach (var item in list)
{
if (item == null)
continue;
// ...
}
I dislike the latter (yeah, I get that some people will dislike the style I actually do want), and every time I copy and paste code it re-formats automatically. I'm getting close to just turning off formatting entirely to prevent this, but does anybody know if there's a toggle to change this without negatively affecting everything else? I've trawled through the options trying everything that sounded related, but no luck. This style has always been supported in VS2013+, and if I remember correctly it even worked when VS2017 first came out.
Changing the "Leave statement and member declarations on the same line" option has no effect.
Turns out after a bit more digging that there was a .editorconfig file in the solution. Apparently this silently overrides your global settings in the VS options dialog. I was going to delete the question, but perhaps this will be useful to somebody else in the future.
In .editorconfig file , set "csharp_preserve_single_line_statements = true"
Related
I use xCode as my C editor for a small makefile project I'm working on. I keep noticing during Git commits that the whitespace is being changed on me. Here's an example:
void parse_key_values(Card *card, Errors *errors)
{
char str[MAX_LINE_LEN];
char key[MAX_LINE_LEN], value[MAX_LINE_LEN];
Although difficult to see, if you cursor through this you will notice there is a single 2-space-wide tab and a space at the front of the lines. The tab is correct and what I would expect to see. I have no idea where the space is coming from. When I remove it manually and re-indent, it re-inserts it.
Here is my setup:
I have tried changing "prefer tabs" to "prefer spaces", and then goes ahead and inserts three spaces.
I suspected that it was counting the opening brace and indenting from there, but then noticed the same problem on code like this:
if(split != NULL) {
if (split[0] == '=') split++;
if (split[0] == ':') split++;
This is driving me crazy, and causing all sorts of annoyance during commits. Anyone know what's going on here?
sigh
So right over on the right side of the screen is this little bit of UI:
Somehow, and it absolutely was not deliberate, the second of those two settings was changed to "3", so xcode was doing precisely what it was told to do.
Having never used that field, ever, I have never even looked at it. I'm still lost as to how I managed to change it.
When I'm working at someone else's code, sometimes it happens that I've found something useful that I likely will need to return later to.
Except the chances are that I forget where I've found that part and then I need to find it again. Nowadays I'm mostly using breakpoints to set these down, But I don't need these breakpoints for debugging, so I don't think that's the most efficient method. Something that could work for me is a sort of marker that functions marking down like a breakpoint, but does not affect debugging.
Will all these ways of breakpoints, search functions, finding references, and adding a Watches. I do have a feeling that such feature is present in Visual Studio already. But I havn't found it yet, I've already tried searching it up, but it looks like it hasn't asked before.
So, is there a feature in Visual Studio that let you mark down code like breakpoints, but just for the purpose of marking down?
There is more than one way to do so.
If I finde something interesting I mark it with a Bookmark.
Just STRG + K, STRK + K and it will be places on your current row.
With these Buttons you can switch to your bookmarks or delete them all. They are client based.
Visual Studio also gives you a "To Do List" Function. This one is triggered by comments in the code and seen for everyone with the right options used.
See here how it works in detail.
Yes, it's called a bookmark. This little toolbar is for bookmarks:
You can also use the Edit, Bookmarks... menu item and the View, Other Windows, Bookmark Window which among other things lets you see all of them and give them names.
One caveat: the bookmark binds to the line number, and doesn't move if you add or remove lines. So if you bookmark the first line of a function, then make changes above that, the bookmark won't be on the first line of a function any more.
I have linked list containing few tens of objects like this:
struct Item {
Item * next;
const char * name;
....
};
When I want to see in debugger in visual studio what item list holds, I need to hover/click on next many times to expand whole list until I hit nullptr. This is slow, error-prone (hand slips and I can start all over again) and not very organized.
Is there any scripting for VS2015 debugger available in which I could iterate whole list and just dump the name into console or whatever?
EDIT: I found about concord extensibility api ( https://blogs.msdn.microsoft.com/visualstudioalm/2015/10/02/announcing-visual-studio-debug-engine-extensibility-samples/ ) but it seems rather complex.
This is supposed to be in-house tool, so speed/ease of development is more important than robustness and/or easy of deployment.
If the data set you are working with is small, I would go with the tried and true method of std::cout.
Or just dump the contents of the list into a file and put a break point after that file is written to so you can check its contents before the program continues.
You can do it. Insert a Tracepoint (Right mouse button> Breakpoint >Insert Tracepoint) and READ CAREFULLY to whole text of that window. Then you will know HOW to print to the Output window WHAT you want.
Insert also a breakpoint on another line that is CONDITIONAL. Just put a normal breakpoint, then over the red ball, Right mouse button > Condition, then input
!next
Notice the !
Let's say I have a trivial if statement as follows:
if(a > b)
{
return false;
}
Whilst the above matches my defined coding style perfectly, what I want to be able to do is highlight these 4 lines of code, and then very quickly be convert them into a single line as follows:
if(a > b) { return false; }
Is there a way of doing this selectively. I do not want to do it throughout the file - only the lines I highlight. In many cases I will want the if statement left on multiple lines.
I have ReSharper if that helps.
Install the VsVim Extension and then you can place your cursor on the if keyword and then press CTRL+ALT+→ twice (assuming you are using the Resharper Visual Studio keymap) to select the whole if block and then press SHIFT+J to join all the lines in to one.
If VsVim is not for you then you could try the CodeMaid Extension that adds join line functionality (using CTRL+M, J)
You can create a custom template. Take a look here. You can then have the standard Resharper fix for the lines you suggested and apply them where you want.
PHPStorm is a very nice IDE, but it does one thing that annoys me.
I (and my team) write our switch statements like so:
switch ($foo) {
case 'a' :
// some code
break;
}
PHPStorm auto-corrects this to be
switch ($foo) {
case 'a' :
// some code
break;
}
Note that the break is indented along with the code. I don't want this to happen.
I've looked in the code style section, but the only option for switches is to indent the case branches.
Does anyone know how stop PHPStorm from doing this?
You can change this default behavior in
settings | Editor | Code Style | PHP | Wrapping and Braces
uncheck indent 'break' from 'case' option
Based on the recommendations in PSR-2 on code style, PHPStorm displays it the recommended way.
If you really want to change it, you can do it in Editor > Code style > PHP > Wrapping and Braces under the 'switch' statement and uncheck the Indent 'break' from 'case.
Disclaimer, the screenshot is from IntelliJ, but you should be able to find the same setting in the same location
Press Ctrl+Alt+S to open the Settings .
Choose Code Style -> PHP as below
Change the Continuation Indent value to 0 as shown. Done.
Maybe you also have to go by the logic that the application is pointing out, a break is not necessarily the end of a case statement. You could have comments after the break statement, both logically and visually. The break is a child of that particular case statement.