com.intellij.json.psi.JsonProperty Java Examples

The following examples show how to use com.intellij.json.psi.JsonProperty. 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: ExtraModulesUtil.java    From weex-language-support with MIT License 6 votes vote down vote up
private static String getHomePage(PsiDirectory directory) {
    PsiFile pkg = directory.findFile("package.json");
    if (pkg != null && pkg instanceof JsonFile) {
        if (((JsonFile) pkg).getTopLevelValue() instanceof JsonObject) {
            JsonObject object = (JsonObject) ((JsonFile) pkg).getTopLevelValue();
            if (object != null) {
                JsonProperty homePage = object.findProperty("homepage");
                if (homePage != null && homePage.getValue() != null && homePage.getValue() instanceof JsonStringLiteral) {
                    JsonStringLiteral propValue = (JsonStringLiteral) homePage.getValue();
                    return propValue.getValue();
                }
            }
        }
    }
    return null;
}
 
Example #2
Source File: ExtraModulesUtil.java    From weex-language-support with MIT License 6 votes vote down vote up
private static PsiFile getMain(PsiDirectory moduleRoot) {
    PsiFile pkg = moduleRoot.findFile("package.json");
    if (pkg != null && pkg instanceof JsonFile) {
        if (((JsonFile) pkg).getTopLevelValue() instanceof JsonObject) {
            JsonObject object = (JsonObject) ((JsonFile) pkg).getTopLevelValue();
            if (object != null) {
                JsonProperty property = object.findProperty("main");
                if (property != null && property.getValue() != null && property.getValue() instanceof JsonStringLiteral) {
                    JsonStringLiteral propValue = (JsonStringLiteral) property.getValue();
                    String value = propValue.getValue();
                    PsiFile psiFile = moduleRoot.findFile(value.replace("./", ""));
                    return psiFile;
                }
            }
        }
    }
    return null;
}
 
Example #3
Source File: ExtraModulesUtil.java    From weex-language-support with MIT License 6 votes vote down vote up
public static String getModuleName(PsiDirectory dir) {
    PsiFile pkg = dir.findFile("package.json");
    String name = dir.getName();
    if (pkg != null && pkg instanceof JsonFile) {
        if (((JsonFile) pkg).getTopLevelValue() instanceof JsonObject) {
            JsonObject object = (JsonObject) ((JsonFile) pkg).getTopLevelValue();
            if (object != null) {
                JsonProperty property = object.findProperty("name");
                JsonProperty property1 = object.findProperty("version");
                if (property != null && property.getValue() != null && property.getValue() instanceof JsonStringLiteral) {
                    JsonStringLiteral propValue = (JsonStringLiteral) property.getValue();
                    name = propValue.getValue();
                    if (property1 != null && property1.getValue() != null && property1.getValue() instanceof JsonStringLiteral) {
                        JsonStringLiteral propValue1 = (JsonStringLiteral) property1.getValue();
                        name = name + ":" + propValue1.getValue();
                    }
                }
            }
        }
    }
    return name;
}
 
Example #4
Source File: SpecIndexer.java    From intellij-swagger with MIT License 6 votes vote down vote up
private Set<String> getReferencedFilesJson(final PsiFile file, final VirtualFile specDirectory) {
  final Set<String> result = new HashSet<>();

  file.accept(
      new JsonRecursiveElementVisitor() {
        @Override
        public void visitProperty(@NotNull JsonProperty property) {
          if (ApiConstants.REF_KEY.equals(property.getName()) && property.getValue() != null) {
            final String refValue = StringUtils.removeAllQuotes(property.getValue().getText());

            if (SwaggerFilesUtils.isFileReference(refValue)) {
              getReferencedFileIndexValue(property.getValue(), refValue, specDirectory)
                  .ifPresent(result::add);
            }
          }
          super.visitProperty(property);
        }
      });

  return result;
}
 
Example #5
Source File: GraphQLIntrospectEndpointUrlLineMarkerProvider.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
private boolean isEndpointUrl(JsonProperty jsonProperty, Ref<String> urlRef) {
    if (jsonProperty.getValue() instanceof JsonStringLiteral) {
        final String url = ((JsonStringLiteral) jsonProperty.getValue()).getValue();
        if (isUrlOrVariable(url)) {
            final JsonProperty parentProperty = PsiTreeUtil.getParentOfType(jsonProperty, JsonProperty.class);
            final JsonProperty grandParentProperty = PsiTreeUtil.getParentOfType(parentProperty, JsonProperty.class);
            if ("url".equals(jsonProperty.getName()) && grandParentProperty != null && "endpoints".equals(grandParentProperty.getName())) {
                // "endpoints": {
                //      "<name>": {
                //          "url": "url" <---
                //      }
                // }
                urlRef.set(url);
                return true;
            }
            if (parentProperty != null && "endpoints".equals(parentProperty.getName())) {
                // "endpoints": {
                //      "<name>": "url" <---
                // }
                urlRef.set(url);
                return true;
            }
        }
    }
    return false;
}
 
Example #6
Source File: GraphQLIntrospectEndpointUrlLineMarkerProvider.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
private String getSchemaPath(JsonProperty urlElement, boolean showNotificationOnMissingPath) {
    JsonObject jsonObject = PsiTreeUtil.getParentOfType(urlElement, JsonObject.class);
    while (jsonObject != null) {
        JsonProperty schemaPathElement = jsonObject.findProperty("schemaPath");
        if (schemaPathElement != null) {
            if (schemaPathElement.getValue() instanceof JsonStringLiteral) {
                String schemaPath = ((JsonStringLiteral) schemaPathElement.getValue()).getValue();
                if (schemaPath.trim().isEmpty()) {
                    Notifications.Bus.notify(new Notification("GraphQL", "Unable to perform introspection", "Please set a non-empty 'schemaPath' field", NotificationType.WARNING), urlElement.getProject());
                }
                return schemaPath;
            } else {
                break;
            }
        }
        jsonObject = PsiTreeUtil.getParentOfType(jsonObject, JsonObject.class);
    }
    if (showNotificationOnMissingPath) {
        Notifications.Bus.notify(new Notification("GraphQL", "Unable to perform introspection", "Please set a non-empty 'schemaPath' field. The introspection result will be written to that file.", NotificationType.WARNING), urlElement.getProject());
    }
    return null;
}
 
Example #7
Source File: JsonReferenceInspection.java    From intellij-swagger with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(
    @NotNull final ProblemsHolder holder,
    final boolean isOnTheFly,
    @NotNull final LocalInspectionToolSession session) {
  final PsiFile file = holder.getFile();
  final VirtualFile virtualFile = file.getVirtualFile();
  final Project project = holder.getProject();

  boolean checkRefs =
      indexFacade.isMainSpecFile(virtualFile, project)
          || indexFacade.isPartialSpecFile(virtualFile, project);

  return new JsonElementVisitor() {
    @Override
    public void visitProperty(@NotNull JsonProperty o) {
      if (!checkRefs) {
        return;
      }
      if ("$ref".equals(o.getName())) {
        JsonValue value = o.getValue();

        if (!(value instanceof JsonStringLiteral)) {
          return;
        }

        final String unquotedValue = StringUtil.unquoteString(value.getText());

        if (!unquotedValue.startsWith("http")) {
          doCheck(holder, value, new CreateJsonReferenceIntentionAction(unquotedValue));
        }
      }
      super.visitProperty(o);
    }
  };
}
 
Example #8
Source File: JsonReferenceContributor.java    From intellij-swagger with MIT License 5 votes vote down vote up
private PsiElementPattern.Capture<JsonLiteral> localDefinitionsPattern() {
  return psiElement(JsonLiteral.class)
      .andOr(
          psiElement()
              .withParent(psiElement(JsonProperty.class).withName(SwaggerConstants.REF_KEY)),
          psiElement().withSuperParent(3, psiElement(JsonProperty.class).withName("mapping")))
      .withText(StandardPatterns.string().contains(SwaggerConstants.REFERENCE_PREFIX))
      .withoutText(FILE_NAME_PATTERN)
      .withoutText(URL_PATTERN)
      .withLanguage(JsonLanguage.INSTANCE);
}
 
Example #9
Source File: JsonReferenceContributor.java    From intellij-swagger with MIT License 5 votes vote down vote up
private PsiElementPattern.Capture<JsonLiteral> mappingSchemaNamePattern() {
  return psiElement(JsonLiteral.class)
      .withSuperParent(3, psiElement(JsonProperty.class).withName("mapping"))
      .withoutText(StandardPatterns.string().contains(SwaggerConstants.REFERENCE_PREFIX))
      .withoutText(FILE_NAME_PATTERN)
      .withoutText(URL_PATTERN)
      .withLanguage(JsonLanguage.INSTANCE);
}
 
Example #10
Source File: SpecReferenceSearch.java    From intellij-swagger with MIT License 5 votes vote down vote up
private boolean isSpec(final PsiElement elementToSearch, final Project project) {
  if (elementToSearch instanceof YAMLKeyValue || elementToSearch instanceof JsonProperty) {
    final VirtualFile virtualFile = elementToSearch.getContainingFile().getVirtualFile();

    return indexFacade.isMainSpecFile(virtualFile, project)
        || indexFacade.isPartialSpecFile(virtualFile, project);
  }

  return false;
}
 
Example #11
Source File: GraphQLIntrospectEndpointUrlLineMarkerProvider.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
    if (!GraphQLConfigManager.GRAPHQLCONFIG.equals(element.getContainingFile().getName())) {
        return null;
    }
    if (element instanceof JsonProperty) {
        final JsonProperty jsonProperty = (JsonProperty) element;
        final Ref<String> urlRef = Ref.create();
        if (isEndpointUrl(jsonProperty, urlRef) && !hasErrors(jsonProperty.getContainingFile())) {
            return new LineMarkerInfo<>(jsonProperty, jsonProperty.getTextRange(), AllIcons.RunConfigurations.TestState.Run, Pass.UPDATE_ALL, o -> "Run introspection query to generate GraphQL SDL schema file", (evt, jsonUrl) -> {

                String introspectionUtl;
                if (jsonUrl.getValue() instanceof JsonStringLiteral) {
                    introspectionUtl = ((JsonStringLiteral) jsonUrl.getValue()).getValue();
                } else {
                    return;
                }
                final GraphQLConfigVariableAwareEndpoint endpoint = getEndpoint(introspectionUtl, jsonProperty);
                if (endpoint == null) {
                    return;
                }

                String schemaPath = getSchemaPath(jsonProperty, true);
                if (schemaPath == null || schemaPath.trim().isEmpty()) {
                    return;
                }

                final Project project = element.getProject();
                final VirtualFile introspectionSourceFile = element.getContainingFile().getVirtualFile();

                GraphQLIntrospectionHelper.getService(project).performIntrospectionQueryAndUpdateSchemaPathFile(endpoint, schemaPath, introspectionSourceFile);

            }, GutterIconRenderer.Alignment.CENTER);
        }
    }
    return null;
}
 
Example #12
Source File: GraphQLIntrospectEndpointUrlLineMarkerProvider.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
private GraphQLConfigVariableAwareEndpoint getEndpoint(String url, JsonProperty urlJsonProperty) {
    try {

        // if the endpoint is just the url string, headers are not supported
        final JsonProperty parent = PsiTreeUtil.getParentOfType(urlJsonProperty, JsonProperty.class);
        final boolean supportsHeaders = parent != null && !"endpoints".equals(parent.getName());

        final String name = supportsHeaders ? parent.getName() : url;

        final GraphQLConfigEndpoint endpointConfig = new GraphQLConfigEndpoint(null, name, url);

        if (supportsHeaders) {
            final Stream<JsonProperty> jsonPropertyStream = PsiTreeUtil.getChildrenOfTypeAsList(urlJsonProperty.getParent(), JsonProperty.class).stream();
            final Optional<JsonProperty> headers = jsonPropertyStream.filter(p -> "headers".equals(p.getName())).findFirst();
            headers.ifPresent(headersProp -> {
                final JsonValue jsonValue = headersProp.getValue();
                if (jsonValue != null) {
                    endpointConfig.headers = new Gson().<Map<String, Object>>fromJson(jsonValue.getText(), Map.class);
                }
            });
        }


        return new GraphQLConfigVariableAwareEndpoint(endpointConfig, urlJsonProperty.getProject());

    } catch (Exception e) {
        Notifications.Bus.notify(new Notification("GraphQL", "GraphQL Configuration Error", e.getMessage(), NotificationType.ERROR), urlJsonProperty.getProject());
    }
    return null;
}