You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
285 lines
8.3 KiB
285 lines
8.3 KiB
7 years ago
|
/*
|
||
|
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||
|
*/
|
||
|
package org.jetbrains.java.decompiler.util;
|
||
10 years ago
|
|
||
7 years ago
|
import org.jetbrains.java.decompiler.main.DecompilerContext;
|
||
10 years ago
|
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
|
||
|
|
||
10 years ago
|
import java.util.*;
|
||
10 years ago
|
|
||
|
/**
|
||
|
* Allows to connect text with resulting lines
|
||
|
*
|
||
|
* @author egor
|
||
|
*/
|
||
7 years ago
|
@SuppressWarnings("UnusedReturnValue")
|
||
10 years ago
|
public class TextBuffer {
|
||
|
private final String myLineSeparator = DecompilerContext.getNewLineSeparator();
|
||
10 years ago
|
private final String myIndent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING);
|
||
10 years ago
|
private final StringBuilder myStringBuilder;
|
||
|
private Map<Integer, Integer> myLineToOffsetMapping = null;
|
||
|
|
||
|
public TextBuffer() {
|
||
|
myStringBuilder = new StringBuilder();
|
||
|
}
|
||
|
|
||
|
public TextBuffer(int size) {
|
||
|
myStringBuilder = new StringBuilder(size);
|
||
|
}
|
||
|
|
||
10 years ago
|
public TextBuffer(String text) {
|
||
|
myStringBuilder = new StringBuilder(text);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public TextBuffer append(String str) {
|
||
|
myStringBuilder.append(str);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public TextBuffer append(char ch) {
|
||
|
myStringBuilder.append(ch);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
10 years ago
|
public TextBuffer append(int i) {
|
||
|
myStringBuilder.append(i);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
10 years ago
|
public TextBuffer appendLineSeparator() {
|
||
|
myStringBuilder.append(myLineSeparator);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
10 years ago
|
public TextBuffer appendIndent(int length) {
|
||
|
while (length-- > 0) {
|
||
|
append(myIndent);
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
|
||
10 years ago
|
public TextBuffer prepend(String s) {
|
||
7 years ago
|
myStringBuilder.insert(0, s);
|
||
|
shiftMapping(s.length());
|
||
10 years ago
|
return this;
|
||
|
}
|
||
|
|
||
|
public TextBuffer enclose(String left, String right) {
|
||
|
prepend(left);
|
||
|
append(right);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public boolean containsOnlyWhitespaces() {
|
||
|
for (int i = 0; i < myStringBuilder.length(); i++) {
|
||
|
if (myStringBuilder.charAt(i) != ' ') {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
@Override
|
||
|
public String toString() {
|
||
|
String original = myStringBuilder.toString();
|
||
|
if (myLineToOffsetMapping == null || myLineToOffsetMapping.isEmpty()) {
|
||
10 years ago
|
if (myLineMapping != null) {
|
||
|
return addOriginalLineNumbers();
|
||
|
}
|
||
10 years ago
|
return original;
|
||
|
}
|
||
|
else {
|
||
|
StringBuilder res = new StringBuilder();
|
||
|
String[] srcLines = original.split(myLineSeparator);
|
||
|
int currentLineStartOffset = 0;
|
||
|
int currentLine = 0;
|
||
|
int previousMarkLine = 0;
|
||
|
int dumpedLines = 0;
|
||
8 years ago
|
ArrayList<Integer> linesWithMarks = new ArrayList<>(myLineToOffsetMapping.keySet());
|
||
10 years ago
|
Collections.sort(linesWithMarks);
|
||
|
for (Integer markLine : linesWithMarks) {
|
||
|
Integer markOffset = myLineToOffsetMapping.get(markLine);
|
||
|
while (currentLine < srcLines.length) {
|
||
|
String line = srcLines[currentLine];
|
||
|
int lineEnd = currentLineStartOffset + line.length() + myLineSeparator.length();
|
||
10 years ago
|
if (markOffset <= lineEnd) {
|
||
10 years ago
|
int requiredLine = markLine - 1;
|
||
|
int linesToAdd = requiredLine - dumpedLines;
|
||
|
dumpedLines = requiredLine;
|
||
|
appendLines(res, srcLines, previousMarkLine, currentLine, linesToAdd);
|
||
10 years ago
|
previousMarkLine = currentLine;
|
||
|
break;
|
||
|
}
|
||
|
currentLineStartOffset = lineEnd;
|
||
|
currentLine++;
|
||
|
}
|
||
|
}
|
||
|
if (previousMarkLine < srcLines.length) {
|
||
|
appendLines(res, srcLines, previousMarkLine, srcLines.length, srcLines.length - previousMarkLine);
|
||
|
}
|
||
|
|
||
|
return res.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private String addOriginalLineNumbers() {
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
int lineStart = 0, lineEnd;
|
||
|
int count = 0, length = myLineSeparator.length();
|
||
|
while ((lineEnd = myStringBuilder.indexOf(myLineSeparator, lineStart)) > 0) {
|
||
|
++count;
|
||
|
sb.append(myStringBuilder.substring(lineStart, lineEnd));
|
||
10 years ago
|
Set<Integer> integers = myLineMapping.get(count);
|
||
|
if (integers != null) {
|
||
|
sb.append("//");
|
||
|
for (Integer integer : integers) {
|
||
|
sb.append(' ').append(integer);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
sb.append(myLineSeparator);
|
||
|
lineStart = lineEnd + length;
|
||
|
}
|
||
|
if (lineStart < myStringBuilder.length()) {
|
||
|
sb.append(myStringBuilder.substring(lineStart));
|
||
|
}
|
||
|
return sb.toString();
|
||
|
}
|
||
|
|
||
10 years ago
|
private void appendLines(StringBuilder res, String[] srcLines, int from, int to, int requiredLineNumber) {
|
||
|
if (to - from > requiredLineNumber) {
|
||
10 years ago
|
List<String> strings = compactLines(Arrays.asList(srcLines).subList(from, to) ,requiredLineNumber);
|
||
10 years ago
|
int separatorsRequired = requiredLineNumber - 1;
|
||
10 years ago
|
for (String s : strings) {
|
||
|
res.append(s);
|
||
10 years ago
|
if (separatorsRequired-- > 0) {
|
||
|
res.append(myLineSeparator);
|
||
|
}
|
||
|
}
|
||
|
res.append(myLineSeparator);
|
||
|
}
|
||
|
else if (to - from <= requiredLineNumber) {
|
||
|
for (int i = from; i < to; i++) {
|
||
|
res.append(srcLines[i]).append(myLineSeparator);
|
||
|
}
|
||
|
for (int i = 0; i < requiredLineNumber - to + from; i++) {
|
||
|
res.append(myLineSeparator);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public int length() {
|
||
|
return myStringBuilder.length();
|
||
|
}
|
||
|
|
||
7 years ago
|
public void setStart(int position) {
|
||
10 years ago
|
myStringBuilder.delete(0, position);
|
||
7 years ago
|
shiftMapping(-position);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public void setLength(int position) {
|
||
|
myStringBuilder.setLength(position);
|
||
10 years ago
|
if (myLineToOffsetMapping != null) {
|
||
7 years ago
|
Map<Integer, Integer> newMap = new HashMap<>();
|
||
10 years ago
|
for (Map.Entry<Integer, Integer> entry : myLineToOffsetMapping.entrySet()) {
|
||
|
if (entry.getValue() <= position) {
|
||
|
newMap.put(entry.getKey(), entry.getValue());
|
||
|
}
|
||
|
}
|
||
|
myLineToOffsetMapping = newMap;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public TextBuffer append(TextBuffer buffer) {
|
||
10 years ago
|
if (buffer.myLineToOffsetMapping != null && !buffer.myLineToOffsetMapping.isEmpty()) {
|
||
|
checkMapCreated();
|
||
|
for (Map.Entry<Integer, Integer> entry : buffer.myLineToOffsetMapping.entrySet()) {
|
||
|
myLineToOffsetMapping.put(entry.getKey(), entry.getValue() + myStringBuilder.length());
|
||
|
}
|
||
|
}
|
||
|
myStringBuilder.append(buffer.myStringBuilder);
|
||
10 years ago
|
return this;
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
private void shiftMapping(int shiftOffset) {
|
||
10 years ago
|
if (myLineToOffsetMapping != null) {
|
||
7 years ago
|
Map<Integer, Integer> newMap = new HashMap<>();
|
||
10 years ago
|
for (Map.Entry<Integer, Integer> entry : myLineToOffsetMapping.entrySet()) {
|
||
10 years ago
|
int newValue = entry.getValue();
|
||
7 years ago
|
if (newValue >= 0) {
|
||
10 years ago
|
newValue += shiftOffset;
|
||
|
}
|
||
|
if (newValue >= 0) {
|
||
|
newMap.put(entry.getKey(), newValue);
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
myLineToOffsetMapping = newMap;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private void checkMapCreated() {
|
||
|
if (myLineToOffsetMapping == null) {
|
||
8 years ago
|
myLineToOffsetMapping = new HashMap<>();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
public int countLines() {
|
||
|
return countLines(0);
|
||
|
}
|
||
|
|
||
|
public int countLines(int from) {
|
||
|
return count(myLineSeparator, from);
|
||
|
}
|
||
|
|
||
10 years ago
|
public int count(String substring, int from) {
|
||
|
int count = 0, length = substring.length(), p = from;
|
||
|
while ((p = myStringBuilder.indexOf(substring, p)) > 0) {
|
||
|
++count;
|
||
|
p += length;
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
10 years ago
|
|
||
|
private static List<String> compactLines(List<String> srcLines, int requiredLineNumber) {
|
||
|
if (srcLines.size() < 2 || srcLines.size() <= requiredLineNumber) {
|
||
|
return srcLines;
|
||
|
}
|
||
8 years ago
|
List<String> res = new LinkedList<>(srcLines);
|
||
10 years ago
|
// first join lines with a single { or }
|
||
|
for (int i = res.size()-1; i > 0 ; i--) {
|
||
|
String s = res.get(i);
|
||
|
if (s.trim().equals("{") || s.trim().equals("}")) {
|
||
|
res.set(i-1, res.get(i-1).concat(s));
|
||
|
res.remove(i);
|
||
|
}
|
||
|
if (res.size() <= requiredLineNumber) {
|
||
|
return res;
|
||
|
}
|
||
|
}
|
||
|
// now join empty lines
|
||
|
for (int i = res.size()-1; i > 0 ; i--) {
|
||
|
String s = res.get(i);
|
||
|
if (s.trim().isEmpty()) {
|
||
|
res.set(i-1, res.get(i-1).concat(s));
|
||
|
res.remove(i);
|
||
|
}
|
||
|
if (res.size() <= requiredLineNumber) {
|
||
|
return res;
|
||
|
}
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
private Map<Integer, Set<Integer>> myLineMapping = null; // new to original
|
||
9 years ago
|
|
||
10 years ago
|
public void dumpOriginalLineNumbers(int[] lineMapping) {
|
||
|
if (lineMapping.length > 0) {
|
||
8 years ago
|
myLineMapping = new HashMap<>();
|
||
9 years ago
|
for (int i = 0; i < lineMapping.length; i += 2) {
|
||
10 years ago
|
int key = lineMapping[i + 1];
|
||
7 years ago
|
Set<Integer> existing = myLineMapping.computeIfAbsent(key, k -> new TreeSet<>());
|
||
10 years ago
|
existing.add(lineMapping[i]);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
7 years ago
|
}
|