Skip to content

Commit

Permalink
only show query start proposals if applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Feb 21, 2023
1 parent 72cf1fb commit 242a052
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ public void provideCompletions(ASTNode node, int offset, IDocument doc, Collecti
for (DomainProperty property : properties) {
completions.add(generateCompletionProposal(offset, prefix, repo, property));
}
DataRepositoryPrefixSensitiveCompletionProvider.addPrefixSensitiveProposals(completions, offset, prefix, repo);
DataRepositoryPrefixSensitiveCompletionProvider.addPrefixSensitiveProposals(completions, doc, offset, prefix, repo);
}
}
}



protected ICompletionProposal generateCompletionProposal(int offset, String prefix, DataRepositoryDefinition repoDef, DomainProperty domainProperty) {
StringBuilder label = new StringBuilder();
label.append("findBy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.eclipse.lsp4j.CompletionItemKind;
import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits;
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal;
import org.springframework.ide.vscode.commons.util.BadLocationException;
import org.springframework.ide.vscode.commons.util.text.IDocument;
import org.springframework.util.StringUtils;

/**
Expand All @@ -31,9 +33,8 @@ private DataRepositoryPrefixSensitiveCompletionProvider() {
//prevent instantiation
}

static void addPrefixSensitiveProposals(Collection<ICompletionProposal> completions, int offset, String prefix, DataRepositoryDefinition repoDef){
String localPrefix = findJavaIdentifierPart(prefix);
addQueryStartProposals(completions, localPrefix, offset);
static void addPrefixSensitiveProposals(Collection<ICompletionProposal> completions, IDocument doc, int offset, String prefix, DataRepositoryDefinition repoDef) {
String localPrefix = findLastJavaIdentifierPart(prefix);
if (localPrefix == null) {
return;
}
Expand All @@ -46,6 +47,24 @@ static void addPrefixSensitiveProposals(Collection<ICompletionProposal> completi
addPropertyProposals(completions, offset, repoDef, parseResult);
}
}
addQueryStartProposals(completions, localPrefix, doc, offset);
}

private static void addQueryStartProposals(Collection<ICompletionProposal> completions, String prefix, IDocument doc, int offset) {
for(QueryMethodSubject queryMethodSubject : QueryMethodSubject.QUERY_METHOD_SUBJECTS){
String toInsert = queryMethodSubject.key() + "By";
if(toInsert.startsWith(prefix)||isOffsetAfterWhitespace(doc, offset)) {
completions.add(DataRepositoryCompletionProcessor.createProposal(offset, CompletionItemKind.Text, prefix, toInsert, toInsert));
}
}
}

private static boolean isOffsetAfterWhitespace(IDocument doc, int offset) {
try {
return offset > 0 && Character.isWhitespace(doc.getChar(offset-1));
}catch (BadLocationException e) {
return false;
}
}

private static void addPropertyProposals(Collection<ICompletionProposal> completions, int offset, DataRepositoryDefinition repoDef, DataRepositoryMethodNameParseResult parseResult) {
Expand Down Expand Up @@ -148,14 +167,7 @@ private static DomainType findMatchingParameter(String name, DomainType type) {
return null;
}

private static void addQueryStartProposals(Collection<ICompletionProposal> completions, String prefix, int offset) {
for(QueryMethodSubject queryMethodSubject : QueryMethodSubject.QUERY_METHOD_SUBJECTS){
String toInsert = queryMethodSubject.key() + "By";
completions.add(DataRepositoryCompletionProcessor.createProposal(offset, CompletionItemKind.Text, prefix, toInsert, toInsert));
}
}

private static String findJavaIdentifierPart(String prefix) {
private static String findLastJavaIdentifierPart(String prefix) {
if (prefix == null) {
return null;
}
Expand Down

0 comments on commit 242a052

Please sign in to comment.