Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Language settings provider question

It stopped working as soon as I tried to remove ManagedCProjectNature.MNG_NATURE_ID.

-sergey


On Mon, Jul 15, 2013 at 1:29 PM, Andrew Gvozdev <angvoz.dev@xxxxxxxxx> wrote:
Source folders and exclusion filters are defined in core package org.eclipse.cdt.core.settings.model. I see that there is code to store them by CDefaultConfigurationData which is also part of core. I think there is a good chance that might work with non-managed build.

Andrew


On Mon, Jul 15, 2013 at 4:08 PM, Sergey Prigogin <eclipse.sprigogin@xxxxxxxxx> wrote:
I preserve the managed build nature to be able to control source folders and exclusion of source files from indexing. Indeed it would be nice to control all that without having to deal with managed build. Andrew, do you think this is possible?

-sergey


On Mon, Jul 15, 2013 at 12:34 PM, Andrew Gvozdev <angvoz.dev@xxxxxxxxx> wrote:
Hi Sergey,
If you do not use managed build for other reasons and do not use buildDefinitions extension point, there is no need to create managed project to set up language settings providers. manager.getProgectDescription() -  cfgDescription.setLanguageProviders() - manager.setProgectDescription() should be enough. 

Thanks,
Andrew


On Mon, Jul 15, 2013 at 2:32 PM, Sergey Prigogin <eclipse.sprigogin@xxxxxxxxx> wrote:
Here is the solution I ended up with.

I completely removed "org.eclipse.cdt.managedbuilder.core.buildDefinitions" extension and left only the following extension in plugin.xml:

  <extension
        point="org.eclipse.cdt.core.LanguageSettingsProvider">
     <provider
           class="org.eclipse.cdt.core.language.settings.providers.LanguageSettingsGenericProvider"
           id="myLanguageSettingsProvider"
           name="My C/C++ Settings"
           prefer-non-shared="true">
        <language-scope id="org.eclipse.cdt.core.gcc"/>
        <language-scope id="org.eclipse.cdt.core.g++"/>
     </provider>
  </extension>

Here are the main pieces of the Java code (the code is made available under EPL):

  private static final String MY_SETTING_PROVIDER_ID = "myLanguageSettingsProvider";

  public configureProject(IProject project, IProgressMonitor monitor) throws CoreException {
    CCorePlugin cPlugin = CCorePlugin.getDefault();
    cPlugin.createCDTProject(project.getDescription(), project, monitor);
    cPlugin.convertProjectFromCtoCC(project, monitor);

    // Configure the project description.
    ICProjectDescriptionManager manager = cPlugin.getProjectDescriptionManager();

    ICProjectDescription projectDescription =
        manager.getProjectDescription(project, ICProjectDescriptionManager.GET_WRITABLE);
    ICConfigurationDescription configDescription = null;
    if (projectDescription == null) {
      projectDescription = manager.createProjectDescription(project, false);
    } else {
      configDescription = projectDescription.getActiveConfiguration();
    }

    if (configDescription == null) {
      ICConfigurationDescription base =
          cPlugin.getPreferenceConfiguration(ManagedBuildManager.CFG_DATA_PROVIDER_ID);
      configDescription =
          projectDescription.createConfiguration("my_configuration_id", "Default", base);
    }

    // Disable CDT's managed build.
    IConfiguration configuration =
        ManagedBuildManager.getConfigurationForDescription(configDescription);
    configuration.setArtifactName(project.getName());
    configuration.setBuildCommand("");
    configuration.setBuildArguments("");
    configuration.setCleanCommand("");
    IBuilder builder = configuration.getEditableBuilder();
    builder.setCommand("");
    builder.setManagedBuildOn(false);
    builder.setAutoBuildEnable(false);
    builder.setCleanBuildEnable(false);
    builder.setFullBuildEnable(false);
    builder.setIncrementalBuildEnable(false);

    // Configure the language settings providers.
    LanguageSettingsSerializableProvider settingsProvider =
        findLanguageSettingsProvider(configDescription);
    if (settingsProvider == null) {
      ILanguageSettingsProvider userSettingsProvider =
          LanguageSettingsManager.getExtensionProviderCopy(USER_LANGUAGE_SETTINGS_PROVIDER_ID,
              true);
      settingsProvider = (LanguageSettingsSerializableProvider)
          LanguageSettingsManager.getExtensionProviderCopy(MY_SETTING_PROVIDER_ID, true);
      ((ILanguageSettingsProvidersKeeper) configDescription).setLanguageSettingProviders(
          ImmutableList.of(userSettingsProvider, settingsProvider));
    }

    projectDescription.setDefaultSettingConfiguration(configDescription);
    projectDescription.setActiveConfiguration(configDescription);
    CConfigurationData data = "">
    // Disable binary parsers.
    data.getTargetPlatformData().setBinaryParserIds(new String[0]);
    // Save the modified project description.
    manager.setProjectDescription(project, projectDescription);
    projectDescription.setReadOnly(true, true);  // Done with project description changes.

    configureLanguageSettings(settingsProvider, configDescription);

    // Remove scanner configuration discovery nature since scanner configuration discovery
    // is not used. ManagedCProjectNature.MNG_NATURE_ID nature is preserved since it
    // allows us to control which files are indexed and which files are not.
    removeNatures(project, ScannerConfigNature.NATURE_ID);
    // Remove CDT builder since the project is using a different build system.
    removeBuilders(project, ManagedCProjectNature.BUILDER_ID);
  }

  /**
   * Finds and returns the language settings provider with id MY_SETTING_PROVIDER_ID.
   */
  private static @Nullable LanguageSettingsSerializableProvider findLanguageSettingsProvider(
      ICConfigurationDescription configDescription) {
    List<ILanguageSettingsProvider> providers =
        ((ILanguageSettingsProvidersKeeper) configDescription).getLanguageSettingProviders();
    for (ILanguageSettingsProvider provider : providers) {
      if (provider.getId().equals(MY_SETTING_PROVIDER_ID)) {
        return (LanguageSettingsSerializableProvider) provider;
      }
    }
    return null;
  }

  /**
   * Populates settingsProvider with include directories and predefined macros.
   */
  private boolean configureLanguageSettings(LanguageSettingsSerializableProvider settingsProvider,
      ICConfigurationDescription configDescription) throws CoreException {
    ...
  }

  /**
   * Removes the natures defined by {@code naturesToRemove} from the {@link IProject}.
   */
  private static void removeNatures(IProject project, String... naturesToRemove)
      throws CoreException {
    IProjectDescription description = project.getDescription();
    String[] natureIds = description.getNatureIds();
    int k = 0;
    for (int i = 0; i < natureIds.length; i++) {
      String natureId = natureIds[i];
      if (indexOf(natureId, naturesToRemove) < 0) {
        natureIds[k++] = natureId;
      }
    }
    if (k < natureIds.length) {
      description.setNatureIds(Arrays.copyOf(natureIds, k));
      project.setDescription(description, null);
    }
  }

  /**
   * Removes the builders defined by {@code buildersToRemove} from the {@link IProject}.
   */
  private static void removeBuilders(IProject project, String... buildersToRemove)
      throws CoreException {
    IProjectDescription description = project.getDescription();
    ICommand[] commands = description.getBuildSpec();
    int k = 0;
    for (int i = 0; i < commands.length; i++) {
      ICommand command = commands[i];
      if (indexOf(command.getBuilderName(), buildersToRemove) < 0) {
        commands[k++] = command;
      }
    }
    if (k < commands.length) {
      description.setBuildSpec(Arrays.copyOf(commands, k));
      project.setDescription(description, IResource.FORCE, null);
    }
  }

  /**
   * Returns the index within {@code array} of the first occurrence of {@code obj},
   * or -1 if {@code obj} is not found in {@code array}.
   */
  private static <T, U extends T> int indexOf(U obj, T[] array) {
    for (int i = 0; i < array.length; i++) {
      if (obj == array[i] || (obj != null && obj.equals(array[i]) {
        return i;
      }
    }
    return -1;
  }

Thanks to Caroline Rieder who reminded me to post my project configuration solution.
-sergey

_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev



_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev



_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev



_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev



Back to the top