Search in sources :

Example 1 with AntlrParser

use of org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser in project sts4 by spring-projects.

the class ValuePropertyReferencesProvider method findReferencesInPropertiesFile.

private List<Location> findReferencesInPropertiesFile(String filePath, String propertyKey) {
    List<Location> foundLocations = new ArrayList<>();
    try {
        String fileContent = FileUtils.readFileToString(new File(filePath));
        Parser parser = new AntlrParser();
        ParseResults parseResults = parser.parse(fileContent);
        if (parseResults != null && parseResults.ast != null) {
            parseResults.ast.getNodes(KeyValuePair.class).forEach(pair -> {
                if (pair.getKey() != null && pair.getKey().decode().equals(propertyKey)) {
                    URI docURI = Paths.get(filePath).toUri();
                    TextDocument doc = new TextDocument(docURI.toString(), null);
                    doc.setText(fileContent);
                    try {
                        int line = doc.getLineOfOffset(pair.getKey().getOffset());
                        int startInLine = pair.getKey().getOffset() - doc.getLineOffset(line);
                        int endInLine = startInLine + (pair.getKey().getLength());
                        Position start = new Position();
                        start.setLine(line);
                        start.setCharacter(startInLine);
                        Position end = new Position();
                        end.setLine(line);
                        end.setCharacter(endInLine);
                        Range range = new Range();
                        range.setStart(start);
                        range.setEnd(end);
                        Location location = new Location(docURI.toString(), range);
                        foundLocations.add(location);
                    } catch (BadLocationException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return foundLocations;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) KeyValuePair(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) URI(java.net.URI) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) AntlrParser(org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser) YamlParser(org.springframework.ide.vscode.commons.yaml.ast.YamlParser) Parser(org.springframework.ide.vscode.java.properties.parser.Parser) AntlrParser(org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) File(java.io.File) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Location(org.eclipse.lsp4j.Location)

Aggregations

File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)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 YamlParser (org.springframework.ide.vscode.commons.yaml.ast.YamlParser)1 AntlrParser (org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser)1 ParseResults (org.springframework.ide.vscode.java.properties.parser.ParseResults)1 Parser (org.springframework.ide.vscode.java.properties.parser.Parser)1 KeyValuePair (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair)1