diff --git a/CHANGELOG.md b/CHANGELOG.md index fd373eb..37d34f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,17 @@ ## [Unreleased] +## [1.2.10] - 2024-09-29 + +### Fixed + +- Fix SQL2Struct copy and paste bug. +- Fix SQL2Struct comment line break bug. + +### Changed + +- Bump guava + ## [1.2.9] - 2024-08-22 ### Fixed @@ -140,7 +151,8 @@ - ORM Code Completion - SQL to Struct -[Unreleased]: https://github.com/maiqingqiang/go-orm-helper/compare/v1.2.9...HEAD +[Unreleased]: https://github.com/maiqingqiang/go-orm-helper/compare/v1.2.10...HEAD +[1.2.9]: https://github.com/maiqingqiang/go-orm-helper/compare/v1.2.9...v1.2.10 [1.2.9]: https://github.com/maiqingqiang/go-orm-helper/compare/v1.2.8...v1.2.9 [1.2.8]: https://github.com/maiqingqiang/go-orm-helper/compare/v1.2.7...v1.2.8 [1.2.7]: https://github.com/maiqingqiang/go-orm-helper/compare/v1.2.6...v1.2.7 diff --git a/gradle.properties b/gradle.properties index 1152aa5..708be77 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = com.github.maiqingqiang.goormhelper pluginName = Go ORM Helper pluginRepositoryUrl = https://github.com/maiqingqiang/go-orm-helper # SemVer format -> https://semver.org -pluginVersion = 1.2.9 +pluginVersion = 1.2.10 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild = 232 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0fe104f..a771578 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,7 +2,7 @@ # libraries junit = "5.11.0" lombok = "1.18.34" -guava = "33.3.0-jre" +guava = "33.3.1-jre" druid = "1.2.23" jsqlparser = "5.0" evoInflector = "1.3" diff --git a/src/main/java/com/github/maiqingqiang/goormhelper/actions/EditorPasteListener.java b/src/main/java/com/github/maiqingqiang/goormhelper/actions/EditorPasteListener.java index 2b53da1..bb684b4 100644 --- a/src/main/java/com/github/maiqingqiang/goormhelper/actions/EditorPasteListener.java +++ b/src/main/java/com/github/maiqingqiang/goormhelper/actions/EditorPasteListener.java @@ -13,11 +13,13 @@ import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Caret; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.actionSystem.EditorActionHandler; import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; import net.sf.jsqlparser.util.validation.Validation; import net.sf.jsqlparser.util.validation.ValidationError; import net.sf.jsqlparser.util.validation.feature.FeaturesAllowed; @@ -27,7 +29,6 @@ import java.awt.datatransfer.DataFlavor; import java.util.Collections; import java.util.List; -import java.util.Objects; public class EditorPasteListener extends EditorActionHandler { @@ -39,76 +40,91 @@ public EditorPasteListener(EditorActionHandler handler) { @Override protected void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataContext dataContext) { - PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext); + if (!(file instanceof GoFile)) { + handler.execute(editor, caret, dataContext); + return; + } - if ((file instanceof GoFile)) { - String text = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); + String text = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); + if (!verifySQL(text)) { + handler.execute(editor, caret, dataContext); + return; + } - if (verifySQL(text)) { - Project project = editor.getProject(); + Project project = editor.getProject(); + if (project == null) { + handler.execute(editor, caret, dataContext); + return; + } - GoORMHelperProjectSettings.State state = Objects.requireNonNull( - GoORMHelperProjectSettings.getInstance(Objects.requireNonNull(project)).getState() - ); + GoORMHelperProjectSettings.State state = GoORMHelperProjectSettings.getInstance(project).getState(); + if (state == null) { + handler.execute(editor, caret, dataContext); + return; + } - Types.ORM selectedORM = state.defaultORM; - Types.Database selectedDatabase = state.defaultDatabase; + Types.ORM selectedORM = state.defaultORM; + Types.Database selectedDatabase = state.defaultDatabase; - if (selectedORM == Types.ORM.AskEveryTime || selectedDatabase == Types.Database.AskEveryTime) { - ConvertSettingDialogWrapper wrapper = new ConvertSettingDialogWrapper(project); - if (!wrapper.showAndGet()) { - this.handler.execute(editor, caret, dataContext); - return; - } + if (selectedORM == Types.ORM.AskEveryTime || selectedDatabase == Types.Database.AskEveryTime) { + ConvertSettingDialogWrapper wrapper = new ConvertSettingDialogWrapper(project); + if (!wrapper.showAndGet()) { + handler.execute(editor, caret, dataContext); + return; + } + selectedORM = (Types.ORM) wrapper.getOrmComponent().getComponent().getSelectedItem(); + selectedDatabase = (Types.Database) wrapper.getDatabaseComponent().getComponent().getSelectedItem(); + } - selectedORM = (Types.ORM) wrapper.getOrmComponent().getComponent().getSelectedItem(); - selectedDatabase = (Types.Database) wrapper.getDatabaseComponent().getComponent().getSelectedItem(); - } + final Types.ORM finalSelectedORM = selectedORM; + final Types.Database finalSelectedDatabase = selectedDatabase; - final Types.ORM finalSelectedORM = selectedORM; - final Types.Database finalSelectedDatabase = selectedDatabase; - - WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> { - if (text == null || text.isEmpty() || finalSelectedORM == null || finalSelectedDatabase == null) - return; - - ISQL2Struct sql2Struct = finalSelectedORM.sql2Struct(text, finalSelectedDatabase.toDbType()); - - Caret currentCaret = editor.getCaretModel().getCurrentCaret(); - int start = currentCaret.getSelectionStart(); - - try { - editor.getDocument().insertString(start, sql2Struct.convert()); - } catch (Exception ignored) { - Notifications.Bus.notify( - new Notification( - GoORMHelperBundle.message("name"), - GoORMHelperBundle.message("sql.convert.struct.not.support"), - GoORMHelperBundle.message("sql.convert.struct.check"), - NotificationType.WARNING), - project - ); - this.handler.execute(editor, caret, dataContext); - } - }); + WriteCommandAction.runWriteCommandAction(project, () -> { + if (text == null || text.isEmpty() || finalSelectedORM == null || finalSelectedDatabase == null) { + return; + } + ISQL2Struct sql2Struct = finalSelectedORM.sql2Struct(text, finalSelectedDatabase.toDbType()); + if (sql2Struct == null) { return; } - } - this.handler.execute(editor, caret, dataContext); + String struct = sql2Struct.convert(); + Caret currentCaret = editor.getCaretModel().getCurrentCaret(); + int start = currentCaret.getSelectionStart(); + int end = currentCaret.getSelectionEnd(); + Document document = editor.getDocument(); + + try { + if (start == end) { + document.insertString(start, struct); + } else { + document.replaceString(start, end, struct); + } + currentCaret.moveToOffset(start + struct.length()); + } catch (Exception ignored) { + Notifications.Bus.notify( + new Notification( + GoORMHelperBundle.message("name"), + GoORMHelperBundle.message("sql.convert.struct.not.support"), + GoORMHelperBundle.message("sql.convert.struct.check"), + NotificationType.WARNING), + project + ); + handler.execute(editor, caret, dataContext); + } + }); } private boolean verifySQL(String sql) { try { + CCJSqlParserUtil.parse(sql); Validation validation = new Validation(Collections.singletonList(FeaturesAllowed.CREATE), sql); List errors = validation.validate(); - return errors.isEmpty(); } catch (Exception e) { return false; } } - -} +} \ No newline at end of file diff --git a/src/main/java/com/github/maiqingqiang/goormhelper/sql2struct/impl/SQL2Struct.java b/src/main/java/com/github/maiqingqiang/goormhelper/sql2struct/impl/SQL2Struct.java index fa59ab3..7681638 100644 --- a/src/main/java/com/github/maiqingqiang/goormhelper/sql2struct/impl/SQL2Struct.java +++ b/src/main/java/com/github/maiqingqiang/goormhelper/sql2struct/impl/SQL2Struct.java @@ -164,7 +164,13 @@ protected String getColumn(@NotNull SQLColumnDefinition definition) { protected String getComment(@NotNull SQLColumnDefinition definition) { if (definition.getComment() == null) return ""; - return Strings.clearSingleQuotn(definition.getComment().toString()); + String comment = definition.getComment().toString(); + + comment = Strings.clearSingleQuotn(comment); + + comment = comment.replaceAll("\\R", " "); + + return comment; } }