package jetbrains.mps.ide.java.newparser;
/*Generated by MPS */
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ListIterator;
import jetbrains.mps.internal.collections.runtime.Sequence;
public class CommentHelper {
public CommentHelper() {
}
public static List<String> splitString(char[] content, int[] lineends, int start, int end) {
List<String> result = ListSequence.fromList(new ArrayList<String>());
for (int i = Math.abs(Arrays.binarySearch(lineends, start) + 1); i < lineends.length && lineends[i] <= end; ++i) {
ListSequence.fromList(result).addElement(new String(content, start, lineends[i] - start));
start = lineends[i] + 1;
}
if (start < end) {
ListSequence.fromList(result).addElement(new String(content, start, end - start));
}
return result;
}
public static List<String> processLines(Iterable<String> lines, String start, String end) {
// remove start and end (if any) tags, indent
List<String> result = ListSequence.fromListWithValues(new ArrayList<String>(), lines);
if (ListSequence.fromList(result).isEmpty()) {
return result;
}
// remove start prefix
if (trim_rf742u_a0a4a2(ListSequence.fromList(result).first()).equals(start)) {
ListSequence.fromList(result).removeElementAt(0);
} else if (ListSequence.fromList(result).first().startsWith(start)) {
String trimmed = ListSequence.fromList(result).getElement(0).substring(start.length());
if (trimmed.startsWith(" ")) {
trimmed = trimmed.substring(1);
}
ListSequence.fromList(result).setElement(0, trimmed);
}
if (ListSequence.fromList(result).isEmpty()) {
return result;
}
if ((end != null && end.length() > 0)) {
if (trim_rf742u_a0a0a6a2(ListSequence.fromList(result).last()).equals(end)) {
ListSequence.fromList(result).removeLastElement();
} else if (ListSequence.fromList(result).last().endsWith(end)) {
String trimmed = ListSequence.fromList(result).last();
trimmed = trimmed.substring(0, trimmed.length() - end.length());
ListSequence.fromList(result).setElement(ListSequence.fromList(result).count() - 1, trimmed);
}
}
// find common indent for nonempty lines
if (ListSequence.fromList(result).isNotEmpty()) {
int mintrim = ListSequence.fromList(result).first().length();
for (String line : result) {
if ((line != null && line.length() > 0)) {
mintrim = Math.min(mintrim, whitespaceOrStar(line));
}
}
boolean trimok = mintrim > 0;
String prefix = ListSequence.fromList(result).first().substring(0, mintrim);
for (String line : result) {
if ((line != null && line.length() > 0)) {
trimok = trimok && line.startsWith(prefix);
}
}
ListIterator<String> iter = ((List<String>) result).listIterator();
while (iter.hasNext()) {
String line = iter.next();
iter.set((trimok && (line != null && line.length() > 0) ? line.substring(mintrim) : line));
}
}
return result;
}
public static List<String> processJavadoc(Iterable<String> lines) {
return processLines(lines, "/**", "*/");
}
public static List<String> processComment(Iterable<String> lines) {
if (Sequence.fromIterable(lines).first().startsWith("//")) {
return processLines(lines, "//", null);
} else {
return processLines(lines, "/*", "*/");
}
}
private static int whitespaceOrStar(String s) {
char[] chars = s.toCharArray();
boolean wasStar = false;
int k = 0;
for (; k < chars.length; k++) {
char c = chars[k];
boolean stillWhitespace = Character.isWhitespace(c) || c == '*' && !(wasStar);
if (!(stillWhitespace)) {
return k;
}
wasStar = wasStar || c == '*';
}
return k;
}
public static String trim_rf742u_a0a4a2(String str) {
return (str == null ? null : str.trim());
}
public static String trim_rf742u_a0a0a6a2(String str) {
return (str == null ? null : str.trim());
}
}