gpt4 book ai didi

java - 如何在 ResourceChangeListener(eclipse 插件)中添加标记?

转载 作者:行者123 更新时间:2023-12-02 10:14:14 27 4
gpt4 key购买 nike

我需要使用标记和注释突出显示文本。我可以在文件激活后突出显示文本 (IPartListener2),但我需要在用户保存文件后突出显示此文本 (IResourceChangeListener)。

我使用以下代码来添加标记,

private void updateMarkers2(IResource resource) throws PartInitException {
IEditorPart editorPart = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
if (editorPart != null) {
FileEditorInput input = (FileEditorInput) editorPart
.getEditorInput();
IFile file = input.getFile();
IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
FileEditorInput fileEditorInput = new FileEditorInput(file);
ITextEditor editor = (ITextEditor)PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage().openEditor(fileEditorInput, desc.getId());
IDocumentProvider dp = editor.getDocumentProvider();
IDocument doc = dp.getDocument(fileEditorInput);

IMarker marker = null;
try {
resource.deleteMarkers("org.eclipse.marker.value.id", true, 1);

final IRegion region = doc.getLineInformation(77);
marker = resource.createMarker("org.eclipse.marker.value.id");
//marker.setAttribute(IMarker.LINE_NUMBER, 77);
marker.setAttribute(IMarker.CHAR_START, region.getOffset());
marker.setAttribute(IMarker.CHAR_END, region.getOffset() + region.getLength());
} catch (CoreException | BadLocationException e) {
e.printStackTrace();
}
}
}

但我遇到以下异常:

org.eclipse.core.internal.resources.ResourceException: The resource tree is locked for modifications. at org.eclipse.core.internal.resources.WorkManager.checkIn(WorkManager.java:119) at org.eclipse.core.internal.resources.Workspace.prepareOperation(Workspace.java:2188) at org.eclipse.core.internal.resources.Resource.deleteMarkers(Resource.java:821)

最佳答案

您无法在实际的 IResourceChangeListener 中添加标记或对资源进行任何其他更改,因为工作区已锁定且不允许更改。

构建要在监听器中设置的标记列表,然后提交 WorkspaceJob 进行更改。这应该在锁释放后运行。

关于java - 如何在 ResourceChangeListener(eclipse 插件)中添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54785210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com