Skip to content

Commit

Permalink
spotless and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
damiano1996 committed Nov 30, 2024
1 parent 2d44ab8 commit 37c6063
Show file tree
Hide file tree
Showing 38 changed files with 218 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@

package com.github.damiano1996.intellijplugin.incoder;

import com.intellij.openapi.util.IconLoader;
import javax.swing.*;

/**
* This class provides access to icons used in the InCoder plugin.
*/
public class InCoderIcons {
/**
* The icon representing the InCoder plugin. It is loaded from the resource file located at /META-INF/pluginIcon.svg.
*/
public static Icon PLUGIN_ICON =
IconLoader.getIcon("/META-INF/pluginIcon.svg", InCoderIcons.class);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.github.damiano1996.intellijplugin.incoder.completion;

import com.intellij.openapi.actionSystem.AnAction;
Expand All @@ -13,28 +14,18 @@ public class CodeCompletionAction extends AnAction implements AnActionListener {
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
Project project = anActionEvent.getProject();
if (project == null) return;

if (project == null) {
log.debug("Project was null, returning.");
return;
}

log.debug("An action was performed. Going to trigger the service.");
log.debug("Performing code completion action for project: {}", project.getName());
CodeCompletionService.getInstance(project).actionPerformed(anActionEvent);
}

@Override
public void beforeActionPerformed(@NotNull AnAction action, @NotNull AnActionEvent event) {
AnActionListener.super.beforeActionPerformed(action, event);

Project project = event.getProject();
if (project == null) return;

if (project == null) {
log.debug("Project was null, returning.");
return;
}

log.debug("An action was performed. Going to trigger the service.");
log.debug("Performing code completion action for project: {}", project.getName());
CodeCompletionService.getInstance(project).beforeActionPerformed(action, event);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@

package com.github.damiano1996.intellijplugin.incoder.completion;

/**
* Interface for handling code completion events in the InCoder plugin.
*/
public interface CodeCompletionListener {

/**
* Called when a code completion prediction is available.
*
* @param prediction The predicted code snippet or suggestion.
*/
void onCodeCompletionPrediction(String prediction);

/**
* Called when an error occurs during code completion processing.
*
* @param throwable The exception that caused the error.
*/
void onCodeCompletionError(Throwable throwable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import com.github.damiano1996.intellijplugin.incoder.language.model.LanguageModelService;
import com.github.damiano1996.intellijplugin.incoder.language.model.client.inline.settings.InlineSettings;
import com.intellij.openapi.project.Project;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CodeCompletionQueue {

public static final String MARKDOWN_CODE_BLOCK_DELIMITER = "```";

private final Project project;
private final CodeCompletionListener listener;

Expand Down Expand Up @@ -48,13 +49,15 @@ public void run() {

try {
String completion =
LanguageModelService.getInstance(project).complete(codeCompletionContext)
LanguageModelService.getInstance(project)
.complete(codeCompletionContext)
.split("\n")[0]
.trim();

if (completion.startsWith(MARKDOWN_CODE_BLOCK_DELIMITER)) continue;

if (queue.isEmpty()) {
log.debug(
"Queue is empty, therefore this prediction is still useful");
log.debug("Queue is empty, therefore this prediction is still useful");

listener.onCodeCompletionPrediction(completion);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;

@Service(Service.Level.PROJECT)
@Slf4j
public final class CodeCompletionService
Expand All @@ -33,8 +31,7 @@ public final class CodeCompletionService
Disposable {

@Getter private final Project project;
@Getter
private final CodeCompletionQueue codeCompletionQueue;
@Getter private final CodeCompletionQueue codeCompletionQueue;
private State state;

public CodeCompletionService(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.damiano1996.intellijplugin.incoder.completion.CodeCompletionService;
import com.github.damiano1996.intellijplugin.incoder.completion.states.BaseState;
import com.github.damiano1996.intellijplugin.incoder.completion.states.idle.IdleState;
import com.github.damiano1996.intellijplugin.incoder.completion.states.preview.PreviewState;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import dev.langchain4j.service.TokenStream;
import java.util.concurrent.CompletableFuture;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;

@Slf4j
@Service(Service.Level.PROJECT)
public final class LanguageModelService {
Expand All @@ -42,11 +41,7 @@ public static LanguageModelService getInstance() {

public void init() throws LanguageModelException {
server =
ServerSettings.getInstance()
.getState()
.modelType
.getServerFactory()
.createServer();
ServerSettings.getInstance().getState().modelType.getServerFactory().createServer();

client = server.createClient();

Expand All @@ -58,9 +53,9 @@ public String getSelectedModelName() {
}

public String complete(@NotNull CodeCompletionContext codeCompletionContext) {
return client.complete(InlineSettings.getInstance().getState().systemMessageInstructions,
codeCompletionContext.leftContext(),
codeCompletionContext.rightContext());
return client.complete(
InlineSettings.getInstance().getState().systemMessageInstructions,
codeCompletionContext.leftContext());
}

@Contract("_ -> new")
Expand All @@ -78,11 +73,13 @@ public TokenStream chat(@NonNull Editor editor, @NonNull String editDescription)
}

public TokenStream chat(@NonNull String editDescription) {
return client.chat(ChatSettings.getInstance().getState().systemMessageInstructions, project.getBasePath(), editDescription);
return client.chat(
ChatSettings.getInstance().getState().systemMessageInstructions,
project.getBasePath(),
editDescription);
}

public String createFileName(String fileContent) {
return client.createFileName(fileContent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ public LanguageModelClientImpl(
}

@Override
public String complete(String instructions, String leftContext, String rightContext) {
public String complete(String instructions, String leftContext) {
log.debug("Completing code...");
return inlineCodingAssistant.complete(instructions, leftContext, rightContext);
return inlineCodingAssistant.complete(instructions, leftContext);
}

@Override
public TokenStream chat(String instructions, String code, String filePath, String projectBasePath, String prompt) {
public TokenStream chat(
String instructions,
String code,
String filePath,
String projectBasePath,
String prompt) {
log.debug("Chatting about codes...");
return chatCodingAssistant.chat(instructions, code, filePath, projectBasePath, prompt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ TokenStream chat(
Instructions:
{{instructions}}
""")
TokenStream chat(@V("instructions") String instructions,
@V("projectBasePath") String projectBasePath, @UserMessage String prompt);
TokenStream chat(
@V("instructions") String instructions,
@V("projectBasePath") String projectBasePath,
@UserMessage String prompt);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public void loadState(@NotNull State state) {
public static class State {
public int maxMessages = 10;

public String systemMessageInstructionsWithCode = """
public String systemMessageInstructionsWithCode =
"""
- You are an AI assistant integrated into a JetBrains plugin, providing expert coding assistance and development support directly within the IDE.
- If the user input pertains to the provided code, respond with the code edited according to the user's instructions.
""";

public String systemMessageInstructions = """
public String systemMessageInstructions =
"""
- You are an AI assistant integrated into a JetBrains plugin, providing expert coding assistance and development support directly within the IDE.
""";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextArea;
import com.intellij.util.ui.FormBuilder;
import lombok.Getter;

import javax.swing.*;
import lombok.Getter;

@Getter
public class ChatSettingsComponent {
Expand All @@ -33,21 +32,31 @@ public ChatSettingsComponent() {
mainPanel =
FormBuilder.createFormBuilder()
.setFormLeftIndent(20)
.addLabeledComponent(
new JBLabel("Max messages:"), maxMessages, 1, false)
.addLabeledComponent(new JBLabel("Max messages:"), maxMessages, 1, false)
.addComponent(new DescriptionLabel("Number of messages to keep in memory."))
.addVerticalGap(20)
.addLabeledComponent(
new JBLabel("System message instructions with code:"),
ScrollPaneFactory.createScrollPane(systemMessageInstructionsWithCodeField),
1, true)
.addComponent(new DescriptionLabel("System message template instructions when code context is included. These instructions will be added to the @SystemMessage."))
ScrollPaneFactory.createScrollPane(
systemMessageInstructionsWithCodeField),
1,
true)
.addComponent(
new DescriptionLabel(
"System message template instructions when code context is"
+ " included. These instructions will be added to the"
+ " @SystemMessage."))
.addVerticalGap(20)
.addLabeledComponent(
new JBLabel("System message instructions:"),
ScrollPaneFactory.createScrollPane(systemMessageInstructionsField),
1, true)
.addComponent(new DescriptionLabel("System message template instructions for general questions. These instructions will be added to the @SystemMessage."))
1,
true)
.addComponent(
new DescriptionLabel(
"System message template instructions for general"
+ " questions. These instructions will be added to the"
+ " @SystemMessage."))
.setFormLeftIndent(0)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.github.damiano1996.intellijplugin.incoder.language.model.client.chat.settings;

import com.intellij.openapi.options.Configurable;
import javax.swing.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public final class ChatSettingsConfigurable implements Configurable {

private ChatSettingsComponent chatSettingsComponent;
Expand Down Expand Up @@ -42,28 +41,39 @@ public JComponent createComponent() {
public boolean isModified() {
var state = getState();

return !chatSettingsComponent.getMaxMessages().getValue().equals(state.maxMessages) ||
!chatSettingsComponent.getSystemMessageInstructionsWithCodeField().getText().equals(state.systemMessageInstructionsWithCode)
|| !chatSettingsComponent.getSystemMessageInstructionsField().getText().equals(state.systemMessageInstructions);
return !chatSettingsComponent.getMaxMessages().getValue().equals(state.maxMessages)
|| !chatSettingsComponent
.getSystemMessageInstructionsWithCodeField()
.getText()
.equals(state.systemMessageInstructionsWithCode)
|| !chatSettingsComponent
.getSystemMessageInstructionsField()
.getText()
.equals(state.systemMessageInstructions);
}

@Override
public void apply() {
var state = getState();

state.maxMessages = (int) chatSettingsComponent.getMaxMessages().getValue();
state.systemMessageInstructionsWithCode = chatSettingsComponent.getSystemMessageInstructionsWithCodeField().getText();
state.systemMessageInstructions = chatSettingsComponent.getSystemMessageInstructionsField().getText();

state.systemMessageInstructionsWithCode =
chatSettingsComponent.getSystemMessageInstructionsWithCodeField().getText();
state.systemMessageInstructions =
chatSettingsComponent.getSystemMessageInstructionsField().getText();
}

@Override
public void reset() {
var state = getState();

chatSettingsComponent.getMaxMessages().setValue(state.maxMessages);
chatSettingsComponent.getSystemMessageInstructionsWithCodeField().setText(state.systemMessageInstructionsWithCode);
chatSettingsComponent.getSystemMessageInstructionsField().setText(state.systemMessageInstructions);
chatSettingsComponent
.getSystemMessageInstructionsWithCodeField()
.setText(state.systemMessageInstructionsWithCode);
chatSettingsComponent
.getSystemMessageInstructionsField()
.setText(state.systemMessageInstructions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ public interface InlineCodingAssistant {

@UserMessage(
"""
Given the following code context, provide only the missing line of code.
Left context:
{{leftContext}}
Right context:
{{rightContext}}
Instructions:
{{instructions}}
Code:
{{leftContext}}
""")
String complete(
@V("instructions") String instructions,
@V("leftContext") String leftContext, @V("rightContext") String rightContext);
String complete(@V("instructions") String instructions, @V("leftContext") String leftContext);
}
Loading

0 comments on commit 37c6063

Please sign in to comment.