generated from ThalesGroup/template-project
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide full file path to clang-format
Clang format will find the parent .clang-format file with formatting options if any
- Loading branch information
Showing
4 changed files
with
135 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
org.eclipse.xsmp.ui/src/org/eclipse/xsmp/ui/formatting/EclipseClangFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
org.eclipse.xsmp/src/org/eclipse/xsmp/formatting2/ClangFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |