com.intellij.json.psi.JsonStringLiteral Java Examples

The following examples show how to use com.intellij.json.psi.JsonStringLiteral. 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: 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 #5
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 #6
Source File: PathFinder.java    From intellij-swagger with MIT License 5 votes vote down vote up
public List<? extends PsiNamedElement> findNamedChildren(
    final String path, final PsiElement psiElement) {
  Predicate<PsiElement> childFilter =
      child -> child instanceof NavigatablePsiElement && !(child instanceof JsonStringLiteral);

  return findChildrenByPathFrom(new PathExpression(path), psiElement, childFilter);
}
 
Example #7
Source File: PathFinder.java    From intellij-swagger with MIT License 5 votes vote down vote up
public List<? extends PsiNamedElement> findDirectNamedChildren(
    final String path, final PsiElement psiElement) {
  Predicate<PsiElement> childFilter =
      child ->
          child instanceof NavigatablePsiElement
              && !(child instanceof JsonStringLiteral)
              && !(child instanceof YAMLSequence)
              && !(child instanceof JsonArray);

  return findChildrenByPathFrom(new PathExpression(path), psiElement, childFilter);
}
 
Example #8
Source File: PathFinder.java    From intellij-swagger with MIT License 5 votes vote down vote up
private Optional<? extends PsiElement> getChildByName(
    final PsiElement psiElement, final String name, Predicate<PsiElement> childFilter) {
  if (ROOT_PATH.equals(name)) {
    return Optional.of(psiElement);
  }

  List<PsiNamedElement> children =
      Arrays.stream(psiElement.getChildren())
          .filter(child -> child instanceof PsiNamedElement)
          .map(child -> (PsiNamedElement) child)
          .collect(Collectors.toList());

  if (children.isEmpty()) {
    Optional<PsiElement> navigatablePsiElement =
        Arrays.stream(psiElement.getChildren())
            .filter(child -> child instanceof NavigatablePsiElement)
            .filter(child -> !(child instanceof JsonStringLiteral))
            .findFirst();

    return navigatablePsiElement.isPresent()
        ? getChildByName(navigatablePsiElement.get(), name, childFilter)
        : Optional.empty();
  }

  final String unescapedName = unescape(name);

  return children.stream().filter(child -> unescapedName.equals(child.getName())).findFirst();
}
 
Example #9
Source File: PathFinder.java    From intellij-swagger with MIT License 5 votes vote down vote up
private PsiElement getNextObjectParent(final PsiElement psiElement) {
  if (psiElement == null) {
    return null;
  }

  if (psiElement instanceof JsonObject
      || (psiElement instanceof YAMLKeyValue || psiElement instanceof YAMLMapping)
          && !(psiElement instanceof JsonStringLiteral)) {
    return psiElement;
  }

  return getNextObjectParent(psiElement.getParent());
}
 
Example #10
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 #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: PathFinder.java    From intellij-swagger with MIT License 4 votes vote down vote up
public Optional<PsiElement> findByPathFrom(final String path, final PsiElement psiElement) {
  Predicate<PsiElement> childFilter =
      child -> child instanceof NavigatablePsiElement && !(child instanceof JsonStringLiteral);

  return findByPathFrom(new PathExpression(path), psiElement, childFilter);
}
 
Example #13
Source File: GraphQLPsiSearchHelper.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
/**
 * Processes GraphQL identifiers whose name matches the specified word within the given schema scope.
 * @param schemaScope the schema scope which limits the processing
 * @param word        the word to match identifiers for
 * @param processor   processor called for all GraphQL identifiers whose name match the specified word
 * @see GraphQLIdentifierIndex
 */
private void processElementsWithWordUsingIdentifierIndex(GlobalSearchScope schemaScope, String word, Processor<PsiNamedElement> processor) {
    FileBasedIndex.getInstance().getFilesWithKey(GraphQLIdentifierIndex.NAME, Collections.singleton(word), virtualFile -> {
        final PsiFile psiFile = psiManager.findFile(virtualFile);
        final Ref<Boolean> continueProcessing = Ref.create(true);
        if (psiFile != null) {
            final Set<GraphQLFile> introspectionFiles = Sets.newHashSetWithExpectedSize(1);
            final Ref<PsiRecursiveElementVisitor> identifierVisitor = Ref.create();
            identifierVisitor.set(new PsiRecursiveElementVisitor() {
                @Override
                public void visitElement(PsiElement element) {
                    if (!continueProcessing.get()) {
                        return; // done visiting as the processor returned false
                    }
                    if (element instanceof PsiNamedElement) {
                        final String name = ((PsiNamedElement) element).getName();
                        if (word.equals(name)) {
                            // found an element with a name that matches
                            continueProcessing.set(processor.process((PsiNamedElement) element));
                        }
                        if (!continueProcessing.get()) {
                            return; // no need to visit other elements
                        }
                    } else if (element instanceof JsonStringLiteral) {
                        final GraphQLFile graphQLFile = element.getContainingFile().getUserData(GraphQLSchemaKeys.GRAPHQL_INTROSPECTION_JSON_TO_SDL);
                        if (graphQLFile != null && introspectionFiles.add(graphQLFile)) {
                            // index the associated introspection SDL from a JSON introspection result file
                            graphQLFile.accept(identifierVisitor.get());
                        }
                        return; // no need to visit deeper
                    } else if (element instanceof PsiLanguageInjectionHost) {
                        if (visitLanguageInjectionHost((PsiLanguageInjectionHost) element, identifierVisitor)) {
                            return;
                        }
                    }
                    super.visitElement(element);
                }
            });

            psiFile.accept(identifierVisitor.get());
        }
        return continueProcessing.get();
    }, schemaScope);
}