JSON Indent macro
Patrick <[email protected]>
| Newsgroups | gmane.editors.jedit.user |
|---|---|
| Message-ID | <CAAo1TF+ODqjx-fmNHR0dyWgHyCiJ6EyNueJNU0n_U6o46Zc7aw@mail.gmail.com> |
Hello,
AFAIK there is no way to format JSON in JEdit. Please correct me if I am
wrong.
So I would like to contribute the following macro to format JSON:
~/jEdit/5.2.0/macros/Text/JSON_indent.bsh :
-------------------------------------------------------------
import java.lang.StringBuffer;
JEditTextArea jeTextArea = (JEditTextArea)textArea;
String originalText = jeTextArea.getText();
char[] chars = originalText.toCharArray();
jeTextArea.setText("");
StringBuffer ret = new StringBuffer();
int indent_width = 1;
int indent = 1;
boolean begin_quotes = false;
String newline = "\n";
for (int i = 0, indent = 0; i < chars.length; i++) {
char c = chars[i];
if (c == '\"') {
ret.append(c);
begin_quotes = !begin_quotes;
continue;
}
if (!begin_quotes) {
switch (c) {
case '{':
case '[':
indent += indent_width;
ret.append(c + newline);
for (int j=0; j<indent; j++) {
ret.append(" ");
}
continue;
case '}':
case ']':
ret.append(newline);
if ((indent -= indent_width) > 0) {
for (int j=0; j<indent; j++) {
ret.append(" ");
}
}
ret.append(c);
continue;
case ':':
ret.append(c + " ");
continue;
case ',':
ret.append(c + newline);
if (indent > 0) {
for (int j=0; j<indent; j++) {
ret.append(" ");
}
}
continue;
default:
if (Character.isWhitespace(c)) {
continue;
} else {
ret.append(c);
}
}
} else {
ret.append(c + (c == '\\' ? "" + chars[++i] : ""));
}
}
jeTextArea.setText(ret.toString());
--
-----------------------------------------------
jEdit Users' List
[email protected]
https://lists.sourceforge.net/lists/listinfo/jedit-users