Java Code Examples for com.intellij.json.psi.JsonFile

The following examples show how to use com.intellij.json.psi.JsonFile. These examples are extracted from open source projects. 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 Project: weex-language-support   Source File: ExtraModulesUtil.java    License: 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 Project: weex-language-support   Source File: ExtraModulesUtil.java    License: 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 Project: weex-language-support   Source File: ExtraModulesUtil.java    License: 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
@Override
public void fillProperties(@NotNull PsiDirectory directory, @NotNull Properties props) {
    JsonFile composerManifest = ComposerUtil.getComposerManifest(directory);
    if (composerManifest == null) {
        return;
    }

    ArrayList<PsiDirectory> directories = NeosUtil.getParentFusionDirectories(directory);
    ArrayList<String> directoryNames = new ArrayList<>();

    for (PsiDirectory dir: directories) {
        directoryNames.add(dir.getName());
    }

    Collections.reverse(directoryNames);
    String fusionPrototypePath = String.join(".", directoryNames);

    PsiDirectory packageDir = composerManifest.getContainingDirectory();

    props.setProperty(FUSION_PROTOTYPE_PATH, fusionPrototypePath);
    props.setProperty(NEOS_PACKAGE_NAME, packageDir.getName());
}
 
Example 5
Source Project: intellij-swagger   Source File: SpecIconProvider.java    License: MIT License 6 votes vote down vote up
@Nullable
@Override
public Icon getIcon(@NotNull PsiElement element, int flags) {
  if (element instanceof YAMLFile || element instanceof JsonFile) {

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

    if (indexService.isMainSpecFile(virtualFile, project)
        || indexService.isPartialSpecFile(virtualFile, project)) {
      return getIcon();
    }
  }

  return null;
}
 
Example 6
public static void openSourceLocation(Project myProject, SourceLocation location, boolean resolveSDLFromJSON) {
    VirtualFile sourceFile = StandardFileSystems.local().findFileByPath(location.getSourceName());
    if (sourceFile != null) {
        PsiFile file = PsiManager.getInstance(myProject).findFile(sourceFile);
        if (file != null) {
            if (file instanceof JsonFile && resolveSDLFromJSON) {
                GraphQLFile graphQLFile = file.getUserData(GraphQLSchemaKeys.GRAPHQL_INTROSPECTION_JSON_TO_SDL);
                if (graphQLFile != null) {
                    // open the SDL file and not the JSON introspection file it was based on
                    file = graphQLFile;
                    sourceFile = file.getVirtualFile();
                }
            }
            new OpenFileDescriptor(myProject, sourceFile, location.getLine() - 1, location.getColumn() - 1).navigate(true);
        }
    }
}
 
Example 7
Source Project: intellij-neos   Source File: ComposerUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Nullable
public static JsonFile getComposerManifest(PsiDirectory currentDirectory) {
    if (currentDirectory == null) {
        return null;
    }

    JsonFile composerFile;
    do {
        composerFile = getComposerManifestInDirectory(currentDirectory);
        currentDirectory = currentDirectory.getParentDirectory();
    } while (composerFile == null && currentDirectory != null && !currentDirectory.equals(guessProjectDir(currentDirectory.getProject())));

    return composerFile;
}
 
Example 8
Source Project: intellij-neos   Source File: ComposerUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Nullable
public static JsonFile getComposerManifestInDirectory(@NotNull PsiDirectory packageDirContext) {
    for (PsiFile psiFile : packageDirContext.getFiles()) {
        if (psiFile instanceof JsonFile && psiFile.getName().equals("composer.json")) {
            return (JsonFile) psiFile;
        }
    }

    return null;
}
 
Example 9
Source Project: intellij-swagger   Source File: JsonSchemaInspection.java    License: MIT License 5 votes vote down vote up
private PsiElementVisitor createVisitor(
    final String schemaFileName,
    final ProblemsHolder holder,
    final LocalInspectionToolSession session,
    final PsiFile file) {

  JsonValue root =
      file instanceof JsonFile
          ? ObjectUtils.tryCast(file.getFirstChild(), JsonValue.class)
          : null;
  if (root == null) return PsiElementVisitor.EMPTY_VISITOR;

  JsonSchemaService service = JsonSchemaService.Impl.get(file.getProject());

  final URL url = ResourceUtil.getResource(getClass(), "schemas", schemaFileName);
  final VirtualFile virtualFile = VfsUtil.findFileByURL(url);

  final JsonSchemaObject schema = service.getSchemaObjectForSchemaFile(virtualFile);

  return new JsonElementVisitor() {
    @Override
    public void visitElement(PsiElement element) {
      if (element == root) {
        final JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(element, schema);
        if (walker == null) return;

        JsonComplianceCheckerOptions options = new JsonComplianceCheckerOptions(false);

        new JsonSchemaComplianceChecker(schema, holder, walker, session, options)
            .annotate(element);
      }
    }
  };
}
 
Example 10
@Override
public boolean shouldHighlightErrorElement(@NotNull PsiErrorElement element) {
    final PsiFile file = element.getContainingFile();
    if(file instanceof JsonFile) {
        if(Boolean.TRUE.equals(file.getVirtualFile().getUserData(JSGraphQLLanguageUIProjectService.IS_GRAPH_QL_VARIABLES_VIRTUAL_FILE))) {
            // this is the variables file for GraphQL, so ignore errors as long as it's empty
            return !file.getText().isEmpty();
        }
    }
    return true;
}
 
Example 11
Source Project: reasonml-idea-plugin   Source File: IconProvider.java    License: MIT License 4 votes vote down vote up
private boolean isEsyPackageJson(PsiFile element) {
    return element instanceof JsonFile && EsyPackageJson.isEsyPackageJson(element.getVirtualFile());
}
 
Example 12
Source Project: weex-language-support   Source File: ExtraModulesUtil.java    License: MIT License 4 votes vote down vote up
public static boolean isNodeModule(PsiDirectory dir) {
    PsiFile pkg = dir.findFile("package.json");
    return pkg != null && pkg instanceof JsonFile;
}
 
Example 13
Source Project: intellij-neos   Source File: ResolveEngine.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Find template for controller action
 *
 * @param method Controller action method
 * @return VirtualFile
 */
public static VirtualFile findTemplate(Method method) {
    PsiFile file = method.getContainingFile();
    if (method.getContainingClass() != null) {
        String actionName = method.getName();
        if (!actionName.endsWith("Action")) {
            return null;
        }

        actionName = actionName.replace("Action", "");
        if (actionName.length() < 2) {
            return null;
        }

        actionName = actionName.substring(0, 1).toUpperCase() + actionName.substring(1);

        String controllerName = method.getContainingClass().getName();
        controllerName = controllerName.replace("Controller", "");

        JsonFile composerFile = ComposerUtil.getComposerManifest(file.getContainingDirectory());
        if (composerFile != null) {
            String namespace = method.getNamespaceName();
            namespace = namespace.substring(1);

            Map<String, String> namespaceMappings = ComposerUtil.getNamespaceMappings(composerFile);
            for(String key : namespaceMappings.keySet()) {
                if (namespace.startsWith(key)) {
                    namespace = namespace.replace(key, "")
                        .replace("\\", "/");

                    if (namespace.startsWith("/") && namespace.length() > 1) {
                        namespace = namespace.substring(1);
                    }

                    namespace = namespace.replace("Controller/", "");
                    break;
                }
            }

            String resourceFile = "Resources/Private/Templates/" + namespace + controllerName + "/" + actionName + ".html";
            return composerFile.getContainingDirectory().getVirtualFile().findFileByRelativePath(resourceFile);
        }
    }

    return null;
}
 
Example 14
Source Project: intellij-swagger   Source File: SpecIndexer.java    License: MIT License 4 votes vote down vote up
private Set<String> getReferencedFiles(final PsiFile file, final VirtualFile specDirectory) {
  return file instanceof JsonFile
      ? getReferencedFilesJson(file, specDirectory)
      : getReferencedFilesYaml(file, specDirectory);
}
 
Example 15
public GraphQLSchemaChangeListener(Project project) {
    myProject = project;
    psiManager = PsiManager.getInstance(myProject);
    listener = new PsiTreeChangeAdapter() {

        private void checkForSchemaChange(PsiTreeChangeEvent event) {
            if (myProject.isDisposed()) {
                psiManager.removePsiTreeChangeListener(listener);
                return;
            }
            if (event.getFile() instanceof GraphQLFile) {
                if (affectsGraphQLSchema(event)) {
                    signalSchemaChanged();
                }
            }
            if (event.getFile() instanceof JSGraphQLEndpointFile) {
                // always consider the schema changed when editing an endpoint file
                signalSchemaChanged();
            }
            if (event.getParent() instanceof PsiLanguageInjectionHost) {
                GraphQLInjectionSearchHelper graphQLInjectionSearchHelper = ServiceManager.getService(GraphQLInjectionSearchHelper.class);
                if (graphQLInjectionSearchHelper != null && graphQLInjectionSearchHelper.isJSGraphQLLanguageInjectionTarget(event.getParent())) {
                    // change in injection target
                    signalSchemaChanged();
                }
            }
            if (event.getFile() instanceof JsonFile) {
                boolean introspectionJsonUpdated = false;
                if (event.getFile().getUserData(GraphQLSchemaKeys.GRAPHQL_INTROSPECTION_JSON_TO_SDL) != null) {
                    introspectionJsonUpdated = true;
                } else {
                    final VirtualFile virtualFile = event.getFile().getVirtualFile();
                    if (virtualFile != null && Boolean.TRUE.equals(virtualFile.getUserData(GraphQLSchemaKeys.IS_GRAPHQL_INTROSPECTION_JSON))) {
                        introspectionJsonUpdated = true;
                    }
                }
                if(introspectionJsonUpdated) {
                    signalSchemaChanged();
                }
            }
        }

        @Override
        public void propertyChanged(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childAdded(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childRemoved(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childMoved(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childReplaced(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
            if (event instanceof PsiTreeChangeEventImpl) {
                if (!((PsiTreeChangeEventImpl) event).isGenericChange()) {
                    // ignore the generic event which fires for all other cases above
                    // if it's not the generic case, children have been replaced, e.g. using the commenter
                    checkForSchemaChange(event);
                }
            }
        }
    };
    psiManager.addPsiTreeChangeListener(listener);

    // also consider the schema changed when the underlying schema configuration files change
    final MessageBusConnection connection = myProject.getMessageBus().connect();
    connection.subscribe(GraphQLConfigManager.TOPIC, this::signalSchemaChanged);
}