Java Code Examples for org.eclipse.lsp4j.jsonrpc.messages.Either#forRight()

The following examples show how to use org.eclipse.lsp4j.jsonrpc.messages.Either#forRight() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HoverTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
protected Either<List<Either<String, MarkedString>>, MarkupContent> readContents(final JsonReader in) throws IOException {
  final JsonToken nextToken = in.peek();
  boolean _equals = Objects.equal(nextToken, JsonToken.STRING);
  if (_equals) {
    final List<Either<String, MarkedString>> value = CollectionLiterals.<Either<String, MarkedString>>newArrayList(Either.<String, MarkedString>forLeft(in.nextString()));
    return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value);
  } else {
    boolean _equals_1 = Objects.equal(nextToken, JsonToken.BEGIN_ARRAY);
    if (_equals_1) {
      final List<Either<String, MarkedString>> value_1 = this.gson.<List<Either<String, MarkedString>>>fromJson(in, HoverTypeAdapter.LIST_STRING_MARKEDSTRING.getType());
      return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value_1);
    } else {
      JsonElement _parse = new JsonParser().parse(in);
      final JsonObject object = ((JsonObject) _parse);
      boolean _has = object.has("language");
      if (_has) {
        final List<Either<String, MarkedString>> value_2 = CollectionLiterals.<Either<String, MarkedString>>newArrayList(Either.<String, MarkedString>forRight(this.gson.<MarkedString>fromJson(object, MarkedString.class)));
        return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value_2);
      } else {
        return Either.<List<Either<String, MarkedString>>, MarkupContent>forRight(this.gson.<MarkupContent>fromJson(object, MarkupContent.class));
      }
    }
  }
}
 
Example 2
Source File: SnippetUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static Either<String, MarkupContent> beautifyDocument(String raw) {
	// remove the placeholder for the plain cursor like: ${0}, ${1:variable}
	String escapedString = raw.replaceAll("\\$\\{\\d:?(.*?)\\}", "$1");

	// Replace the reserved variable with empty string.
	// See: https://github.com/eclipse/eclipse.jdt.ls/issues/1220
	escapedString = escapedString.replaceAll(TM_SELECTED_TEXT, "");

	if (JavaLanguageServerPlugin.getPreferencesManager() != null && JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences() != null
			&& JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isSupportsCompletionDocumentationMarkdown()) {
		MarkupContent markupContent = new MarkupContent();
		markupContent.setKind(MarkupKind.MARKDOWN);
		markupContent.setValue(String.format("```%s\n%s\n```", MARKDOWN_LANGUAGE, escapedString));
		return Either.forRight(markupContent);
	} else {
		return Either.forLeft(escapedString);
	}
}
 
Example 3
Source File: Diagnostic.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setCode(final Number code) {
  if (code == null) {
    this.code = null;
    return;
  }
  this.code = Either.forRight(code);
}
 
Example 4
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setFoldingRangeProvider(final FoldingRangeProviderOptions foldingRangeProvider) {
  if (foldingRangeProvider == null) {
    this.foldingRangeProvider = null;
    return;
  }
  this.foldingRangeProvider = Either.forRight(foldingRangeProvider);
}
 
Example 5
Source File: EitherTest.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testLeftEqualsNull() {
	Either<Object, String> either1 = Either.forRight("Testing");
	Either<Object, String> either2 = Either.forRight("Testing");

	assertTrue(either1.equals(either2));
}
 
Example 6
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setRenameProvider(final RenameOptions renameProvider) {
  if (renameProvider == null) {
    this.renameProvider = null;
    return;
  }
  this.renameProvider = Either.forRight(renameProvider);
}
 
Example 7
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setCodeActionProvider(final CodeActionOptions codeActionProvider) {
  if (codeActionProvider == null) {
    this.codeActionProvider = null;
    return;
  }
  this.codeActionProvider = Either.forRight(codeActionProvider);
}
 
Example 8
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setImplementationProvider(final StaticRegistrationOptions implementationProvider) {
  if (implementationProvider == null) {
    this.implementationProvider = null;
    return;
  }
  this.implementationProvider = Either.forRight(implementationProvider);
}
 
Example 9
Source File: ParameterInformation.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setDocumentation(final MarkupContent documentation) {
  if (documentation == null) {
    this.documentation = null;
    return;
  }
  this.documentation = Either.forRight(documentation);
}
 
Example 10
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setTextDocumentSync(final TextDocumentSyncOptions textDocumentSync) {
  if (textDocumentSync == null) {
    this.textDocumentSync = null;
    return;
  }
  this.textDocumentSync = Either.forRight(textDocumentSync);
}
 
Example 11
Source File: PublishDiagnosticsCapabilities.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setTagSupport(final DiagnosticsTagSupport tagSupport) {
  if (tagSupport == null) {
    this.tagSupport = null;
    return;
  }
  this.tagSupport = Either.forRight(tagSupport);
}
 
Example 12
Source File: Hover.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setContents(final MarkupContent contents) {
  if (contents == null) {
    Preconditions.checkNotNull(contents, "contents");
    this.contents = null;
    return;
  }
  this.contents = Either.forRight(contents);
}
 
Example 13
Source File: SignatureInformation.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setDocumentation(final MarkupContent documentation) {
  if (documentation == null) {
    this.documentation = null;
    return;
  }
  this.documentation = Either.forRight(documentation);
}
 
Example 14
Source File: EitherTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected Either<L, R> createRight(R obj) throws IOException {
	if (Either3.class.isAssignableFrom(typeToken.getRawType()))
		return (Either<L, R>) Either3.forRight3((Either<?, ?>) obj);
	else
		return Either.forRight(obj);
}
 
Example 15
Source File: NonProjectFixProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private Either<Command, CodeAction> getDiagnosticsFixes(String message, String uri, String scope, boolean syntaxOnly) {
	Command command = new Command(message, REFRESH_DIAGNOSTICS_COMMAND, Arrays.asList(uri, scope, syntaxOnly));
	if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(CodeActionKind.QuickFix)) {
		CodeAction codeAction = new CodeAction(message);
		codeAction.setKind(CodeActionKind.QuickFix);
		codeAction.setCommand(command);
		codeAction.setDiagnostics(Collections.EMPTY_LIST);
		return Either.forRight(codeAction);
	} else {
		return Either.forLeft(command);
	}
}
 
Example 16
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setTypeHierarchyProvider(final StaticRegistrationOptions typeHierarchyProvider) {
  if (typeHierarchyProvider == null) {
    this.typeHierarchyProvider = null;
    return;
  }
  this.typeHierarchyProvider = Either.forRight(typeHierarchyProvider);
}
 
Example 17
Source File: DebugResponseMessage.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
public void setResponseId(int id) {
	this.responseId = Either.forRight(id);
}
 
Example 18
Source File: EitherTest.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testParseEither() {
	MyObjectA object = new MyObjectA();
	object.myProperty = Either.forRight(7);
	assertParse(object, "{\"myProperty\":7}");
}
 
Example 19
Source File: DebugNotificationMessage.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
public void setId(int id) {
	this.id = Either.forRight(id);
}
 
Example 20
Source File: MessageTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Message read(JsonReader in) throws IOException, JsonIOException, JsonSyntaxException {
	if (in.peek() == JsonToken.NULL) {
		in.nextNull();
		return null;
	}
	
	in.beginObject();
	String jsonrpc = null, method = null;
	Either<String, Number> id = null;
	Object rawParams = null;
	Object rawResult = null;
	ResponseError responseError = null;
	try {
		
		while (in.hasNext()) {
			String name = in.nextName();
			switch (name) {
			case "jsonrpc": {
				jsonrpc = in.nextString();
				break;
			}
			case "id": {
				if (in.peek() == JsonToken.NUMBER)
					id = Either.forRight(in.nextInt());
				else
					id = Either.forLeft(in.nextString());
				break;
			}
			case "method": {
				method = in.nextString();
				break;
			}
			case "params": {
				rawParams = parseParams(in, method);
				break;
			}
			case "result": {
				rawResult = parseResult(in, id != null ? id.get().toString() : null);
				break;
			}
			case "error": {
				responseError = gson.fromJson(in, ResponseError.class);
				break;
			}
			default:
				in.skipValue();
			}
		}
		Object params = parseParams(rawParams, method);
		Object result = parseResult(rawResult, id != null ? id.get().toString() : null);
		
		in.endObject();
		return createMessage(jsonrpc, id, method, params, result, responseError);
		
	} catch (JsonSyntaxException | MalformedJsonException | EOFException exception) {
		if (id != null || method != null) {
			// Create a message and bundle it to an exception with an issue that wraps the original exception
			Message message = createMessage(jsonrpc, id, method, rawParams, rawResult, responseError);
			MessageIssue issue = new MessageIssue("Message could not be parsed.", ResponseErrorCode.ParseError.getValue(), exception);
			throw new MessageIssueException(message, issue);
		} else {
			throw exception;
		}
	}
}