Skip to content

Commit

Permalink
provide full file path to clang-format
Browse files Browse the repository at this point in the history
Clang format will find the parent .clang-format file with formatting
options if any
  • Loading branch information
ydaveluy committed Apr 9, 2024
1 parent 180e947 commit fbf8f32
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -265,4 +267,8 @@ public Provider<IAllContainersState> provideIAllContainersState()
return XsmpEditor.class;
}

public Class< ? extends ClangFormatter> bindClangFormatter()
{
return EclipseClangFormatter.class;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit fbf8f32

Please sign in to comment.