Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright 2024-2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.util;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.launchbar.core.ILaunchDescriptor;

public class LaunchUtil
{
private final ILaunchManager launchManager;

public LaunchUtil(ILaunchManager launchManager)
{
this.launchManager = launchManager;
}

public ILaunchConfiguration findAppropriateLaunchConfig(ILaunchDescriptor descriptor, String configIndentifier)
throws CoreException
{
IProject project = descriptor.getAdapter(IProject.class);
for (ILaunchConfiguration config : launchManager.getLaunchConfigurations())
{
IResource[] mappedResource = config.getMappedResources();
if (mappedResource != null && mappedResource.length > 0 && mappedResource[0].getProject().equals(project)
&& config.getType().getIdentifier().contentEquals(configIndentifier))
{
return config;
}
}
return null;
Comment on lines +23 to +36
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method findAppropriateLaunchConfig is well-implemented and follows good practices in searching for the appropriate launch configuration. However, consider adding a null check for descriptor at the beginning of the method to prevent potential NullPointerExceptions if a null descriptor is passed. This would enhance the robustness of the method.

public ILaunchConfiguration findAppropriateLaunchConfig(ILaunchDescriptor descriptor, String configIndentifier)
        throws CoreException
{
+   if (descriptor == null) {
+       throw new IllegalArgumentException("Descriptor cannot be null");
+   }
    IProject project = descriptor.getAdapter(IProject.class);
    for (ILaunchConfiguration config : launchManager.getLaunchConfigurations())
    {
        IResource[] mappedResource = config.getMappedResources();
        if (mappedResource != null && mappedResource.length > 0 && mappedResource[0].getProject().equals(project)
                && config.getType().getIdentifier().contentEquals(configIndentifier))
        {
            return config;
        }
    }
    return null;
}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public ILaunchConfiguration findAppropriateLaunchConfig(ILaunchDescriptor descriptor, String configIndentifier)
throws CoreException
{
IProject project = descriptor.getAdapter(IProject.class);
for (ILaunchConfiguration config : launchManager.getLaunchConfigurations())
{
IResource[] mappedResource = config.getMappedResources();
if (mappedResource != null && mappedResource.length > 0 && mappedResource[0].getProject().equals(project)
&& config.getType().getIdentifier().contentEquals(configIndentifier))
{
return config;
}
}
return null;
public ILaunchConfiguration findAppropriateLaunchConfig(ILaunchDescriptor descriptor, String configIndentifier)
throws CoreException
{
if (descriptor == null) {
throw new IllegalArgumentException("Descriptor cannot be null");
}
IProject project = descriptor.getAdapter(IProject.class);
for (ILaunchConfiguration config : launchManager.getLaunchConfigurations())
{
IResource[] mappedResource = config.getMappedResources();
if (mappedResource != null && mappedResource.length > 0 && mappedResource[0].getProject().equals(project)
&& config.getType().getIdentifier().contentEquals(configIndentifier))
{
return config;
}
}
return null;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.launchbar.core.ILaunchDescriptor;
import org.eclipse.launchbar.core.target.ILaunchTarget;

import com.espressif.idf.core.build.IDFLaunchConstants;
import com.espressif.idf.core.util.LaunchUtil;

public class IDFCoreLaunchConfigProvider extends CoreBuildGenericLaunchConfigProvider
{

Expand All @@ -22,25 +26,17 @@ public class IDFCoreLaunchConfigProvider extends CoreBuildGenericLaunchConfigPro
public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target)
throws CoreException
{

ILaunchConfiguration configuration = null;
IProject project = descriptor.getAdapter(IProject.class);
if (project != null)
{

String targetConfig = descriptor.getName();
configuration = configs.computeIfAbsent(project, key -> new HashMap<>()).get(targetConfig);
if (configuration == null)
{
// do we already have one with the descriptor?
configuration = descriptor.getAdapter(ILaunchConfiguration.class);
if (configuration == null)
{
configuration = createLaunchConfiguration(descriptor, target);
}
configs.get(project).put(configuration.getName(), configuration);
}
}
if (project == null)
return null;

String targetConfig = descriptor.getName();
Map<String, ILaunchConfiguration> projectConfigs = configs.computeIfAbsent(project, key -> new HashMap<>());
ILaunchConfiguration configuration = projectConfigs.get(targetConfig);
configuration = configuration == null ? new LaunchUtil(DebugPlugin.getDefault().getLaunchManager())
.findAppropriateLaunchConfig(descriptor, IDFLaunchConstants.RUN_LAUNCH_CONFIG_TYPE) : configuration;
configuration = configuration == null ? createLaunchConfiguration(descriptor, target) : configuration;
projectConfigs.put(configuration.getName(), configuration);
return configuration;
}

Expand Down Expand Up @@ -105,5 +101,4 @@ public void launchTargetRemoved(ILaunchTarget target) throws CoreException
{
// Nothing to do
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.eclipse.launchbar.ui.NewLaunchConfigWizard;
import org.eclipse.launchbar.ui.NewLaunchConfigWizardDialog;
import org.eclipse.launchbar.ui.internal.dialogs.NewLaunchConfigEditPage;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tools.templates.core.IGenerator;
Expand All @@ -41,6 +39,7 @@
import com.espressif.idf.core.IDFConstants;
import com.espressif.idf.core.build.IDFLaunchConstants;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.LaunchUtil;
import com.espressif.idf.lsp.ClangdConfigFileHandler;
import com.espressif.idf.ui.UIPlugin;
import com.espressif.idf.ui.handlers.EclipseHandler;
Expand Down Expand Up @@ -116,27 +115,28 @@ public boolean performFinish()
}

final String target = projectCreationWizardPage.getSelectedTarget();
this.getShell().addDisposeListener(new DisposeListener()
{
@Override
public void widgetDisposed(DisposeEvent event)
this.getShell().addDisposeListener(event -> {
ILaunchBarManager launchBarManager = UIPlugin.getService(ILaunchBarManager.class);
TargetSwitchJob targetSwtichJob = new TargetSwitchJob(target);
targetSwtichJob.schedule();
try
{
ILaunchBarManager launchBarManager = UIPlugin.getService(ILaunchBarManager.class);
TargetSwitchJob targetSwtichJob = new TargetSwitchJob(target);
targetSwtichJob.schedule();
try
ILaunchDescriptor desc = launchBarManager.getActiveLaunchDescriptor();
if (new LaunchUtil(DebugPlugin.getDefault().getLaunchManager()).findAppropriateLaunchConfig(desc,
IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE) == null)
{

ILaunchDescriptor desc = launchBarManager.getActiveLaunchDescriptor();
// this ensures that the configuration exists
launchBarManager.getActiveLaunchConfiguration();

createDefaultDebugConfig();
launchBarManager.setActiveLaunchDescriptor(desc);
}
catch (CoreException e)
{
Logger.log(e);
}

}
catch (CoreException e)
{
Logger.log(e);
}
});
return performFinish;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*******************************************************************************
* Copyright 2024-2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.util.test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.launchbar.core.ILaunchDescriptor;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

import com.espressif.idf.core.build.IDFLaunchConstants;
import com.espressif.idf.core.util.LaunchUtil;

public class LaunchConfigFinderTest
{
@Mock
private ILaunchManager launchManager;
@Mock
private ILaunchConfiguration launchConfiguration;
@Mock
private IProject project;
@Mock
private ILaunchDescriptor launchDescriptor;

@Mock
private ILaunchConfigurationType launchConfigType;

private LaunchUtil launchConfigFinder;

@Before
public void setUp()
{
this.launchManager = Mockito.mock(ILaunchManager.class);
this.launchConfiguration = Mockito.mock(ILaunchConfiguration.class);
this.launchConfigType = Mockito.mock(ILaunchConfigurationType.class);
this.launchDescriptor = Mockito.mock(ILaunchDescriptor.class);
this.project = Mockito.mock(IProject.class);
launchConfigFinder = new LaunchUtil(launchManager);
}

@Test
public void testFindAppropriateDebugConfig() throws CoreException
{
when(project.getProject()).thenReturn(project);
when(launchManager.getLaunchConfigurations()).thenReturn(new ILaunchConfiguration[] { launchConfiguration });
when(launchConfiguration.getMappedResources()).thenReturn(new IProject[] { project });
when(launchConfiguration.getType()).thenReturn(launchConfigType);
when(launchConfiguration.getType().getIdentifier()).thenReturn(IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE);
when(launchDescriptor.getAdapter(IProject.class)).thenReturn(project);

ILaunchConfiguration result = launchConfigFinder.findAppropriateLaunchConfig(launchDescriptor,
IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE);

assertEquals(launchConfiguration, result);
}

@Test
public void testFindExistingLaunchConfiguration() throws CoreException
{
when(project.getProject()).thenReturn(project);
when(launchManager.getLaunchConfigurations()).thenReturn(new ILaunchConfiguration[] { launchConfiguration });
when(launchConfiguration.getMappedResources()).thenReturn(new IProject[] { project });
when(launchConfiguration.getType()).thenReturn(launchConfigType);
when(launchConfiguration.getType().getIdentifier()).thenReturn(IDFLaunchConstants.RUN_LAUNCH_CONFIG_TYPE);
when(launchDescriptor.getAdapter(IProject.class)).thenReturn(project);
when(launchConfiguration.getName()).thenReturn("name");
when(launchDescriptor.getName()).thenReturn("name");

ILaunchConfiguration result = launchConfigFinder.findAppropriateLaunchConfig(launchDescriptor,
IDFLaunchConstants.RUN_LAUNCH_CONFIG_TYPE);

assertEquals(launchConfiguration, result);
}

@Test
public void testFindAppropriateDebugConfig_NoMappedResources() throws CoreException
{
when(project.getProject()).thenReturn(project);
when(launchManager.getLaunchConfigurations()).thenReturn(new ILaunchConfiguration[] { launchConfiguration });
when(launchConfiguration.getMappedResources()).thenReturn(null);
when(launchDescriptor.getAdapter(IProject.class)).thenReturn(project);

ILaunchConfiguration result = launchConfigFinder.findAppropriateLaunchConfig(launchDescriptor,
IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE);

assertEquals(null, result);
}

@Test
public void testFindAppropriateDebugConfig_MappedResourcesAreEmpty() throws CoreException
{
when(project.getProject()).thenReturn(project);
when(launchManager.getLaunchConfigurations()).thenReturn(new ILaunchConfiguration[] { launchConfiguration });
when(launchConfiguration.getMappedResources()).thenReturn(new IProject[] {});
when(launchDescriptor.getAdapter(IProject.class)).thenReturn(project);

ILaunchConfiguration result = launchConfigFinder.findAppropriateLaunchConfig(launchDescriptor,
IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE);

assertEquals(null, result);
}

@Test
public void testFindAppropriateDebugConfig_NoMatchingType() throws CoreException
{
when(project.getProject()).thenReturn(project);
when(launchManager.getLaunchConfigurations()).thenReturn(new ILaunchConfiguration[] { launchConfiguration });
when(launchConfiguration.getMappedResources()).thenReturn(new IProject[] { project });
when(launchConfiguration.getType()).thenReturn(launchConfigType);
when(launchConfigType.getIdentifier()).thenReturn("notDebugType");
when(launchDescriptor.getAdapter(IProject.class)).thenReturn(project);

ILaunchConfiguration result = launchConfigFinder.findAppropriateLaunchConfig(launchDescriptor,
IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE);

assertEquals(null, result);
}

@Test
public void testFindExistingLaunchConfiguration_NoMatchingName() throws CoreException
{
when(project.getProject()).thenReturn(project);
when(launchManager.getLaunchConfigurations()).thenReturn(new ILaunchConfiguration[] { launchConfiguration });
when(launchConfiguration.getMappedResources()).thenReturn(new IProject[] { project });
when(launchConfiguration.getName()).thenReturn("anotherName");
when(launchDescriptor.getName()).thenReturn("name");
when(launchConfiguration.getType()).thenReturn(launchConfigType);
when(launchConfiguration.getType().getIdentifier()).thenReturn(IDFLaunchConstants.RUN_LAUNCH_CONFIG_TYPE);

ILaunchConfiguration result = launchConfigFinder.findAppropriateLaunchConfig(launchDescriptor,
IDFLaunchConstants.RUN_LAUNCH_CONFIG_TYPE);

assertEquals(null, result);
}
}