wbond / sublime_alignment

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

Better multiselection alignment #32

Open bordaigorl opened 12 years ago

bordaigorl commented 12 years ago

In a situation like

a & b & c
aaa & bbb & ccc

one would like to be able to select all the &, press ctrl+alt+a and obtain

a   & b   & c
aaa & bbb & ccc

but currently the plugin only aligns the first ones (with too many spaces):

a         & b & c
aaa       & bbb & ccc

My suggestion is to change the code handling multiple selections as follows:

        # This handles aligning multiple selections
        else:
            col = {}
            curline = view.rowcol(sel[0].begin())[0]
            j=0
            for i in range(0,len(sel)):
                ln = view.rowcol(sel[i].begin())[0]
                if ln != curline:
                    j=0
                    curline = ln
                if j in col.keys():
                    col[j].append(i)
                else:
                    col[j] = [i]
                j+=1
            for j in col.keys():
                max_col = max([normed_rowcol(view, sel[i].b)[1] for i in col[j]])
                for i in col[j]:
                    region = sel[i]
                    length = max_col - normed_rowcol(view, region.b)[1]
                    view.insert(edit, region.b, ' ' * length)
                    if settings.get('mid_line_tabs') and not use_spaces:
                        convert_to_mid_line_tabs(view, edit, tab_size, region.b, length)

which groups the selections by rows and processes the first sel of each row, then the seconds etc.

This is particularly useful when writing LaTeX tables for example.

amisarca commented 12 years ago

It works really well! Good job!

As an improvement, it would be nice if it worked when selecting the whole table, not only the separators.

L.E: I fixed the issue by creating a new table cleaner package that can be found here https://github.com/mishu91/Sublime-Text-2-Table-Cleaner

Regards, Andrei

radubrehar commented 11 years ago

Would be nice to have multiline selection support. Have the following example:

    return {
       isString: function(value){                                  // <- selected line
            return typeof value == 'string';
       },

       isBoolean: function(value){                              // <- selected line
           return typeof value == 'boolean'
       }
    }

If the lines are selected as indicated in the comments, I want to align them by ":", as below

    return {
       isString  : function(value){                                  // <- selected line
           ....
       isBoolean : function(value){                              // <- selected line
           ....
    }

But currently this is not possible. Instead I have to select all the lines and align them, with the result below

    return {
            isString                : function(value){
                return typeof value == 'string';
            },

            isBoolean               : function(value){
                return typeof value == 'boolean'
            },
    }

which is not what I need.

This happens because I have both = and : as alignment characters ("alignment_space_chars": ["=",":"]) Also, it would be nice to have an option to only align same character, and not both = and : under the same alignment.

Thanks.

bordaigorl commented 11 years ago

Hi radubrehar,

using the patch I suggested in this thread, you can achieve the effect you described by simply selecting the two colons and pressing Ctrl+Alt+A.

In my opinion this is the most flexible, yet simple, solution that allows for very quick editing and supports any delimiter the user might need. The pattern is: select all the occurrences of the delimiters to be aligned (maybe multicolumn); this can be done by manually selecting or via Ctrl+D or regex matching. Then press Ctrl+Alt+A and the job is done.

radubrehar commented 11 years ago

Thanks bordaigorl!

Indeed it worked well! Yes, this is very flexible. It would still be useful to have an additional plugin or functionality over this, that performs this automatically, depending on some configs,

But anyway, works great!