Skip to content

Commit

Permalink
IEP-1277 Create .clang_format (#1022)
Browse files Browse the repository at this point in the history
* feat: creating clang-format file
  • Loading branch information
sigmaaa authored Aug 2, 2024
1 parent e97546f commit 0066004
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
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 @@ -84,6 +84,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 @@ -344,6 +345,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,9 @@
# We'll use defaults from the LLVM style, but with some modifications so that it's close to the CDT K&R style.
BasedOnStyle: LLVM
UseTab: Always
IndentWidth: 4
TabWidth: 4
PackConstructorInitializers: NextLineOnly
BreakConstructorInitializers: AfterColon
IndentAccessModifiers: false
AccessModifierOffset: -4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* 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.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;

import com.espressif.idf.core.ILSPConstants;

public class ClangFormatFileHandler
{
private final IFile clangFormatFile;

public ClangFormatFileHandler(IProject project) throws CoreException
{
this.clangFormatFile = project.getFile(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
{
if (clangFormatFile.exists())
{
return;
}
try (final var source = getClass().getResourceAsStream(".clang-format-project");) //$NON-NLS-1$
{
clangFormatFile.create(source, true, new NullProgressMonitor());
}
}
}
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

0 comments on commit 0066004

Please sign in to comment.