Skip to content

Commit

Permalink
Fixed #138 parser for windows end of line.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Jan 11, 2024
1 parent fc4636d commit 519c049
Show file tree
Hide file tree
Showing 1,589 changed files with 11,672 additions and 168 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* Copyright (c) 2023, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -70,6 +70,11 @@ public class NewModuleWizard extends Wizard implements INewWizard {
*/
private static final String OK_MESSAGE = "Acceleo 4 module %s created.";

/**
* The new line {@link String}.
*/
private static final String NEW_LINE = System.lineSeparator();

/**
* The {@link Job} writing the {@link Module} file.
*
Expand Down Expand Up @@ -102,17 +107,17 @@ protected IStatus run(IProgressMonitor monitor) {
moduleDocumentation.setDocumentedElement(module);
final CommentBody moduleDocumentationBody = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createCommentBody();
moduleDocumentationBody.setValue("\n * The documentation of the module " + module.getName()
+ ".\n");
moduleDocumentationBody.setValue(NEW_LINE + " * The documentation of the module " + module
.getName() + "." + NEW_LINE);
final String username = System.getProperty("user.name");
if (username != null && !"".equals(username)) {
moduleDocumentationBody.setValue(moduleDocumentationBody.getValue() + " * "
+ AcceleoParser.AUTHOR_TAG + username + "\n");
+ AcceleoParser.AUTHOR_TAG + username + NEW_LINE);
}
moduleDocumentationBody.setValue(moduleDocumentationBody.getValue() + " * "
+ AcceleoParser.VERSION_TAG + "1.0.0\n");
+ AcceleoParser.VERSION_TAG + "1.0.0" + NEW_LINE);
moduleDocumentationBody.setValue(moduleDocumentationBody.getValue() + " * "
+ AcceleoParser.SINCE_TAG + "1.0.0\n");
+ AcceleoParser.SINCE_TAG + "1.0.0" + NEW_LINE);
moduleDocumentationBody.setValue(moduleDocumentationBody.getValue() + " *");

moduleDocumentation.setBody(moduleDocumentationBody);
Expand All @@ -134,7 +139,7 @@ protected IStatus run(IProgressMonitor monitor) {
template.setDocumentation(createModuleElementDocumentation(template));
if (moduleConfiguration.isMainTemplate()) {
final CommentBody body = template.getDocumentation().getBody();
body.setValue(body.getValue() + " " + AcceleoParser.MAIN_TAG + "\n *");
body.setValue(body.getValue() + " " + AcceleoParser.MAIN_TAG + NEW_LINE + " *");
}
} else if (moduleConfiguration.isMainTemplate()) {
final Comment mainComment = AcceleoPackage.eINSTANCE.getAcceleoFactory().createComment();
Expand Down Expand Up @@ -167,9 +172,10 @@ protected IStatus run(IProgressMonitor monitor) {
module.getModuleElements().add(query);
}

final AcceleoAstSerializer serializer = new AcceleoAstSerializer();
final AcceleoAstSerializer serializer = new AcceleoAstSerializer(NEW_LINE);
// TODO at some point we should reuse the parser constants for this.
final String moduleString = "[comment encoding = UTF-8 /]\n" + serializer.serialize(module);
final String moduleString = "[comment encoding = UTF-8 /]" + NEW_LINE + serializer.serialize(
module);
try {
final ByteArrayInputStream moduleInputStream = new ByteArrayInputStream(moduleString.getBytes(
"UTF-8"));
Expand Down Expand Up @@ -221,9 +227,9 @@ private Block createTemplateBody(ModuleConfiguration configuration) {
res.setInlined(false);
final TextStatement text = AcceleoPackage.eINSTANCE.getAcceleoFactory().createTextStatement();
if (configuration.isGenerateFile()) {
text.setValue("\n");
text.setValue(NEW_LINE);
} else {
text.setValue(initialContent.replaceAll("\n", "\n ") + "\n");
text.setValue(initialContent.replaceAll("\\r\\n|\\n", NEW_LINE + " ") + NEW_LINE);
}
if (configuration.isGenerateFile()) {
final FileStatement fileStatement = AcceleoPackage.eINSTANCE.getAcceleoFactory()
Expand All @@ -235,7 +241,8 @@ private Block createTemplateBody(ModuleConfiguration configuration) {
fileBlock.setInlined(false);
final TextStatement fileText = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createTextStatement();
fileText.setValue(initialContent.replaceAll("\n", "\n ") + "\n ");
fileText.setValue(initialContent.replaceAll("\\r\\n|\\n", NEW_LINE + " ") + NEW_LINE
+ " ");
fileBlock.getStatements().add(fileText);
fileStatement.setBody(fileBlock);
res.getStatements().add(fileStatement);
Expand All @@ -262,17 +269,17 @@ private Documentation createModuleElementDocumentation(
res.setBody(documentationBody);
if (moduleElement instanceof Template) {
final Template template = (Template)moduleElement;
documentationBody.setValue("\n * The documentation of the template " + template.getName()
+ ".\n *");
documentationBody.setValue(NEW_LINE + " * The documentation of the template " + template
.getName() + "." + NEW_LINE + " *");
documentationBody.setValue(documentationBody.getValue() + " @param " + template
.getParameters().get(0).getName() + "\n *");
.getParameters().get(0).getName() + NEW_LINE + " *");
res.setDocumentedElement(template);
} else if (moduleElement instanceof Query) {
final Query query = (Query)moduleElement;
documentationBody.setValue("\n * The documentation of the query " + query.getName()
+ ".\n *");
documentationBody.setValue(NEW_LINE + " * The documentation of the query " + query.getName()
+ "." + NEW_LINE + " *");
documentationBody.setValue(documentationBody.getValue() + " @param " + query.getParameters()
.get(0).getName() + "\n *");
.get(0).getName() + NEW_LINE + " *");
res.setDocumentedElement(query);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 Obeo.
* Copyright (c) 2020, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -910,7 +910,8 @@ public List<Either<Command, CodeAction>> getCodeActions(int atStartIndex, int at
final ASTNode astNode = acceleoAstResult.getAstNode(atEndIndex);
if (astNode != null) {
final AcceleoQuickFixesSwitch quickFixesSwitch = new AcceleoQuickFixesSwitch(
getQueryEnvironment(), acceleoValidationResult, getModuleQualifiedName(), getContents());
getQueryEnvironment(), acceleoValidationResult, getModuleQualifiedName(), getContents(),
System.lineSeparator());
final List<IAstQuickFix> quickFixes = quickFixesSwitch.getQuickFixes(astNode);
res = quickFixes.stream().map(qf -> transform(qf)).collect(Collectors.toList());
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2023 Obeo.
* Copyright (c) 2017, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -148,7 +148,7 @@ public void migrate(Path mtlFile) throws IOException {
binFolderPath), targetFolderPath).migrate(emtlFile, mtlFile.toFile());

// serialize
String a4Content = new AcceleoAstSerializer().serialize(module);
String a4Content = new AcceleoAstSerializer(System.lineSeparator()).serialize(module);

// write result
Path targetMtlFile = targetFolderPath.resolve(relativePath).getParent().resolve(module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ public class AcceleoAstSerializer extends AcceleoSwitch<Object> {
*/
private static final String SPACE = " ";

/**
* A new line.
*/
private static final String NEW_LINE = "\n";

/**
* A dummy {@link Object} to prevent switching in super types.
*/
Expand Down Expand Up @@ -97,6 +92,15 @@ public class AcceleoAstSerializer extends AcceleoSwitch<Object> {
*/
private int currentBlockHeaderStartColumn;

/**
* The new line {@link String}.
*/
private final String newLine;

public AcceleoAstSerializer(String newLine) {
this.newLine = newLine;
}

/**
* Serializes the given {@link AcceleoASTNode}.
*
Expand Down Expand Up @@ -135,12 +139,12 @@ public Object caseBlock(Block block) {
final String savedIndentation = currentIndentation;
try {
final StringBuilder blockIndentation = new StringBuilder();
for (int i = 0; i < currentBlockHeaderStartColumn; i++) {
blockIndentation.append(SPACE);
}
if (block.isInlined()) {
currentIndentation = "";
} else {
for (int i = 0; i < currentBlockHeaderStartColumn; i++) {
blockIndentation.append(SPACE);
}
if (block.getStatements().isEmpty()) {
currentIndentation = blockIndentation.toString();
} else {
Expand Down Expand Up @@ -588,12 +592,12 @@ public Object caseVariable(Variable variable) {

@Override
public Object caseNewLineStatement(NewLineStatement newLineStatement) {
final int lastIndexOfNewLine = builder.lastIndexOf(NEW_LINE);
final int lastIndexOfNewLine = builder.lastIndexOf(newLine);
if (!newLineStatement.isIndentationNeeded()) {
if (builder.substring(lastIndexOfNewLine).trim().isEmpty()) {
builder.setLength(lastIndexOfNewLine + NEW_LINE.length());
builder.setLength(lastIndexOfNewLine + newLine.length());
} else {
builder.append(NEW_LINE);
builder.append(newLine);
}
}
insertNewLine();
Expand All @@ -604,19 +608,19 @@ public Object caseNewLineStatement(NewLineStatement newLineStatement) {
* Inserts a new line.
*/
private void insertNewLine() {
builder.append(NEW_LINE);
builder.append(newLine);
builder.append(currentIndentation);
}

/**
* Updates the current block header start column to the current position.
*/
private void updateCurrentBlockHeaderStartColumn() {
final int lastNewLineIndex = builder.lastIndexOf(NEW_LINE);
final int lastNewLineIndex = builder.lastIndexOf(newLine);
if (lastNewLineIndex <= 0) {
currentBlockHeaderStartColumn = builder.length();
} else {
currentBlockHeaderStartColumn = builder.length() - (lastNewLineIndex + NEW_LINE.length());
currentBlockHeaderStartColumn = builder.length() - (lastNewLineIndex + newLine.length());
}
}

Expand Down

0 comments on commit 519c049

Please sign in to comment.