File: Move_Lines_Up.bsh

package info (click to toggle)
jedit 5.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,252 kB
  • ctags: 11,190
  • sloc: java: 98,480; xml: 94,070; makefile: 52; sh: 42; cpp: 6; python: 6
file content (113 lines) | stat: -rw-r--r-- 4,513 bytes parent folder | download | duplicates (2)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Move_Lines_Up.bsh - Beanshell macro to move a selection of lines down by one
line.  This should handle multiple selections, but doesn't work quite right.

Copyright (c) Dale Anson, 2004
 :tabSize=4:indentSize=4:noTabs=false:
 :folding=explicit:collapseFolds=1:

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//Localization
final static String NotEditableMessage = jEdit.getProperty("macro.rs.general.ErrorNotEditableDialog.message", "Buffer is not editable");
final static String NoMultipleSelectionError = jEdit.getProperty("macro.rs.MoveLines.NoMultipleSelection.error", "Line move does not work with multiple selection.");

// get the current selection or the current line if no selection
Selection[] selections = textArea.getSelection();

// this doesn't work right with multiple selection, so don't do anything
if (selections.length > 1)
{
	Macros.error( view, NoMultipleSelectionError );
	return;
}

// the approach is to remove the line immediately preceding the selection
// and insert it immediately following the selection
int startLine;
int endLine;
int prevLine;
int insertionPoint;
int insertionLine;

if (selections.length == 0) {
	// nothing is selected, so use the line containing the caret
	startLine = textArea.getCaretLine();
	endLine = startLine;
}
else {
	startLine = textArea.getLineOfOffset(selections[0].getStart());
	endLine = textArea.getLineOfOffset(selections[0].getEnd());
	// limit to lines that are actually selected
	if (textArea.getLineStartOffset(endLine) == selections[0].getEnd()) {
		-- endLine;	
	}
}
if (startLine == 0) {
	// already at top, can't move further up
	return;	
}

// get the text of the line immediately preceding the selection, this is the 
// text to be moved
prevLine = startLine - 1;
String prevLineText = buffer.getLineText(prevLine);

// remove the previous line from the buffer
int removalStart = buffer.getLineStartOffset(prevLine);
int removalLength = buffer.getLineStartOffset(startLine) - buffer.getLineStartOffset(prevLine);
buffer.remove(removalStart, removalLength);

// then insert the previous line immediately after the selection
insertionLine = endLine;
boolean endLineIsLast = endLine == buffer.getLineCount() - 1;
String lineSeparator = buffer.getStringProperty("lineSeparator");
if (endLineIsLast) {
	insertionPoint = buffer.getLength();
	buffer.insertIndented(insertionPoint, lineSeparator + prevLineText);
}
else {
	insertionPoint = textArea.getLineStartOffset(insertionLine);
	buffer.insertIndented(insertionPoint, prevLineText + lineSeparator);
}

// indent the selection	
Mode mode = buffer.getMode();
boolean shouldIndent = false;
String[] indentProps = new String[]{"indentOpenBrackets", "indentOpenBrackets", "unalignedOpenBrackets", "unalignedCloseBrackets", "indentNextLine", "unindentThisLine", "electricKeys", "doubleBracketIndent", "lineUpClosingBracket"} ;
for (String name : indentProps) {
    if (mode.getProperty(name) != null) {
        shouldIndent = true;
        break;
    }
}
if (shouldIndent) {
	buffer.indentLines(startLine - 1, endLine - 1);	
}

// reselect
textArea.selectNone();
selection = new Selection.Range(textArea.getLineStartOffset(prevLine), insertionPoint);
textArea.addToSelection(selection);