Skip to content

Commit

Permalink
#6870: handle incorrect code (#6872)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete authored and scottdraves committed Feb 18, 2018
1 parent a6fb832 commit dc36f79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,20 @@ public GroovyAutocomplete(GroovyClasspathScanner _cps) {
registry = AutocompleteRegistryFactory.createRegistry(cps);
}

/*
* These are meant to be extended by personalized plugin versions.
*/
private void moreSetup(AutocompleteRegistry r) {
}

private GroovyClassUtils createClassUtils(ClassLoader l) {
return new GroovyClassUtils(cps, l);
public AutocompleteResult doAutocomplete(String txt, int cur, ClassLoader l, Imports imports) {
try {
return tryFindAutocomplete(txt, cur, l, imports);
} catch (Exception e) {
return new AutocompleteResult(new ArrayList<>(), 0);
}
}

public AutocompleteResult doAutocomplete(String txt, int cur, ClassLoader l, Imports imports) {
private AutocompleteResult tryFindAutocomplete(String txt, int cur, ClassLoader l, Imports imports) {
registry = AutocompleteRegistryFactory.createRegistry(cps);
GroovyClassUtils cu = createClassUtils(l);
setup(cu, registry);
AutocompleteRegistryFactory.addDefaultImports(cu, registry, imports.toListOfStrings(), cps);
AutocompleteRegistryFactory.moreSetup(cu);
moreSetup(registry);

Lexer lexer = new GroovyLexer(new ANTLRInputStream(txt));
lexer.removeErrorListeners();
Expand Down Expand Up @@ -126,4 +123,8 @@ private int getStartIndex(GroovyImportDeclarationCompletion extractor, GroovyNam
return 0;
}

private GroovyClassUtils createClassUtils(ClassLoader l) {
return new GroovyClassUtils(cps, l);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,13 @@ public void autocompleteToFileMethods() throws Exception {
assertThat(autocomplete.getStartIndex()).isEqualTo(code.length()-1);
}

@Test
public void shouldReturnEmptyResultForIncorrectCode() throws Exception {
String code = "]";
//when
AutocompleteResult autocomplete = groovyEvaluator.autocomplete(code, code.length());
//then
assertThat(autocomplete.getMatches()).isEmpty();
assertThat(autocomplete.getStartIndex()).isEqualTo(0);
}
}

0 comments on commit dc36f79

Please sign in to comment.