wbond / sublime_alignment

Easy alignment of multiple selections and multi-line selections
http://wbond.net/sublime_packages/alignment
523 stars 110 forks source link

Lack concept of context and aligns first align character in each line #54

Closed phpmycoder closed 9 years ago

phpmycoder commented 11 years ago

I ran into an bug when trying to align the following PHP:

$urlMap = array(
    '/old-url' => '/new-url-for-old-url',
    '/another-old-url' => '/new-url-for-other-old-url',
    '/older-url?page=cart' => '/cart',
    '/older-url?page=home' => '/',
    '/bad-url' => '/better-url'
);

Running sublime_alignment, modifies the block of code to look like this:

$urlMap = array(
    '/old-url'         => '/new-url-for-old-url',
    '/another-old-url' => '/new-url-for-other-old-url',
    '/older-url?page   =cart' => '/cart',  // aligned to wrong char
    '/older-url?page   =home' => '/',  // aligned to wrong char
    '/bad-url'         => '/better-url'
);

What I expected:

$urlMap = array(
    '/old-url'             => '/new-url-for-old-url',
    '/another-old-url'     => '/new-url-for-other-old-url',
    '/older-url?page=cart' => '/cart',
    '/older-url?page=home' => '/',
    '/bad-url'             => '/better-url'
);

The problem here, I presume, is that alignment has no knowledge that the first equals is within a string. I know that it would very difficult (if not impossible) to have it parse code in all of the languages with which it is compatible to determine whether an equals is actually an assignment operator that should be aligned to. However, I think it is pretty universal that characters surrounded by single or double quotes are strings. With this assumption, I would like to propose adding a rudimentary understanding of strings to sublime_alignment, so that it doesn't incorrectly align to characters within a string.

Any thoughts?

Thanks!

phpmycoder commented 11 years ago

Here's a really rough (and sloppily coded) idea of something that might work. It's untested though, and right now it ignores backslash escapes, but I believe it will ignore alignment chars within quotes:

https://github.com/phpmycoder/sublime_alignment/commit/9e6f852aff012081da7cd6c2ac19dfa5e34f382e

steven-pribilinskiy commented 11 years ago

the only solution i see is AlignTab by randy3k it allows to break the lines with regular expressions

wbond commented 9 years ago

While this may not be the answer you want to hear, I see this as "functioning properly." This package is really meant as a quick aide for helping align some code, and is not meant to be a general purpose code formatting tool. I think that language-specific packages with formatting rules are probably the kind of thing that should be use for more complex situations.

Perhaps https://packagecontrol.io/packages/phpfmt would be useful?

However, that said, here is how I would align the equals:

$urlMap = array(
    '/old-url' => '/new-url-for-old-url',
    '/another-old-url' => '/new-url-for-other-old-url',
    '/older-url?page=cart' => '/cart',
    '/older-url?page=home' => '/',
    '/bad-url' => '/better-url'
);
  1. Select all of the lines within the array
  2. Press cmd+shift+l to split to one cursor per line
  3. Use home (or cmd+left) to go to the beginning of each line
  4. Move right one place with an arrow
  5. Press cmd+shift+space to select the string "scope" on each line
  6. Move right once to move all cursors to the = sign
  7. Use Alignment to align them

Now, seeing this list, it might sound complicated, but it takes all of three seconds once you are familiar with the scope selection. Additionally, such features work with any language and can be used to format code in situations where automated formatting makes things a mess.