Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Bundle CDT-LSP plugins with Espressif-IDE #877

Merged
merged 18 commits into from
Mar 27, 2024
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
5 changes: 4 additions & 1 deletion bundles/com.espressif.idf.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ Export-Package: com.espressif.idf.core,
com.espressif.idf.core.util,
com.espressif.idf.core.variable,
org.json.simple,
org.json.simple.parser
org.json.simple.parser,
org.yaml.snakeyaml,
org.yaml.snakeyaml.parser,
org.yaml.snakeyaml.util
Bundle-ClassPath: .,
lib/json-simple-1.1.1.jar,
lib/commons-collections4-4.4.jar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
{
Logger.log("Initializing toolchain..."); //$NON-NLS-1$
List<String> paths = new ArrayList<>();
String idfToolsExportPath = StringUtil.isEmpty(envValue) ? getIdfToolsExportPath() : envValue;

Check warning on line 124 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/toolchain/ESPToolChainManager.java

View workflow job for this annotation

GitHub Actions / spotbugs

UWF_UNWRITTEN_FIELD

Unwritten field: com.espressif.idf.core.toolchain.ESPToolChainManager.envValue
Raw output
This field is never written.  All reads of it will return the default value. Check for errors (should it have been initialized?), or remove it if it is useless.
if (idfToolsExportPath != null)
{
paths.add(idfToolsExportPath);
Expand Down Expand Up @@ -151,6 +151,14 @@
.map(espToolChainElement -> findToolChain(getAllPaths(), espToolChainElement.debuggerPattern))
.findFirst().orElse(null);
}

public File findCompiler(String target)
{
return toolchainElements.values().stream()
.filter(espToolChainElement -> espToolChainElement.name.equals(target))
.map(espToolChainElement -> findToolChain(getAllPaths(), espToolChainElement.compilerPattern))
.findFirst().orElse(null);
}

public File findToolChain(List<String> paths, String filePattern)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.launchbar.core.ILaunchBarManager;
import org.eclipse.launchbar.core.target.ILaunchTarget;
import org.osgi.service.prefs.BackingStoreException;

import com.espressif.idf.core.IDFConstants;
import com.espressif.idf.core.IDFCorePlugin;
import com.espressif.idf.core.IDFEnvironmentVariables;
import com.espressif.idf.core.ProcessBuilderFactory;
import com.espressif.idf.core.SystemExecutableFinder;
import com.espressif.idf.core.build.IDFLaunchConstants;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.toolchain.ESPToolChainManager;

Expand Down Expand Up @@ -337,6 +339,31 @@
}
return getXtensaToolchainExecutablePathByTarget(projectEspTarget);
}

public static String getXtensaToolchainExePathForActiveTarget()
{
ILaunchBarManager launchBarManager = IDFCorePlugin.getService(ILaunchBarManager.class);
try
{
ILaunchTarget launchTarget = launchBarManager.getActiveLaunchTarget();
if (launchTarget != null)
{
File file = new ESPToolChainManager()
.findCompiler(launchTarget.getAttribute(IDFLaunchConstants.ATTR_IDF_TARGET, StringUtil.EMPTY));
if (file != null)
{
return file.getAbsolutePath();
}

}
}
catch (CoreException e)
{
Logger.log(e);
}

return null;
}

public static String getXtensaToolchainExecutablePathByTarget(String projectEspTarget)
{
Expand Down Expand Up @@ -372,7 +399,7 @@
File dir = new File(dirStr);
if (dir.isDirectory())
{
for (File file : dir.listFiles())

Check warning on line 402 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IDFUtil.java

View workflow job for this annotation

GitHub Actions / spotbugs

NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE

Possible null pointer dereference in com.espressif.idf.core.util.IDFUtil.getXtensaToolchainExecutableAddr2LinePath(IProject) due to return value of called method
Raw output
The return value from a method is dereferenced without a null check, and the return value of that method is one that should generally be checked for null. This may lead to a NullPointerException when the code is executed.
{
if (file.isDirectory())
{
Expand Down
7 changes: 7 additions & 0 deletions bundles/com.espressif.idf.lsp/.classpath
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to change a lot of our classpath files to have Java 17 you can see the warnings when building.
We can have a separate ticket for this

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 2 additions & 0 deletions bundles/com.espressif.idf.lsp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target/
/bin/
33 changes: 33 additions & 0 deletions bundles/com.espressif.idf.lsp/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.espressif.idf.lsp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
23 changes: 23 additions & 0 deletions bundles/com.espressif.idf.lsp/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: com.espressif.idf.lsp;singleton:=true
Bundle-Vendor: ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD
Bundle-Version: 1.0.1.qualifier
Export-Package: com.espressif.idf.lsp
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.cdt.lsp.clangd,
org.eclipse.core.resources,
org.eclipse.cdt.lsp,
com.espressif.idf.core,
org.eclipse.cdt.core,
org.apache.batik.i18n;bundle-version="1.16.0",
org.apache.batik.css;bundle-version="1.16.0",
org.apache.batik.util;bundle-version="1.16.0",
org.apache.batik.constants;bundle-version="1.16.0"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: com.espressif.idf.lsp
Bundle-ActivationPolicy: lazy
Bundle-Name: ESP-IDF LSP Plugin
Service-Component: OSGI-INF/com.espressif.idf.lsp.preferences.IDFClangdEnable.xml,
OSGI-INF/com.espressif.idf.lsp.preferences.IDFClangdOptionsDefaults.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="com.espressif.idf.lsp.preferences.IDFClangdEnable">
<property name="service.ranking" type="Integer" value="100"/>
<service>
<provide interface="org.eclipse.cdt.lsp.editor.LanguageServerEnable"/>
</service>
<implementation class="com.espressif.idf.lsp.preferences.IDFClangdEnable"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="com.espressif.idf.lsp.preferences.IDFClangdOptionsDefaults">
<property name="service.ranking" type="Integer" value="100"/>
<service>
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdOptionsDefaults"/>
</service>
<implementation class="com.espressif.idf.lsp.preferences.IDFClangdOptionsDefaults"/>
</scr:component>
19 changes: 19 additions & 0 deletions bundles/com.espressif.idf.lsp/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
###############################################################################
# Copyright (c) 2023 Contributors to the Eclipse Foundation.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/.
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# See git history
###############################################################################

bin.includes = META-INF/,\
.,\
OSGI-INF/,\
plugin.xml
source.. = src/
output.. = bin/
5 changes: 5 additions & 0 deletions bundles/com.espressif.idf.lsp/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

</plugin>
15 changes: 15 additions & 0 deletions bundles/com.espressif.idf.lsp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>com.espressif.idf.lsp</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

<parent>
<groupId>com.espressif.idf</groupId>
<artifactId>com.espressif.idf.bundles</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*******************************************************************************
* Copyright 2024 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.lsp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.yaml.snakeyaml.Yaml;

import com.espressif.idf.core.logging.Logger;

/**
* @author Kondal Kolipaka <[email protected]>
*/
public class ClangdConfigFileHandler
{
@SuppressWarnings("unchecked")
public void update(IProject project) throws FileNotFoundException
{
File file = getClangdConfigFile(project);

// Load existing clangd file
FileInputStream inputStream = new FileInputStream(file);

Check warning on line 32 in bundles/com.espressif.idf.lsp/src/com/espressif/idf/lsp/ClangdConfigFileHandler.java

View workflow job for this annotation

GitHub Actions / spotbugs

OBL_UNSATISFIED_OBLIGATION

com.espressif.idf.lsp.ClangdConfigFileHandler.update(IProject) may fail to clean up java.io.InputStream
Raw output
This method may fail to clean up (close, dispose of) a stream, database object, or other resource requiring an explicit cleanup operation.

In general, if a method opens a stream or other resource, the method should use a try/finally block to ensure that the stream or resource is cleaned up before the method returns.

This bug pattern is essentially the same as the OS_OPEN_STREAM and ODR_OPEN_DATABASE_RESOURCE bug patterns, but is based on a different (and hopefully better) static analysis technique. We are interested is getting feedback about the usefulness of this bug pattern. For sending feedback, check:

 * contributing guideline [https://github.com/spotbugs/spotbugs/blob/master/.github/CONTRIBUTING.md]
 * mailinglist [https://github.com/spotbugs/discuss/issues?q=]

In particular, the false-positive suppression heuristics for this bug pattern have not been extensively tuned, so reports about false positives are helpful to us.

See Weimer and Necula, Finding and Preventing Run-Time Error Handling Mistakes (PDF [https://people.eecs.berkeley.edu/~necula/Papers/rte_oopsla04.pdf]), for a description of the analysis technique.
Yaml yaml = new Yaml();
Object obj = yaml.load(inputStream);
if (obj instanceof Map)
{
Map<String, Object> data = (Map<String, Object>) obj;

// Add new attribute to CompileFlags
Map<String, Object> compileFlags = (Map<String, Object>) data.get("CompileFlags"); //$NON-NLS-1$
compileFlags.put("Remove", new String[] { "-m*", "-f*" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

// Write updated clangd back to file
try (Writer writer = new FileWriter(file))

Check warning on line 44 in bundles/com.espressif.idf.lsp/src/com/espressif/idf/lsp/ClangdConfigFileHandler.java

View workflow job for this annotation

GitHub Actions / spotbugs

DM_DEFAULT_ENCODING

Found reliance on default encoding in com.espressif.idf.lsp.ClangdConfigFileHandler.update(IProject): new java.io.FileWriter(File)
Raw output
Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behavior to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.
{
yaml.dump(data, writer);
}
catch (IOException e)
{
Logger.log("Error writing .clangd file: " + e.getMessage()); //$NON-NLS-1$
}
}
else
{
Logger.log("Invalid .clangd file format."); //$NON-NLS-1$
}
}

private File getClangdConfigFile(IProject project)
{
// Path to the existing clangd file
IFile file = project.getFile(ILSPConstants.CLANGD_CONFIG_FILE);
return file.getLocation().toFile();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*******************************************************************************
* Copyright 2024 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.lsp;

/**
* @author Kondal Kolipaka <[email protected]>
*/
public interface ILSPConstants
{
String CLANGD_EXECUTABLE = "clangd"; //$NON-NLS-1$
String CLANGD_CONFIG_FILE = ".clangd"; // $NON-NLS-1$
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright 2024 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.lsp.preferences;

import org.eclipse.cdt.lsp.editor.LanguageServerEnable;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.osgi.service.component.annotations.Component;

import com.espressif.idf.core.IDFProjectNature;
import com.espressif.idf.core.logging.Logger;

/**
* @author Kondal Kolipaka <[email protected]>
*/
@Component(property = { "service.ranking:Integer=100" })
public class IDFClangdEnable implements LanguageServerEnable
{

@Override
public boolean isEnabledFor(IProject project)
{
if (project != null)
{
try
{
return project.hasNature(IDFProjectNature.ID); // IDF nature
}
catch (CoreException e)
{
Logger.log(e);
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright 2024 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.lsp.preferences;

import java.util.Optional;

import org.eclipse.cdt.lsp.clangd.BuiltinClangdOptionsDefaults;
import org.eclipse.cdt.lsp.clangd.ClangdOptionsDefaults;
import org.osgi.service.component.annotations.Component;

import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.IDFUtil;
import com.espressif.idf.lsp.ILSPConstants;

/**
* @author Kondal Kolipaka <[email protected]>
*
*/
@SuppressWarnings("restriction")
@Component(service = ClangdOptionsDefaults.class, property = { "service.ranking:Integer=100" })
public class IDFClangdOptionsDefaults extends BuiltinClangdOptionsDefaults
{

@Override
public String clangdPath()
{
String clandPath = IDFUtil.findCommandFromBuildEnvPath(ILSPConstants.CLANGD_EXECUTABLE);
Logger.log("clangd: " + clandPath); //$NON-NLS-1$
return Optional.ofNullable(clandPath).orElse(ILSPConstants.CLANGD_EXECUTABLE);
}

}
5 changes: 4 additions & 1 deletion bundles/com.espressif.idf.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.tools.templates.core,
org.eclipse.tools.templates.ui,
org.freemarker.freemarker,
org.eclipse.tools.templates.freemarker
org.eclipse.tools.templates.freemarker,
org.eclipse.cdt.lsp;bundle-version="1.0.0",
org.eclipse.cdt.lsp.clangd;bundle-version="1.0.0",
com.espressif.idf.lsp
Automatic-Module-Name: com.espressif.idf.ui
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-11
Expand Down
Loading
Loading