|
From: SVN by r. <sv...@ca...> - 2009-09-26 13:29:54
|
Author: roy
Date: 2009-09-26 15:29:39 +0200 (Sat, 26 Sep 2009)
New Revision: 423
Modified:
src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java
src/main/java/nl/improved/sqlclient/history/HistoryPersister.java
Log:
clean up code for history
some more history fixes
Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java
===================================================================
--- src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2009-09-26 12:51:06 UTC (rev 422)
+++ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2009-09-26 13:29:39 UTC (rev 423)
@@ -69,10 +69,6 @@
*/
private List<SQLCommand> commandHistory = new LimitedArrayList<SQLCommand>(50);
/**
- * Index for browsing commands.
- */
- private int commandIndex = 0;
- /**
* Some debug info holding the last trace of an exception.
*/
private String lastExceptionDetails;
@@ -192,8 +188,8 @@
specialActionKeys.put(InputKey.SpecialKey.UP, new KeyAction() {
@Override
public void execute() {
- if (commandIndex > 0) {
- commandLines = commandHistory.get(--commandIndex);
+ if (commandHistory.indexOf(commandLines) > 0) {
+ commandLines = commandHistory.get(commandHistory.indexOf(commandLines)-1);
Point cursorPosition = screen.getCursorPosition();
cursorPosition.y = commandLines.getLines().size()-1;
CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
@@ -208,8 +204,8 @@
specialActionKeys.put(InputKey.SpecialKey.DOWN, new KeyAction() {
@Override
public void execute() {
- if (commandIndex < commandHistory.size()-1) {
- commandLines = commandHistory.get(++commandIndex);
+ if (commandHistory.indexOf(commandLines) < commandHistory.size()-1) {
+ commandLines = commandHistory.get(commandHistory.indexOf(commandLines)+1);
Point cursorPosition = screen.getCursorPosition();
cursorPosition.y = commandLines.getLines().size()-1;
CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
@@ -480,18 +476,6 @@
return commandHistory;
}
- public int getCommandIndex() {
- return commandIndex;
- }
-
- /**
- * Change the current command index.
- * @param i
- */
- public void setCommandIndex(int i) {
- this.commandIndex = i;
- }
-
public abstract void show();
/**
@@ -662,14 +646,13 @@
if (command.length() > 0 && command.charAt(0) == '/') { // search in history
String matchPattern=".*"+command.substring(1,command.length())+".*";
for (int cIndex = commandHistory.size()-1; cIndex >=0; cIndex--) {
- if (cIndex == commandIndex) {
+ if (cIndex == commandHistory.indexOf(commandLines)) {
continue; // skip current command
}
SQLCommand newSqlCommand = commandHistory.get(cIndex);
String commandString = newSqlCommand.getCommandString();
if (commandString.matches(matchPattern)) {
commandHistory.remove(commandLines);
- commandIndex = commandHistory.indexOf(newSqlCommand);
commandLines = newSqlCommand;
Point cursorPosition = screen.getCursorPosition();
cursorPosition.y = 0;
@@ -681,9 +664,12 @@
beep(); // TODO clear search??
return;
} else if (executeCommand(sqlCommand)) {
+ if (commandHistory.indexOf(sqlCommand) < commandHistory.size()-1) {
+ commandHistory.remove(sqlCommand);
+ commandHistory.add(sqlCommand);
+ }
commandLines = new SQLCommand();
commandHistory.add(commandLines);
- commandIndex = commandHistory.size()-1;
newLine();
Point cursorPosition = screen.getCursorPosition();
cursorPosition.y = commandLines.getLines().size()-1;
@@ -721,6 +707,9 @@
} else {
Point cursorPosition = screen.getCursorPosition();
List<StringBuffer> editableLines = getEditableCommand().getEditableLines();
+ if (editableLines.size() < cursorPosition.y) {
+ editableLines.add(new StringBuffer());
+ }
StringBuffer currentLine = editableLines.get(cursorPosition.y);
if (cursorPosition.x > currentLine.length()) {
for (int i = currentLine.length(); i < cursorPosition.x; i++) {
@@ -780,16 +769,19 @@
* @return the editable version of the commandlines.
*/
protected SQLCommand getEditableCommand() {
- if (commandIndex != commandHistory.size() -1) {
+ if (commandHistory.indexOf(commandLines) != commandHistory.size() -1) {
SQLCommand current = commandLines;
commandLines = new SQLCommand();
commandLines.getEditableLines().clear();
for (StringBuffer buf : current.getEditableLines()) {
commandLines.getEditableLines().add(new StringBuffer(buf));
}
+ SQLCommand last = commandHistory.get(commandHistory.size()-1);
+ // clean up empty last
+ if (last.getLines().size() == 1 && last.getLines().get(0).length() == 0) {
+ commandHistory.remove(last);
+ }
commandHistory.add(commandLines);
- commandIndex = commandHistory.size() -1;
-
}
return commandLines;
}
Modified: src/main/java/nl/improved/sqlclient/history/HistoryPersister.java
===================================================================
--- src/main/java/nl/improved/sqlclient/history/HistoryPersister.java 2009-09-26 12:51:06 UTC (rev 422)
+++ src/main/java/nl/improved/sqlclient/history/HistoryPersister.java 2009-09-26 13:29:39 UTC (rev 423)
@@ -27,7 +27,6 @@
import nl.improved.sqlclient.AbstractSQLShellWindow;
import nl.improved.sqlclient.SQLCommand;
-import nl.improved.sqlclient.SQLShell;
import nl.improved.sqlclient.history.exception.CouldNotLoadHistoryException;
import nl.improved.sqlclient.history.exception.CouldNotSaveHistoryException;
@@ -84,7 +83,7 @@
}
log("ok 2");
r.close();
- shell.setCommandIndex(history.size() - 1);
+ //shell.setCommandIndex(history.size() - 1);
return tot;
} catch (IOException e) {
history.add(EMPTY_COMMAND);
@@ -102,7 +101,6 @@
public static void saveHistory(final AbstractSQLShellWindow shell, final String key,
final boolean excludeLastCommand) throws CouldNotSaveHistoryException {
List<SQLCommand> history = shell.getCommandHistory();
- int commandIndex = shell.getCommandIndex();
log("saving history... (" + history.size() + " commands)");
|