-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support method name lookup generation (#280)
- Loading branch information
1 parent
3d95269
commit 845c7b4
Showing
9 changed files
with
162 additions
and
9 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
2 changes: 2 additions & 0 deletions
2
codegpt-core/src/main/resources/prompts/method-name-generator.txt
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,2 @@ | ||
Given an existing function or method body, generate five alternative names for the function. | ||
The response must be a comma-separated list of names. Exclude any additional information. |
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
99 changes: 99 additions & 0 deletions
99
src/main/java/ee/carlrobert/codegpt/completions/CompletionLookupService.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,99 @@ | ||
package ee.carlrobert.codegpt.completions; | ||
|
||
import com.intellij.codeInsight.completion.PrefixMatcher; | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder; | ||
import com.intellij.codeInsight.lookup.LookupManagerListener; | ||
import com.intellij.codeInsight.lookup.impl.LookupImpl; | ||
import com.intellij.openapi.application.Application; | ||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.components.Service; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.util.PsiUtilCore; | ||
import ee.carlrobert.codegpt.Icons; | ||
import ee.carlrobert.codegpt.credentials.OpenAICredentialsManager; | ||
import ee.carlrobert.codegpt.settings.configuration.ConfigurationState; | ||
import ee.carlrobert.codegpt.settings.state.SettingsState; | ||
import java.util.Optional; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@Service(Service.Level.PROJECT) | ||
public final class CompletionLookupService { | ||
|
||
private final Project project; | ||
|
||
private CompletionLookupService(Project project) { | ||
this.project = project; | ||
} | ||
|
||
public void subscribeToLookupTopic() { | ||
project.getMessageBus() | ||
.connect() | ||
.subscribe(LookupManagerListener.TOPIC, getLookupManagerListener()); | ||
} | ||
|
||
private @Nullable String getCompletionResponse(String prompt) { | ||
var selectedService = SettingsState.getInstance().getSelectedService(); | ||
switch (selectedService) { | ||
case OPENAI: | ||
case AZURE: | ||
return Optional.ofNullable(CompletionClientProvider.getOpenAIClient() | ||
.getChatCompletion( | ||
CompletionRequestProvider.buildOpenAILookupCompletionRequest(prompt)) | ||
.getChoices()) | ||
.map(choices -> choices.get(0).getMessage().getContent()) | ||
.orElse(null); | ||
// TODO | ||
/*case LLAMA_CPP: | ||
var request = CompletionRequestProvider.buildLlamaLookupCompletionRequest(prompt); | ||
return CompletionClientProvider.getLlamaClient() | ||
.getChatCompletion(request) | ||
.getContent();*/ | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
private void addCompletionLookupValues( | ||
LookupImpl lookup, | ||
Application application, | ||
String prompt) { | ||
Optional.ofNullable(getCompletionResponse(prompt)) | ||
.ifPresent(response -> { | ||
for (var value : response.split(",")) { | ||
application.runReadAction(() -> { | ||
lookup.addItem( | ||
LookupElementBuilder.create(value.trim()).withIcon(Icons.SparkleIcon), | ||
PrefixMatcher.ALWAYS_TRUE); | ||
}); | ||
application.invokeLater(() -> lookup.refreshUi(true, true)); | ||
} | ||
}); | ||
} | ||
|
||
private LookupManagerListener getLookupManagerListener() { | ||
var application = ApplicationManager.getApplication(); | ||
var configuration = ConfigurationState.getInstance(); | ||
var credentialsManager = OpenAICredentialsManager.getInstance(); | ||
return (oldLookup, newLookup) -> { | ||
if (!configuration.isMethodNameGenerationEnabled() | ||
|| !credentialsManager.isApiKeySet() | ||
|| !(newLookup instanceof LookupImpl)) { | ||
return; | ||
} | ||
|
||
var lookup = (LookupImpl) newLookup; | ||
Optional.ofNullable(lookup.getPsiElement()) | ||
.map(PsiElement::getContext) | ||
.ifPresent(context -> | ||
application.runReadAction(() -> { | ||
var type = PsiUtilCore.getElementType(context); | ||
if ("METHOD".equals(type.toString())) { | ||
var selection = context.getText(); | ||
application.executeOnPooledThread( | ||
() -> addCompletionLookupValues(lookup, application, selection)); | ||
} | ||
})); | ||
}; | ||
} | ||
} |
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
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