Skip to content

Commit

Permalink
Added Windows end line support in AcceleoEvaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Jan 25, 2024
1 parent fa89664 commit ed5f01e
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 62 deletions.
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 @@ -227,7 +227,8 @@ private GenerationResult launchGeneration() {
options, resolver, resourceSetForModels, false);

try {
AcceleoEvaluator evaluator = new AcceleoEvaluator(queryEnvironment.getLookupEngine());
final String newLine = options.getOrDefault(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
AcceleoEvaluator evaluator = new AcceleoEvaluator(queryEnvironment.getLookupEngine(), newLine);

resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.acceleo.aql.ide,
org.eclipse.equinox.common,
org.eclipse.emf.common.ui,
org.eclipse.acceleo.aql.profiler
org.eclipse.acceleo.aql.profiler,
com.google.gson
Automatic-Module-Name: org.eclipse.acceleo.aql.ls.debug.ide.ui
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.acceleo.aql.ls.debug.ide.ui.dialog,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
*******************************************************************************/
package org.eclipse.acceleo.aql.ls.debug.ide.ui.launch;

import com.google.gson.Gson;

import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.ide.AcceleoPlugin;
import org.eclipse.acceleo.aql.ls.debug.AcceleoDebugger;
import org.eclipse.acceleo.aql.ls.debug.ide.AcceleoDebugPlugin;
Expand Down Expand Up @@ -48,6 +54,21 @@ public class AcceleoMainTab extends AbstractLaunchConfigurationTab {
*/
protected static final String BROWSE = "Browse...";

/**
* New line.
*/
protected static final String NEW_LINE = "\n";

/**
* New line.
*/
protected static final String WINDOWS_NEW_LINE = "\r\n";

/**
* The {@link Gson} instance.
*/
protected static final Gson GSON = new Gson();

/**
* The module resource.
*/
Expand All @@ -63,6 +84,16 @@ public class AcceleoMainTab extends AbstractLaunchConfigurationTab {
*/
protected String destination;

/**
* The options {@link Map}.
*/
protected Map<String, String> options;

/**
* The options.
*/
private String optionsString;

/**
* The initial module.
*/
Expand All @@ -78,6 +109,11 @@ public class AcceleoMainTab extends AbstractLaunchConfigurationTab {
*/
private String initialDestination;

/**
* The initial options.
*/
private String initialOptionsString;

/**
* The module {@link Text}.
*/
Expand All @@ -88,6 +124,16 @@ public class AcceleoMainTab extends AbstractLaunchConfigurationTab {
*/
private Text modelText;

/**
* The Unix end line radio {@link Button}.
*/
private Button unixEndLineButton;

/**
* The Windows end line radio {@link Button}.
*/
private Button windowsEndLineButton;

/**
* The destination {@link Text}.
*/
Expand All @@ -103,6 +149,7 @@ public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
configuration.setAttribute(AcceleoDebugger.MODULE, module);
configuration.setAttribute(AcceleoDebugger.MODEL, model);
configuration.setAttribute(AcceleoDebugger.DESTINATION, destination);
configuration.setAttribute(AcceleoDebugger.OPTIONS, optionsString);
}

@Override
Expand All @@ -120,6 +167,20 @@ public void initializeFrom(ILaunchConfiguration configuration) {
destinationText.setText(configuration.getAttribute(AcceleoDebugger.DESTINATION, ""));
initialDestination = destination;
}
if (configuration.hasAttribute(AcceleoDebugger.OPTIONS)) {
options = GSON.fromJson(configuration.getAttribute(AcceleoDebugger.OPTIONS, ""),
AcceleoDebugger.OPTION_MAP_TYPE);
} else {
options = new LinkedHashMap<>();
if (!options.containsKey(AcceleoUtil.NEW_LINE_OPTION)) {
options.put(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
}
}
optionsString = GSON.toJson(options);
initialOptionsString = optionsString;
final String newLine = options.get(AcceleoUtil.NEW_LINE_OPTION);
unixEndLineButton.setSelection(NEW_LINE.equals(newLine));
windowsEndLineButton.setSelection(WINDOWS_NEW_LINE.equals(newLine));
} catch (CoreException e) {
AcceleoDebugPlugin.getPlugin().log(new Status(IStatus.ERROR, AcceleoDebugPlugin.ID,
"couldn't initialize from launch configuration", e));
Expand All @@ -132,6 +193,7 @@ public void performApply(ILaunchConfigurationWorkingCopy configuration) {
initialModule = module;
initialModel = model;
initialDestination = destination;
initialOptionsString = optionsString;
setDirty(isDirty());
}

Expand All @@ -143,7 +205,10 @@ public boolean isValid(ILaunchConfiguration launchConfig) {
if (launchConfig.hasAttribute(AcceleoDebugger.MODULE)) {
final IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(launchConfig
.getAttribute(AcceleoDebugger.MODULE, "")));
if (!AcceleoPlugin.isAcceleoMain(resource)) {
if (!resource.exists()) {
setErrorMessage("The selected Acceleo module doesn't exists.");
res = false;
} else if (!AcceleoPlugin.isAcceleoMain(resource)) {
setErrorMessage("The selected Acceleo module doesn't contain a main template.");
res = false;
}
Expand Down Expand Up @@ -193,6 +258,7 @@ public void createControl(Composite parent) {
moduleText = createModuleComposite(control);
modelText = createModelComposite(control);
destinationText = createDestinationComposite(control);
createNewLine(control);

setControl(control);
}
Expand Down Expand Up @@ -294,8 +360,36 @@ public void handleEvent(Event event) {
return res;
}

/**
* Creates the new line selection {@link Composite}.
*
* @param parent
* the parent
*/
private void createNewLine(final Composite parent) {
final Group group = new Group(parent, parent.getStyle());
group.setLayout(new GridLayout(2, false));
group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
group.setText("Enf of line:");
unixEndLineButton = new Button(group, SWT.RADIO);
unixEndLineButton.setText("Unix");
unixEndLineButton.addListener(SWT.Selection, e -> {
if (unixEndLineButton.getSelection()) {
putOption(AcceleoUtil.NEW_LINE_OPTION, NEW_LINE);
}
});
windowsEndLineButton = new Button(group, SWT.RADIO);
windowsEndLineButton.setText("Windows");
windowsEndLineButton.addListener(SWT.Selection, e -> {
if (windowsEndLineButton.getSelection()) {
putOption(AcceleoUtil.NEW_LINE_OPTION, WINDOWS_NEW_LINE);
}
});
}

protected boolean isDirty() {
return module != initialModule || model != initialModel || destination != initialDestination;
return module != initialModule || model != initialModel || destination != initialDestination
|| !optionsString.equals(initialOptionsString);
}

private void handleBrowseModuleButton() {
Expand All @@ -308,6 +402,22 @@ private void handleBrowseModuleButton() {
}
}

/**
* Puts the given option.
*
* @param name
* the option name
* @param value
* the option value
*/
protected void putOption(String name, String value) {
options.put(name, value);
optionsString = GSON.toJson(options);
setDirty(isDirty());
updateLaunchConfigurationDialog();

}

private void handleBrowseModelButton() {
FilteredResourcesSelectionDialog dialog = new FilteredResourcesSelectionDialog(getShell(), false,
ResourcesPlugin.getWorkspace().getRoot(), IResource.FILE);
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 @@ -72,6 +72,10 @@ public void launch(ILaunchConfiguration configuration, String mode, ILaunch laun
final URI destinationUri = destination.getLocation().toFile().getAbsoluteFile().toURI();
param.addProperty(AcceleoDebugger.DESTINATION, destinationUri.toString());
}
if (wc.hasAttribute(AcceleoDebugger.OPTIONS)) {
param.addProperty(AcceleoDebugger.OPTIONS, wc.getAttribute(AcceleoDebugger.OPTIONS,
(String)null));
}
if (ILaunchManager.PROFILE_MODE.equals(mode)) {
if (wc.hasAttribute(AcceleoDebugger.PROFILE_MODEL)) {
final IFile profileModel = root.getFile(new Path(wc.getAttribute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Require-Bundle: org.eclipse.emf.ecore,
org.eclipse.lsp4e,
org.eclipse.swt,
org.eclipse.acceleo.query.ide,
org.eclipse.acceleo.aql.profiler
org.eclipse.acceleo.aql.profiler,
com.google.gson
Automatic-Module-Name: org.eclipse.acceleo.aql.ls.debug
Export-Package: org.eclipse.acceleo.aql.ls.debug
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 All @@ -10,7 +10,11 @@
*******************************************************************************/
package org.eclipse.acceleo.aql.ls.debug;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -79,7 +83,7 @@ public void run() {
if (isNoDebug()) {
generateNoDebug(queryEnvironment, module, model);
} else {
evaluator = new AcceleoDebugEvaluator(queryEnvironment);
evaluator = new AcceleoDebugEvaluator(queryEnvironment, newLine);

final IQualifiedNameResolver resolver = queryEnvironment.getLookupEngine().getResolver();
resolver.clearLoaders();
Expand Down Expand Up @@ -135,9 +139,11 @@ private final class AcceleoDebugEvaluator extends AcceleoEvaluator {
*
* @param queryEnvironment
* the {@link IQualifiedNameQueryEnvironment}
* @param newLine
* the new line {@link String}
*/
AcceleoDebugEvaluator(IQualifiedNameQueryEnvironment queryEnvironment) {
super(queryEnvironment.getLookupEngine());
AcceleoDebugEvaluator(IQualifiedNameQueryEnvironment queryEnvironment, String newLine) {
super(queryEnvironment.getLookupEngine(), newLine);
}

@Override
Expand Down Expand Up @@ -182,6 +188,17 @@ public Object doSwitch(EObject eObject) {
*/
public static final String DESTINATION = "destination";

/**
* The JSON map of options.
*/
public static final String OPTIONS = "options";

/**
* The option map type.
*/
public static final Type OPTION_MAP_TYPE = new TypeToken<LinkedHashMap<String, String>>() {
}.getType();

/**
* The profile model
*/
Expand All @@ -207,6 +224,8 @@ public Object doSwitch(EObject eObject) {
*/
private URI destination;

private Map<String, String> options;

/**
* The profile model {@link URI}.
*/
Expand All @@ -227,6 +246,11 @@ public Object doSwitch(EObject eObject) {
*/
private Resource model;

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

/**
* The {@link AcceleoDebugEvaluator}.
*/
Expand All @@ -251,6 +275,13 @@ public void initialize(boolean noDebug, Map<String, Object> arguments) {
final URI modelURI = URI.createURI((String)arguments.get(MODEL));
destination = URI.createURI((String)arguments.get(DESTINATION));

final String optionString = (String)arguments.get(OPTIONS);
if (optionString != null) {
options = new Gson().fromJson(optionString, OPTION_MAP_TYPE);
} else {
options = new LinkedHashMap<>();
}

final String profileModel = (String)arguments.get(PROFILE_MODEL);
if (profileModel != null) {
profileModelURI = URI.createFileURI(URI.decode(profileModel));
Expand All @@ -265,8 +296,7 @@ public void initialize(boolean noDebug, Map<String, Object> arguments) {
AcceleoPlugin.getPlugin().getClass().getClassLoader(), project,
AcceleoParser.QUALIFIER_SEPARATOR, false);

// TODO get options form the launch configuration
final Map<String, String> options = new LinkedHashMap<>();
newLine = options.getOrDefault(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
final ArrayList<Exception> exceptions = new ArrayList<>();
resourceSetForModels = AQLUtils.createResourceSetForModels(exceptions, this, new ResourceSetImpl(),
options);
Expand Down Expand Up @@ -327,9 +357,9 @@ protected void generateNoDebug(IQualifiedNameQueryEnvironment environment, Modul
if (profileModelURI != null && profilerModelRepresentation != null) {
profiler = ProfilerUtils.getProfiler(profilerModelRepresentation, ProfilerPackage.eINSTANCE
.getProfilerFactory());
noDebugEvaluator = new AcceleoProfilerEvaluator(queryEnvironment, profiler);
noDebugEvaluator = new AcceleoProfilerEvaluator(queryEnvironment, newLine, profiler);
} else {
noDebugEvaluator = new AcceleoEvaluator(environment.getLookupEngine());
noDebugEvaluator = new AcceleoEvaluator(environment.getLookupEngine(), newLine);
profiler = null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 Huawei.
* Copyright (c) 2020, 2024 Huawei.
* All rights reserved.
*
* Contributors:
Expand Down Expand Up @@ -29,11 +29,14 @@ public class AcceleoProfilerEvaluator extends AcceleoEvaluator {
*
* @param queryEnvironment
* the {@link IQualifiedNameQueryEnvironment}
* @param newLine
* the new line {@link String}
* @param profiler
* the {@link IProfiler}
*/
public AcceleoProfilerEvaluator(IQualifiedNameQueryEnvironment queryEnvironment, IProfiler profiler) {
super(queryEnvironment.getLookupEngine());
public AcceleoProfilerEvaluator(IQualifiedNameQueryEnvironment queryEnvironment, String newLine,
IProfiler profiler) {
super(queryEnvironment.getLookupEngine(), newLine);
this.profiler = profiler;
}

Expand Down
Loading

0 comments on commit ed5f01e

Please sign in to comment.