This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Kate auto aligning array, objects structure

Tags: None
(comma "," separated)
sirsimon
Registered Member
Posts
7
Karma
0
OS
Helo everyone :)

Is there posible to align associative array structure or objects elements in php like this:
http://vimcasts.org/episodes/aligning-t ... bular-vim/

Cheers :)
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Can you please post an example of the desired text structure?
Kate should keep the current alignment when you move to the next line, which may help.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
scummos
Global Moderator
Posts
1175
Karma
7
OS
(this is why I hate videos explaining how to do things: I don't want to watch a 5 minute video just to reply to this -- if it was in screenshots + text I could grasp the relevant information much more quickly)

You can write your own indenters for kate in JavaScript. There you can do anything you want with alignment.

Greetings!


I'm working on the KDevelop IDE.
sirsimon
Registered Member
Posts
7
Karma
0
OS
@bcooksley

Example 1.
Oginal:
Code: Select all
array = (
    'var' => 1,
    'foobar' => 2,
    'anotherexample' => 3
)
What I want:
Code: Select all
array = (
    'var'            => 1,
    'foobar'         => 2,
    'anotherexample' => 3
)


Example 2.
Oginal:
Code: Select all
Class::var = 1;
Class::foobar = 2;
Class::anotherexample = 3;
What I want:
Code: Select all
Class::var            = 1;
Class::foobar         = 2;
Class::anotherexample = 3;



@scummos
I know I can do it in Javascript, but I hope, someone already wrote it.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
If you do end up writing such a solution yourself, please do post it here so others can use it.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
sirsimon
Registered Member
Posts
7
Karma
0
OS
I've written some code, but it work only in javascript console and only for equal char as delimiter. If anyone feels up to make plugin with this code (maby I, bun not now ;D) and add the ability to choose char of indent, welcome :). The code:

Code: Select all
var selectionRange = view.selection();
if (view.hasSelection()) {
    if (selectionRange.isValid()) {
        var fromLine = selectionRange.start.line;
        var toLine = selectionRange.end.line;
        var maxIndent = 0;
        var linesIndents = [];
        var searchChar = '=';

        for (i = fromLine; i <= toLine; i++) {
            linesIndents[i] = document.line(i).indexOf(searchChar);
            if(linesIndents[i] > maxIndent)
                maxIndent = linesIndents[i];
        }

        for (i = fromLine; i <= toLine; i++) {
            var currentText = document.line(i);
            var spaces = '';

            for(j = 0; j < maxIndent - linesIndents[i]; j++)
                spaces = spaces + ' ';

            document.editBegin();
            document.removeText(i, 0, i, currentText.length);
            document.insertText(i, 0, currentText.replace(searchChar, spaces + searchChar));
            document.editEnd();
        }
    }
}
sirsimon
Registered Member
Posts
7
Karma
0
OS
The plugin:
Code: Select all
/* kate-script
 * author: sirsimon
 * license: LGPL
 * revision: 3
 * kate-version: 3.4
 * type: commands
 * functions: align
 */

require ('range.js');

function align()
{
    var selectionRange = view.selection();
    if (view.hasSelection()) {
        if (selectionRange.isValid()) {
            var fromLine     = selectionRange.start.line;
            var toLine       = selectionRange.end.line;
            var maxIndent    = 0;
            var linesIndents = [];
            var searchChar   = '=';

            for (i = fromLine; i <= toLine; i++) {
                linesIndents[i] = document.line(i).indexOf(searchChar);
                if(linesIndents[i] > maxIndent)
                    maxIndent = linesIndents[i];
            }

            for (i = fromLine; i <= toLine; i++) {
                var currentText = document.line(i);
                var spaces      = '';

                for(j = 0; j < maxIndent - linesIndents[i]; j++)
                    spaces = spaces + ' ';

                document.editBegin();
                document.removeText(i, 0, i, currentText.length);
                document.insertText(i, 0, currentText.replace(searchChar, spaces + searchChar));
                document.editEnd();
            }
        }
    }
}

function action(cmd)
{

    var a = new Object();
    a.icon        = '';
    a.category    = i18n('Editing');
    a.interactive = false;

    if (cmd == 'align') {
        a.text     = i18n('Equal the right side');
        a.shortcut = 'Ctrl+Alt+Right';
    }

    return a;
}

function help(cmd)
{
    if (cmd == 'align') {
        return i18n('Equal the right side by the equal sign.');
    }
}


Keyboar shortcut - Ctrl + Alt + Right
Still align only to '=', so it will work for PHP's '=>' to.

Take, test, use, copy, fix, whatever :)
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Thanks for posting your solution to this - i've now marked it as solved (which should make it easier for future searchers to find).


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
sirsimon
Registered Member
Posts
7
Karma
0
OS
Please, edit translations, if required, my english is a bit lame. Would great if someone with regexp skill, add option to autodetect character of align, like example ':' in javascripts objects, or something else.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]