Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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,45 @@
/*******************************************************************************
* 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.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.launchbar.core.ILaunchDescriptor;

public class LaunchConfigFinder
{
private final ILaunchManager launchManager;

public LaunchConfigFinder()
{
launchManager = DebugPlugin.getDefault().getLaunchManager();
}

public LaunchConfigFinder(ILaunchManager launchManager)
{
this.launchManager = launchManager;
Comment thread
sigmaaa marked this conversation as resolved.
}

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 thread
kolipakakondal marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
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.LaunchConfigFinder;

public class IDFCoreLaunchConfigProvider extends CoreBuildGenericLaunchConfigProvider
{

Expand All @@ -22,25 +25,19 @@ 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 LaunchConfigFinder().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 +102,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.LaunchConfigFinder;
import com.espressif.idf.ui.UIPlugin;
import com.espressif.idf.ui.handlers.EclipseHandler;
import com.espressif.idf.ui.handlers.NewProjectHandlerUtil;
Expand Down Expand Up @@ -114,25 +113,23 @@ 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 LaunchConfigFinder().findAppropriateLaunchConfig(desc,
IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE) == null)
{
ILaunchDescriptor desc = launchBarManager.getActiveLaunchDescriptor();
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.LaunchConfigFinder;

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

@Mock
private ILaunchConfigurationType launchConfigType;

private LaunchConfigFinder 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 LaunchConfigFinder(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);
}
}