我正在使用 IOConsole 实现 Eclipse 插件,该 IOConsole 从键盘接收输入并生成输出(IOConsoleInputStream、IOConsoleOutputStream)。 我试图通过按照此处的建议扩展 TextConsoleViewer,将插入符号始终放在最后一个字符
How to set the Caret of the IOConsole
问题是,当打印输出后需要更改插入符位置时,由另一个引用输出流的线程写入的输出字符不计入控制台字符数。
这是我的代码链接
谢谢
最佳答案
setCaretOffset()
的源代码显示,如果您使用的偏移量大于文本的长度,那么将使用文本的长度,实际上是将插入符号放在末尾文本。因此,将 Integer.MAX_VALUE
设置为偏移量是一个可行的选项,无需对文本长度进行任何检查。
如果您无法收到关于冲洗何时真正完成的通知,我建议您将插入符号的放置延迟几百毫秒。它不会分散用户的注意力,并为您提供强大的解决方案。
作为引用,这里是setCaretOffset()
的源代码:
public void setCaretOffset(int offset) {
checkWidget();
int length = getCharCount();
if (length > 0 && offset != caretOffset) {
if (offset < 0) {
offset = 0;
} else if (offset > length) {
offset = length; // <-- use the length as offset
} else {
if (isLineDelimiter(offset)) {
// offset is inside a multi byte line delimiter. This is an
// illegal operation and an exception is thrown. Fixes 1GDKK3R
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
}
setCaretOffset(offset, PREVIOUS_OFFSET_TRAILING);
// clear the selection if the caret is moved.
// don't notify listeners about the selection change.
if (blockSelection) {
clearBlockSelection(true, false);
} else {
clearSelection(false);
}
}
setCaretLocation();
}
关于java - Eclipse IConsole 插入符位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18711112/