jEdit Community - Resources for users of the jEdit Text Editor
Shuffle variables within selection?
Submitted by optigon on Thursday, 19 January, 2012 - 22:03
Long time user of Jedit, but only used to the things I do on a regular basis.

It dawned on me today that there might be a macro or plugin for the following, but I am not really sure what it would be called.

Situation:
There is a line of code with a function including 2 or more variables.
eg. foo(A, B, C)

By selecting "A, B, C", is there a way to suffle their order left or right?
The result would be foo(B, C, A) after using the command once.

I ask because to do this using copy/paste normally requires 2 copy/paste operations.
1. Select and copy "A, "
2. Pasting at the end of the statement gives foo(B, CA, )
3. Select and copy ", " and paste it between CA

Shuffling would either be to the left or right, moving the first or last element to the opposite end of the selection. Returning the with all the items still selected would be useful for multiple iterations.

A shuffle tool that was space and comma sensitive would reduce these edits to a single operation. Perhaps there could be a setting to tell the command what characters represent variable separators (comma, space, math operators, colons, etc)

Having this work for collumn selections would be useful too.

Maybe I'm a lazy coder Smiling
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
I did this in about 30 mins.
by gary_m on Sat, 25/05/2013 - 14:40
I did this in about 30 mins. I copied part of the reverse lines standard macro. There are two options. One just reverses the selection and the other is a bit better, dealing only with comma delimiters. Maybe you can make it better?


--- BEGIN MACRO ---
reverseCommaList1(textArea) {
JEditBuffer buffer = textArea.getBuffer();
Selection[] selections = textArea.getSelection();

// doesn't work with rectangular selections, check for them up-front
for(int i = 0; i < selections.length; i++) {
if(selections[i] instanceof Selection.Rect) {
Macros.error(view, NotWorkingRectangularSelectionError);
return;
}
}

startLine = selections[0].getStartLine();
startOffset = buffer.getLineStartOffset(startLine);
text = buffer.getLineText(startLine);

sb = new StringBuilder();
for (i = text.length() - 1; i >= 0; i--) {
sb.append(text.charAt(i));
}

reversedStr = sb.toString();

buffer.remove(startOffset, reversedStr.length());
buffer.insert(startOffset, reversedStr);

}

reverseCommaList(textArea) {
JEditBuffer buffer = textArea.getBuffer();
Selection[] selections = textArea.getSelection();

// doesn't work with rectangular selections, check for them up-front
for(int i = 0; i < selections.length; i++) {
if(selections[i] instanceof Selection.Rect) {
Macros.error(view, NotWorkingRectangularSelectionError);
return;
}
}

startLine = selections[0].getStartLine();
startOffset = buffer.getLineStartOffset(startLine);
text = buffer.getLineText(startLine);

tk = new StringTokenizer(text, ",");

words = new String[tk.countTokens()];
lastElement = tk.countTokens() - 1;

while (tk.hasMoreTokens()) {
words[lastElement--] = tk.nextToken().trim();
}

sb = new StringBuffer();
for (int i = 0; i < words.length; i++) {
sb.append(words[i]);
if (i < words.length - 1) {
sb.append(", ");
}
}

reversedStr = sb.toString();

buffer.remove(startOffset, reversedStr.length());
buffer.insert(startOffset, reversedStr);

}

if(buffer.isReadOnly())
Macros.error(view, NotEditableMessage);
else
reverseCommaList(textArea);
--- END MACRO ---
User login
Browse archives
« April 2024  
MoTuWeThFrSaSu
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
 
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   82348
Context Free Art (*.cfdg)   0.31   46055
JBuilder scheme   .001   18495
BBEdit scheme   1.0   18116
ColdFusion scheme   1.0   18024
R Edit Mode - extensive version   0.1   17473
Advanced HTML edit mode   1.0   16206
Matlab Edit Mode   1.0   16068
jEdit XP icons   1.0   15229
XP icons for jEdit   1.1   14293