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

问题描述 投票:0回答:1

我需要使用标记和注释突出显示文本。我可以在文件激活后突出显示文本(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:资源树被锁定以进行修改。 org.eclipse.core.internal.resources.WorkManager.checkIn(WorkManager.java:119)org.eclipse.core.internal.resources.Workspace.prepareOperation(Workspace.java:2188)org.eclipse.core.internal .resources.Resource.deleteMarkers(Resource.java:821)

提前致谢。

java eclipse-plugin
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.