Skip to content

Commit

Permalink
0.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Jan 15, 2023
1 parent 4537e47 commit 1ff284c
Show file tree
Hide file tree
Showing 55 changed files with 2,177 additions and 1,190 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

## [Unreleased]

## [0.1.9]

### Added
- Dev mode now adds a API request preview/testing UI for each request
- Various UI polish
- Refactored code so Actions are independent components

## [0.1.8]

### Added
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ repositories {

// Set the JVM language level used to build project. Use Java 11 for 2020.3+, and Java 17 for 2022.2+.
kotlin {
// jvmToolchain(17)
jvmToolchain(11)
}

Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.simiacryptus
pluginName = intellij-aicoder
pluginRepositoryUrl = https://github.com/SimiaCryptus/intellij-aicoder
# SemVer format -> https://semver.org
pluginVersion = 0.1.8
pluginVersion = 0.1.9

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 203
Expand All @@ -13,6 +13,7 @@ pluginUntilBuild = 223.*
# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType = IC
platformVersion = 2021.3.3
#platformVersion = 2022.3.1

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
Expand Down
688 changes: 0 additions & 688 deletions src/main/java/com/github/simiacryptus/aicoder/EditorMenu.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.simiacryptus.aicoder.actions;

import com.github.simiacryptus.aicoder.config.AppSettingsState;
import com.github.simiacryptus.aicoder.openai.CompletionRequest;
import com.github.simiacryptus.aicoder.util.ComputerLanguage;
import com.github.simiacryptus.aicoder.util.UITools;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import org.jetbrains.annotations.NotNull;

import static com.github.simiacryptus.aicoder.util.UITools.replaceString;
import static java.util.Objects.requireNonNull;

public class CommentsAction extends AnAction {

@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabledAndVisible(isEnabled(e));
super.update(e);
}

private static boolean isEnabled(@NotNull AnActionEvent e) {
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final CaretModel caretModel = editor.getCaretModel();
final Caret primaryCaret = caretModel.getPrimaryCaret();
int selectionStart = primaryCaret.getSelectionStart();
int selectionEnd = primaryCaret.getSelectionEnd();
if(selectionStart == selectionEnd) return false;
if (null == ComputerLanguage.getComputerLanguage(e)) return false;
return true;
}

@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final CaretModel caretModel = editor.getCaretModel();
final Caret primaryCaret = caretModel.getPrimaryCaret();
int selectionStart = primaryCaret.getSelectionStart();
int selectionEnd = primaryCaret.getSelectionEnd();
String selectedText = primaryCaret.getSelectedText();
String outputHumanLanguage = AppSettingsState.getInstance().humanLanguage;
ComputerLanguage language = ComputerLanguage.getComputerLanguage(e);
AppSettingsState settings = AppSettingsState.getInstance();
CompletionRequest request = settings.createTranslationRequest()
.setInputType(requireNonNull(language).name())
.setOutputType(language.name())
.setInstruction(UITools.getInstruction("Rewrite to include detailed " + outputHumanLanguage + " code comments for every line"))
.setInputAttribute("type", "uncommented")
.setOutputAttrute("type", "commented")
.setOutputAttrute("style", settings.style)
.setInputText(selectedText)
.buildCompletionRequest();
Caret caret = e.getData(CommonDataKeys.CARET);
CharSequence indent = UITools.getIndent(caret);
UITools.redoableRequest(request, indent, e, newText -> replaceString(editor.getDocument(), selectionStart, selectionEnd, newText));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.simiacryptus.aicoder.actions;

import com.github.simiacryptus.aicoder.util.ComputerLanguage;
import com.github.simiacryptus.aicoder.config.AppSettingsState;
import com.github.simiacryptus.aicoder.openai.CompletionRequest;
import com.github.simiacryptus.aicoder.util.IndentedText;
import com.github.simiacryptus.aicoder.util.UITools;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

import static com.github.simiacryptus.aicoder.util.UITools.replaceString;
import static java.util.Objects.requireNonNull;

public class CustomEditAction extends AnAction {

@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabledAndVisible(isEnabled(e));
super.update(e);
}

private static boolean isEnabled(@NotNull AnActionEvent e) {
return null != ComputerLanguage.getComputerLanguage(e);
}

@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final CaretModel caretModel = editor.getCaretModel();
final Caret primaryCaret = caretModel.getPrimaryCaret();
int selectionStart = primaryCaret.getSelectionStart();
int selectionEnd = primaryCaret.getSelectionEnd();
String selectedText = primaryCaret.getSelectedText();
String computerLanguage = requireNonNull(ComputerLanguage.getComputerLanguage(e)).name();
String instruction = JOptionPane.showInputDialog(null, "Instruction:", "Edit Code", JOptionPane.QUESTION_MESSAGE);
AppSettingsState settings = AppSettingsState.getInstance();
settings.addInstructionToHistory(instruction);
CompletionRequest request = settings.createTranslationRequest()
.setInputType(computerLanguage)
.setOutputType(computerLanguage)
.setInstruction(instruction)
.setInputAttribute("type", "before")
.setOutputAttrute("type", "after")
.setInputText(IndentedText.fromString(selectedText).getTextBlock())
.buildCompletionRequest();
Caret caret = e.getData(CommonDataKeys.CARET);
CharSequence indent = UITools.getIndent(caret);
UITools.redoableRequest(request, indent, e, newText -> replaceString(editor.getDocument(), selectionStart, selectionEnd, newText));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.github.simiacryptus.aicoder.actions;

import com.github.simiacryptus.aicoder.util.*;
import com.github.simiacryptus.aicoder.config.AppSettingsState;
import com.github.simiacryptus.aicoder.openai.CompletionRequest;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import static com.github.simiacryptus.aicoder.util.UITools.replaceString;

public class DescribeAction extends AnAction {

@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabledAndVisible(isEnabled(e));
super.update(e);
}

private static boolean isEnabled(@NotNull AnActionEvent e) {
return null != ComputerLanguage.getComputerLanguage(e);
}

@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
final CaretModel caretModel = editor.getCaretModel();
final Caret primaryCaret = caretModel.getPrimaryCaret();
int selectionStart = primaryCaret.getSelectionStart();
int selectionEnd = primaryCaret.getSelectionEnd();
String selectedText = primaryCaret.getSelectedText();
ComputerLanguage language = ComputerLanguage.getComputerLanguage(event);
assert language != null;

if(null == selectedText || selectedText.isEmpty()) {
Document document = editor.getDocument();
int lineNumber = document.getLineNumber(selectionStart);
int lineStartOffset = document.getLineStartOffset(lineNumber);
int lineEndOffset = document.getLineEndOffset(lineNumber);
String currentLine = document.getText().substring(lineStartOffset, lineEndOffset);
selectionStart = lineStartOffset;
selectionEnd = lineEndOffset;
selectedText = currentLine;
}

actionPerformed(event, editor, selectionStart, selectionEnd, selectedText, language);
}

private static void actionPerformed(@NotNull AnActionEvent event, Editor editor, int selectionStart, int selectionEnd, String selectedText, ComputerLanguage language) {
CharSequence indent = UITools.getIndent(event);
AppSettingsState settings = AppSettingsState.getInstance();
CompletionRequest request = settings.createTranslationRequest()
.setInputType(Objects.requireNonNull(language).name())
.setOutputType(settings.humanLanguage)
.setInstruction(UITools.getInstruction("Explain this " + language.name() + " in " + settings.humanLanguage))
.setInputAttribute("type", "code")
.setOutputAttrute("type", "description")
.setOutputAttrute("style", settings.style)
.setInputText(IndentedText.fromString(selectedText).getTextBlock().trim())
.buildCompletionRequest();
UITools.redoableRequest(request, indent, event, newText -> transformCompletion(selectedText, language, indent, newText), newText -> replaceString(editor.getDocument(), selectionStart, selectionEnd, newText));
}

@NotNull
private static CharSequence transformCompletion(String selectedText, ComputerLanguage language, CharSequence indent, CharSequence x) {
String wrapping = StringTools.lineWrapping(x.toString().trim(), 120);
TextBlockFactory<?> commentStyle;
if(wrapping.trim().split("\n").length == 1) {
commentStyle = language.lineComment;
} else {
commentStyle = language.blockComment;
}
return "\n" + indent + Objects.requireNonNull(commentStyle).fromString(wrapping).withIndent(indent) + "\n" + indent + selectedText;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.simiacryptus.aicoder.actions;

import com.github.simiacryptus.aicoder.config.AppSettingsState;
import com.github.simiacryptus.aicoder.openai.CompletionRequest;
import com.github.simiacryptus.aicoder.util.psi.PsiUtil;
import com.github.simiacryptus.aicoder.util.*;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import static com.github.simiacryptus.aicoder.util.UITools.replaceString;

public class DocAction extends AnAction {

@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabledAndVisible(isEnabled(e));
super.update(e);
}

private static boolean isEnabled(@NotNull AnActionEvent e) {
ComputerLanguage computerLanguage = ComputerLanguage.getComputerLanguage(e);
if (null == computerLanguage) return false;
if (null == computerLanguage.docStyle || computerLanguage.docStyle.isEmpty()) return false;
return true;
}

@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
ComputerLanguage language = ComputerLanguage.getComputerLanguage(event);
Caret caret = event.getData(CommonDataKeys.CARET);
PsiFile psiFile = event.getRequiredData(CommonDataKeys.PSI_FILE);
PsiElement smallestIntersectingMethod = PsiUtil.getSmallestIntersecting(psiFile, Objects.requireNonNull(caret).getSelectionStart(), caret.getSelectionEnd());
if (null == smallestIntersectingMethod) return;
AppSettingsState settings = AppSettingsState.getInstance();
String code = smallestIntersectingMethod.getText();
IndentedText indentedInput = IndentedText.fromString(code);
CharSequence indent = indentedInput.getIndent();
int startOffset = smallestIntersectingMethod.getTextRange().getStartOffset();
int endOffset = smallestIntersectingMethod.getTextRange().getEndOffset();
CompletionRequest completionRequest = settings.createTranslationRequest()
.setInputType(Objects.requireNonNull(language).name())
.setOutputType(language.name())
.setInstruction(UITools.getInstruction("Rewrite to include detailed " + language.docStyle))
.setInputAttribute("type", "uncommented")
.setOutputAttrute("type", "commented")
.setOutputAttrute("style", settings.style)
.setInputText(indentedInput.getTextBlock())
.buildCompletionRequest()
.addStops(Objects.requireNonNull(language.getMultilineCommentSuffix()));
Document document = event.getRequiredData(CommonDataKeys.EDITOR).getDocument();
UITools.redoableRequest(completionRequest, "", event, docString -> transformCompletion(language, indentedInput, indent, docString), docString -> replaceString(document, startOffset, endOffset, docString));
}

@NotNull
private static CharSequence transformCompletion(ComputerLanguage language, IndentedText indentedInput, CharSequence indent, CharSequence docString) {
TextBlock reindented = Objects.requireNonNull(language.docComment).fromString(docString.toString().trim()).withIndent(indent);
return reindented + "\n" + indent + StringTools.trimPrefix(indentedInput.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.simiacryptus.aicoder.actions;

import com.github.simiacryptus.aicoder.util.ComputerLanguage;
import com.github.simiacryptus.aicoder.config.AppSettingsState;
import com.github.simiacryptus.aicoder.openai.CompletionRequest;
import com.github.simiacryptus.aicoder.util.UITools;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import org.jetbrains.annotations.NotNull;

import static com.github.simiacryptus.aicoder.util.UITools.replaceString;
import static java.util.Objects.requireNonNull;

public class FromHumanLanguageAction extends AnAction {

public FromHumanLanguageAction() {
super("", "", null);
}

@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
final CaretModel caretModel = editor.getCaretModel();
final Caret primaryCaret = caretModel.getPrimaryCaret();
int selectionStart = primaryCaret.getSelectionStart();
int selectionEnd = primaryCaret.getSelectionEnd();
String selectedText = primaryCaret.getSelectedText();
CompletionRequest request = AppSettingsState.getInstance().createTranslationRequest()
.setInputType(AppSettingsState.getInstance().humanLanguage.toLowerCase())
.setOutputType(requireNonNull(ComputerLanguage.getComputerLanguage(event)).name())
.setInstruction("Implement this specification")
.setInputAttribute("type", "input")
.setOutputAttrute("type", "output")
.setInputText(selectedText)
.buildCompletionRequest();
Caret caret = event.getData(CommonDataKeys.CARET);
CharSequence indent = UITools.getIndent(caret);
UITools.redoableRequest(request, indent, event, newText -> replaceString(editor.getDocument(), selectionStart, selectionEnd, newText));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.simiacryptus.aicoder.actions;

import com.github.simiacryptus.aicoder.config.AppSettingsState;
import com.github.simiacryptus.aicoder.openai.CompletionRequest;
import com.github.simiacryptus.aicoder.util.UITools;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Document;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class GenericAppend extends AnAction {

@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabledAndVisible(isEnabled(e));
super.update(e);
}

@SuppressWarnings("unused")
private static boolean isEnabled(@NotNull AnActionEvent e) {
Caret data = e.getData(CommonDataKeys.CARET);
if (!data.hasSelection()) return false;
return true;
}

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
Caret caret = event.getData(CommonDataKeys.CARET);
CharSequence before = Objects.requireNonNull(caret).getSelectedText();
AppSettingsState settings = AppSettingsState.getInstance();
CompletionRequest completionRequest = settings.createCompletionRequest().appendPrompt(before);
Document document = event.getRequiredData(CommonDataKeys.EDITOR).getDocument();
int selectionEnd = caret.getSelectionEnd();
UITools.redoableRequest(completionRequest, "", event, newText -> UITools.insertString(document, selectionEnd, newText));
}
}
Loading

0 comments on commit 1ff284c

Please sign in to comment.