![](/img/trans.png)
[英]Get current line when some previous lines was collapsed in my own eclipse editor
[英]Get text in current line when some previous lines was collapsed in my own eclipse editor
我試圖將當前行的開頭文本添加到鼠標光標。
ITextSelection textSelection= (ITextSelection) textEditor.getSelectionProvider().getSelection();
IRegion lineInfo = null;
int offsetCurrentCursor = textSelection.getOffset();
int offsetLine; // THIS IS THE VALUE THAT I WANT
try {
int lineNumber = document.getLineOfOffset(offsetCurrentCursor);
lineInfo = document.getLineInformation(lineNumber);
offsetLine = lineInfo.getOffset();
} catch (BadLocationException e) {
}
StyledText styledText = (StyledText) textEditor.getAdapter(Control.class);
String currentText = "";
if (offsetLine <= offsetCurrentCursor - 1) {
currentText = styledText.getText(offsetLine, offsetCurrentCursor - 1);
}
但是,當某些前幾行合攏時, StyledText
getText
方法不起作用。
另一個問題是,我試圖使用StyledText.setCaretOffset(int offset)
移動光標,並且在折疊某些先前的行時它也不起作用。
使用IDocument
public String get(int offset, int length)
獲取文本的方法。
要從模型(文檔)偏移量獲取StyledText偏移量,AbstractTextViewer使用:
protected static int modelOffset2WidgetOffset(ISourceViewer viewer, int modelOffset) {
if (viewer instanceof ITextViewerExtension5) {
ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
return extension.modelOffset2WidgetOffset(modelOffset);
}
return modelOffset - viewer.getVisibleRegion().getOffset();
}
要使用此功能,您需要訪問文本編輯器ISourceViewer
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.