Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-8557: No Dto available for FormattingOptions #8784

Merged
merged 7 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -101,12 +102,11 @@ public static <T> T newDto(Class<T> dtoInterface) {
// It helps avoid reflection when need create copy of exited DTO instance.
private final Map<Class<?>, DtoProvider<?>> dtoImpl2Providers = new ConcurrentHashMap<>();
private final Gson dtoGson =
new GsonBuilder()
.registerTypeAdapterFactory(
new NullAsEmptyTAF<>(Collection.class, Collections.emptyList()))
.registerTypeAdapterFactory(new NullAsEmptyTAF<>(Map.class, Collections.emptyMap()))
.registerTypeAdapterFactory(new DtoInterfaceTAF())
.create();
buildDtoParser(
ServiceLoader.load(TypeAdapterFactory.class).iterator(),
new NullAsEmptyTAF<>(Collection.class, Collections.emptyList()),
new NullAsEmptyTAF<>(Map.class, Collections.emptyMap()),
new DtoInterfaceTAF());

/**
* Created deep copy of DTO object.
Expand Down Expand Up @@ -490,4 +490,19 @@ public T read(JsonReader in) throws IOException {
}

private DtoFactory() {}

private static Gson buildDtoParser(
Iterator<TypeAdapterFactory> factoryIterator, TypeAdapterFactory... factories) {
GsonBuilder builder = new GsonBuilder();

for (Iterator<TypeAdapterFactory> it = factoryIterator; it.hasNext(); ) {
TypeAdapterFactory factory = it.next();
builder.registerTypeAdapterFactory(factory);
}

for (TypeAdapterFactory factory : factories) {
builder.registerTypeAdapterFactory(factory);
}
return builder.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private void applyEdits(List<TextEdit> edits, Document document) {
}

private FormattingOptions getFormattingOptions() {
FormattingOptions options = dtoFactory.createDto(FormattingOptions.class);
FormattingOptions options = new FormattingOptions();
options.setInsertSpaces(Boolean.parseBoolean(getEditorProperty(EditorProperties.EXPAND_TAB)));
options.setTabSize(Integer.parseInt(getEditorProperty(EditorProperties.TAB_SIZE)));
return options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,27 @@ private void generateFromJson(
}

private void generateMapConversion(
String indent, PrintWriter out, String varName, String jsonValName, Type paramType) {
String indent, PrintWriter out, String varName, String jsonValName, Type inputParamType) {
Type paramType = inputParamType;
if (!(paramType instanceof ParameterizedType)) {
paramType = ((Class<?>) paramType).getGenericSuperclass();
}
ParameterizedType genericType = (ParameterizedType) paramType;
Type containedType = genericType.getActualTypeArguments()[1];
String objectName = varName + "o";
String containedName = objectName + "X";
String typeName = inputParamType.getTypeName();
String instantiateTypeName = typeName;
if (getRawClass(inputParamType).isInterface()) {
if (inputParamType instanceof ParameterizedType) {
instantiateTypeName = String.format("HashMap<String, %1$s>", containedType.getTypeName());
} else {
throw new RuntimeException(
"Unsupported Map Conversion. Generator needs to be updated for new LSP4J construct");
}
}
out.println(
indent
+ String.format(
"HashMap<String, %1$s> %2$s= new HashMap<String, %3$s>();",
containedType.getTypeName(), varName, containedType.getTypeName()));
indent + String.format("%1$s %2$s= new %3$s();", typeName, varName, instantiateTypeName));
out.println(
indent
+ String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,27 @@ private void generateEitherConversion(
}

private void generateMapConversion(
String indent, PrintWriter out, String varName, String valueAccess, Type paramType) {
String indent, PrintWriter out, String varName, String valueAccess, Type inputParamType) {
Type paramType = inputParamType;
if (!(paramType instanceof ParameterizedType)) {
paramType = ((Class<?>) paramType).getGenericSuperclass();
}
ParameterizedType genericType = (ParameterizedType) paramType;
Type containedType = genericType.getActualTypeArguments()[1];
Type valueType = genericType.getActualTypeArguments()[1];
String containedName = varName + "X";
String typeName = inputParamType.getTypeName();
String instantiateTypeName = typeName;
if (getRawClass(inputParamType).isInterface()) {
if (inputParamType instanceof ParameterizedType) {
instantiateTypeName = String.format("HashMap<String, %1$s>", containedType.getTypeName());
} else {
throw new RuntimeException(
"Unsupported Map Conversion. Generator needs to be updated for new LSP4J construct");
}
}
out.println(
indent
+ String.format(
"HashMap<String, %1$s> %2$s= new HashMap<String, %3$s>();",
containedType.getTypeName(), varName, containedType.getTypeName()));
indent + String.format("%1$s %2$s= new %3$s();", typeName, varName, instantiateTypeName));
out.println(
String.format(
indent + "for (Entry<String, %1$s> %2$s : %3$s.entrySet()) {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ private static boolean matches(JsonElement element, JsonDecision decision) {
}
}

return element.isJsonObject();
if (decision == JsonDecision.OBJECT) {
return element.isJsonObject();
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapterFactory
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.che.api.languageserver.dto;

import com.google.gson.JsonElement;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
Expand All @@ -25,12 +26,16 @@
import org.eclipse.che.api.languageserver.server.dto.DtoServerImpls.WorkspaceEditDto;
import org.eclipse.che.api.languageserver.shared.model.ExtendedCompletionItem;
import org.eclipse.che.api.languageserver.shared.model.ExtendedCompletionList;
import org.eclipse.che.dto.server.DtoFactory;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.DocumentFormattingParams;
import org.eclipse.lsp4j.FormattingOptions;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.MarkedString;
import org.eclipse.lsp4j.ParameterInformation;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
Expand Down Expand Up @@ -83,6 +88,88 @@ public void testMapConversion() {
Assert.assertTrue(reflectionEquals(originalDto, convertedDto));
}

@Test
public void testDocumentFormattingParamsDeserializerWithGson() throws Exception {
FormattingOptions formattingOptions = new FormattingOptions(4, true);
TextDocumentIdentifier textDocument =
DtoFactory.getInstance().createDto(TextDocumentIdentifier.class);
textDocument.setUri("/console-java-simple/src/main/java/org/eclipse/che/examples/A.java");
DocumentFormattingParams documentFormattingParams =
DtoFactory.getInstance().createDto(DocumentFormattingParams.class);
documentFormattingParams.setOptions(formattingOptions);
documentFormattingParams.setTextDocument(textDocument);
JsonElement json = DtoFactory.getInstance().toJsonElement(documentFormattingParams);

DocumentFormattingParams params =
DtoFactory.getInstance().createDtoFromJson(json.toString(), DocumentFormattingParams.class);

Assert.assertTrue(params.getOptions().isInsertSpaces());
}

@Test
public void testDocumentFormattingParamsDeserializerWithDto() throws Exception {
FormattingOptions formattingOptions = new FormattingOptions(4, true);
TextDocumentIdentifier textDocument =
DtoFactory.getInstance().createDto(TextDocumentIdentifier.class);
textDocument.setUri("/console-java-simple/src/main/java/org/eclipse/che/examples/A.java");
DocumentFormattingParams documentFormattingParams =
DtoFactory.getInstance().createDto(DocumentFormattingParams.class);
documentFormattingParams.setOptions(formattingOptions);
documentFormattingParams.setTextDocument(textDocument);
JsonElement json = DtoFactory.getInstance().toJsonElement(documentFormattingParams);

DocumentFormattingParams params =
DtoFactory.getInstance().createDtoFromJson(json, DocumentFormattingParams.class);

Assert.assertTrue(params.getOptions().isInsertSpaces());
}

/** Test the Either conversion by using a Hover object. */
@Test
public void testEitherDeserializerWithDto() throws Exception {
Hover hover = DtoFactory.getInstance().createDto(Hover.class);
MarkedString markedString = DtoFactory.getInstance().createDto(MarkedString.class);
markedString.setLanguage("Language");
markedString.setValue("Value");
List<Either<String, MarkedString>> list = new ArrayList<>();
list.add(Either.forRight(markedString));
list.add(Either.forLeft("normal String"));
hover.setContents(list);

JsonElement json = DtoFactory.getInstance().toJsonElement(hover);

Hover params = DtoFactory.getInstance().createDtoFromJson(json, Hover.class);

Assert.assertTrue(params.getContents().get(0).isRight());
Assert.assertEquals("Value", params.getContents().get(0).getRight().getValue());
Assert.assertEquals("Language", params.getContents().get(0).getRight().getLanguage());
Assert.assertTrue(params.getContents().get(1).isLeft());
Assert.assertEquals("normal String", params.getContents().get(1).getLeft());
}

/** Test the Either conversion by using a Hover object. */
@Test
public void testEitherDeserializerWithGson() throws Exception {
Hover hover = DtoFactory.getInstance().createDto(Hover.class);
MarkedString markedString = DtoFactory.getInstance().createDto(MarkedString.class);
markedString.setLanguage("Language");
markedString.setValue("Value");
List<Either<String, MarkedString>> list = new ArrayList<>();
list.add(Either.forRight(markedString));
list.add(Either.forLeft("normal String"));
hover.setContents(list);

JsonElement json = DtoFactory.getInstance().toJsonElement(hover);

Hover params = DtoFactory.getInstance().createDtoFromJson(json.toString(), Hover.class);

Assert.assertTrue(params.getContents().get(0).isRight());
Assert.assertEquals("Value", params.getContents().get(0).getRight().getValue());
Assert.assertEquals("Language", params.getContents().get(0).getRight().getLanguage());
Assert.assertTrue(params.getContents().get(1).isLeft());
Assert.assertEquals("normal String", params.getContents().get(1).getLeft());
}

@Test
public void testEitherConversion() {
Either<String, MarkedString> either1 = Either.forLeft("foobar");
Expand Down