1313import com .intellij .openapi .actionSystem .DataContext ;
1414import com .intellij .openapi .command .WriteCommandAction ;
1515import com .intellij .openapi .editor .Caret ;
16+ import com .intellij .openapi .editor .Document ;
1617import com .intellij .openapi .editor .Editor ;
1718import com .intellij .openapi .editor .actionSystem .EditorActionHandler ;
1819import com .intellij .openapi .ide .CopyPasteManager ;
1920import com .intellij .openapi .project .Project ;
2021import com .intellij .psi .PsiFile ;
22+ import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
2123import net .sf .jsqlparser .util .validation .Validation ;
2224import net .sf .jsqlparser .util .validation .ValidationError ;
2325import net .sf .jsqlparser .util .validation .feature .FeaturesAllowed ;
2729import java .awt .datatransfer .DataFlavor ;
2830import java .util .Collections ;
2931import java .util .List ;
30- import java .util .Objects ;
3132
3233public class EditorPasteListener extends EditorActionHandler {
3334
@@ -39,76 +40,91 @@ public EditorPasteListener(EditorActionHandler handler) {
3940
4041 @ Override
4142 protected void doExecute (@ NotNull Editor editor , @ Nullable Caret caret , DataContext dataContext ) {
42-
4343 PsiFile file = CommonDataKeys .PSI_FILE .getData (dataContext );
44+ if (!(file instanceof GoFile )) {
45+ handler .execute (editor , caret , dataContext );
46+ return ;
47+ }
4448
45- if ((file instanceof GoFile )) {
46- String text = CopyPasteManager .getInstance ().getContents (DataFlavor .stringFlavor );
49+ String text = CopyPasteManager .getInstance ().getContents (DataFlavor .stringFlavor );
50+ if (!verifySQL (text )) {
51+ handler .execute (editor , caret , dataContext );
52+ return ;
53+ }
4754
48- if (verifySQL (text )) {
49- Project project = editor .getProject ();
55+ Project project = editor .getProject ();
56+ if (project == null ) {
57+ handler .execute (editor , caret , dataContext );
58+ return ;
59+ }
5060
51- GoORMHelperProjectSettings .State state = Objects .requireNonNull (
52- GoORMHelperProjectSettings .getInstance (Objects .requireNonNull (project )).getState ()
53- );
61+ GoORMHelperProjectSettings .State state = GoORMHelperProjectSettings .getInstance (project ).getState ();
62+ if (state == null ) {
63+ handler .execute (editor , caret , dataContext );
64+ return ;
65+ }
5466
55- Types .ORM selectedORM = state .defaultORM ;
56- Types .Database selectedDatabase = state .defaultDatabase ;
67+ Types .ORM selectedORM = state .defaultORM ;
68+ Types .Database selectedDatabase = state .defaultDatabase ;
5769
58- if (selectedORM == Types .ORM .AskEveryTime || selectedDatabase == Types .Database .AskEveryTime ) {
59- ConvertSettingDialogWrapper wrapper = new ConvertSettingDialogWrapper (project );
60- if (!wrapper .showAndGet ()) {
61- this .handler .execute (editor , caret , dataContext );
62- return ;
63- }
70+ if (selectedORM == Types .ORM .AskEveryTime || selectedDatabase == Types .Database .AskEveryTime ) {
71+ ConvertSettingDialogWrapper wrapper = new ConvertSettingDialogWrapper (project );
72+ if (!wrapper .showAndGet ()) {
73+ handler .execute (editor , caret , dataContext );
74+ return ;
75+ }
76+ selectedORM = (Types .ORM ) wrapper .getOrmComponent ().getComponent ().getSelectedItem ();
77+ selectedDatabase = (Types .Database ) wrapper .getDatabaseComponent ().getComponent ().getSelectedItem ();
78+ }
6479
65- selectedORM = (Types .ORM ) wrapper .getOrmComponent ().getComponent ().getSelectedItem ();
66- selectedDatabase = (Types .Database ) wrapper .getDatabaseComponent ().getComponent ().getSelectedItem ();
67- }
80+ final Types .ORM finalSelectedORM = selectedORM ;
81+ final Types .Database finalSelectedDatabase = selectedDatabase ;
6882
69- final Types .ORM finalSelectedORM = selectedORM ;
70- final Types .Database finalSelectedDatabase = selectedDatabase ;
71-
72- WriteCommandAction .runWriteCommandAction (editor .getProject (), () -> {
73- if (text == null || text .isEmpty () || finalSelectedORM == null || finalSelectedDatabase == null )
74- return ;
75-
76- ISQL2Struct sql2Struct = finalSelectedORM .sql2Struct (text , finalSelectedDatabase .toDbType ());
77-
78- Caret currentCaret = editor .getCaretModel ().getCurrentCaret ();
79- int start = currentCaret .getSelectionStart ();
80-
81- try {
82- editor .getDocument ().insertString (start , sql2Struct .convert ());
83- } catch (Exception ignored ) {
84- Notifications .Bus .notify (
85- new Notification (
86- GoORMHelperBundle .message ("name" ),
87- GoORMHelperBundle .message ("sql.convert.struct.not.support" ),
88- GoORMHelperBundle .message ("sql.convert.struct.check" ),
89- NotificationType .WARNING ),
90- project
91- );
92- this .handler .execute (editor , caret , dataContext );
93- }
94- });
83+ WriteCommandAction .runWriteCommandAction (project , () -> {
84+ if (text == null || text .isEmpty () || finalSelectedORM == null || finalSelectedDatabase == null ) {
85+ return ;
86+ }
9587
88+ ISQL2Struct sql2Struct = finalSelectedORM .sql2Struct (text , finalSelectedDatabase .toDbType ());
89+ if (sql2Struct == null ) {
9690 return ;
9791 }
98- }
9992
100- this .handler .execute (editor , caret , dataContext );
93+ String struct = sql2Struct .convert ();
94+ Caret currentCaret = editor .getCaretModel ().getCurrentCaret ();
95+ int start = currentCaret .getSelectionStart ();
96+ int end = currentCaret .getSelectionEnd ();
97+ Document document = editor .getDocument ();
98+
99+ try {
100+ if (start == end ) {
101+ document .insertString (start , struct );
102+ } else {
103+ document .replaceString (start , end , struct );
104+ }
105+ currentCaret .moveToOffset (start + struct .length ());
106+ } catch (Exception ignored ) {
107+ Notifications .Bus .notify (
108+ new Notification (
109+ GoORMHelperBundle .message ("name" ),
110+ GoORMHelperBundle .message ("sql.convert.struct.not.support" ),
111+ GoORMHelperBundle .message ("sql.convert.struct.check" ),
112+ NotificationType .WARNING ),
113+ project
114+ );
115+ handler .execute (editor , caret , dataContext );
116+ }
117+ });
101118 }
102119
103120 private boolean verifySQL (String sql ) {
104121 try {
122+ CCJSqlParserUtil .parse (sql );
105123 Validation validation = new Validation (Collections .singletonList (FeaturesAllowed .CREATE ), sql );
106124 List <ValidationError > errors = validation .validate ();
107-
108125 return errors .isEmpty ();
109126 } catch (Exception e ) {
110127 return false ;
111128 }
112129 }
113-
114- }
130+ }
0 commit comments