VS 2010 Is possible to turn off collapsing JS code? - visual-studio-2010

in VS 2010 Ultimate if You type a JS code an then press Enter, You'll notice that the 1st bracket is in the same line as e.g function header. How to turn it off ? It is very annoying for me
after pressed enter... =>
function a() {
}
I want it as:
function a()
{
}

Using braces on the same line as the function declaration is proper JavaScript coding style (see Crockford). Using braces on the same line as the opening block is recommended due to the way JavaScript inserts semicolons wherever possible. Take, for example, this code:
return
{
hello: "world"
};
JavaScript parsers will rewrite this as:
return;
{
hello: "world"
};
This has a substantially different meaning, and there is no warning to the developer that this has happened, other than incorrect behavior from their script. While function declarations are ok, since function foo(); is not valid JavaScript, and parsers will therefore not insert a semicolon there, such formatting is still strongly discouraged.
If you still want to do this, you can alter this setting: Tools - Options - Text Editor - JScript - Formatting - Place open brace on new line for functions.

Code convention aside, the option is there in Visual Studio 2010 (Premium in my case)
Tools > Options > Text Editor > JScript > Formatting > Place open brace on new line for functions [check]

Related

Syntax Highlighting on Visual Studio

I'm writing a test for my C# diagnostic analyzer with code fixes, which requires input code and expected fixed code.
The most simple way to specify the codes is to write them as string literal.
However, it bothers me because there is no syntax highlighting in the string literal.
So I thought it would be better to write the codes as separate files.
At this time I'm considering the following syntax:
class C
{
void M()
{
var i = 0;
- {|#0:i = 1|};
+ var i1 = 1;
}
}
- and + represent the rows to be deleted and added by the code fix, respectively, and {|#n:...|} indicates the location of the diagnostic result.
However, writing in a separate file does not solve the syntax highlighting problem.
I'd like to indicate deletions and additions with colors (like red and green) and also apply C# syntax highlighting.
So my question is, is there a simple way to apply my own syntax highlighting to a specific extension (e.g. cst) in Visual Studio?
If it is too difficult or too much work for the benefits, I'll give up.

How do I control indentation of wrapped lines in Visual Studio

I have Visual Studio 2019 (Community at home; Professional in the office). I also have ReSharper. I set up line length to be 120; indentation at 4, and autoformatting on close brace or semicolon. The language is C#
The problem is that indentation of the wrapped line is completely unpredictable (at least to me). Sometimes it is indented 4 characters as I expect; sometimes it is under a parenthesis or to the right of => or same operator (like &&). So, frequently wrapped line starts at character 80 or 90!
How can I make it always indent 4 characters from the previous line?
Needless to say that I tried a dozen of combinations of options under Visual Studio and Resharper Code Editing - Formatting options. Visual studio doesn't seem to have any option that would make it push the new line. Resharper has Parenthesis section under C# - Formatting style. But nothing makes wrapping simple
Example:
lstGroup.Select(s => new Group
{
...
Status = lookup.Where(w => w.LookupType == LookupConstants.CRIMETYPE &&
(int?)w.LookupIndex == (string.IsNullOrEmpty(s.TempCrimeType) ? 0
: Convert.ToInt32(s.TempCrimeType))).Select(w => w.LookupDescription).FirstOrDefault(),
...
});
(it may not be clear, but the lines don't run past char; so I would like it to stay this way!). After hitting semicolon at the end of the statement, it turns into
Status = lookup.Where(w => w.LookupType == LookupConstants.CRIMETYPE &&
(int?) w.LookupIndex == (string.IsNullOrEmpty(s.TempCrimeType)
? 0
: Convert.ToInt32(s.TempCrimeType))).Select(w => w.LookupDescription).FirstOrDefault(),
I'm not sure if you can completely simplify indents for wrapped statements/expressions, but the following options should make it much easier. If something still irritates you, please amend your question with examples.
Tabs, Indents, Alignment
Set all options in "Parenthesis" section to "Parenthesis and inside equally"
Turn off all options in "Align Multiline Constructs"
Brace Layout
Set "Expressions (initializers, switch expressions)" to "At next line indented (Whitesmiths style)"
Consider also to whether you want to also change "Lambda and delegate" to the same style
UPDATE: To prevent ReSharper from re-wrapping your code, go to "Line breaks and wrapping" page and look for options called "Wrap ..." with values "Chop if long or multiline" and "Chop always". Change them to "Simple wrap" as needed. You may also want to ensure that all "Keep existing arrangement..." options are turned on. Your particular example is influenced by a setting called "Wrap ternary expression".

Does Sublime Text 2 have the ability to region code similar to Visual Studio?

In Visual Studio you can minimize huge chunks of code using regions; they essentially just surround the code and minimize it in the window.
Does Sublime have a feature similar to this?
By default, you can select some code the go to Edit > Code Folding > Fold. There are tons of plugins that leverage the code-folding api for more options.
There's a request on the official site to "ask for features" here.
But apparently:
FYI, Jon has stated that this is not possible in the current
implementation of the editor control. Looks like we're waiting till V3
guys.
Jon being the programmer behind Sublime Text 2.
There might be a way to fake it by creating a plugin that looks for markers and removes the code region in between the markers, but it probably wouldn't look good. With the current API, it's probably your best bet!
By the way, there is some code folding in Sublime Text, if you hover your mouse next to the line number, you will see some arrows appearing when you can fold / unfold.
I ended up using custom comment tags, indented one level less than the code I want to fold. It doesn't look best, though it serves its purpose.
class Foobar {
// ...some code
// <fold
function foo() {
}
function bar() {
}
// </fold
// more code...
}
This (at the moment) folds to:
class Foobar {
// ...some code
// <fold[...]
// </fold
// more code...
}
Having a native ST2 support for this would be nice.
This looks what you are looking for. You can define tags for #region and #endregion for each language, or a generic tag for all of them.
If you are obsessed with intendation, this solution may make you uncomfortable but here it is, once upon a time while I had been writing a semi-complex jQuery plugin I've had constants, variables, private and public functions sections and foldings like so;
;(function($, undefined, window) {...
/* Consts */
var FOO = "BAR";
/* Variables */
var m_Foo = "bar";
/* Functions */
/* Public Functions */
function foo() {...}
function bar() {...}
/* Private Functions */
function _foo() {...}
function _bar() {...}
})(jQuery, window);
As you can see it is all about intendation. Sections can be folded; Consts, Variables, Functions. And also inside Functions section, Public Functions and Private Functions are both can be folded.
You can also use one line comment (//) to name your fold. So the idea underneath that is simple; ST2 thinks that the more intended lines belongs to first less-intended comment above them, like C/C++ compilers how handle brackets as own unique code blocks.
To fold the code select the code and press
ctrl + shift + [
To unfold the code put the cursor there and press
ctrl + shift + ]
I think that like myself, the OP has come to appreciate a little-known feature in VS called regions that many equate to code-folding, but is FAR more powerful and above, Dio Phung provided the answer that I wanted, and I suspect the OP wanted, but he didn't share as an answer so here it is.
The difference between "code-folding" as it's provided in Sublime Text is that it's based on code/compiler syntax while "regions" and what this plugin does, allow you infinitely more freedom, though it's a freedom that's more or less dependant on the code you're working with to begin with (deeply nested, or properly modularized).
If you are on Sublime Text 3, here is a plugin that can do it :
github.com/jamalsenouci/sublimetext-syntaxfold – Dio Phung
In languages which support 3 types of comments (e.g. PHP) I use the hashtag comment for regions, as shown in the other answers. It's also good for keeping track of what's being done
# default options
$a = 3;
$b = 'bob';
$old_code = 1;
# bugfix #130
$result = magic_function($data);
fix_stuff($result);
$old_code = $result;
Otherwise use triple slash ///, or //# etc.
In sublime text, it works like this, it shades the lines you want to collapse and presses (Control + Shift +?)
I have the most recent version of sublimetext.

VS 2008 Code Snippet Indentation

In VS 2008, there are XML code snippets that seem to be pretty awesome :) However, it seems that the snippets we created do not indent properly. For example if we have the following code:
{
...
{
...
{
InsertSnippet here.
We get something like:
{
...
{
...
{
FirstLineofSnippet
SecondLineOfSnippet
ThirdLineOfSnippet
Is there any way to make it so that all lines keep the same indentation?
This usually indicates that the code you are inserting is semantically incorrect - at the time of insert, not when you filled in the snippet values.
To prevent this you can use default values in your snippet to make the snipped syntactivaly correct. E.g. to declare an argument list for a function:
<Literal Editable="true">
<ID>methodArguments</ID>
<ToolTip>methodArguments</ToolTip>
<Default>params object[] args</Default>
<Function>
</Function>
</Literal>
The <Default> Element provides the declaration. So using this in a snippet declaring a funciton will lead to this:
public void FunctionName(params object[] args) { }
And it then lets you replace params object[] args with whatever argument declarations you need.
paste the first line without indentation, then select the block and tab until you get the whole block to the indentation you want. AFAIK what you see is the standard behavior and it happens because the location you paste from wasn't indented as far as you wanted.
My unit test snippet appears to be indenting correctly. It may be due to the fact that the code is wrapped in a CDATA block. VisualStudio also tends to auto-format the block as soon as it is added to the code. Does your snippet have as many open braces as closing ones? If the two match up, and the snippet is inserted in a valid/compilable location, it should auto-format.
It seems that Visual Studio does an auto-format on the inserted lines. My snippet wasn't correctly formatted (i.e. indented) either, neither by snippet insertion, nor by manual format (Ctrl-K-F). It contained "delegate{...}", maybe the formatter cannot handle this.
Try swithing from Insert spaces to Keep tabs in Tools > Options > TextEditor > C# > Tabs, it worked for me.

Invert assignment direction in Visual Studio [duplicate]

This question already has answers here:
How can I reverse code around an equal sign in Visual Studio?
(6 answers)
Closed 4 years ago.
I have a bunch of assignment operations in Visual Studio, and I want to reverse them:
i.e
i = j;
would become
j = i;
i.e. replacing everything before the equals with what's after the equals, and vice versa
Is there any easy way to do this, say something in the regular expression engine?
Select the lines you want to swap, Ctrl+H, then replace:
{:i}:b*=:b*{:i};
with:
\2 = \1;
with "Look in:" set to "Selection"
That only handles C/C++ style identifiers, though (via the ":i"). Replace that with:
{.*}:b*=:b*{.*};
to replace anything on either side of the "=".
Also, since you mentioned in a comment you use ReSharper, you can just highlight the "=", Alt+Enter, and "Reverse assignment".
Just a slight improvement on Chris's answer...
Ctrl+H, then replace:
{:b*}{[^:b]*}:b*=:b*{[^:b]*}:b*;
with:
\1\3 = \2;
(better handling of whitespace, esp. at beginning of line)
EDIT:
For Visual Studio 2012 and higher (I tried it on 2015):
Replace
(\s*)([^\s]+)\s*=\s*([^\s]+)\s*;
with:
$1$3 = $2;
In Visual Studio 2015+ after selecting code block press Ctrl + H (Find & Replace window) and check "Use Regular Expression" option, then:
Find: (\w+.\w+) = (\w+);
Replace: $2 = $1;
For example:
entity.CreateDate = CreateDate;
changes to:
CreateDate = entity.CreateDate;
Thank you #Nagesh and Revious, mentioned details added.
The robust way to do this is to use a refactoring tool. They know the syntax of the language, so they understand the concept of "assignment statement" and can correctly select the entire expression on either side of the assignment operator rather than be limited to a single identifier, which is what I think all the regular expressions so far have covered. Refactoring tools treat your code as structured code instead of just text. I found mention two Visual Studio add-ins that can do it:
ReSharper
MZ-Tools
(Inverting assignment isn't technically refactoring since it changes the behavior of the program, but most refactoring tools extend the meaning to include other generic code modifications like that.)
Please see this question: Is there a method to swap the left and right hand sides of a set of expressions in Visual Studio?
My answer to that question has a macro that you can use to swap the assignments for a block of code.
I've improved the expression a little.
Replace
(\t+)(\s*)(\S*) = (\S*);
with
$1$2$4 = $3;
The reason is, it will look for lines starting with tab (\t). It will skip the lines starting with definition. E.g.:
TestClass tc = new TestClass();
int a = 75;
int b = 76;
int c = 77;
a = tc.a;
b = tc.b;
a = tc.c;
Would ignore the int a, int b and int c and swap only the assignments.
what about replace all (CTRL-H)
you can replace for example "i = j;" by "j = i;"
you can use regular expressions in that dialog. I'm not so sure about how you should pop-up help about them however. In that dialog, press F1, then search that page for more information on regular expressions.
I like this dialog because it allows you to go through each replacement. Because the chance of breaking something is high, I think this is a more secure solution
You can do search and replace with regular expressions in Visual Studio, but it would be safer to just do a normal search and replace for each assignment you want to change rather than a bulk change.
Unfortunatly I don't have Visual Studio, so I can't try in the target environment, but if it uses standard regexps, you could probably do it like this:
Search for "(:Al) = (:Al);", and replace with "\2 = \1". (\1 and \2 are references to the first and second capture groups, in this case the parenthesises around the \w:s)
EDIT
Ok, not \w... But according to MSDN, we can instead use :Al. Edited above to use that instead.
Also, from the MSDN page I gather that it should work, as the references seem to work as usual.

Resources