如何以编程方式更改文件的Eclipse CDT工具设置? [英] How do I programmatically change the Eclipse CDT tool settings for a file?

查看:331
本文介绍了如何以编程方式更改文件的Eclipse CDT工具设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式(从插件中)更改CDT托管构建项目中单个文件的工具设置选项卡中其他设置中的其他标志字段。 (请参阅 this Eclipse文档页面,以了解如何使用UI进行更改的屏幕截图和简要说明。)

I want to programmatically (from a plugin) change the "Other flags" field in the Miscellaneous settings in the Tool Settings tab for an individual file in a CDT managed build project. (See this Eclipse documentation page for a screenshot and brief description of how this change would be made using the UI.)

注意:我已更新此现在两次,我接近一个解决方案。但是,我正在修改整个问题,而不是在最后添加更新(就像我在较短的问题中)。如果它有助于查看导致我现在所在的导航痕迹,您可以阅读历史。

以下代码将导致写入的设置。 cproject文件(我将在下面详细介绍),但是当我打开文件的属性对话框,然后单击C / C ++ Build-> Settings,然后单击Miscellaneous,这些更改不会显示在其他标志中字段(与使用对话框进行更改时一样)。

The following code will result in settings written to the .cproject file (I'll go into detail on that below), but when I open the Properties dialog for the file, and click on C/C++ Build->Settings and then Miscellaneous, the changes do not appear in the "Other flags" field (as they do when I make changes using the dialog).

IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow workbenchwindow = workbench.getActiveWorkbenchWindow();
IWorkbenchPage workbenchpage = workbenchwindow.getActivePage();
IEditorPart editorpart = workbenchpage.getActiveEditor();
IEditorInput editorinput = editorpart.getEditorInput();
IResource file = ((IFileEditorInput)editorinput).getFile();
IProject project = file.getProject();
IManagedBuildInfo buildInfo = ManagedBuildManager.getBuildInfo(project);
IConfiguration[] configurations = buildInfo.getManagedProject().getConfigurations();
IConfiguration conf = configurations[0];
IFileInfo fileInfo = conf.createFileInfo(file.getFullPath());
ITool[] tools = fileInfo.getTools();
ITool tool = tools[0];
IOption option = tool.getOptionById("my.gnu.compiler.misc.other");
String oldOptionValue = option.getDefaultValue().toString();
String newOptionValue = oldOptionValue + " -eg";
ManagedBuildManager.setOption(fileInfo, tool, option, newOptionValue);
ManagedBuildManager.saveBuildInfo(project, true);

我查看了.cproject文件之间的差异,我手动更改了工具设置,运行上述代码后,不同的.cproject文件。以下是每个相关的位。

I looked at the differences between the .cproject file where I changed the tool settings manually, and in a different .cproject file after running the above code. Below are the pertinent bits from each.

以下是.cproject文件中的手动更改工具设置的XML:

Here is the XML from the .cproject file where the tool settings were changed manually:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
    <storageModule moduleId="org.eclipse.cdt.core.settings">
        <cconfiguration id="com.company.product.toolchain.configuration.changed.1964078554">
...
            <storageModule moduleId="cdtBuildSystem" version="4.0.0">
                <configuration artifactName="${ProjName}" buildProperties="" cleanCommand="rm -f" description="" id="com.company.product.toolchain.configuration.changed.1964078554" name="changed" parent="com.company.product.toolchain.configuration.changed">
...
                    <fileInfo id="com.company.product.toolchain.configuration.changed.1964078554.915152327" name="code.cpp" rcbsApplicability="disable" resourcePath="src/code.cpp" toolsToInvoke="com.company.product.toolchain.compiler.1643348654.1411455203">
                        <tool id="com.company.product.toolchain.compiler.1643348654.1411455203" name="Company GNU compilers" superClass="com.company.product.toolchain.compiler.1643348654">
                            <option id="company.gnu.compiler.misc.other.789167779" name="Other flags" superClass="company.gnu.compiler.misc.other" value="-c -fmessage-length=0 -eg" valueType="string"/>
                            <inputType id="com.company.product.toolchain.cxxinputtype.877052163" name="C++ Input" superClass="com.company.product.toolchain.cxxinputtype"/>
                            <inputType id="com.company.product.toolchain.cinputtype.1390394900" name="C input" superClass="com.company.product.toolchain.cinputtype"/>
                        </tool>
                    </fileInfo>
...

这里是.cproject文件中被更改的XML以编程方式:

And here is the XML from the .cproject file where they were changed programmatically:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
    <storageModule moduleId="org.eclipse.cdt.core.settings">
        <cconfiguration id="com.company.product.toolchain.configuration.changed.2057644715">
...
            <storageModule moduleId="cdtBuildSystem" version="4.0.0">
                <configuration artifactName="${ProjName}" buildProperties="" cleanCommand="rm -f" description="" id="com.company.product.toolchain.configuration.changed.2057644715" name="changed" parent="com.company.product.toolchain.configuration.changed">
...
                    <fileInfo id="com.company.product.toolchain.configuration.changed.2057644715./test3/src/code.cpp" name="code.cpp" rcbsApplicability="disable" resourcePath="test3/src/code.cpp" toolsToInvoke="com.company.product.toolchain.compiler.1482360042.1005761865">
                        <tool id="com.company.product.toolchain.compiler.1482360042.1005761865" name="Company GNU compilers" superClass="com.company.product.toolchain.compiler.1482360042">
                            <option id="company.gnu.compiler.misc.other.999025984" superClass="company.gnu.compiler.misc.other" value="-c -fmessage-length=0 -eg" valueType="string"/>
                            <inputType id="com.company.product.toolchain.cxxinputtype.1253686787" name="C++ Input" superClass="com.company.product.toolchain.cxxinputtype"/>
                            <inputType id="com.company.product.toolchain.cinputtype.1141524787" name="C input" superClass="com.company.product.toolchain.cinputtype"/>
                        </tool>
                    </fileInfo>
...

除了数字,我假设是某种GUID,有三个区别:

Aside from the numbers, which I assume are some kind of GUIDs, there are three differences:


  1. 由于某种原因, fileInfo 元素中的程序变更包含文件的路径,包括项目名称: id =com.company.product.toolchain.configuration.changed.2057644715./test3/src/code。 cpp

  1. For some reason, the id attribute of the fileInfo element in the programmatic changes contains the path to the file, including the project name: id="com.company.product.toolchain.configuration.changed.2057644715./test3/src/code.cpp"

<$ c $的资源路径属性值 fileInfo 元素包含项目名称,仅在编程更改中: resourcePath =test3 / src / code.cpp

The resourcePath attribute value of the fileInfo element contains the project name, only in the programmatic changes: resourcePath="test3/src/code.cpp".

fileInfo 元素中的选项元素作为名称属性( name =其他标志)与对话框中的字段匹配,仅在手动更改中。

The option element in the fileInfo element as a name attribute (name="Other flags") that matches the field in the dialog, only in the manual changes.

我猜这些差异中的一个或全部阻止我的程序化变化是兼容的在手册中(所以它们显示在对话框中)。我试图找出如何创建IFileInfo,IOption(和ITool?)对象,而不会造成这些差异。我试图找到这些对象是如何创建的对话框,但还没有。

I'm guessing that one or all of these differences are preventing my programmatic changes from being "compatible" with the manual ones (so they show up in the dialog). I'm trying to figure out how to create the IFileInfo, IOption (and ITool?) objects without causing these differences. I'm trying to find how these objects are created for the dialog, but haven't yet.

任何建议赞赏。

更新:我获得了 Doug Schaefer的答案关于我的 cdt-dev邮件列表的问题/ A>。他建议我在某个地方坚持断点,看看用户界面如何。我写道:

Update: I got an answer from Doug Schaefer to my question on the cdt-dev mailing list. He suggested I "stick breakpoints in places and see how the UI does it." I wrote back:

我一直在这样做我在 org.eclipse.cdt.managedbuilder.core.ManagedBuildManager.setOption(IResourceInfo,IHoldsOptions,IOption,String)中设置一个断点,当我打开属性对话框一个文件,展开C / C ++ Build,选择设置,然后在工具设置选项卡中选择其他。我可以看到参数,大概如果我可以使用相同的参数调用setOption,我将在.cproject文件中获得相同的结果。但是我还没有弄清楚如何做到这一点。你有什么建议,我如何弄清楚这些对象的创建位置,以便我可以设置断点?

I have been doing that. I set a breakpoint in org.eclipse.cdt.managedbuilder.core.ManagedBuildManager.setOption(IResourceInfo, IHoldsOptions, IOption, String) and it triggers when I open the Properties dialog for a file, expand "C/C++ Build", select "Settings", and then "Miscellaneous" in the Tool Settings tab. I can see the arguments, and presumably if I can call setOption with the same arguments I'll get the same results in the .cproject file. But I haven't been able to figure out how to do that. Do you have any suggestions for how I figure out where those objects are created so I can set breakpoints there?

更新#2: Vladimir 的答案解决了我的问题。虽然我更关心上述两个.cproject文件片段中的差异3,但关键是差异2,项目名称的含义不同1。

Update #2: Vladimir's answer solved my problem. While I was more concerned with difference number 3 in the two .cproject file fragments above, the key was difference 2 and the inclusion of the project name in difference 1.

I更改代码以创建 IFileInfo 对象到:

I changed the code to create the IFileInfo object to:

IFileInfo fileInfo = conf.createFileInfo(file.getProjectRelativePath());

...导致fileInfo元素不具有项目名称:

...which resulted in the fileInfo element not having the project name:

<fileInfo id="com.company.product.toolchain.configuration.changed.838214286.src/code.cpp" name="code.cpp" rcbsApplicability="disable" resourcePath="src/code.cpp" toolsToInvoke="com.company.product.toolchain.compiler.2027651356.1970239371">

...最重要的是,导致在其他标志字段中显示的编程更改在各个文件的工具设置选项卡的杂项设置中。 (我猜想.cproject文件中的其他区别并不重要。)

...and most importantly, resulted in the programmatic changes showing up in the "Other flags" field in the Miscellaneous settings in the Tool Settings tab for the individual file. (I guess the other differences in the .cproject files are not important.)

推荐答案

我怀疑你可能有问题IFileInfo创建。这是我们用于获取IResourceInfo的代码,用于翻译单元设置每个文件选项:

I suspect there might be problem in you IFileInfo creation. Here's the code that we use to obtain IResourceInfo for a translation unit to set per-file options:

protected IResourceInfo getResourceInfo(ITranslationUnit translationUnit, ICProjectDescription prjDescription) {

    ICProject cProject = translationUnit.getCProject();
    if (cProject != null) {
        ICConfigurationDescription cfgDescription = prjDescription.getActiveConfiguration();
        IConfiguration configuration = ManagedBuildManager.getConfigurationForDescription(cfgDescription);
        IPath projectPath = translationUnit.getResource().getProjectRelativePath(); 
        IResourceInfo ri = configuration.getResourceInfo(projectPath, true);

        if (ri == null) { 
            ri = configuration.createFileInfo(projectPath);
        }

        return ri;
    }

    return null;
}

请注意这一行,特别是:

Note this line, in particular:

IPath projectPath = translationUnit.getResource().getProjectRelativePath();

也许,您需要的是在代码中使用getProjectRelativePath()?

Maybe, all you need is to use getProjectRelativePath() in your code?

这篇关于如何以编程方式更改文件的Eclipse CDT工具设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆