Code alignment is a practice for formating your code.
Based on principals from maths and other displines, code alignment gives extra meaning to your code by lining up similar code parts into columns.
This is a practice everyone performs on most data using tables and spreadshee...
Visual Studio Express doesn't allow extensions. I looked into it as there was some confusion because it has a Extension manager. But that is only to provide updates to NuGet and Visual Studio itself.
I'm so glad someone took the time to create this extension. It works as advertised, and now it's just that much easier to make code look like it should look.
This is exactly how I have been manually structuring my code for the last 15 years. It is so great to discover I am not alone in the universe and even better to finally have a tool to do it for me! Thank you!
Thanks, As for the context menu, I think in visual studio especially there is far too many items in that for me to add more to. I suggest trying out the shortcuts, It's easily the fastest way to use the extension.
To each their own, though firing someone who uses a technique which has been proven incredibly useful in many fields - from maths to anything that uses excel, seems pretty harsh.
I will say though that this is meant to be a review of the extension, not a statement of your belief - there's a Q and A section where you could write that (which could have let us have a proper discussion about it). If you haven't even installed the extension, writing a negative review is just trolling.
Also, this tool has uses outside of coding. I've used it as a quick way to extract data. Or to make computer generated outputs more readable.
No coding style is forced upon you when you install this extension, it's a tool which gives you the power to format your code or data how you like.
There doesn't appear to be a way to use align by space to line up the MyEnum.blah statements in something like this.
Frobinate(0, 100, "Cat", MyEnum.Item100));
Frobinate(8, 110, "Mouse", MyEnum.Item110));
Frobinate(9, 111, "Dog", MyEnum.Item111));
Frobinate(10, 200, "Rabbit", MyEnum.Item200));
Frobinate(11, 201, "Fox", MyEnum.Item201));
Frobinate(20, 233, "Polar Bear", MyEnum.Item233));
Depending on where I put the cursor I get something like this:
Frobinate(0, 100, "Cat", MyEnum.Item100));
Frobinate(8, 110, "Mouse", MyEnum.Item110));
Frobinate(9, 111, "Dog", MyEnum.Item111));
Frobinate(10, 200, "Rabbit", MyEnum.Item200));
Frobinate(11, 201, "Fox", MyEnum.Item201));
Frobinate(20, 233, "Polar Bear", MyEnum.Item233));
or this:
Frobinate(0, 100, "Cat", MyEnum.Item100));
Frobinate(8, 110, "Mouse", MyEnum.Item110));
Frobinate(9, 111, "Dog", MyEnum.Item111));
Frobinate(10, 200, "Rabbit", MyEnum.Item200));
Frobinate(11, 201, "Fox", MyEnum.Item201));
Frobinate(20, 233, "Polar Bear", MyEnum.Item233));
What I want is:
Frobinate(0, 100, "Cat", MyEnum.Item100));
Frobinate(8, 110, "Mouse", MyEnum.Item110));
Frobinate(9, 111, "Dog", MyEnum.Item111));
Frobinate(10, 200, "Rabbit", MyEnum.Item200));
Frobinate(11, 201, "Fox", MyEnum.Item201));
Frobinate(20, 233, "Polar Bear", MyEnum.Item233));
Which I think is either dependent on being able to reject spaces inside a quoted string; or being able to do smarter alignment off the commas.
Align by caret using hte comma comes closest; but aligning by putting space in front of the comma instead of after it looks really weird.
ex
Frobinate(0 , 100, "Cat" , MyEnum.Item100));
Frobinate(8 , 110, "Mouse" , MyEnum.Item110));
Frobinate(9 , 111, "Dog" , MyEnum.Item111));
Frobinate(10, 200, "Rabbit" , MyEnum.Item200));
Frobinate(11, 201, "Fox" , MyEnum.Item201));
Frobinate(20, 233, "Polar Bear", MyEnum.Item233));
Thanks for the question,
Sorry about the delay, I normally get a notification when someone posts, for some reason I didn't get one.
This can be achieved with a regex...
,\s*(?<x>[^\s])
If you look at the align by space, it's very similar. The difference is the comma at the beginning, and * instead of + because you still want to align if there is no space between the , and the word.
With regexes if you include a group with the name x that's where the alignment happens. So this regex say
find a comma, skip the spaces and align by the first non-space.
I recommend going into the code alignment options and assigning it as a shortcut for Comma. (Tick all the boxes too)
The LineEmUp macro was just about perfect for my needs. You could select a block of code and run the macro, and it would line up on the first blank in the line. Then, if you ran it again on the same block, it would line up on the next unaligned blank, etc. Can CodeAlignment do this?
Pretty much. You can select a block (or let the extension figure out the scope for you) perform an alignment, move the caret past where the alignment is and perform the same alignment with the "Align from caret checked" (Tip: Any shortcut can become an align from caret by pressing the shift key with the second key press, eg instead of Ctrl = = you use Ctrl = Shift =)
Hope that helps.
Thanks for the reply. I have been moving the cursor around to align. Just got a bit spoiled by LineEmUp. Any chance of having it added someday? I seem to recall that this addin is open source. Is that true? I'm not opposed to looking into it myself.
Hello,
That's a great plugin. Just lacks a bit of documentation :p
Could you tell me how can I align the following code as shown. I can't find how to do it:
cmd.Parameters.AddWithValue("@a", a);
cmd.Parameters.AddWithValue("@bbbbb", bbbbb);
cmd.Parameters.AddWithValue("@ccc", ccc);
... should be align to:
cmd.Parameters.AddWithValue("@a", a);
cmd.Parameters.AddWithValue("@bbbbb", bbbbb);
cmd.Parameters.AddWithValue("@ccc", ccc);
Thanks, works perfectly!
So there is some documentation after all :p
That wiki is quite useful, you should add a link to the wiki on your website codealignment.com. I didn't find it by myself.
This extension does not really work with German keyboard layout. But the positive thing is, that the extension seems not to overwrite existing shortcuts. It would be nice to use them (without placing all shortcuts manually).
Hi, good tool!
I would like to see a way to 1) align by Tab (yes, I know you hate tabs) since I don't see a way to enter a tab, or 2) align by a regular expression which would seem to make the tool even more versatile.
Good work.
Love the idea of allowing regular expressions. I won't implement the align by tabs directly but the align by regular expression should allow you to do that.
When the "Use IDE tab settings for alignments" is checked, the alignment ends up with mixed spaces and tabs for some reason. I wish this could produce pure tab alignment rather than mixed with spaces. Also, this yeilds JHint warnings.
It would be nice to have the extra spaces removed. For example, the following code:
m_drive = drive;
m_fileSystem = fileSystem;
...will align to:
m_drive = drive;
m_fileSystem = fileSystem;
...but a cleaner result (according to my personal taste ;o) would be:
m_drive = drive;
m_fileSystem = fileSystem;
Is there a way to accomplish this?
Hard to post aligned code on MSDN... Post editing uses fixed-size font, but not the final post :o(
Let's try again... The following code:
m_fileSystem = fileSystem;
m_fileSystem = fileSystem;
...will align to:
m_fileSystem = fileSystem;
m_fileSystem = fileSystem;
...and I would like to get:
m_fileSystem = fileSystem;
m_fileSystem = fileSystem;
Thank you!
Sorry about the late response, this is definitely something that annoys me too. I hope to get to it in one of the next versions.
In the mean time I suggest using visual studio column marking. Go to the first equals, press ALT SHIFT DOWN til you get to the last one, then press SHIFT tab, hold that down til they are all gone and press space.
Can you please add an "align to column number" option while defining shortcuts?
I'd like to be able to have a column of comments in my code that all start
at a specific column number.
That is, I'd like to match (?<=[^/]+)//.* and then have all matches aligned
to column 90, pulling them BACK if necessary.
You might also have an option to either ignore lines that cannot be pulled back, or move them to the next line.
It's an interesting idea, I'll consider it but I wonder if it's a common enough usage to warrant extra complexity.
Does it really matter if it starts at column 90? Couldn't you just add another symbol to the comment then align on the combined string, eg
//>
on the whole document, this would prevent unwanted comments from getting aligned with it.
Well, what happens is that while I'm coding the comment that I want aligned will get pushed way off to the right of the screen. Were I to align like that, all my comments would get pushed off the screen. Having a set column lets me forget about "pre-alignment" where I have to pull comments back from the extreme edge or put them on a new line. Having the ability to auto line-break means I never have to go back and fix my comments. I just code and don't worry about where the comment ends up.
I think I originally misunderstood what you were saying.
So in this case, what would happen if a line was too long and the comment was at column 100? Do they all move to 100 or does the ones past column 90 get ignored?
I guess, looking at it, the most important part of what your asking isn't the fixed column, it's the excess white space. A lot of people have mentioned this and I definitely agree.
Finally got round to logging it into github - https://github.com/cpmcgrath/codealignment/issues/12
May I suggest a few things are confusing to new users and suggest they be renamed?
I had no idea what "Align from caret position" meant. Perhaps this should be, "Align from caret to end".
I still don't understand what the "add space" or "Use IDE tab settings for alignments" option does. (Does this have something to do with the tab character, or with the tabs of the window?)
I don't see how "caret to end" is any more clear than "caret position". To me the confusing part is the word caret, "to end" adds little more meaning, while position makes it clear you're talking about, well, the position. I agree caret is a bit confusing but it's the most accurate term. Sometimes cursor and column is used but cursor implies mouse and column implies y-coord not but doesn't say where from.
Add space makes sure there result always has at least one space before it so for example
foo = 5;
verylongfoo= 9;
will make sure verylongfoo becomes
verylongfoo = 9;
I admit it's not very clear, but it was a limited space issue, maybe I'll add a tooltip.
As for the tab settings, long ago people asked if there IDE setting was to use tabs then the extension should insert tabs instead of spaces. I originally did this (I never use tabs) but the more I thought about it the more this was wrong. The idea of using a variable width character to align makes no sense, if I was to use tabs I'd use them for indentation not alignment. I got some emails which agreed with me so I changed it to use spaces, but made an option people could override if the really liked tabs that much.
As for the name, I think it's pretty clear it's talking about tab characters, but maybe I could change it to "Use IDE tab or space settings for alignments" or something
Maybe it would be more useful to explain some possible meanings of "align from caret position" I considered when trying to figure it out...
I thought it could mean...
* Align only from the caret position to the end of the file (same as normal align with text selected from caret position to end of file)
* Align the entire file, but only align matching items that appear in the same or later column as that of the caret. (align a block of text that does not include things left of the block)
* Align the entire file, but pull/push all matches until they are in the column of the caret (take the horizontal destination position from the caret, not form the maximum column.)
The actual meaning (i think) is to align from the caret to the end of the current line only, using the surrounding text to obtain the alignment information.
A tooltip would be a nice way to expand the explaination.
What it means is when doing an alignment, start looking for the matches from the x-coordinate of the caret's position on, instead of from the start of the line.
It's to deal with the common case of trying to do an alignment on the second instance of a match on a line.
It has nothing to do with scope selection, the scope selection logic is identical to a normal alignment.
I think that the majority of the names in code alignment (including align from caret) are as accurate and informative as you can have in a few words. Does that mean there's no ambiguity? No. We're dealing with ideas which are very, very hard to explain with words - Even with the extra space a tooltip gives you.
Because of this I have two ways to help with it.
First, developers need to explore. All actions can be undone, all actions are fast.
Secondly, I've started a wiki - https://github.com/cpmcgrath/codealignment/wiki - it's a work in progress. But it gives the ability to have examples, screenshots which makes it much easier to understand than any plain text could.
Hi.
How can I make a regex to put spaces in the left of the string making a right justification?
Example:
var test1 = new Class1() {var1 = 2, var2 = 4000};
var test2 = new Class1() {var1 = -12, var2 = 2.1};
I made the regex below
[0-9\.\-]{1,}[,|}]
that will align the numbers "2" and "-12". But they aready are aligned in the left.
I want to align the numbers in the right, inserting spaces on the left and leaving the comma aligned. Making some like this:
var test1 = new Class1() {var1 = 2, var2 = 4000};
var test2 = new Class1() {var1 = -12, var2 = 2.1};
Other question. How can I make a reget to make change on all occurrences in the line? No only in the first occurrence.
Neither can be done as you want it. But the second could be done using Align from caret. After the first alignment, put your caret after the first, then do the alignment again with the "Align from caret" on (On any shortcut you can force it to use align from caret by adding Shift to the second shortcut key. For example, to align by equals from caret – Ctrl Equals, Shift Equals)
I've definitely thought of both cases so they might be implemented in future versions.