Search in sources :

Example 1 with YamlParser

use of org.springframework.ide.vscode.commons.yaml.ast.YamlParser 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)

Aggregations

File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)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 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)1 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)1 YamlASTProvider (org.springframework.ide.vscode.commons.yaml.ast.YamlASTProvider)1 YamlFileAST (org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST)1 YamlParser (org.springframework.ide.vscode.commons.yaml.ast.YamlParser)1 Yaml (org.yaml.snakeyaml.Yaml)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1 Node (org.yaml.snakeyaml.nodes.Node)1