Search in sources :

Example 1 with YamlFileAST

use of org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST in project sts4 by spring-projects.

the class BoshDeploymentManifestSchema method getCurrentStemcell.

private StemcellModel getCurrentStemcell(DynamicSchemaContext dc) throws Exception {
    YamlPath path = dc.getPath();
    YamlFileAST ast = asts.getAst(dc.getDocument(), true);
    return new StemcellModel(path.dropLast().traverseToNode(ast));
}
Also used : YamlPath(org.springframework.ide.vscode.commons.yaml.path.YamlPath) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST) StemcellModel(org.springframework.ide.vscode.bosh.models.StemcellModel)

Example 2 with YamlFileAST

use of org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST in project sts4 by spring-projects.

the class BoshCommandBasedModelProvider method parseYaml.

protected YamlFileAST parseYaml(String block) throws Exception {
    TextDocument doc = new TextDocument(null, LanguageId.BOSH_CLOUD_CONFIG);
    doc.setText(block);
    YamlFileAST ast = yamlParser.getAST(doc);
    return ast;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST)

Example 3 with YamlFileAST

use of org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST in project sts4 by spring-projects.

the class YamlAstTest method assertPath.

protected void assertPath(MockYamlEditor input, String nodeText, String expected) throws Exception {
    YamlFileAST ast = input.parse();
    String path = pathString(ast.findPath(input.middleOf(nodeText)));
    assertEquals(expected, path);
}
Also used : YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST)

Example 4 with YamlFileAST

use of org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST in project sts4 by spring-projects.

the class ValuePropertyReferencesProvider method findReferencesInYMLFile.

private List<Location> findReferencesInYMLFile(String filePath, String propertyKey) {
    List<Location> foundLocations = new ArrayList<>();
    try {
        String fileContent = FileUtils.readFileToString(new File(filePath));
        Yaml yaml = new Yaml();
        YamlASTProvider parser = new YamlParser(yaml);
        URI docURI = Paths.get(filePath).toUri();
        TextDocument doc = new TextDocument(docURI.toString(), null);
        doc.setText(fileContent);
        YamlFileAST ast = parser.getAST(doc);
        List<Node> nodes = ast.getNodes();
        if (nodes != null && !nodes.isEmpty()) {
            for (Node node : nodes) {
                Node foundNode = findNode(node, "", propertyKey);
                if (foundNode != null) {
                    Position start = new Position();
                    start.setLine(foundNode.getStartMark().getLine());
                    start.setCharacter(foundNode.getStartMark().getColumn());
                    Position end = new Position();
                    end.setLine(foundNode.getEndMark().getLine());
                    end.setCharacter(foundNode.getEndMark().getColumn());
                    Range range = new Range();
                    range.setStart(start);
                    range.setEnd(end);
                    Location location = new Location(docURI.toString(), range);
                    foundLocations.add(location);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return foundLocations;
}
Also used : YamlParser(org.springframework.ide.vscode.commons.yaml.ast.YamlParser) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST) Position(org.eclipse.lsp4j.Position) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) YamlASTProvider(org.springframework.ide.vscode.commons.yaml.ast.YamlASTProvider) Range(org.eclipse.lsp4j.Range) URI(java.net.URI) Yaml(org.yaml.snakeyaml.Yaml) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) File(java.io.File) Location(org.eclipse.lsp4j.Location)

Example 5 with YamlFileAST

use of org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST in project sts4 by spring-projects.

the class YamlHoverInfoProvider method getHoverInfo.

@Override
public Tuple2<Renderable, IRegion> getHoverInfo(IDocument doc, int offset) throws Exception {
    YamlFileAST ast = getAst(doc);
    if (ast != null) {
        IRegion region = getHoverRegion(ast, offset);
        if (region != null) {
            YamlDocument ymlDoc = new YamlDocument(doc, structureProvider);
            YamlAssistContext assistContext = assistContextProvider.getGlobalAssistContext(ymlDoc);
            if (assistContext != null) {
                List<NodeRef<?>> astPath = ast.findPath(offset);
                final YamlPath path = YamlPath.fromASTPath(astPath);
                if (path != null) {
                    YamlPath assistPath = path;
                    if (assistPath.pointsAtKey()) {
                        // When a path points at a key we must tramsform it to a
                        // 'value-terminating path'
                        // to be able to reuse the 'getHoverInfo' method on
                        // YamlAssistContext (as navigation
                        // into 'key' is not defined for YamlAssistContext.
                        String key = path.getLastSegment().toPropString();
                        assistPath = path.dropLast().append(YamlPathSegment.valueAt(key));
                    }
                    assistContext = assistPath.traverse(assistContext);
                    if (assistContext != null) {
                        Renderable info = path.pointsAtValue() ? assistContext.getValueHoverInfo(ymlDoc, new DocumentRegion(doc, region)) : assistContext.getHoverInfo();
                        // Fix for: PT 134914895. If assist context cannot provide an info, then don't return a Tuple.
                        if (info != null) {
                            return Tuples.of(info, region);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : NodeRef(org.springframework.ide.vscode.commons.yaml.ast.NodeRef) YamlPath(org.springframework.ide.vscode.commons.yaml.path.YamlPath) Renderable(org.springframework.ide.vscode.commons.util.Renderable) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST) YamlDocument(org.springframework.ide.vscode.commons.yaml.structure.YamlDocument) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) YamlAssistContext(org.springframework.ide.vscode.commons.yaml.completion.YamlAssistContext) IRegion(org.springframework.ide.vscode.commons.util.text.IRegion)

Aggregations

YamlFileAST (org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST)10 YamlPath (org.springframework.ide.vscode.commons.yaml.path.YamlPath)5 Node (org.yaml.snakeyaml.nodes.Node)3 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)2 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)2 File (java.io.File)1 URI (java.net.URI)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Stream (java.util.stream.Stream)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 Location (org.eclipse.lsp4j.Location)1 Position (org.eclipse.lsp4j.Position)1 Range (org.eclipse.lsp4j.Range)1 BoshCliConfig (org.springframework.ide.vscode.bosh.BoshCliConfig)1 StemcellModel (org.springframework.ide.vscode.bosh.models.StemcellModel)1 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)1 CollectorUtil (org.springframework.ide.vscode.commons.util.CollectorUtil)1 ExternalCommand (org.springframework.ide.vscode.commons.util.ExternalCommand)1