generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4537e47
commit 1ff284c
Showing
55 changed files
with
2,177 additions
and
1,190 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
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
688 changes: 0 additions & 688 deletions
688
src/main/java/com/github/simiacryptus/aicoder/EditorMenu.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
src/main/java/com/github/simiacryptus/aicoder/actions/CommentsAction.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 @@ | ||
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)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/github/simiacryptus/aicoder/actions/CustomEditAction.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,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)); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/github/simiacryptus/aicoder/actions/DescribeAction.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,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; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/github/simiacryptus/aicoder/actions/DocAction.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,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()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/github/simiacryptus/aicoder/actions/FromHumanLanguageAction.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,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)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/github/simiacryptus/aicoder/actions/GenericAppend.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,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)); | ||
} | ||
} |
Oops, something went wrong.