1919import com .intellij .openapi .editor .ex .EditorEx ;
2020import com .intellij .openapi .project .Project ;
2121import com .intellij .openapi .vfs .VirtualFile ;
22+ import org .jetbrains .annotations .NotNull ;
23+ import org .jetbrains .annotations .Nullable ;
2224
2325public class GraphQLExecuteEditorAction extends AnAction {
2426
@@ -27,54 +29,59 @@ public GraphQLExecuteEditorAction() {
2729 }
2830
2931 @ Override
30- public void update (AnActionEvent e ) {
32+ public void update (@ NotNull AnActionEvent e ) {
3133 final Editor editor = e .getData (CommonDataKeys .EDITOR_EVEN_IF_INACTIVE );
32- if (editor != null ) {
34+ if (editor != null ) {
3335 final GraphQLEndpointsModel endpointsModel = editor .getUserData (GraphQLUIProjectService .GRAPH_QL_ENDPOINTS_MODEL );
34- if (endpointsModel == null || endpointsModel .getSelectedItem () == null ) {
36+ if (endpointsModel == null || endpointsModel .getSelectedItem () == null ) {
3537 e .getPresentation ().setEnabled (false );
3638 return ;
3739 }
38- final Boolean querying = Boolean .TRUE .equals (editor .getUserData (GraphQLUIProjectService .GRAPH_QL_EDITOR_QUERYING ));
40+ boolean querying = Boolean .TRUE .equals (editor .getUserData (GraphQLUIProjectService .GRAPH_QL_EDITOR_QUERYING ));
3941 e .getPresentation ().setEnabled (!querying );
4042 }
4143 }
4244
4345 @ Override
44- public void actionPerformed (AnActionEvent e ) {
46+ public void actionPerformed (@ NotNull AnActionEvent e ) {
4547 VirtualFile virtualFile = e .getData (CommonDataKeys .VIRTUAL_FILE );
4648 final Project project = e .getData (CommonDataKeys .PROJECT );
47- if (isQueryableFile (project , virtualFile )) {
48- Editor editor = e .getData (CommonDataKeys .EDITOR );
49- if (project != null && editor instanceof EditorEx ) {
50- final Boolean querying = Boolean .TRUE .equals (editor .getUserData (GraphQLUIProjectService .GRAPH_QL_EDITOR_QUERYING ));
51- if (querying ) {
52- // already doing a query
53- return ;
54- }
55- final Editor queryEditor = editor .getUserData (GraphQLUIProjectService .GRAPH_QL_QUERY_EDITOR );
56- if (queryEditor != null ) {
57- // this action comes from the variables editor, so we need to resolve the query editor which contains the GraphQL
58- editor = queryEditor ;
59- virtualFile = CommonDataKeys .VIRTUAL_FILE .getData (((EditorEx )editor ).getDataContext ());
60- }
61- GraphQLUIProjectService .getService (project ).executeGraphQL (editor , virtualFile );
62- }
49+ if (project == null ) {
50+ return ;
51+ }
52+ if (!isQueryableFile (project , virtualFile )) {
53+ return ;
54+ }
55+ Editor editor = e .getData (CommonDataKeys .EDITOR );
56+ if (!(editor instanceof EditorEx )) {
57+ return ;
58+ }
59+
60+ boolean querying = Boolean .TRUE .equals (editor .getUserData (GraphQLUIProjectService .GRAPH_QL_EDITOR_QUERYING ));
61+ if (querying ) {
62+ // already doing a query
63+ return ;
64+ }
65+ Editor queryEditor = editor .getUserData (GraphQLUIProjectService .GRAPH_QL_QUERY_EDITOR );
66+ if (queryEditor != null ) {
67+ // this action comes from the variables editor, so we need to resolve the query editor which contains the GraphQL
68+ editor = queryEditor ;
69+ virtualFile = CommonDataKeys .VIRTUAL_FILE .getData (((EditorEx ) editor ).getDataContext ());
6370 }
71+ GraphQLUIProjectService .getService (project ).executeGraphQL (editor , virtualFile );
6472 }
6573
66- private boolean isQueryableFile (Project project , VirtualFile virtualFile ) {
67- if (virtualFile != null ) {
68- if (virtualFile .getFileType () == GraphQLFileType .INSTANCE ) {
69- return true ;
70- }
71- if (GraphQLFileType .isGraphQLScratchFile (project , virtualFile )) {
72- return true ;
73- }
74- if (virtualFile .getFileType () == JsonFileType .INSTANCE && Boolean .TRUE .equals (virtualFile .getUserData (GraphQLUIProjectService .IS_GRAPH_QL_VARIABLES_VIRTUAL_FILE ))) {
75- return true ;
76- }
74+ private boolean isQueryableFile (@ NotNull Project project , @ Nullable VirtualFile virtualFile ) {
75+ if (virtualFile == null ) {
76+ return false ;
77+ }
78+ if (virtualFile .getFileType () == GraphQLFileType .INSTANCE ) {
79+ return true ;
80+ }
81+ if (GraphQLFileType .isGraphQLScratchFile (project , virtualFile )) {
82+ return true ;
7783 }
78- return false ;
84+ return virtualFile .getFileType () == JsonFileType .INSTANCE && Boolean .TRUE .equals (
85+ virtualFile .getUserData (GraphQLUIProjectService .IS_GRAPH_QL_VARIABLES_VIRTUAL_FILE ));
7986 }
8087}
0 commit comments