Skip to content

Commit

Permalink
Formatter: add option => save file
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Jun 23, 2019
1 parent 8a79075 commit 86aa543
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Messages extends NLS {
public static String WollokLauncherOptions_JAR_LIBRARIES;
public static String WollokLauncherOptions_WOLLOK_FILES;
public static String WollokLauncherOptions_DONT_VALIDATE;
public static String WollokLauncherOptions_SAVE_FILE;

static {
// initialize resource bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.uqbar.project.wollok.launch

import com.google.inject.Injector
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import org.eclipse.xtext.resource.SaveOptions
import org.eclipse.xtext.serializer.ISerializer
import org.uqbar.project.wollok.wollokDsl.WFile
Expand All @@ -14,8 +16,15 @@ class WollokFormatter extends WollokChecker {

override doSomething(WFile parsed, Injector injector, File mainFile, WollokLauncherParameters parameters) {
val serializer = injector.getInstance(ISerializer)
val options = SaveOptions.newBuilder.format().getOptions()
println(serializer.serialize(parsed, options))
val formattedFile = serializer.serialize(parsed, SaveOptions.newBuilder.format.options)
if (parameters.saveFile) {
new BufferedWriter(new FileWriter(mainFile)) => [
write(formattedFile)
close
]
} else {
println(formattedFile)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class WollokLauncherParameters {
boolean noAnsiFormat = false
boolean severalFiles = false
boolean validate = true
boolean saveFile = false

Integer numberOfDecimals = null
String printingStrategy = null
Expand Down Expand Up @@ -60,6 +61,9 @@ class WollokLauncherParameters {
buildNumberPreferences(sb)
buildListOption(sb, libraries, "lib", ',')
buildListOption(sb, wollokFiles, "wf", ' ')
// only for formatter
if (saveFile) sb.append("-save ")
//
sb.toString
}

Expand Down Expand Up @@ -107,6 +111,8 @@ class WollokLauncherParameters {
printingStrategy = parseParameterString(cmdLine, "printingStrategy")
coercingStrategy = parseParameterString(cmdLine, "coercingStrategy")

saveFile = cmdLine.hasOption("save")

if ((requestsPort == 0 && eventsPort != 0) || (requestsPort != 0 && eventsPort == 0)) {
throw new RuntimeException(Messages.WollokLauncher_REQUEST_PORT_EVENTS_PORT_ARE_BOTH_REQUIRED)
}
Expand Down Expand Up @@ -205,6 +211,8 @@ class WollokLauncherParameters {
add("printingStrategy", Messages.WollokLauncherOptions_DECIMAL_PRINTING_STRATEGY, "name", 1)
add("coercingStrategy", Messages.WollokLauncherOptions_DECIMAL_CONVERSION_STRATEGY, "name", 1)

add("save", Messages.WollokLauncherOptions_SAVE_FILE, "save", 0)

addList("lib", Messages.WollokLauncherOptions_JAR_LIBRARIES, "libs", ',')
addList("wf", Messages.WollokLauncherOptions_WOLLOK_FILES, "files", ' ')
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ WollokLauncherOptions_DECIMAL_PRINTING_STRATEGY = Strategy to use when printing
WollokLauncherOptions_DECIMAL_CONVERSION_STRATEGY = Strategy to use when converting numbers
WollokLauncherOptions_JAR_LIBRARIES = Extra JAR libraries
WollokLauncherOptions_WOLLOK_FILES = Wollok files
WollokLauncherOptions_DONT_VALIDATE = Avoid running Wollok validator, just execute program.
WollokLauncherOptions_DONT_VALIDATE = Avoid running Wollok validator, just execute program.
WollokLauncherOptions_SAVE_FILE=Save formatter result in the same file
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ WollokLauncherOptions_DECIMAL_PRINTING_STRATEGY = Criterio a usar para imprimir
WollokLauncherOptions_DECIMAL_CONVERSION_STRATEGY = Criterio a usar para convertir n\u00FAmeros con muchos decimales
WollokLauncherOptions_JAR_LIBRARIES = Librer\u00EDas JAR extras
WollokLauncherOptions_WOLLOK_FILES = Archivos Wollok a ejecutar
WollokLauncherOptions_DONT_VALIDATE = Ejecutar el programa sin utilizar el validador de Wollok
WollokLauncherOptions_DONT_VALIDATE = Ejecutar el programa sin utilizar el validador de Wollok
WollokLauncherOptions_SAVE_FILE=Grabar el resultado del formateo en el mismo archivo

0 comments on commit 86aa543

Please sign in to comment.