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

How can I add markers in ResourceChangeListener (eclipse plugin)?

我需要使用标记和注释突出显示文本。 我可以在文件激活后突出显示文本 (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 以进行更改。释放锁后应该运行。