-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out static methods for Spring Data repository completions
- Loading branch information
Showing
11 changed files
with
211 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...springframework/ide/vscode/boot/java/data/providers/DataRepositoryCompletionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Pivotal, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Pivotal, Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.springframework.ide.vscode.boot.java.data.providers; | ||
|
||
import java.util.Collection; | ||
|
||
import org.springframework.ide.vscode.boot.java.data.DataRepositoryDefinition; | ||
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal; | ||
import org.springframework.ide.vscode.commons.util.text.IDocument; | ||
|
||
/** | ||
* Responsible for creating proposals related to Spring Data repositories. | ||
* @author danthe1st | ||
*/ | ||
public interface DataRepositoryCompletionProvider { | ||
|
||
void addProposals(Collection<ICompletionProposal> completions, IDocument doc, int offset, String prefix, DataRepositoryDefinition repo); | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...ework/ide/vscode/boot/java/data/providers/DataRepositoryQueryStartCompletionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Pivotal, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Pivotal, Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.springframework.ide.vscode.boot.java.data.providers; | ||
|
||
import java.util.Collection; | ||
|
||
import org.eclipse.lsp4j.CompletionItemKind; | ||
import org.springframework.ide.vscode.boot.java.data.DataRepositoryDefinition; | ||
import org.springframework.ide.vscode.boot.java.data.FindByCompletionProposal; | ||
import org.springframework.ide.vscode.boot.java.data.providers.prefixsensitive.DataRepositoryPrefixSensitiveCompletionProvider; | ||
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; | ||
|
||
/** | ||
* This class creates text roposals for query method subjects, e.g. {@code countBy}. | ||
* @author danthe1st | ||
*/ | ||
public class DataRepositoryQueryStartCompletionProvider implements DataRepositoryCompletionProvider{ | ||
|
||
@Override | ||
public void addProposals(Collection<ICompletionProposal> completions, IDocument doc, int offset, String prefix, DataRepositoryDefinition repo) { | ||
String localPrefix = DataRepositoryPrefixSensitiveCompletionProvider.findLastJavaIdentifierPart(prefix); | ||
for(QueryMethodSubject queryMethodSubject : QueryMethodSubject.QUERY_METHOD_SUBJECTS){ | ||
String toInsert = queryMethodSubject.key() + "By"; | ||
if(prefix == null || toInsert.startsWith(localPrefix)||isOffsetAfterWhitespace(doc, offset)) { | ||
completions.add(FindByCompletionProposal.createProposal(offset, CompletionItemKind.Text, prefix, toInsert, toInsert)); | ||
} | ||
} | ||
} | ||
|
||
private boolean isOffsetAfterWhitespace(IDocument doc, int offset) { | ||
try { | ||
return offset > 0 && Character.isWhitespace(doc.getChar(offset-1)); | ||
}catch (BadLocationException e) { | ||
return false; | ||
} | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...amework/ide/vscode/boot/java/data/providers/DataRepositoryStandardCompletionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Pivotal, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Pivotal, Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.springframework.ide.vscode.boot.java.data.providers; | ||
|
||
import java.util.Collection; | ||
|
||
import org.eclipse.lsp4j.CompletionItemKind; | ||
import org.springframework.ide.vscode.boot.java.data.DataRepositoryDefinition; | ||
import org.springframework.ide.vscode.boot.java.data.DomainProperty; | ||
import org.springframework.ide.vscode.boot.java.data.DomainType; | ||
import org.springframework.ide.vscode.boot.java.data.FindByCompletionProposal; | ||
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal; | ||
import org.springframework.ide.vscode.commons.util.text.IDocument; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Provides content assist proposals for querying by a single attribute in Spring Data repositories. | ||
* @author Martin Lippert | ||
*/ | ||
public class DataRepositoryStandardCompletionProvider implements DataRepositoryCompletionProvider { | ||
|
||
public void addProposals(Collection<ICompletionProposal> completions, IDocument doc, int offset, String prefix, DataRepositoryDefinition repo) { | ||
DomainType domainType = repo.getDomainType(); | ||
DomainProperty[] properties = domainType.getProperties(); | ||
for (DomainProperty property : properties) { | ||
completions.add(generateCompletionProposal(offset, prefix, repo, property)); | ||
} | ||
} | ||
|
||
private ICompletionProposal generateCompletionProposal(int offset, String prefix, DataRepositoryDefinition repoDef, DomainProperty domainProperty) { | ||
StringBuilder label = new StringBuilder(); | ||
label.append("findBy"); | ||
label.append(StringUtils.capitalize(domainProperty.getName())); | ||
label.append("("); | ||
label.append(domainProperty.getType().getSimpleName()); | ||
label.append(" "); | ||
label.append(StringUtils.uncapitalize(domainProperty.getName())); | ||
label.append(");"); | ||
|
||
StringBuilder completion = new StringBuilder(); | ||
completion.append("List<"); | ||
completion.append(repoDef.getDomainType().getSimpleName()); | ||
completion.append("> findBy"); | ||
completion.append(StringUtils.capitalize(domainProperty.getName())); | ||
completion.append("("); | ||
completion.append(domainProperty.getType().getSimpleName()); | ||
completion.append(" "); | ||
completion.append(StringUtils.uncapitalize(domainProperty.getName())); | ||
completion.append(");"); | ||
|
||
return FindByCompletionProposal.createProposal(offset, CompletionItemKind.Method, prefix, label.toString(), completion.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.