admin管理员组文章数量:1393563
We've developed a plugin that retrieves data from a third-party API and inserts it into the editor. We need to apply specific styling to the inserted content, which should persist until the user performs the next action (e.g., pressing a key).
Currently, the text insertion and initial styling application work correctly. However, the style is immediately removed.
Code:
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (!(editor instanceof ITextEditor))
return null;
try {
final ITextEditor textEditor = (ITextEditor) editor;
final IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
final ITextViewer iTextViewer = (ITextViewer) textEditor.getAdapter(ITextViewer.class);
if (iTextViewer == null) {
return null;
}
styledText = iTextViewer.getTextWidget();
int insertPosition = styledText.getCaretOffset();
RestPayload job = new RestPayload();
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (event.getResult() != null) {
IStatus result = event.getResult();
Display.getDefault().asyncExec(() -> {
if (result.isOK()) {
try {
String generatedCode = job.getPayload();
document.replace(insertPosition, 0, generatedCode);
suggestionStyle = new StyleRange();
suggestionStyle.start = insertPosition;
suggestionStyle.foreground = new Color(Display.getDefault(), 192, 192, 192);
suggestionStyle.length = generatedCode.length();
styledText.setStyleRange(suggestionStyle);
styledText.update();
Display.getDefault().timerExec(2000, () -> {
styledText.setStyleRange(suggestionStyle);
styledText.redraw();
});
} catch (Exception ex) {
logger.warn("Error inserting/styling code", ex);
}
} else {
showAlert("Code Assist Error", "Code assist failed: " + result.getMessage());
}
});
}
}
});
job.schedule();
} catch (Throwable eGenThrowable) {
showAlert("Issue", eGenThrowable.getMessage());
eGenThrowable.printStackTrace();
logger.warn("Issue while exchanging data", eGenThrowable);
}
return styledText;
}
Note:
Display.getDefault().timerExec(1000, () -> {
styledText.setStyleRange(suggestionStyle);
styledText.redraw();
});
The above code snippet obscures the inserted code. However, it is not an applicable solution.
本文标签: Eclipse Set Style Range not working as expectedStack Overflow
版权声明:本文标题:Eclipse Set Style Range not working as expected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744764538a2623958.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论