13
13
import com .intellij .openapi .actionSystem .DataContext ;
14
14
import com .intellij .openapi .command .WriteCommandAction ;
15
15
import com .intellij .openapi .editor .Caret ;
16
+ import com .intellij .openapi .editor .Document ;
16
17
import com .intellij .openapi .editor .Editor ;
17
18
import com .intellij .openapi .editor .actionSystem .EditorActionHandler ;
18
19
import com .intellij .openapi .ide .CopyPasteManager ;
19
20
import com .intellij .openapi .project .Project ;
20
21
import com .intellij .psi .PsiFile ;
22
+ import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
21
23
import net .sf .jsqlparser .util .validation .Validation ;
22
24
import net .sf .jsqlparser .util .validation .ValidationError ;
23
25
import net .sf .jsqlparser .util .validation .feature .FeaturesAllowed ;
27
29
import java .awt .datatransfer .DataFlavor ;
28
30
import java .util .Collections ;
29
31
import java .util .List ;
30
- import java .util .Objects ;
31
32
32
33
public class EditorPasteListener extends EditorActionHandler {
33
34
@@ -39,76 +40,91 @@ public EditorPasteListener(EditorActionHandler handler) {
39
40
40
41
@ Override
41
42
protected void doExecute (@ NotNull Editor editor , @ Nullable Caret caret , DataContext dataContext ) {
42
-
43
43
PsiFile file = CommonDataKeys .PSI_FILE .getData (dataContext );
44
+ if (!(file instanceof GoFile )) {
45
+ handler .execute (editor , caret , dataContext );
46
+ return ;
47
+ }
44
48
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
+ }
47
54
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
+ }
50
60
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
+ }
54
66
55
- Types .ORM selectedORM = state .defaultORM ;
56
- Types .Database selectedDatabase = state .defaultDatabase ;
67
+ Types .ORM selectedORM = state .defaultORM ;
68
+ Types .Database selectedDatabase = state .defaultDatabase ;
57
69
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
+ }
64
79
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 ;
68
82
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
+ }
95
87
88
+ ISQL2Struct sql2Struct = finalSelectedORM .sql2Struct (text , finalSelectedDatabase .toDbType ());
89
+ if (sql2Struct == null ) {
96
90
return ;
97
91
}
98
- }
99
92
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
+ });
101
118
}
102
119
103
120
private boolean verifySQL (String sql ) {
104
121
try {
122
+ CCJSqlParserUtil .parse (sql );
105
123
Validation validation = new Validation (Collections .singletonList (FeaturesAllowed .CREATE ), sql );
106
124
List <ValidationError > errors = validation .validate ();
107
-
108
125
return errors .isEmpty ();
109
126
} catch (Exception e ) {
110
127
return false ;
111
128
}
112
129
}
113
-
114
- }
130
+ }
0 commit comments