org.jetbrains.yaml.psi.YAMLDocument Java Examples

The following examples show how to use org.jetbrains.yaml.psi.YAMLDocument. 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: YamlCompletionContributor.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
public YamlCompletionContributor() {

        extend(
            CompletionType.BASIC, PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement(YAMLDocument.class)).inFile(
            PlatformPatterns.psiFile().withName(PlatformPatterns.string().endsWith(".info.yml"))
        ),
            new CompletionProvider<CompletionParameters>() {
                @Override
                protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {

                    if(!DrupalProjectComponent.isEnabled(completionParameters.getOriginalPosition())) {
                        return;
                    }

                    for(String key: MODULE_KEYS) {
                        completionResultSet.addElement(LookupElementBuilder.create(key).withIcon(DrupalIcons.DRUPAL));
                    }

                }
            }
        );
    }
 
Example #2
Source File: DuplicateLocalRouteInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void visitFile(PsiFile file) {
    // @TODO: detection of routing files in right way
    // routing.yml
    // comment.routing.yml
    // routing/foo.yml
    if(!YamlHelper.isRoutingFile(file)) {
        return;
    }

    YAMLDocument document = PsiTreeUtil.findChildOfType(file, YAMLDocument.class);
    if(document == null) {
        return;
    }

    YAMLValue topLevelValue = document.getTopLevelValue();
    if(topLevelValue != null) {
        YamlHelper.attachDuplicateKeyInspection(topLevelValue, holder);
    }
}
 
Example #3
Source File: RouteHelperTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper#getYamlRouteDefinitions
 */
public void testGetYamlRouteDefinitionsWithMethods() {
    Collection<StubIndexedRoute> yamlRouteDefinitions = RouteHelper.getYamlRouteDefinitions(YamlPsiElementFactory.createFromText(getProject(), YAMLDocument.class,
        "route1:\n" +
        "    path: /foo\n" +
        "    methods: [GET]\n" +
        "route2:\n" +
        "    path: /foo\n" +
        "    methods: GET\n" +
        "route3:\n" +
        "    path: /foo\n" +
        "    methods: [GET,   POST]\n"
    ));

    assertContainsElements(Collections.singletonList("get"), ContainerUtil.find(yamlRouteDefinitions, new MyEqualStubIndexedRouteCondition("route1")).getMethods());
    assertContainsElements(Collections.singletonList("get"), ContainerUtil.find(yamlRouteDefinitions, new MyEqualStubIndexedRouteCondition("route2")).getMethods());
    assertContainsElements(Arrays.asList("get", "post"), ContainerUtil.find(yamlRouteDefinitions, new MyEqualStubIndexedRouteCondition("route3")).getMethods());
}
 
Example #4
Source File: RamlFile.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
public List<YAMLDocument> getDocuments()
{
    ArrayList<YAMLDocument> result = new ArrayList<>();
    ASTNode[] var2 = this.getNode().getChildren(TokenSet.create(YAMLElementTypes.DOCUMENT));
    for (ASTNode node : var2)
    {
        result.add((YAMLDocument) node.getPsi());
    }

    return result;
}
 
Example #5
Source File: PrototypeLineMarkerProvider.java    From intellij-neos with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
    for (PsiElement el : elements) {
        if (!NeosProjectService.isEnabled(el)) {
            return;
        }

        if (!el.getContainingFile().getVirtualFile().getName().startsWith("NodeTypes.")) {
            continue;
        }

        if (!(el instanceof YAMLKeyValue)) {
            continue;
        }

        YAMLMapping parentMapping = ((YAMLKeyValue) el).getParentMapping();
        if (parentMapping != null && parentMapping.getParent() instanceof YAMLDocument) {
            String nodeType = ((YAMLKeyValue) el).getKeyText();
            String[] nodeTypeSplit = nodeType.split(":");

            if (nodeTypeSplit.length < 2) {
                continue;
            }

            List<PsiElement> targets = ResolveEngine.getPrototypeDefinitions(el.getProject(), nodeTypeSplit[1], nodeTypeSplit[0]);
            if (!targets.isEmpty()) {
                RelatedItemLineMarkerInfo info = NavigationGutterIconBuilder
                        .create(FusionIcons.PROTOTYPE)
                        .setTargets(targets)
                        .setTooltipText("Go to Fusion prototype")
                        .createLineMarkerInfo(el);
                result.add(info);
            }
        }
    }
}
 
Example #6
Source File: YamlSchemaInspection.java    From intellij-swagger with MIT License 5 votes vote down vote up
private PsiElementVisitor createVisitor(
    final String schemaFileName,
    final ProblemsHolder holder,
    final LocalInspectionToolSession session,
    final PsiFile file) {
  List<YAMLDocument> documents = ((YAMLFile) file).getDocuments();
  if (documents.size() != 1) return PsiElementVisitor.EMPTY_VISITOR;

  PsiElement root = documents.get(0).getTopLevelValue();
  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 YamlPsiElementVisitor() {
    @Override
    public void visitElement(PsiElement element) {
      if (element != root) return;
      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 #7
Source File: KubernetesYamlPsiUtil.java    From intellij-kubernetes with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the top-level mapping in the document, if present.
 *
 * @param element an element within the document.
 * @return the top-level mapping, or {@code null} if one is not defined (e.g. in an empty document).
 */
@Nullable
public static YAMLMapping getTopLevelMapping(final PsiElement element) {
    final YAMLDocument document = PsiTreeUtil.getParentOfType(element, YAMLDocument.class);
    if (document != null) {
        final YAMLValue topLevelValue = document.getTopLevelValue();
        if (topLevelValue instanceof YAMLMapping) {
            return (YAMLMapping) topLevelValue;
        }
    }
    return null;
}
 
Example #8
Source File: Unity3dMetaIndexExtension.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
public static String findGUIDFromFile(@Nonnull YAMLFile psiFile)
{
	String guid = null;
	List<YAMLDocument> documents = psiFile.getDocuments();
	for(YAMLDocument document : documents)
	{
		YAMLValue topLevelValue = document.getTopLevelValue();
		if(topLevelValue instanceof YAMLMapping)
		{
			YAMLKeyValue guidValue = ((YAMLMapping) topLevelValue).getKeyValueByKey(Unity3dMetaManager.GUID_KEY);
			guid = guidValue == null ? null : guidValue.getValueText();
		}
	}
	return guid;
}
 
Example #9
Source File: ConfigCompletionGoto.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(PsiElement psiElement) {

    PsiElement element = psiElement.getParent();
    if(!(element instanceof StringLiteralExpression)) {
        return Collections.emptyList();
    }

    final String contents = ((StringLiteralExpression) element).getContents();
    if(StringUtils.isBlank(contents)) {
        return Collections.emptyList();
    }

    final Collection<PsiElement> psiElements = new ArrayList<>();
    FileBasedIndex.getInstance().getFilesWithKey(ConfigSchemaIndex.KEY, new HashSet<>(Collections.singletonList(contents)), virtualFile -> {

        PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(virtualFile);
        if(psiFile == null) {
            return true;
        }

        YAMLDocument yamlDocument = PsiTreeUtil.getChildOfType(psiFile, YAMLDocument.class);
        if(yamlDocument == null) {
            return true;
        }

        for(YAMLKeyValue yamlKeyValue: PsiTreeUtil.getChildrenOfTypeAsList(yamlDocument, YAMLKeyValue.class)) {
            String keyText = PsiElementUtils.trimQuote(yamlKeyValue.getKeyText());
            if(StringUtils.isNotBlank(keyText) && keyText.equals(contents)) {
                psiElements.add(yamlKeyValue);
            }
        }

        return true;
    }, GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.allScope(getProject()), YAMLFileType.YML));

    return psiElements;
}
 
Example #10
Source File: RouteHelperTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper#getYamlRouteDefinitions
 */
public void testGetYamlRouteDefinitionsForControllerKeyword() {
    Collection<StubIndexedRoute> yamlRouteDefinitions = RouteHelper.getYamlRouteDefinitions(YamlPsiElementFactory.createFromText(getProject(), YAMLDocument.class,
        "foo_keyword:\n" +
            "   path: /foo\n" +
            "   controller: 'AppBundle:Blog:list'\n"
    ));

    StubIndexedRoute route = ContainerUtil.find(yamlRouteDefinitions, new MyEqualStubIndexedRouteCondition("foo_keyword"));
    assertNotNull(route);

    assertContainsElements(Collections.singletonList("AppBundle:Blog:list"), route.getController());
}
 
Example #11
Source File: ConfigCompletionProvider.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {

    PsiElement element = completionParameters.getPosition();
    if(!Symfony2ProjectComponent.isEnabled(element)) {
        return;
    }

    PsiElement yamlScalar = element.getParent();
    if(yamlScalar == null) {
        return;
    }

    PsiElement yamlCompount = yamlScalar.getParent();

    // yaml document root context
    if(yamlCompount.getParent() instanceof YAMLDocument) {
        attachRootConfig(completionResultSet, element);
        return;
    }

    // check inside yaml key value context
    if(!(yamlCompount instanceof YAMLCompoundValue || yamlCompount instanceof YAMLKeyValue)) {
        return;
    }

    // get all parent yaml keys
    List<String> items = YamlHelper.getParentArrayKeys(element);
    if(items.size() == 0) {
        return;
    }

    // normalize for xml
    items = ContainerUtil.map(items, s -> s.replace('_', '-'));

    // reverse to get top most item first
    Collections.reverse(items);

    Document document = getConfigTemplate(ProjectUtil.getProjectDir(element));
    if(document == null) {
        return;
    }

    Node configNode = getMatchingConfigNode(document, items);
    if(configNode == null) {
        return;
    }

    getConfigPathLookupElements(completionResultSet, configNode, false);

    // map shortcuts like eg <dbal default-connection="">
    if(configNode instanceof Element) {
        NamedNodeMap attributes = configNode.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            String attributeName = attributes.item(i).getNodeName();
            if(attributeName.startsWith("default-")) {
                Node defaultNode = getElementByTagNameWithUnPluralize((Element) configNode, attributeName.substring("default-".length()));
                if(defaultNode != null) {
                    getConfigPathLookupElements(completionResultSet, defaultNode, true);
                }
            }
        }
    }

}
 
Example #12
Source File: RouteHelperTest.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper#getYamlRouteDefinitions
 */
public void testGetYamlRouteDefinitionsAsHashAndKeyValue() {
    Collection<String[]> providers = new ArrayList<String[]>() {{
        add(new String[] {"'MyController::fooAction'", "MyController::fooAction"});
        add(new String[] {"MyController::fooAction", "MyController::fooAction"});
        add(new String[] {"\"MyController::fooAction\"", "MyController::fooAction"});
    }};

    for (String[] provider : providers) {
        Collection<YAMLDocument> yamlDocuments = new ArrayList<YAMLDocument>();

        yamlDocuments.add(YamlPsiElementFactory.createFromText(getProject(), YAMLDocument.class, String.format(
                "route1:\n" +
                "    path: /foo\n" +
                "    defaults: { _controller: %s }",
            provider[1]
        )));

        yamlDocuments.add(YamlPsiElementFactory.createFromText(getProject(), YAMLDocument.class, String.format(
            "route1:\n" +
                "    path: /foo\n" +
                "    defaults:\n" +
                "       _controller: %s",
            provider[1]
        )));

        yamlDocuments.add(YamlPsiElementFactory.createFromText(getProject(), YAMLDocument.class, String.format(
            "route1:\n" +
                "    pattern: /foo\n" +
                "    defaults:\n" +
                "       _controller: %s",
            provider[1]
        )));

        for (YAMLDocument yamlDocument : yamlDocuments) {
            StubIndexedRoute route1 = ContainerUtil.find(RouteHelper.getYamlRouteDefinitions(yamlDocument), new MyEqualStubIndexedRouteCondition("route1"));
            assertNotNull(route1);

            assertEquals(provider[1], route1.getController());
            assertEquals("route1", route1.getName());
            assertEquals("/foo", route1.getPath());
        }
    }
}
 
Example #13
Source File: KubernetesYamlCompletionContributor.java    From intellij-kubernetes with Apache License 2.0 2 votes vote down vote up
/**
 * Gets whether a given {@link YAMLKeyValue} is at the root level of the document.
 *
 * @param keyValue the element to evaluate.
 * @return {@code true} if this is a top-level mapping; otherwise, {@code false}.
 */
private static boolean isTopLevelMapping(final YAMLKeyValue keyValue) {
    return keyValue.getParentMapping() != null && keyValue.getParentMapping().getParent() instanceof YAMLDocument;
}