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

IEP-1277 Create .clang_format #1022

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -11,4 +11,5 @@ public interface ILSPConstants
{
String CLANGD_EXECUTABLE = "clangd"; //$NON-NLS-1$
String CLANGD_CONFIG_FILE = ".clangd"; //$NON-NLS-1$
String CLANG_FORMAT_FILE = ".clang-format"; //$NON-NLS-1$
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import com.espressif.idf.core.internal.CMakeConsoleWrapper;
import com.espressif.idf.core.internal.CMakeErrorParser;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.ClangFormatFileHandler;
import com.espressif.idf.core.util.ClangdConfigFileHandler;
import com.espressif.idf.core.util.DfuCommandsUtil;
import com.espressif.idf.core.util.HintsUtil;
Expand Down Expand Up @@ -339,6 +340,7 @@ public IProject[] build(int kind, Map<String, String> args, IConsole console, IP
}
runCmakeBuildCommand(console, monitor, project, start, generator, infoStream, buildDir);
new ClangdConfigFileHandler().update(project);
new ClangFormatFileHandler(project).update();
return new IProject[] { project };
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*******************************************************************************
* Copyright 2024 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.util;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import com.espressif.idf.core.ILSPConstants;

public class ClangFormatFileHandler {

private final IProject project;
private final Path clangFormatPath;

public ClangFormatFileHandler(IProject project) throws CoreException {
this.project = project;
this.clangFormatPath = project.getLocation().toPath().resolve(ILSPConstants.CLANG_FORMAT_FILE);
}

/**
* Updates the .clang-format file. If the file does not exist, it is created and initialized with default settings.
*
* @throws IOException if an I/O error occurs during file creation or writing
* @throws CoreException if an error occurs while refreshing the project
*/
public void update() throws IOException, CoreException {
boolean isNewFile = createNewClangFormatFile();

if (isNewFile) {
Map<String, Object> data = new LinkedHashMap<>();
data.put("BasedOnStyle", "LLVM"); //$NON-NLS-1$ //$NON-NLS-2$
data.put("IndentWidth", 4); //$NON-NLS-1$

writeYamlFile(data);
}
}

private DumperOptions createYamlOptions() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
return options;
}

private void writeYamlFile(Map<String, Object> data) throws IOException {
try (Writer writer = new FileWriter(clangFormatPath.toFile())) {
new Yaml(createYamlOptions()).dump(data, writer);
}
}

/**
* Ensures that the .clang-format file exists. If the file does not exist, it is created and the project is
* refreshed.
*
* @return true if the file was created, false if it already existed
* @throws IOException if an I/O error occurs during file creation
* @throws CoreException if an error occurs while refreshing the project
*/
private boolean createNewClangFormatFile() throws IOException, CoreException
{
if (Files.exists(clangFormatPath)) {
return false;
}

try {
Files.createFile(clangFormatPath);
project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
return true;
} catch (IOException e) {
throw new IOException("Failed to create .clang_format file: " + e.getMessage(), e); //$NON-NLS-1$
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ public void updateLspQueryDrivers()
metadata.queryDriver().defaultValue());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,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.ClangFormatFileHandler;
import com.espressif.idf.core.util.ClangdConfigFileHandler;
import com.espressif.idf.core.util.LaunchUtil;
import com.espressif.idf.ui.UIPlugin;
Expand Down Expand Up @@ -110,7 +111,7 @@ public boolean performFinish()
String projectName = projectCreationWizardPage.getProjectName();
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
selProvider.setSelection(new StructuredSelection(project));
updateClangdFile(project);
updateClangFiles(project);
}
}

Expand Down Expand Up @@ -141,11 +142,12 @@ public boolean performFinish()
return performFinish;
}

private void updateClangdFile(IProject project)
private void updateClangFiles(IProject project)
{
try
{
new ClangdConfigFileHandler().update(project);
new ClangFormatFileHandler(project).update();
}
catch (Exception e)
{
Expand Down
Loading