Показать сообщение отдельно
Старый 22.01.2013, 20:49   #1  
alex55 is offline
alex55
MCTS
MCBMSS
 
224 / 145 (5) +++++
Регистрация: 13.02.2007
Адрес: Москва
AXAligner - скрипт редактора для выравнивания X++ кода
Приветствую!

Набросал скрипт для выравнивания X++ кода, желающие могут попробовать beta-версию. Проверял работоспособность в AX 2009 и AX 2012, по идее должен работать и в предыдущих версиях.

Возможности:
- Обработка блока выделенных строк;
- Выравнивание первой границы слева по верхней строке;
- Выравнивание второй границы слева по максимальному значению в блоке;

Установка:
- Добавить в качестве нового метода в класс EditorScripts и скомпилировать.

P.S. Конструктивная критика, мысли по доработке и вариации на тему приветствуются.

X++:
//AXAligner ver. 1.0 beta (for AX 2009, AX 2012)
//Developed by alex55 (AXforum.info), 22.01.2013
//Aligns first and second borders from the left for selected block of code
public void aAXAligner(Editor _editor)
{
    #define.EmptyString('')
    #define.Space(' ')
    #define.AllLeftSpaces('<: *')
    #define.AllCharsBeforeSecondLeftBorder('<[^: ]*')

    int i;
    int leftBorderWidth;
    int firstRowLength;
    int maxSecondLeftBorderWidth;
    int startLine = _editor.selectionStartLine();
    int endLine   = _editor.selectionEndLine();

    Map         map         = new Map(Types::Integer, Types::Container);
    TextBuffer  textBuffer  = new TextBuffer();

    str removeAllLeftSpaces(str _row)
    {
        ;

        textBuffer.setText(_row);
        textBuffer.replace(#AllLeftSpaces, #EmptyString);

        return textBuffer.getText();
    }

    void getMaxSecondLeftBorderWidth(str _row, int _rowNum)
    {
        int newRowLength;
        int secondLeftBorderWidth;
        int rowLength;
        ;

        _row = removeAllLeftSpaces(_row);

        rowLength = strlen(_row);

        textBuffer.setText(_row);
        textBuffer.replace(#AllCharsBeforeSecondLeftBorder, #EmptyString);
        textBuffer.replace(#AllLeftSpaces, #EmptyString);

        newRowLength = strlen(textBuffer.getText());

        secondLeftBorderWidth = rowLength - newRowLength;

        map.insert(_rowNum, [secondLeftBorderWidth, rowLength, _row]);

        if (secondLeftBorderWidth > maxSecondLeftBorderWidth)
        {
            maxSecondLeftBorderWidth = secondLeftBorderWidth;
        }
    }

    str alignSecondLeftBorder(int _rowNum)
    {
        int newRowLength;
        int secondLeftBorderWidth;
        int rowLength;
        str secondLeftBorderString;
        str row;
        ;

        [secondLeftBorderWidth, rowLength, row] = map.lookup(_rowNum);

        secondLeftBorderString  = substr(row, 1, secondLeftBorderWidth);
        secondLeftBorderString += strrep(#Space, maxSecondLeftBorderWidth - secondLeftBorderWidth);

        return strrep(#Space, leftBorderWidth) + secondLeftBorderString + substr(row, secondLeftBorderWidth + 1, rowLength - secondLeftBorderWidth);
    }

    str getEditorRow(int _rowNum)
    {
        _editor.gotoLine(_rowNum);
        
        return _editor.currentLine();
    }

    void replaceEditorRow(int _rowNum, str _rowValue)
    {
        _editor.gotoLine(i);
        _editor.gotoCol(1);
        _editor.deleteLines(1);
        _editor.insertLines('\n');
        _editor.gotoLine(_rowNum);
        _editor.gotoCol(1);
        _editor.insertLines(_rowValue);
    }
    ;

    firstRowLength  = strlen(getEditorRow(startLine));
    leftBorderWidth = firstRowLength - strlen(removeAllLeftSpaces(getEditorRow(startLine)));

    for (i = startLine; i <= endLine; i++)
    {
        getMaxSecondLeftBorderWidth(getEditorRow(i), i);
    }

    for (i = startLine; i <= endLine; i++)
    {
        replaceEditorRow(i, alignSecondLeftBorder(i));
    }
}
За это сообщение автора поблагодарили: perestoronin (1).