summaryrefslogtreecommitdiffstats
path: root/examples/rectangle-commands.js
blob: ba8a6eb05ffe9d801636a1693a87917a0b0404ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var editor = editors.current()

var anchorPos = editor.position(PositionOperation.Anchor)
var anchorColumn = anchorPos.column
var anchorLine = anchorPos.line

var pointPos = editor.position(PositionOperation.Current)
var pointColumn = pointPos.column
var pointLine = pointPos.line

var startColumn, endColumn, startLine, endLine
if ( anchorLine < pointLine || (anchorLine == pointLine && anchorColumn < pointColumn)) {
    startColumn = anchorColumn
    endColumn = pointColumn
    startLine = anchorLine
    endLine = pointLine
}
else {
    startColumn = pointColumn
    endColumn = anchorColumn
    startLine = pointLine
    endLine = anchorLine
}


function replaceTextInRectangle(text) {
    if ( startLine != -1 || startColumn != -1) {
        var line = startLine
        while ( line <= endLine ) {
            editor.gotoLine(line,startColumn)
            editor.replace(endColumn-startColumn, text)
            line = line +1
        }
    }
}

function spaces(count) {
    var result = ""
    for (var i=0; i < count; i++) {
        result = result + " "
    }
    return result
}

function openRectangle() {
    if ( startLine != -1 || startColumn != -1) {
        var line = startLine
        while ( line <= endLine ) {
            editor.gotoLine(line,startColumn)
            editor.replace(0, spaces(endColumn-startColumn))
            line = line +1
        }

    }
}