From fbf8f32944ddbd5376845b34f2ba8439c0d36726 Mon Sep 17 00:00:00 2001 From: Yannick Daveluy Date: Tue, 9 Apr 2024 23:20:39 +0200 Subject: [PATCH] provide full file path to clang-format Clang format will find the parent .clang-format file with formatting options if any --- .../xsmp/generator/cpp/CppGenerator.java | 61 ++----------- .../org/eclipse/xsmp/ui/XsmpcatUiModule.java | 6 ++ .../ui/formatting/EclipseClangFormatter.java | 39 +++++++++ .../xsmp/formatting2/ClangFormatter.java | 85 +++++++++++++++++++ 4 files changed, 135 insertions(+), 56 deletions(-) create mode 100644 org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/formatting/EclipseClangFormatter.java create mode 100644 org.eclipse.xsmp/src/org/eclipse/xsmp/formatting2/ClangFormatter.java diff --git a/org.eclipse.xsmp.generator.cpp/src/org/eclipse/xsmp/generator/cpp/CppGenerator.java b/org.eclipse.xsmp.generator.cpp/src/org/eclipse/xsmp/generator/cpp/CppGenerator.java index a5153c57..08a6a798 100644 --- a/org.eclipse.xsmp.generator.cpp/src/org/eclipse/xsmp/generator/cpp/CppGenerator.java +++ b/org.eclipse.xsmp.generator.cpp/src/org/eclipse/xsmp/generator/cpp/CppGenerator.java @@ -10,15 +10,9 @@ ******************************************************************************/ package org.eclipse.xsmp.generator.cpp; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.nio.charset.StandardCharsets; - import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.xsmp.formatting2.ClangFormatter; import org.eclipse.xsmp.generator.cpp.type.ArrayGenerator; import org.eclipse.xsmp.generator.cpp.type.ClassGenerator; import org.eclipse.xsmp.generator.cpp.type.ComponentGenerator; @@ -203,61 +197,16 @@ protected AbstractFileGenerator getGenerator(Type t) }; } - /** - * Format the content with the CDT formatter - * - * @param fileName - * the file name of the generated file - * @param content - * content to format - * @return formatted content - */ - protected CharSequence format(String fileName, CharSequence content) - { - try - { - final var pb = new ProcessBuilder("clang-format", "-style=LLVM", - "-assume-filename=" + fileName); - - final var process = pb.start(); - - final var writer = new PrintWriter( - new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8), true); - writer.append(content).close(); - - final var reader = new BufferedReader( - new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)); - final var result = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) - { - result.append(line).append(System.lineSeparator()); - } - reader.close(); - - final var exitCode = process.waitFor(); - if (exitCode == 0) - { - return result; - } - } - catch (final IOException e) - { - // ignore - } - catch (final InterruptedException e) - { - Thread.currentThread().interrupt(); - } - return content; - } + @Inject + private ClangFormatter formatter; protected void generateFile(IFileSystemAccess2 fsa, String fileName, String outputConfigurationName, CharSequence contents) { if (contents != null) { - fsa.generateFile(fileName, outputConfigurationName, format(fileName, contents)); + fsa.generateFile(fileName, outputConfigurationName, + formatter.format(fsa.getURI(fileName, outputConfigurationName), contents)); } } diff --git a/org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/XsmpcatUiModule.java b/org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/XsmpcatUiModule.java index a7f468ad..eed68eed 100644 --- a/org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/XsmpcatUiModule.java +++ b/org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/XsmpcatUiModule.java @@ -13,6 +13,7 @@ import org.eclipse.jface.text.templates.persistence.TemplateStore; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.eclipse.xsmp.extension.IExtensionManager; +import org.eclipse.xsmp.formatting2.ClangFormatter; import org.eclipse.xsmp.ide.contentassist.IReferenceFilter; import org.eclipse.xsmp.ide.contentassist.XsmpcatReferenceFilter; import org.eclipse.xsmp.ide.hover.IKeywordHovers; @@ -30,6 +31,7 @@ import org.eclipse.xsmp.ui.editor.model.XsmpTerminalsTokenTypeToPartitionMapper; import org.eclipse.xsmp.ui.extension.ExtensionManager; import org.eclipse.xsmp.ui.folding.XsmpFoldingRegionProvider; +import org.eclipse.xsmp.ui.formatting.EclipseClangFormatter; import org.eclipse.xsmp.ui.generator.XsmpGeneratorDelegate; import org.eclipse.xsmp.ui.highlighting.XsmpAntlrTokenToAttributeIdMapper; import org.eclipse.xsmp.ui.highlighting.XsmpHighlightingConfiguration; @@ -265,4 +267,8 @@ public Provider provideIAllContainersState() return XsmpEditor.class; } + public Class< ? extends ClangFormatter> bindClangFormatter() + { + return EclipseClangFormatter.class; + } } diff --git a/org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/formatting/EclipseClangFormatter.java b/org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/formatting/EclipseClangFormatter.java new file mode 100644 index 00000000..980e4748 --- /dev/null +++ b/org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/formatting/EclipseClangFormatter.java @@ -0,0 +1,39 @@ +/******************************************************************************* +* Copyright (C) 2024 THALES ALENIA SPACE FRANCE. +* +* All rights reserved. This program and the accompanying materials +* are made available under the terms of the Eclipse Public License 2.0 +* which accompanies this distribution, and is available at +* https://www.eclipse.org/legal/epl-2.0/ +* +* SPDX-License-Identifier: EPL-2.0 +******************************************************************************/ +package org.eclipse.xsmp.ui.formatting; + +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.runtime.Path; +import org.eclipse.emf.common.util.URI; +import org.eclipse.xsmp.formatting2.ClangFormatter; + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +@Singleton +public class EclipseClangFormatter extends ClangFormatter +{ + @Inject + private IWorkspaceRoot root; + + @Override + public String toFileString(URI uri) + { + if (uri.isPlatform()) + { + return root.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true))) + .getLocation().toString(); + } + + return super.toFileString(uri); + } + +} diff --git a/org.eclipse.xsmp/src/org/eclipse/xsmp/formatting2/ClangFormatter.java b/org.eclipse.xsmp/src/org/eclipse/xsmp/formatting2/ClangFormatter.java new file mode 100644 index 00000000..d781a366 --- /dev/null +++ b/org.eclipse.xsmp/src/org/eclipse/xsmp/formatting2/ClangFormatter.java @@ -0,0 +1,85 @@ +/******************************************************************************* +* Copyright (C) 2024 THALES ALENIA SPACE FRANCE. +* +* All rights reserved. This program and the accompanying materials +* are made available under the terms of the Eclipse Public License 2.0 +* which accompanies this distribution, and is available at +* https://www.eclipse.org/legal/epl-2.0/ +* +* SPDX-License-Identifier: EPL-2.0 +******************************************************************************/ +package org.eclipse.xsmp.formatting2; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; + +import org.eclipse.emf.common.util.URI; + +import com.google.inject.Singleton; + +@Singleton +public class ClangFormatter +{ + + protected String toFileString(URI uri) + { + if (uri.isFile()) + { + return uri.toFileString(); + } + + return uri.lastSegment(); + } + + /** + * Format the content with clang-format + * + * @param uri + * the file URI of the generated file + * @param content + * content to format + * @return formatted content + */ + public CharSequence format(URI uri, CharSequence content) + { + try + { + final var pb = new ProcessBuilder("clang-format", "-assume-filename=" + toFileString(uri)); + + final var process = pb.start(); + + final var writer = new PrintWriter( + new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8), true); + writer.append(content).close(); + + final var reader = new BufferedReader( + new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)); + final var result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) + { + result.append(line).append(System.lineSeparator()); + } + reader.close(); + + final var exitCode = process.waitFor(); + if (exitCode == 0) + { + return result; + } + } + catch (final IOException e) + { + // ignore + } + catch (final InterruptedException e) + { + Thread.currentThread().interrupt(); + } + return content; + } +}