Skip to content

Commit

Permalink
Include version and GitHub project link in Settings panel
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanj committed Jun 25, 2024
1 parent e574363 commit d804466
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 73 deletions.
25 changes: 25 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.*

plugins {
java
id("org.jetbrains.intellij") version "1.17.2"
Expand All @@ -11,6 +13,29 @@ repositories {
mavenCentral()
}

tasks.register("updateProperties") {
doLast {
val projectVersion = version
val propertiesFile = file("src/main/resources/application.properties")

if (propertiesFile.exists()) {
val properties = Properties().apply {
load(propertiesFile.inputStream())
}

properties.setProperty("version", projectVersion.toString())

properties.store(propertiesFile.outputStream(), null)
} else {
println("application.properties file not found!")
}
}
}

tasks.named("buildPlugin") {
dependsOn("updateProperties")
}

dependencies {
// Add the dependencies for the core module
implementation("com.devoxx.genie:core:0.1.16")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static LLMProviderService getInstance() {
return ApplicationManager.getApplication().getService(LLMProviderService.class);
}


public List<String> getAvailableLLMProviders() {
DevoxxGenieStateService settings = DevoxxGenieStateService.getInstance();
Map<String, Supplier<String>> providerKeyMap = new HashMap<>();
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/devoxx/genie/service/PropertiesService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.devoxx.genie.service;

import com.intellij.openapi.application.ApplicationManager;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesService {

private final Properties properties = new Properties();

public PropertiesService() {
loadProperties();
}

@NotNull
public static PropertiesService getInstance() {
return ApplicationManager.getApplication().getService(PropertiesService.class);
}

private void loadProperties() {
try (InputStream input = getClass().getClassLoader().getResourceAsStream("application.properties")) {
if (input == null) {
System.out.println("Sorry, unable to find application.properties");
return;
}
properties.load(input);
} catch (IOException ex) {
ex.printStackTrace();
}
}

public String getVersion() {
return properties.getProperty("version");
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
package com.devoxx.genie.ui.processor;

import com.devoxx.genie.model.request.ChatMessageContext;
import com.devoxx.genie.ui.component.JHoverButton;
import com.devoxx.genie.ui.component.StyleSheetsFactory;
import com.devoxx.genie.ui.util.NotificationUtil;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.devoxx.genie.ui.util.CodeSnippetAction;
import org.commonmark.node.FencedCodeBlock;
import org.commonmark.renderer.html.HtmlRenderer;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

import static com.devoxx.genie.ui.util.DevoxxGenieColors.CODE_BG_COLOR;
import static com.devoxx.genie.ui.util.DevoxxGenieColors.CODE_BORDER_BG_COLOR;
import static com.devoxx.genie.ui.util.DevoxxGenieIcons.CopyIcon;
import static com.devoxx.genie.ui.util.DevoxxGenieIcons.InsertCodeIcon;

public class FencedCodeBlockProcessor implements NodeProcessor {

private final ChatMessageContext chatMessageContext;
private final FencedCodeBlock fencedCodeBlock;
private final CodeSnippetAction codeSnippetAction;

public FencedCodeBlockProcessor(ChatMessageContext chatMessageContext,
FencedCodeBlock fencedCodeBlock) {
this.chatMessageContext = chatMessageContext;
this.fencedCodeBlock = fencedCodeBlock;
this.codeSnippetAction = new CodeSnippetAction(chatMessageContext);
}

/**
Expand All @@ -53,67 +44,8 @@ public JPanel process() {

// Add components to the overlay panel in the correct order
overlayPanel.add(editorPane, BorderLayout.CENTER); // Editor pane at the bottom
overlayPanel.add(createClipBoardButtonPanel(fencedCodeBlock, editorPane), BorderLayout.NORTH); // Button panel on top
overlayPanel.add(codeSnippetAction.createClipBoardButtonPanel(fencedCodeBlock), BorderLayout.NORTH); // Button panel on top

return overlayPanel;
}

/**
* Add a clipboard button to the panel.
*
* @param fencedCodeBlock the fenced code block
*/
private JPanel createClipBoardButtonPanel(FencedCodeBlock fencedCodeBlock, JEditorPane editorPane) {
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 2));
buttonPanel.setOpaque(true);
buttonPanel.setVisible(true);

JHoverButton copyButton = new JHoverButton(CopyIcon, true);
copyButton.setToolTipText("Copy to clipboard");
copyButton.addActionListener(e -> copyToClipboard(fencedCodeBlock.getLiteral()));
copyButton.setVisible(true);
copyButton.setOpaque(true);
buttonPanel.add(copyButton);

JHoverButton insertButton = new JHoverButton(InsertCodeIcon, true);
insertButton.setToolTipText("Insert code");
insertButton.addActionListener(e -> insertCode(fencedCodeBlock.getLiteral()));
insertButton.setVisible(true);
insertButton.setOpaque(true);
buttonPanel.add(insertButton);
return buttonPanel;
}

/**
* Copy the text to the clipboard.
*
* @param codeSnippet the code snippet to copy
*/
private void copyToClipboard(String codeSnippet) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(codeSnippet), null);
NotificationUtil.sendNotification(chatMessageContext.getProject(), "Code copied to clipboard");
}

/**
* Insert the code snippet into the editor.
*
* @param codeSnippet the code snippet to insert
*/
private void insertCode(String codeSnippet) {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(chatMessageContext.getProject());
Editor selectedTextEditor = fileEditorManager.getSelectedTextEditor();
Editor editor = fileEditorManager.getSelectedTextEditor();
if (editor != null && selectedTextEditor != null) {
Document document = selectedTextEditor.getDocument();
CaretModel caretModel = editor.getCaretModel();
WriteCommandAction.runWriteCommandAction(chatMessageContext.getProject(), () -> {
try {
document.insertString(caretModel.getOffset(), codeSnippet.subSequence(0, codeSnippet.length()));
} catch (Exception e) {
Logger.getInstance(getClass()).error(e.getMessage());
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devoxx.genie.ui.settings.llm;

import com.devoxx.genie.service.PropertiesService;
import com.devoxx.genie.ui.settings.DevoxxGenieStateService;
import com.devoxx.genie.ui.settings.SettingsComponent;
import com.devoxx.genie.ui.util.NotificationUtil;
Expand All @@ -22,6 +23,8 @@ public class LLMSettingsComponent implements SettingsComponent {
public static final String LINK_EMOJI = "\uD83D\uDD17";
public static final String PASSWORD_EMOJI = "\uD83D\uDD11";

@Getter
private final JTextField projectVersion = new JTextField(PropertiesService.getInstance().getVersion());
@Getter
private final JTextField ollamaModelUrlField = new JTextField(stateService.getOllamaModelUrl());
@Getter
Expand Down Expand Up @@ -101,6 +104,10 @@ public JPanel createSettingsPanel() {
.addComponent(createTextWithPasswordButton(googleCSIApiKeyField, "https://programmablesearchengine.google.com/controlpanel/create"))
.addComponent(new JLabel("Hide Search Providers"))
.addComponent(hideSearchButtonsField)
.addVerticalGap(20)
.addComponent(new JXTitledSeparator("Plugin version"))
.addComponent(new JLabel("v" + projectVersion.getText()))
.addComponent(createTextWithLinkButton(new JLabel("View on GitHub"), "https://github.com/devoxx/DevoxxGenieIDEAPlugin"))
.getPanel();
}

Expand Down
68 changes: 68 additions & 0 deletions src/main/java/com/devoxx/genie/ui/util/CodeSnippetAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.devoxx.genie.ui.util;

import com.devoxx.genie.model.request.ChatMessageContext;
import com.devoxx.genie.ui.component.JHoverButton;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import org.commonmark.node.FencedCodeBlock;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Clipboard;
import java.awt.Toolkit;

import static com.devoxx.genie.ui.util.DevoxxGenieIcons.CopyIcon;
import static com.devoxx.genie.ui.util.DevoxxGenieIcons.InsertCodeIcon;

public class CodeSnippetAction {

private final ChatMessageContext chatMessageContext;

public CodeSnippetAction(ChatMessageContext chatMessageContext) {
this.chatMessageContext = chatMessageContext;
}

public JPanel createClipBoardButtonPanel(FencedCodeBlock fencedCodeBlock) {
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 2));
buttonPanel.setOpaque(true);

JHoverButton copyButton = new JHoverButton(CopyIcon, true);
copyButton.setToolTipText("Copy to clipboard");
copyButton.addActionListener(e -> copyToClipboard(fencedCodeBlock.getLiteral()));
buttonPanel.add(copyButton);

JHoverButton insertButton = new JHoverButton(InsertCodeIcon, true);
insertButton.setToolTipText("Insert code");
insertButton.addActionListener(e -> insertCode(fencedCodeBlock.getLiteral()));
buttonPanel.add(insertButton);

return buttonPanel;
}

private void copyToClipboard(String codeSnippet) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(codeSnippet), null);
NotificationUtil.sendNotification(chatMessageContext.getProject(), "Code copied to clipboard");
}

private void insertCode(String codeSnippet) {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(chatMessageContext.getProject());
Editor editor = fileEditorManager.getSelectedTextEditor();
if (editor != null) {
Document document = editor.getDocument();
CaretModel caretModel = editor.getCaretModel();
WriteCommandAction.runWriteCommandAction(chatMessageContext.getProject(), () -> {
try {
document.insertString(caretModel.getOffset(), codeSnippet);
} catch (Exception e) {
Logger.getInstance(getClass()).error(e.getMessage());
}
});
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
<applicationService serviceImplementation="com.devoxx.genie.service.PSIAnalyzerService"/>
<applicationService serviceImplementation="com.devoxx.genie.service.WebSearchService"/>
<applicationService serviceImplementation="com.devoxx.genie.service.LLMProviderService"/>
<applicationService serviceImplementation="com.devoxx.genie.service.PropertiesService"/>
</extensions>

<extensions defaultExtensionNs="com.intellij">
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Tue Jun 25 13:55:28 CEST 2024
version=0.1.20

0 comments on commit d804466

Please sign in to comment.