JsonXpathSuggestionSource implements SuggestionSource<BasicSuggestion> { @Override public List<BasicSuggestion> getSuggestions(SuggestionQuery query) { final String text = query.getValue(); final int caretLocation = query.getCaretLocation(); String line = lineForPosition(text, caretLocation); final boolean matches = line.matches("\\w+\\s*=\\s*.*"); int position = caretLocation - lineStartIndexAtPosition(text, caretLocation); if (line.equalsIgnoreCase("\n")) { position = position - 1; } if (matches) { final int indexOf = line.indexOf("="); if (position < indexOf) { return getKeysSuggestion(line.substring(0, position), typedProperties(text)); } else if (position == indexOf) { return Collections.singletonList(new BasicSuggestion("=", "=")); } else { return getValuesSuggestions(line, position, indexOf); } } else if (StringUtils.isBlank(line)) { return getKeysSuggestion(StringUtils.EMPTY, typedProperties(text)); } return getKeysSuggestion(line.substring(0, position), typedProperties(text)); } JsonXpathSuggestionSource(); void setJsonPaths(Set<String> jsonPaths); void setJsonDoc(String jsonDoc); void setPropertyDoc(String propertyDoc); @Override List<BasicSuggestion> getSuggestions(SuggestionQuery query); static final String JSON; }
@Test(dataProvider = "suggestions") public void testGetSuggestions(String name, String text, Collection<String> xpaths, int caretPosition, List<BasicSuggestion> expectedSuggestions ) throws Exception { final JsonXpathSuggestionSource underTest = new JsonXpathSuggestionSource(); underTest.setJsonPaths(new HashSet<>(xpaths)); final List<BasicSuggestion> suggestions = underTest.getSuggestions(new SuggestionQuery(text, caretPosition)); assertEquals(suggestions, expectedSuggestions); }