Search in sources :

Example 11 with ParseResults

use of org.springframework.ide.vscode.java.properties.parser.ParseResults 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)

Example 12 with ParseResults

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

the class PropertiesHoverCalculator method calculate.

Tuple2<Renderable, IRegion> calculate() {
    ParseResults parseResults = parser.parse(doc.get());
    Node node = parseResults.ast.findNode(offset);
    if (node instanceof Value) {
        return getValueHover((Value) node);
    } else if (node instanceof Key) {
        return getPropertyHover((Key) node);
    }
    return null;
}
Also used : Node(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Node) Value(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Value) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Key(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Key)

Example 13 with ParseResults

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

the class PropertiesCompletionProposalsCalculator method calculate.

/**
 * Create completions proposals in the context of a properties text editor.
 */
public Collection<ICompletionProposal> calculate() throws BadLocationException {
    ParseResults parseResults = parser.parse(doc.get());
    Node node = parseResults.ast.findNode(offset);
    if (node instanceof Value) {
        return getValueCompletions((Value) node);
    } else if (node instanceof Key || node instanceof EmptyLine || node == null) {
        return getPropertyCompletions();
    }
    return Collections.emptyList();
}
Also used : Node(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Node) Value(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Value) EmptyLine(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.EmptyLine) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Key(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Key)

Example 14 with ParseResults

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

the class SpringPropertiesReconcileEngine method reconcile.

@Override
public void reconcile(IDocument doc, IProblemCollector problemCollector) {
    FuzzyMap<PropertyInfo> index = fIndexProvider.getIndex(doc);
    problemCollector.beginCollecting();
    try {
        ParseResults results = parser.parse(doc.get());
        DuplicateNameChecker duplicateNameChecker = new DuplicateNameChecker(problemCollector);
        results.syntaxErrors.forEach(syntaxError -> {
            problemCollector.accept(problem(PROP_SYNTAX_ERROR, syntaxError.getMessage(), syntaxError.getOffset(), syntaxError.getLength()));
        });
        if (index == null || index.isEmpty()) {
            // some problem putting information about properties into the index.
            return;
        }
        results.ast.getNodes(KeyValuePair.class).forEach(pair -> {
            try {
                DocumentRegion propertyNameRegion = createRegion(doc, pair.getKey());
                String keyName = PropertiesFileEscapes.unescape(propertyNameRegion.toString());
                duplicateNameChecker.check(propertyNameRegion);
                PropertyInfo validProperty = SpringPropertyIndex.findLongestValidProperty(index, keyName);
                if (validProperty != null) {
                    // in PropertyNavigator (probably these changes are also for the better making it simpler as well)
                    if (validProperty.isDeprecated()) {
                        problemCollector.accept(problemDeprecated(propertyNameRegion, validProperty));
                    }
                    int offset = validProperty.getId().length() + propertyNameRegion.getStart();
                    PropertyNavigator navigator = new PropertyNavigator(doc, problemCollector, typeUtilProvider.getTypeUtil(doc), propertyNameRegion);
                    Type valueType = navigator.navigate(offset, TypeParser.parse(validProperty.getType()));
                    if (valueType != null) {
                        reconcileType(doc, valueType, pair.getValue(), problemCollector);
                    }
                } else {
                    // validProperty==null
                    // The name is invalid, with no 'prefix' of the name being a valid property name.
                    PropertyInfo similarEntry = index.findLongestCommonPrefixEntry(propertyNameRegion.toString());
                    CharSequence validPrefix = commonPrefix(similarEntry.getId(), keyName);
                    problemCollector.accept(problemUnkownProperty(propertyNameRegion, similarEntry, validPrefix));
                }
            // end: validProperty==null
            } catch (Exception e) {
                Log.log(e);
            }
        });
    } catch (Throwable e2) {
        Log.log(e2);
    } finally {
        problemCollector.endCollecting();
    }
}
Also used : KeyValuePair(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Type(org.springframework.ide.vscode.boot.metadata.types.Type) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo)

Example 15 with ParseResults

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

the class PropertiesAntlrParserTest method testMultipleSyntaxErrors.

@Test
public void testMultipleSyntaxErrors() throws Exception {
    String text = "abrakadabra\nkey:value\nsdcsdc";
    ParseResults results = parser.parse(text);
    assertEquals(2, results.syntaxErrors.size());
    assertTrue(results.problems.isEmpty());
    // One property line recorded. With key and empty value
    assertEquals(3, results.ast.getAllNodes().size());
    List<KeyValuePair> lines = results.ast.getNodes(KeyValuePair.class);
    assertEquals(3, lines.size());
    // Test valid part
    KeyValuePair validLine = lines.get(1);
    assertNotNull(validLine.getKey());
    assertNotNull(validLine.getValue());
    assertEquals("key", text.substring(validLine.getKey().getOffset(), validLine.getKey().getOffset() + validLine.getKey().getLength()));
    assertEquals("value", text.substring(validLine.getValue().getOffset(), validLine.getValue().getOffset() + validLine.getValue().getLength()));
    // Test errors
    Problem syntaxError1 = results.syntaxErrors.get(0);
    assertEquals(0, syntaxError1.getOffset());
    assertEquals(11, syntaxError1.getLength());
    Problem syntaxError2 = results.syntaxErrors.get(1);
    assertEquals(22, syntaxError2.getOffset());
    assertEquals(6, syntaxError2.getLength());
}
Also used : KeyValuePair(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair) Problem(org.springframework.ide.vscode.java.properties.parser.Problem) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Test(org.junit.Test)

Aggregations

ParseResults (org.springframework.ide.vscode.java.properties.parser.ParseResults)21 Test (org.junit.Test)14 Node (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Node)9 KeyValuePair (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair)5 Value (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Value)5 Problem (org.springframework.ide.vscode.java.properties.parser.Problem)3 Key (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Key)3 ArrayList (java.util.ArrayList)2 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)2 Comment (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Comment)2 EmptyLine (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.EmptyLine)2 File (java.io.File)1 URI (java.net.URI)1 BitSet (java.util.BitSet)1 ANTLRErrorListener (org.antlr.v4.runtime.ANTLRErrorListener)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 ATNConfigSet (org.antlr.v4.runtime.atn.ATNConfigSet)1 DFA (org.antlr.v4.runtime.dfa.DFA)1