-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created formatter interface * Implement interface + first Test * Fix formatting test + implementation * Enhanced test suite * Implement suggested changes * Improved interface methods/exception handling and added logging * Added formatting tool method * Add dependencies for logging in tools project * Improve implementation of command line tool * Formatting java code * Made formatting happen in-memory * Update logs when resource saved * Fixed logger class name * Starting code for formatter plugin * Incorporate 'format' goal in existing plugin * Cleanup files * Add test cases for edge cases and unusual behavior * Update code to deal with runtime exceptions * Fix formatting errors with Choice and parenthesis * Implement suggested changes * Fix comment * Implement suggestion * Fix only exists serialization issue * Cleanup test * Update plugin to also use user-given formatting options * Update plugin to use formatting options * Update formatting strategy to avoid serialization * Add handler support to service for processing formatted content * Implemented suggested changes * Update cmd tool + cleanup * Format closing brackets to collapse in nested constructors * Introduce FormattingOptionsAdaptor for shared FormattingOptions logic * Formatted collapsable closing brackets in nested constructors * Fix collapsing closing brackets logic * Add endpoint for default formatting options * Implement suggested changes * Fix failing test * Cleanup code
- Loading branch information
1 parent
f45ca93
commit f1f8f37
Showing
24 changed files
with
807 additions
and
176 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
13 changes: 13 additions & 0 deletions
13
rosetta-ide/src/main/java/com/regnosys/rosetta/ide/server/RosettaLanguageServer.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,13 @@ | ||
package com.regnosys.rosetta.ide.server; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.eclipse.lsp4j.FormattingOptions; | ||
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest; | ||
import org.eclipse.lsp4j.services.LanguageServer; | ||
|
||
public interface RosettaLanguageServer extends LanguageServer{ | ||
|
||
@JsonRequest | ||
CompletableFuture<FormattingOptions> getDefaultFormattingOptions(); | ||
} |
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
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
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
94 changes: 94 additions & 0 deletions
94
rosetta-lang/src/main/java/com/regnosys/rosetta/formatting2/FormattingOptionsAdaptor.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,94 @@ | ||
package com.regnosys.rosetta.formatting2; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
import org.eclipse.emf.mwe.core.resources.ResourceLoader; | ||
import org.eclipse.lsp4j.FormattingOptions; | ||
import org.eclipse.xtext.preferences.ITypedPreferenceValues; | ||
import org.eclipse.xtext.preferences.MapBasedPreferenceValues; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.base.Strings; | ||
|
||
public class FormattingOptionsAdaptor { | ||
public static String PREFERENCE_INDENTATION_KEY = "indentation"; | ||
public static String PREFERENCE_MAX_LINE_WIDTH_KEY = "maxLineWidth"; | ||
public static String PREFERENCE_CONDITIONAL_MAX_LINE_WIDTH_KEY = "conditionalMaxLineWidth"; | ||
|
||
private static final String DEFAULT_FORMATTING_OPTIONS_PATH = "default-formatting-options.json"; | ||
|
||
public ITypedPreferenceValues createPreferences(FormattingOptions options) { | ||
MapBasedPreferenceValues preferences = new MapBasedPreferenceValues(); | ||
|
||
String indent = "\t"; | ||
if (options != null) { | ||
if (options.isInsertSpaces()) { | ||
indent = Strings.padEnd("", options.getTabSize(), ' '); | ||
} | ||
} | ||
preferences.put(PREFERENCE_INDENTATION_KEY, indent); | ||
|
||
if (options == null) { | ||
return preferences; | ||
} | ||
|
||
Number conditionalMaxLineWidth = options.getNumber(PREFERENCE_CONDITIONAL_MAX_LINE_WIDTH_KEY); | ||
if (conditionalMaxLineWidth != null) { | ||
preferences.put(RosettaFormatterPreferenceKeys.conditionalMaxLineWidth, conditionalMaxLineWidth.intValue()); | ||
} | ||
Number maxLineWidth = options.getNumber(PREFERENCE_MAX_LINE_WIDTH_KEY); | ||
if (maxLineWidth != null) { | ||
preferences.put(RosettaFormatterPreferenceKeys.maxLineWidth, maxLineWidth.intValue()); | ||
if (conditionalMaxLineWidth == null) { | ||
int defaultConditionalMaxLineWidth = RosettaFormatterPreferenceKeys.conditionalMaxLineWidth | ||
.toValue(RosettaFormatterPreferenceKeys.conditionalMaxLineWidth.getDefaultValue()); | ||
int defaultMaxLineWidth = RosettaFormatterPreferenceKeys.maxLineWidth | ||
.toValue(RosettaFormatterPreferenceKeys.maxLineWidth.getDefaultValue()); | ||
double defaultRatio = (double) defaultConditionalMaxLineWidth / defaultMaxLineWidth; | ||
preferences.put(RosettaFormatterPreferenceKeys.conditionalMaxLineWidth, | ||
(int) (maxLineWidth.doubleValue() * defaultRatio)); | ||
} | ||
} | ||
|
||
return preferences; | ||
} | ||
|
||
public FormattingOptions readFormattingOptions(String optionsPath) throws IOException { | ||
InputStream resourceStream; | ||
// If path not given, use default one | ||
if (optionsPath == null) { | ||
// Retrieve resource as an InputStream | ||
resourceStream = ResourceLoader.class.getClassLoader().getResourceAsStream(DEFAULT_FORMATTING_OPTIONS_PATH); | ||
} else { | ||
resourceStream = new FileInputStream(optionsPath); | ||
} | ||
|
||
// Create an ObjectMapper, read JSON into a Map | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
Map<String, Object> map = null; | ||
map = objectMapper.readValue(resourceStream, Map.class); | ||
|
||
// Create a FormattingOptions object | ||
FormattingOptions formattingOptions = new FormattingOptions(); | ||
|
||
// Populate the FormattingOptions object | ||
for (Map.Entry<String, Object> entry : map.entrySet()) { | ||
String key = entry.getKey(); | ||
Object value = entry.getValue(); | ||
|
||
if (value instanceof String) { | ||
formattingOptions.putString(key, (String) value); | ||
} else if (value instanceof Number) { | ||
formattingOptions.putNumber(key, (Number) value); | ||
} else if (value instanceof Boolean) { | ||
formattingOptions.putBoolean(key, (Boolean) value); | ||
} else { | ||
throw new IllegalArgumentException("Unsupported value type for key: " + key); | ||
} | ||
} | ||
return formattingOptions; | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
rosetta-lang/src/main/java/com/regnosys/rosetta/formatting2/IFormattedResourceAcceptor.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,25 @@ | ||
package com.regnosys.rosetta.formatting2; | ||
|
||
import org.eclipse.emf.ecore.resource.Resource; | ||
|
||
/** | ||
* Functional interface for handling a formatted resource and its corresponding | ||
* formatted text. | ||
* <p> | ||
* This interface allows customization of how formatted resources are processed, | ||
* such as saving the content, logging it, or using it for validations or | ||
* assertions in tests. | ||
* </p> | ||
*/ | ||
@FunctionalInterface | ||
public interface IFormattedResourceAcceptor { | ||
|
||
/** | ||
* Accepts a formatted resource and its formatted content for further | ||
* processing. | ||
* | ||
* @param resource the {@link Resource} that was formatted | ||
* @param formattedText the formatted content as a {@link String} | ||
*/ | ||
void accept(Resource resource, String formattedText); | ||
} |
Oops, something went wrong.