<commit_before>/*
 * Copyright 2018 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package io.flutter.dart;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.dartlang.analysis.server.protocol.FlutterWidgetPropertyValue;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

/**
 * A utilities class for generating analysis server json requests.
 */
public class FlutterRequestUtilities {
  private static final String FILE = "file";
  private static final String ID = "id";
  private static final String VALUE = "value";
  private static final String METHOD = "method";
  private static final String PARAMS = "params";
  private static final String OFFSET = "offset";
  private static final String SUBSCRIPTIONS = "subscriptions";

  private static final String METHOD_FLUTTER_GET_CHANGE_ADD_FOR_DESIGN_TIME_CONSTRUCTOR = "flutter.getChangeAddForDesignTimeConstructor";
  private static final String METHOD_FLUTTER_SET_SUBSCRIPTIONS = "flutter.setSubscriptions";
  private static final String METHOD_FLUTTER_GET_WIDGET_DESCRIPTION = "flutter.getWidgetDescription";
  private static final String METHOD_FLUTTER_SET_WIDGET_PROPERTY_VALUE = "flutter.setWidgetPropertyValue";

  private FlutterRequestUtilities() {
  }

  public static JsonObject generateFlutterGetWidgetDescription(String id, String file, int offset) {
    final JsonObject params = new JsonObject();
    params.addProperty(FILE, file);
    params.addProperty(OFFSET, offset);
    return buildJsonObjectRequest(id, METHOD_FLUTTER_GET_WIDGET_DESCRIPTION, params);
  }

  public static JsonObject generateFlutterSetWidgetPropertyValue(String id, int propertyId, FlutterWidgetPropertyValue value) {
    final JsonObject params = new JsonObject();
    params.addProperty(ID, propertyId);
    // An omitted value indicates that the property should be removed.
    if (value != null) {
      params.add(VALUE, value.toJson());
    }
    return buildJsonObjectRequest(id, METHOD_FLUTTER_SET_WIDGET_PROPERTY_VALUE, params);
  }

  /**
   * Generate and return a {@value #METHOD_FLUTTER_SET_SUBSCRIPTIONS} request.
   * <p>
   * <pre>
   * request: {
   *   "id": String
   *   "method": "flutter.setSubscriptions"
   *   "params": {
   *     "subscriptions": Map&gt;FlutterService, List&lt;FilePath&gt;&gt;<commit_msg>Add missing Nullable annotation. (#4122)<commit_after>/*
 * Copyright 2018 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package io.flutter.dart;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.dartlang.analysis.server.protocol.FlutterWidgetPropertyValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

/**
 * A utilities class for generating analysis server json requests.
 */
public class FlutterRequestUtilities {
  private static final String FILE = "file";
  private static final String ID = "id";
  private static final String VALUE = "value";
  private static final String METHOD = "method";
  private static final String PARAMS = "params";
  private static final String OFFSET = "offset";
  private static final String SUBSCRIPTIONS = "subscriptions";

  private static final String METHOD_FLUTTER_GET_CHANGE_ADD_FOR_DESIGN_TIME_CONSTRUCTOR = "flutter.getChangeAddForDesignTimeConstructor";
  private static final String METHOD_FLUTTER_SET_SUBSCRIPTIONS = "flutter.setSubscriptions";
  private static final String METHOD_FLUTTER_GET_WIDGET_DESCRIPTION = "flutter.getWidgetDescription";
  private static final String METHOD_FLUTTER_SET_WIDGET_PROPERTY_VALUE = "flutter.setWidgetPropertyValue";

  private FlutterRequestUtilities() {
  }

  public static JsonObject generateFlutterGetWidgetDescription(String id, String file, int offset) {
    final JsonObject params = new JsonObject();
    params.addProperty(FILE, file);
    params.addProperty(OFFSET, offset);
    return buildJsonObjectRequest(id, METHOD_FLUTTER_GET_WIDGET_DESCRIPTION, params);
  }

  public static JsonObject generateFlutterSetWidgetPropertyValue(String id, int propertyId, @Nullable FlutterWidgetPropertyValue value) {
    final JsonObject params = new JsonObject();
    params.addProperty(ID, propertyId);
    // An omitted value indicates that the property should be removed.
    if (value != null) {
      params.add(VALUE, value.toJson());
    }
    return buildJsonObjectRequest(id, METHOD_FLUTTER_SET_WIDGET_PROPERTY_VALUE, params);
  }

  /**
   * Generate and return a {@value #METHOD_FLUTTER_SET_SUBSCRIPTIONS} request.
   * <p>
   * <pre>
   * request: {
   *   "id": String
   *   "method": "flutter.setSubscriptions"
   *   "params": {
   *     "subscriptions": Map&gt;FlutterService, List&lt;FilePath&gt;&gt;
{'fraction_non_alphanumeric': 0.08698201720093823, 'fraction_numerical': 0.0023455824863174357, 'mean_word_length': 4.977803738317757, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2883649', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1466, 'n_words': 408}