Skip to content

Commit

Permalink
0.1.6 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski authored Jan 10, 2023
1 parent 89ced21 commit d051887
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 63 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

## [Unreleased]

## [0.1.6]

### Added
- Fix compatibility issues

## [0.1.5]

### Added
- Added dropdown model selection
- Added dropdown model selection
- Improved comment parsing
- Added functions to create new list items and table rows and columns (markdown)

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pluginGroup = com.github.simiacryptus
pluginName = intellij-aicoder
pluginRepositoryUrl = https://github.com/SimiaCryptus/intellij-aicoder
# SemVer format -> https://semver.org
pluginVersion = 0.1.5
pluginVersion = 0.1.6

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 213
pluginSinceBuild = 203
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
120 changes: 64 additions & 56 deletions src/main/java/com/github/simiacryptus/aicoder/EditorMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public static boolean hasSelection(@NotNull AnActionEvent e) {
if (!language.docStyle.isEmpty()) children.add(docAction(extension, language));

if (language == ComputerLanguage.Markdown) {
addIfNotNull(children, markdownListAction(e, inputHumanLanguage));
addIfNotNull(children, markdownNewTableRowsAction(e, inputHumanLanguage));
addIfNotNull(children, markdownNewTableColsAction(e, inputHumanLanguage));
addIfNotNull(children, markdownNewTableColsAction2(e, inputHumanLanguage));
addIfNotNull(children, markdownListAction(e));
addIfNotNull(children, markdownNewTableRowsAction(e));
addIfNotNull(children, markdownNewTableColsAction(e));
addIfNotNull(children, markdownNewTableColsAction2(e));
}

if (hasSelection(e)) {
Expand Down Expand Up @@ -312,11 +312,14 @@ public static TextReplacementAction customEdit(String computerLanguage, String i
}

@Nullable
public static AnAction markdownListAction(@NotNull AnActionEvent e, String humanLanguage) {
Caret caret = e.getRequiredData(CommonDataKeys.CARET);
PsiFile psiFile = e.getRequiredData(CommonDataKeys.PSI_FILE);
PsiElement list = PsiUtil.getSmallestIntersecting(psiFile, caret.getSelectionStart(), caret.getSelectionEnd(), "MarkdownListImpl");
if (null != list) {
public static AnAction markdownListAction(@NotNull AnActionEvent e) {
try {
Caret caret = e.getData(CommonDataKeys.CARET);
if(null == caret) return null;
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
if(null == psiFile) return null;
PsiElement list = PsiUtil.getSmallestIntersecting(psiFile, caret.getSelectionStart(), caret.getSelectionEnd(), "MarkdownListImpl");
if (null == list) return null;
return new AnAction("Add _List Items", "Add list items", null) {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
Expand All @@ -335,8 +338,10 @@ public void actionPerformed(@NotNull AnActionEvent event) {
});
}
};
} catch (Exception ex) {
log.error(ex);
return null;
}
return null;
}

@NotNull
Expand Down Expand Up @@ -375,54 +380,54 @@ private static List<String> getNewItems(AppSettingsState settings, List<String>
}

@Nullable
public static AnAction markdownNewTableColsAction(@NotNull AnActionEvent e, String humanLanguage) {
Caret caret = e.getRequiredData(CommonDataKeys.CARET);
PsiFile psiFile = e.getRequiredData(CommonDataKeys.PSI_FILE);
public static AnAction markdownNewTableColsAction(@NotNull AnActionEvent e) {
Caret caret = e.getData(CommonDataKeys.CARET);
if (null == caret) return null;
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
if (null == psiFile) return null;
PsiElement table = PsiUtil.getSmallestIntersecting(psiFile, caret.getSelectionStart(), caret.getSelectionEnd(), "MarkdownTableImpl");
if (null != table) {
List<String> rows = Arrays.asList(transposeMarkdownTable(PsiUtil.getAll(table, "MarkdownTableRowImpl").stream().map(PsiElement::getText).collect(Collectors.joining("\n")), false, false).split("\n"));
String n = Integer.toString(rows.size() * 2);
return new AnAction("Add _Table Columns", "Add table columns", null) {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
AppSettingsState settings = AppSettingsState.getInstance();
String indent = getIndent(caret);
List<String> newRows = newRows(settings, n, rows, "");
String newTableTxt = transposeMarkdownTable(Stream.concat(rows.stream(), newRows.stream()).collect(Collectors.joining("\n")), false, true);
WriteCommandAction.runWriteCommandAction(event.getProject(), () -> {
final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
editor.getDocument().replaceString(table.getTextRange().getStartOffset(), table.getTextRange().getEndOffset(), newTableTxt.replace("\n", "\n" + indent));
});
}
};
}
return null;
if (null == table) return null;
List<String> rows = Arrays.asList(transposeMarkdownTable(PsiUtil.getAll(table, "MarkdownTableRowImpl").stream().map(PsiElement::getText).collect(Collectors.joining("\n")), false, false).split("\n"));
String n = Integer.toString(rows.size() * 2);
return new AnAction("Add _Table Columns", "Add table columns", null) {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
AppSettingsState settings = AppSettingsState.getInstance();
String indent = getIndent(caret);
List<String> newRows = newRows(settings, n, rows, "");
String newTableTxt = transposeMarkdownTable(Stream.concat(rows.stream(), newRows.stream()).collect(Collectors.joining("\n")), false, true);
WriteCommandAction.runWriteCommandAction(event.getProject(), () -> {
final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
editor.getDocument().replaceString(table.getTextRange().getStartOffset(), table.getTextRange().getEndOffset(), newTableTxt.replace("\n", "\n" + indent));
});
}
};
}

@Nullable
public static AnAction markdownNewTableColsAction2(@NotNull AnActionEvent e, String humanLanguage) {
Caret caret = e.getRequiredData(CommonDataKeys.CARET);
PsiFile psiFile = e.getRequiredData(CommonDataKeys.PSI_FILE);
public static AnAction markdownNewTableColsAction2(@NotNull AnActionEvent e) {
Caret caret = e.getData(CommonDataKeys.CARET);
if (null == caret) return null;
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
if (null == psiFile) return null;
PsiElement table = PsiUtil.getSmallestIntersecting(psiFile, caret.getSelectionStart(), caret.getSelectionEnd(), "MarkdownTableImpl");
if (null != table) {
List<String> rows = Arrays.asList(transposeMarkdownTable(PsiUtil.getAll(table, "MarkdownTableRowImpl").stream().map(PsiElement::getText).collect(Collectors.joining("\n")), false, false).split("\n"));
String n = Integer.toString(rows.size() * 2);
return new AnAction("Add Table _Column...", "Add table column...", null) {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
AppSettingsState settings = AppSettingsState.getInstance();
String indent = getIndent(caret);
String columnName = JOptionPane.showInputDialog(null, "Column Name:", "Add Column", JOptionPane.QUESTION_MESSAGE);
List<String> newRows = newRows(settings, n, rows, "| " + columnName + " | ");
String newTableTxt = transposeMarkdownTable(Stream.concat(rows.stream(), newRows.stream()).collect(Collectors.joining("\n")), false, true);
WriteCommandAction.runWriteCommandAction(event.getProject(), () -> {
final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
editor.getDocument().replaceString(table.getTextRange().getStartOffset(), table.getTextRange().getEndOffset(), newTableTxt.replace("\n", "\n" + indent));
});
}
};
}
return null;
if (null == table) return null;
List<String> rows = Arrays.asList(transposeMarkdownTable(PsiUtil.getAll(table, "MarkdownTableRowImpl").stream().map(PsiElement::getText).collect(Collectors.joining("\n")), false, false).split("\n"));
String n = Integer.toString(rows.size() * 2);
return new AnAction("Add Table _Column...", "Add table column...", null) {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
AppSettingsState settings = AppSettingsState.getInstance();
String indent = getIndent(caret);
String columnName = JOptionPane.showInputDialog(null, "Column Name:", "Add Column", JOptionPane.QUESTION_MESSAGE);
List<String> newRows = newRows(settings, n, rows, "| " + columnName + " | ");
String newTableTxt = transposeMarkdownTable(Stream.concat(rows.stream(), newRows.stream()).collect(Collectors.joining("\n")), false, true);
WriteCommandAction.runWriteCommandAction(event.getProject(), () -> {
final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
editor.getDocument().replaceString(table.getTextRange().getStartOffset(), table.getTextRange().getEndOffset(), newTableTxt.replace("\n", "\n" + indent));
});
}
};
}

static String transposeMarkdownTable(String table, boolean inputHeader, boolean outputHeader) {
Expand Down Expand Up @@ -467,10 +472,13 @@ private static String[][] parseMarkdownTable(String table, boolean removeHeader)
}

@Nullable
public static AnAction markdownNewTableRowsAction(@NotNull AnActionEvent e, String humanLanguage) {
Caret caret = e.getRequiredData(CommonDataKeys.CARET);
PsiFile psiFile = e.getRequiredData(CommonDataKeys.PSI_FILE);
public static AnAction markdownNewTableRowsAction(@NotNull AnActionEvent e) {
Caret caret = e.getData(CommonDataKeys.CARET);
if (null == caret) return null;
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
if (null == psiFile) return null;
PsiElement table = PsiUtil.getSmallestIntersecting(psiFile, caret.getSelectionStart(), caret.getSelectionEnd(), "MarkdownTableImpl");
if (null == table) return null;
if (null != table) {
List<String> rows = trim(PsiUtil.getAll(table, "MarkdownTableRowImpl").stream().map(PsiElement::getText).collect(Collectors.toList()), 10, true);
String n = Integer.toString(rows.size() * 2);
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/github/simiacryptus/aicoder/psi/PsiUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.simiacryptus.aicoder.psi;

import com.github.simiacryptus.aicoder.text.StringTools;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
Expand All @@ -10,6 +11,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

public class PsiUtil {

Expand All @@ -36,7 +38,7 @@ public void visitElement(@NotNull PsiElement element) {
TextRange textRange = element.getTextRange();
boolean within = (textRange.getStartOffset() <= selectionStart && textRange.getEndOffset() + 1 >= selectionStart && textRange.getStartOffset() <= selectionEnd && textRange.getEndOffset() + 1 >= selectionEnd);
String simpleName = element.getClass().getSimpleName();
if (Arrays.asList(types).contains(simpleName)) {
if (Arrays.asList(expand(types)).contains(simpleName)) {
if (within) {
largest.updateAndGet(s -> (s == null ? 0 : s.getText().length()) > element.getText().length() ? s : element);
}
Expand All @@ -55,7 +57,7 @@ public static List<PsiElement> getAll(@NotNull PsiElement element, String... typ
@Override
public void visitElement(@NotNull PsiElement element) {
if (null == element) return;
if (Arrays.asList(types).contains(element.getClass().getSimpleName())) {
if (Arrays.asList(expand(types)).contains(element.getClass().getSimpleName())) {
elements.add(element);
} else {
element.acceptChildren(visitor.get());
Expand Down Expand Up @@ -98,7 +100,7 @@ public void visitElement(@NotNull PsiElement element) {
TextRange textRange = element.getTextRange();
boolean within = (textRange.getStartOffset() <= selectionStart && textRange.getEndOffset() + 1 >= selectionStart && textRange.getStartOffset() <= selectionEnd && textRange.getEndOffset() + 1 >= selectionEnd);
String simpleName = element.getClass().getSimpleName();
if (Arrays.asList(types).contains(simpleName)) {
if (Arrays.asList(expand(types)).contains(simpleName)) {
if (within) {
largest.updateAndGet(s -> (s == null ? Integer.MAX_VALUE : s.getText().length()) < element.getText().length() ? s : element);
}
Expand All @@ -112,6 +114,10 @@ public void visitElement(@NotNull PsiElement element) {
return largest.get();
}

private static String[] expand(String[] types) {
return Arrays.stream(types).flatMap(x-> Stream.of(x, StringTools.stripSuffix(x, "Impl"))).distinct().toArray(String[]::new);
}

public static PsiElement getFirstBlock(@NotNull PsiElement element, String blockType) {
PsiElement[] children = element.getChildren();
if(null == children || 0 == children.length) return null;
Expand Down
6 changes: 6 additions & 0 deletions test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


1. Apple
2. Orange


0 comments on commit d051887

Please sign in to comment.