package jdk.internal.jline.extra;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Supplier;
import jdk.internal.jline.console.ConsoleReader;
import jdk.internal.jline.console.KeyMap;
import jdk.internal.jline.console.history.History;
import jdk.internal.jline.console.history.History.Entry;
import jdk.internal.jline.console.history.MemoryHistory;
public abstract class implements History {
private final History ;
private History ;
protected (ConsoleReader in, Iterable<? extends String> originalHistory) {
MemoryHistory fullHistory = new MemoryHistory();
fullHistory.setIgnoreDuplicates(false);
this.fullHistory = fullHistory;
this.currentDelegate = fullHistory;
bind(in, CTRL_UP,
(Runnable) () -> moveHistoryToSnippet(in, ((EditingHistory) in.getHistory())::previousSnippet));
bind(in, CTRL_DOWN,
(Runnable) () -> moveHistoryToSnippet(in, ((EditingHistory) in.getHistory())::nextSnippet));
if (originalHistory != null) {
load(originalHistory);
}
}
private void (ConsoleReader in, Supplier<Boolean> action) {
if (!action.get()) {
try {
in.beep();
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
} else {
try {
Method setBuffer = ConsoleReader.class.getDeclaredMethod("setBuffer", String.class);
setBuffer.setAccessible(true);
setBuffer.invoke(in, in.getHistory().current().toString());
in.flush();
} catch (ReflectiveOperationException | IOException ex) {
throw new IllegalStateException(ex);
}
}
}
private void (ConsoleReader in, String shortcut, Object action) {
KeyMap km = in.getKeys();
for (int i = 0; i < shortcut.length(); i++) {
Object value = km.getBound(Character.toString(shortcut.charAt(i)));
if (value instanceof KeyMap) {
km = (KeyMap) value;
} else {
km.bind(shortcut.substring(i), action);
}
}
}
private static final String = "\033\133\061\073\065\101";
private static final String = "\033\133\061\073\065\102";
@Override
public int () {
return currentDelegate.size();
}
@Override
public boolean () {
return currentDelegate.isEmpty();
}
@Override
public int () {
return currentDelegate.index();
}
@Override
public void () {
if (currentDelegate != fullHistory)
throw new IllegalStateException("narrowed");
currentDelegate.clear();
}
@Override
public CharSequence (int index) {
return currentDelegate.get(index);
}
@Override
public void (CharSequence line) {
NarrowingHistoryLine currentLine = null;
int origIndex = fullHistory.index();
int fullSize;
try {
fullHistory.moveToEnd();
fullSize = fullHistory.index();
if (currentDelegate == fullHistory) {
if (origIndex < fullHistory.index()) {
for (Entry entry : fullHistory) {
if (!(entry.value() instanceof NarrowingHistoryLine))
continue;
int[] cluster = ((NarrowingHistoryLine) entry.value()).span;
if (cluster[0] == origIndex && cluster[1] > cluster[0]) {
currentDelegate = new MemoryHistory();
for (int i = cluster[0]; i <= cluster[1]; i++) {
currentDelegate.add(fullHistory.get(i));
}
}
}
}
}
fullHistory.moveToEnd();
while (fullHistory.previous()) {
CharSequence c = fullHistory.current();
if (c instanceof NarrowingHistoryLine) {
currentLine = (NarrowingHistoryLine) c;
break;
}
}
} finally {
fullHistory.moveTo(origIndex);
}
if (currentLine == null || currentLine.span[1] != (-1)) {
line = currentLine = new NarrowingHistoryLine(line, fullSize);
}
StringBuilder complete = new StringBuilder();
for (int i = currentLine.span[0]; i < fullSize; i++) {
complete.append(fullHistory.get(i));
}
complete.append(line);
if (isComplete(complete)) {
currentLine.span[1] = fullSize;
currentDelegate = fullHistory;
}
fullHistory.add(line);
}
protected abstract boolean (CharSequence input);
@Override
public void (int index, CharSequence item) {
if (currentDelegate != fullHistory)
throw new IllegalStateException("narrowed");
currentDelegate.set(index, item);
}
@Override
public CharSequence (int i) {
if (currentDelegate != fullHistory)
throw new IllegalStateException("narrowed");
return currentDelegate.remove(i);
}
@Override
public CharSequence () {
if (currentDelegate != fullHistory)
throw new IllegalStateException("narrowed");
return currentDelegate.removeFirst();
}
@Override
public CharSequence () {
if (currentDelegate != fullHistory)
throw new IllegalStateException("narrowed");
return currentDelegate.removeLast();
}
@Override
public void (CharSequence item) {
if (currentDelegate != fullHistory)
throw new IllegalStateException("narrowed");
currentDelegate.replace(item);
}
@Override
public ListIterator<Entry> (int index) {
return currentDelegate.entries(index);
}
@Override
public ListIterator<Entry> () {
return currentDelegate.entries();
}
@Override
public Iterator<Entry> () {
return currentDelegate.iterator();
}
@Override
public CharSequence () {
return currentDelegate.current();
}
@Override
public boolean () {
return currentDelegate.previous();
}
@Override
public boolean () {
return currentDelegate.next();
}
@Override
public boolean () {
return currentDelegate.moveToFirst();
}
@Override
public boolean () {
return currentDelegate.moveToLast();
}
@Override
public boolean (int index) {
return currentDelegate.moveTo(index);
}
@Override
public void () {
currentDelegate.moveToEnd();
}
public boolean () {
while (previous()) {
if (current() instanceof NarrowingHistoryLine) {
return true;
}
}
return false;
}
public boolean () {
boolean success = false;
while (next()) {
success = true;
if (current() instanceof NarrowingHistoryLine) {
return true;
}
}
return success;
}
public final void (Iterable<? extends String> originalHistory) {
NarrowingHistoryLine currentHistoryLine = null;
boolean start = true;
int currentLine = 0;
for (String historyItem : originalHistory) {
StringBuilder line = new StringBuilder(historyItem);
int trailingBackSlashes = countTrailintBackslashes(line);
boolean continuation = trailingBackSlashes % 2 != 0;
line.delete(line.length() - trailingBackSlashes / 2 - (continuation ? 1 : 0), line.length());
if (start) {
class extends NarrowingHistoryLine implements PersistentEntryMarker {
public (CharSequence delegate, int start) {
super(delegate, start);
}
}
fullHistory.add(currentHistoryLine = new PersistentNarrowingHistoryLine(line, currentLine));
} else {
class implements CharSequence, PersistentEntryMarker {
private final CharSequence ;
public (CharSequence delegate) {
this.delegate = delegate;
}
@Override public int () {
return delegate.length();
}
@Override public char (int index) {
return delegate.charAt(index);
}
@Override public CharSequence (int start, int end) {
return delegate.subSequence(start, end);
}
@Override public String () {
return delegate.toString();
}
}
fullHistory.add(new PersistentLine(line));
}
start = !continuation;
currentHistoryLine.span[1] = currentLine;
currentLine++;
}
}
public Collection<? extends String> () {
Collection<String> result = new ArrayList<>();
Iterator<Entry> entries = fullHistory.iterator();
if (entries.hasNext()) {
Entry entry = entries.next();
while (entry != null) {
StringBuilder historyLine = new StringBuilder(entry.value());
int trailingBackSlashes = countTrailintBackslashes(historyLine);
for (int i = 0; i < trailingBackSlashes; i++) {
historyLine.append("\\");
}
entry = entries.hasNext() ? entries.next() : null;
if (entry != null && !(entry.value() instanceof NarrowingHistoryLine)) {
historyLine.append("\\");
}
result.add(historyLine.toString());
}
}
return result;
}
private int (CharSequence text) {
int count = 0;
for (int i = text.length() - 1; i >= 0; i--) {
if (text.charAt(i) == '\\') {
count++;
} else {
break;
}
}
return count;
}
public List<String> () {
List<String> result = new ArrayList<>();
for (Entry e : fullHistory) {
if (!(e.value() instanceof PersistentEntryMarker)) {
result.add(e.value().toString());
}
}
return result;
}
public void (String source) {
fullHistory.removeLast();
for (String line : source.split("\\R")) {
fullHistory.add(line);
}
}
private class implements CharSequence {
private final CharSequence ;
private final int[] ;
public (CharSequence delegate, int start) {
this.delegate = delegate;
this.span = new int[] {start, -1};
}
@Override
public int () {
return delegate.length();
}
@Override
public char (int index) {
return delegate.charAt(index);
}
@Override
public CharSequence (int start, int end) {
return delegate.subSequence(start, end);
}
@Override
public String () {
return delegate.toString();
}
}
private interface {}
}