在执行操作之前检查项目错误

发布于 2024-12-08 10:59:55 字数 129 浏览 0 评论 0原文

我有一个 Eclipse 插件,它提供了一个菜单项,可以选择该菜单项来对当前活动文件运行命令。我希望插件在当前活动文件有任何错误时显示警告消息(如问题视图中报告的那样),类似于当您尝试运行有错误的 java 项目时 Eclipse 的行为方式。

I have an eclipse plug-in which provides a menu item which can be selected to run a command on the currently active file. I would like the plug-in to display a warning message if the currently active file has any errors on it (as reported in the Problems view), similar to how Eclipse acts when you try to run a java project with errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人接听 2024-12-15 10:59:55

我知道这是一个老问题,但我找到了与提议的解决方案类似的解决方案。执行您所描述的操作的代码位于 org.eclipse.debug.core.model.LaunchConfigurationDelegate 中。它检查项目是否有错误并在需要时显示对话框。以下是来自 Eclipse Luna 的相关代码:

/**
 * Returns whether the given project contains any problem markers of the
 * specified severity.
 *
 * @param proj the project to search
 * @return whether the given project contains any problems that should
 *  stop it from launching
 * @throws CoreException if an error occurs while searching for
 *  problem markers
 */
protected boolean existsProblems(IProject proj) throws CoreException {
    IMarker[] markers = proj.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
    if (markers.length > 0) {
        for (int i = 0; i < markers.length; i++) {
            if (isLaunchProblem(markers[i])) {
                return true;
            }
        }
    }
    return false;
}

/**
 * Returns whether the given problem should potentially abort the launch.
 * By default if the problem has an error severity, the problem is considered
 * a potential launch problem. Subclasses may override to specialize error
 * detection.
 *
 * @param problemMarker candidate problem
 * @return whether the given problem should potentially abort the launch
 * @throws CoreException if any exceptions occur while accessing marker attributes
 */
protected boolean isLaunchProblem(IMarker problemMarker) throws CoreException {
    Integer severity = (Integer)problemMarker.getAttribute(IMarker.SEVERITY);
    if (severity != null) {
        return severity.intValue() >= IMarker.SEVERITY_ERROR;
    }

    return false;
}

相同的代码可以在任何 IResource 而不是 IProject 上运行。

当显示对话框时,我通过从调试器挂起并在相关类上设置断点并从那里追溯来轻松找到它。

I know it's an old question, but I found a solution similar to the one proposed. The code that does what you descrive is in org.eclipse.debug.core.model.LaunchConfigurationDelegate. It checks if the project has errors and show the dialog if needed. Here is the relevant code, from Eclipse Luna:

/**
 * Returns whether the given project contains any problem markers of the
 * specified severity.
 *
 * @param proj the project to search
 * @return whether the given project contains any problems that should
 *  stop it from launching
 * @throws CoreException if an error occurs while searching for
 *  problem markers
 */
protected boolean existsProblems(IProject proj) throws CoreException {
    IMarker[] markers = proj.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
    if (markers.length > 0) {
        for (int i = 0; i < markers.length; i++) {
            if (isLaunchProblem(markers[i])) {
                return true;
            }
        }
    }
    return false;
}

/**
 * Returns whether the given problem should potentially abort the launch.
 * By default if the problem has an error severity, the problem is considered
 * a potential launch problem. Subclasses may override to specialize error
 * detection.
 *
 * @param problemMarker candidate problem
 * @return whether the given problem should potentially abort the launch
 * @throws CoreException if any exceptions occur while accessing marker attributes
 */
protected boolean isLaunchProblem(IMarker problemMarker) throws CoreException {
    Integer severity = (Integer)problemMarker.getAttribute(IMarker.SEVERITY);
    if (severity != null) {
        return severity.intValue() >= IMarker.SEVERITY_ERROR;
    }

    return false;
}

The same code can run on any IResource instead of an IProject.

I managed to find it easily by suspending from the debugger when the dialog was shown and setting a breakpoint on the relevant class and tracing back from there.

吃颗糖壮壮胆 2024-12-15 10:59:55

错误通常在资源上保存为 IMarkers(在您的情况下为 IFile),因此您可以在 IFile 中查询您要查找的标记。

您需要在查找之前了解标记的类型(通过调试并获取所有当前标记,或者通过查看在文件验证过程中贡献它们的代码)。

希望有帮助。

Errors are usually saved as IMarkers on a resource (IFile in your case), so you can query the IFile for the markers you are looking for.

You'll need to know the type of the markers before the look-up (either by debugging and get all the current markers, or by looking at the code the contributed them in the validation process of the file).

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文