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;
}
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;
}
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();
}
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();
}
}
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());
}
Aggregations