diff --git a/CHANGELOG.md b/CHANGELOG.md index f40a482b..989644cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/gradle.properties b/gradle.properties index f885ca9a..e32d2968 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/com/github/simiacryptus/aicoder/EditorMenu.java b/src/main/java/com/github/simiacryptus/aicoder/EditorMenu.java index 7725d086..8b977ff7 100644 --- a/src/main/java/com/github/simiacryptus/aicoder/EditorMenu.java +++ b/src/main/java/com/github/simiacryptus/aicoder/EditorMenu.java @@ -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)) { @@ -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) { @@ -335,8 +338,10 @@ public void actionPerformed(@NotNull AnActionEvent event) { }); } }; + } catch (Exception ex) { + log.error(ex); + return null; } - return null; } @NotNull @@ -375,54 +380,54 @@ private static List getNewItems(AppSettingsState settings, List } @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 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 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 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 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 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 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 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 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) { @@ -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 rows = trim(PsiUtil.getAll(table, "MarkdownTableRowImpl").stream().map(PsiElement::getText).collect(Collectors.toList()), 10, true); String n = Integer.toString(rows.size() * 2); diff --git a/src/main/java/com/github/simiacryptus/aicoder/psi/PsiUtil.java b/src/main/java/com/github/simiacryptus/aicoder/psi/PsiUtil.java index 96012f69..f4d4d3f2 100644 --- a/src/main/java/com/github/simiacryptus/aicoder/psi/PsiUtil.java +++ b/src/main/java/com/github/simiacryptus/aicoder/psi/PsiUtil.java @@ -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; @@ -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 { @@ -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); } @@ -55,7 +57,7 @@ public static List 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()); @@ -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); } @@ -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; diff --git a/test.md b/test.md new file mode 100644 index 00000000..24219e1d --- /dev/null +++ b/test.md @@ -0,0 +1,6 @@ + + +1. Apple +2. Orange + +