Skip to content

Commit

Permalink
Introduced type adapter for CompletionItemDefaults.editRange
Browse files Browse the repository at this point in the history
Harmonized Naming convention and package structure

Fixes #624

Signed-off-by: Christian Dietrich <christian.dietrich@itemis.de>
  • Loading branch information
cdietrich committed May 18, 2022
1 parent 3f1f2bd commit e107f37
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ package org.eclipse.lsp4j.debug;
import com.google.gson.annotations.JsonAdapter
import com.google.gson.annotations.SerializedName
import java.util.Map
import org.eclipse.lsp4j.debug.adapters.RestartArgumentsTypeAdapter
import org.eclipse.lsp4j.generator.JsonRpcData
import org.eclipse.lsp4j.jsonrpc.messages.Either
import org.eclipse.lsp4j.jsonrpc.validation.NonNull
import org.eclipse.lsp4j.debug.adapters.RestartArgumentsArgumentsTypeAdapter

/**
* Declaration of parameters, response bodies, and event bodies for
Expand Down Expand Up @@ -940,7 +940,7 @@ class RestartArguments {
* <p>
* Since 1.47
*/
@JsonAdapter(RestartArgumentsTypeAdapter)
@JsonAdapter(RestartArgumentsArgumentsTypeAdapter)
Either<LaunchRequestArguments, AttachRequestArguments> arguments;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

public class RestartArgumentsTypeAdapter implements TypeAdapterFactory {
public class RestartArgumentsArgumentsTypeAdapter implements TypeAdapterFactory {

private static final TypeToken<Either<LaunchRequestArguments, AttachRequestArguments>> ELEMENT_TYPE = new TypeToken<Either<LaunchRequestArguments, AttachRequestArguments>>() {
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.debug.test;
package org.eclipse.lsp4j.debug.test.adapters;

import static org.junit.Assert.assertTrue;

Expand All @@ -19,7 +19,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class RestartArgumentsTypeAdapterFactoryTest {
public class RestartArgumentsArgumentsTypeAdapterTest {

@Test
public void test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode
import org.eclipse.lsp4j.jsonrpc.messages.Tuple
import org.eclipse.lsp4j.jsonrpc.validation.NonNull
import org.eclipse.lsp4j.util.Preconditions
import org.eclipse.lsp4j.adapters.CompletionItemDefaultsEditRangeTypeAdapter

@JsonRpcData
class DynamicRegistrationCapabilities {
Expand Down Expand Up @@ -3111,6 +3112,7 @@ class CompletionItemDefaults {
/**
* A default edit range
*/
@JsonAdapter(CompletionItemDefaultsEditRangeTypeAdapter)
Either<Range, InsertReplaceRange> editRange

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/******************************************************************************
* Copyright (c) 2022 itemis AG and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.adapters;

import java.util.function.Predicate;

import org.eclipse.lsp4j.InsertReplaceRange;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter;
import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter.PropertyChecker;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

public class CompletionItemDefaultsEditRangeTypeAdapter implements TypeAdapterFactory {
private static final TypeToken<Either<Range, InsertReplaceRange>> ELEMENT_TYPE = new TypeToken<Either<Range, InsertReplaceRange>>() {
};

@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Predicate<JsonElement> leftChecker = new PropertyChecker("start");
Predicate<JsonElement> rightChecker = new PropertyChecker("insert").or(new PropertyChecker("replace"));
return (TypeAdapter<T>) new EitherTypeAdapter<>(gson, ELEMENT_TYPE, leftChecker, rightChecker);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/******************************************************************************
* Copyright (c) 2022 itemis AG and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.test.adapters;

import static org.junit.Assert.assertTrue;

import org.eclipse.lsp4j.CompletionItemDefaults;
import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CompletionItemDefaultsEditRangeAdapterTest {

@Test
public void test() {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
CompletionItemDefaults object = gson.fromJson("{\"editRange\":{\"start\":{\"line\":0,\"character\":1},\"end\":{\"line\":0,\"character\":2}}}", CompletionItemDefaults.class);
assertTrue(object.toString(), object.getEditRange().isLeft());
object = gson.fromJson("{\"editRange\":{\"insert\":{\"start\":{\"line\":0,\"character\":1},\"end\":{\"line\":0,\"character\":2}},\"replace\":{\"start\":{\"line\":1,\"character\":1},\"end\":{\"line\":1,\"character\":2}}}}", CompletionItemDefaults.class);
assertTrue(object.toString(), object.getEditRange().isRight());
}
}

0 comments on commit e107f37

Please sign in to comment.