Skip to content

Commit 1badfa4

Browse files
committed
Removed internal API usage from GraphQLExecuteEditorActionHandler
1 parent c5c6211 commit 1badfa4

File tree

2 files changed

+57
-68
lines changed

2 files changed

+57
-68
lines changed

src/main/com/intellij/lang/jsgraphql/ide/actions/GraphQLExecuteEditorAction.java

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.intellij.openapi.editor.ex.EditorEx;
2020
import com.intellij.openapi.project.Project;
2121
import com.intellij.openapi.vfs.VirtualFile;
22+
import org.jetbrains.annotations.NotNull;
23+
import org.jetbrains.annotations.Nullable;
2224

2325
public 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
}

src/main/com/intellij/lang/jsgraphql/ide/actions/GraphQLExecuteEditorActionHandler.java

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,46 @@
77
*/
88
package com.intellij.lang.jsgraphql.ide.actions;
99

10-
import com.intellij.ide.IdeEventQueue;
1110
import com.intellij.lang.jsgraphql.ide.project.GraphQLUIProjectService;
1211
import com.intellij.lang.jsgraphql.psi.GraphQLFile;
1312
import com.intellij.openapi.actionSystem.*;
1413
import com.intellij.openapi.editor.Caret;
1514
import com.intellij.openapi.editor.Editor;
1615
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
17-
import com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher;
18-
import com.intellij.openapi.keymap.impl.KeyProcessorContext;
1916
import com.intellij.openapi.vfs.VirtualFile;
17+
import com.intellij.psi.PsiFile;
18+
import org.jetbrains.annotations.NotNull;
2019
import org.jetbrains.annotations.Nullable;
2120

22-
import java.awt.event.InputEvent;
23-
2421
/**
2522
* Control+Enter is "split line" by default in IDEA and the JS GraphQL editor uses that binding to execute queries for .graphqil files.
2623
* This EditorActionHandler stops that default behavior and runs the "execute GraphQL" action.
2724
*/
2825
public class GraphQLExecuteEditorActionHandler extends EditorActionHandler {
2926

30-
private final EditorActionHandler myOriginalHandler;
27+
private final EditorActionHandler myDelegate;
3128

32-
public GraphQLExecuteEditorActionHandler(EditorActionHandler originalHandler) {
33-
this.myOriginalHandler = originalHandler;
29+
public GraphQLExecuteEditorActionHandler(EditorActionHandler delegate) {
30+
myDelegate = delegate;
3431
}
3532

3633
@Override
37-
protected void doExecute(Editor editor, @Nullable Caret caret, DataContext dataContext) {
38-
final Object file = dataContext.getData(LangDataKeys.PSI_FILE.getName());
39-
if(file instanceof GraphQLFile || isQueryVariablesFile(dataContext)) {
40-
final InputEvent event = getKeyboardEvent();
41-
if(event != null) {
42-
final AnAction executeGraphQLAction = ActionManager.getInstance().getAction(GraphQLExecuteEditorAction.class.getName());
43-
final AnActionEvent actionEvent = AnActionEvent.createFromInputEvent(event, ActionPlaces.EDITOR_TOOLBAR, executeGraphQLAction.getTemplatePresentation(), dataContext);
44-
executeGraphQLAction.actionPerformed(actionEvent);
45-
return;
46-
}
34+
protected void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataContext dataContext) {
35+
PsiFile file = dataContext.getData(LangDataKeys.PSI_FILE);
36+
if (file instanceof GraphQLFile || isQueryVariablesFile(dataContext)) {
37+
final AnAction executeGraphQLAction = ActionManager.getInstance().getAction(GraphQLExecuteEditorAction.class.getName());
38+
final AnActionEvent actionEvent = AnActionEvent.createFromInputEvent(null, ActionPlaces.EDITOR_TOOLBAR,
39+
executeGraphQLAction.getTemplatePresentation(), dataContext);
40+
executeGraphQLAction.actionPerformed(actionEvent);
41+
return;
4742
}
48-
myOriginalHandler.execute(editor, caret, dataContext);
43+
myDelegate.execute(editor, caret, dataContext);
4944
}
5045

5146
private boolean isQueryVariablesFile(DataContext dataContext) {
52-
final VirtualFile virtualFile = (VirtualFile)dataContext.getData(CommonDataKeys.VIRTUAL_FILE.getName());
53-
if(virtualFile != null && Boolean.TRUE.equals(virtualFile.getUserData(GraphQLUIProjectService.IS_GRAPH_QL_VARIABLES_VIRTUAL_FILE))) {
54-
return true;
55-
}
56-
return false;
57-
}
58-
59-
private InputEvent getKeyboardEvent() {
60-
final IdeKeyEventDispatcher keyEventDispatcher = IdeEventQueue.getInstance().getKeyEventDispatcher();
61-
if (keyEventDispatcher != null) {
62-
final KeyProcessorContext context = keyEventDispatcher.getContext();
63-
if (context != null) {
64-
return context.getInputEvent();
65-
}
66-
}
67-
return null;
47+
final VirtualFile virtualFile = dataContext.getData(CommonDataKeys.VIRTUAL_FILE);
48+
return virtualFile != null && Boolean.TRUE.equals(
49+
virtualFile.getUserData(GraphQLUIProjectService.IS_GRAPH_QL_VARIABLES_VIRTUAL_FILE));
6850
}
6951

7052
}

0 commit comments

Comments
 (0)