[groovy] console: updates

- display text for 'Select Module' action
- use editor notifications API to decorate console editor with toolbar
The toolbar was removed after ctrl+F. This change fixes it.
This commit is contained in:
Daniil Ovchinnikov
2017-05-05 14:03:49 +03:00
parent 9f60994585
commit acc7a353d5
6 changed files with 135 additions and 95 deletions
@@ -3,6 +3,7 @@
<scratch.rootType implementation="org.jetbrains.plugins.groovy.console.GroovyConsoleRootType"/>
<projectService serviceImplementation="org.jetbrains.plugins.groovy.console.GroovyConsoleStateService"/>
<resolveScopeProvider implementation="org.jetbrains.plugins.groovy.console.GroovyConsoleResolveScopeProvider"/>
<editorNotificationProvider implementation="org.jetbrains.plugins.groovy.console.GroovyConsoleEditorDecorator"/>
</extensions>
<actions>
@@ -0,0 +1,48 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.console
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.editor.impl.EditorHeaderComponent
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.EditorNotifications.Provider
import org.jetbrains.plugins.groovy.console.GroovyConsoleRootType.EXECUTE_ACTION
import org.jetbrains.plugins.groovy.console.actions.GrSelectModuleAction
import javax.swing.JComponent
class GroovyConsoleEditorDecorator(private val project: Project) : Provider<JComponent>() {
companion object {
private val myKey = Key.create<JComponent>("groovy.console.toolbar")
}
override fun getKey(): Key<JComponent> = myKey
override fun createNotificationPanel(file: VirtualFile, fileEditor: FileEditor): JComponent? {
val consoleService = GroovyConsoleStateService.getInstance(project)
if (!consoleService.isProjectConsole(file)) return null
val actionGroup = DefaultActionGroup(EXECUTE_ACTION, GrSelectModuleAction(project, file))
val menu = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, actionGroup, true)
return EditorHeaderComponent().apply {
add(menu.component)
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,8 @@
package org.jetbrains.plugins.groovy.console;
import com.intellij.execution.console.ConsoleRootType;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorHeaderComponent;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.CommonShortcuts;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
@@ -29,9 +28,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.GroovyFileType;
import org.jetbrains.plugins.groovy.console.actions.GrExecuteCommandAction;
import org.jetbrains.plugins.groovy.console.actions.GrSelectModuleAction;
import javax.swing.*;
public final class GroovyConsoleRootType extends ConsoleRootType {
@@ -77,18 +73,9 @@ public final class GroovyConsoleRootType extends ConsoleRootType {
@Override
public void fileOpened(@NotNull final VirtualFile file, @NotNull FileEditorManager source) {
final Project project = source.getProject();
final GroovyConsoleStateService projectConsole = GroovyConsoleStateService.getInstance(project);
for (FileEditor fileEditor : source.getAllEditors(file)) {
if (!(fileEditor instanceof TextEditor)) continue;
final Editor editor = ((TextEditor)fileEditor).getEditor();
final JPanel panel = new EditorHeaderComponent();
final DefaultActionGroup actionGroup = new DefaultActionGroup(EXECUTE_ACTION, new GrSelectModuleAction(projectConsole, file));
final ActionToolbar menu = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, actionGroup, true);
panel.add(menu.getComponent());
editor.setHeaderComponent(panel);
EXECUTE_ACTION.registerCustomShortcutSet(CommonShortcuts.CTRL_ENTER, editor.getComponent());
EXECUTE_ACTION.registerCustomShortcutSet(CommonShortcuts.CTRL_ENTER, fileEditor.getComponent());
}
}
}
@@ -1,78 +0,0 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.console.actions;
import com.intellij.icons.AllIcons;
import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.console.GroovyConsole;
import org.jetbrains.plugins.groovy.console.GroovyConsoleStateService;
import org.jetbrains.plugins.groovy.console.GroovyConsoleUtil;
public class GrSelectModuleAction extends AnAction {
private final GroovyConsoleStateService myProjectConsole;
private final VirtualFile myFile;
public GrSelectModuleAction(GroovyConsoleStateService console, VirtualFile file) {
super(null, "Which module to use classpath of?", AllIcons.Nodes.Module);
myProjectConsole = console;
myFile = file;
}
@Override
public boolean displayTextInToolbar() {
return super.displayTextInToolbar();
}
@Override
public void update(AnActionEvent e) {
if (myProjectConsole.isProjectConsole(myFile)) {
final Module module = myProjectConsole.getSelectedModule(myFile);
e.getPresentation().setText(getText(module));
}
else {
e.getPresentation().setVisible(false);
}
}
@NotNull
public static String getText(@Nullable Module module) {
return module == null || module.isDisposed() ? "Select module..." : GroovyConsoleUtil.getTitle(module);
}
@Override
public void actionPerformed(AnActionEvent e) {
final Project project = e.getProject();
if (project == null) return;
GroovyConsoleUtil.selectModuleAndRun(project, module -> {
final Module existingModule = myProjectConsole.getSelectedModule(myFile);
if (module.equals(existingModule)) return;
final GroovyConsole existingConsole = myFile.getUserData(GroovyConsole.GROOVY_CONSOLE);
if (existingConsole != null) existingConsole.stop();
myProjectConsole.setFileModule(myFile, module);
myFile.putUserData(GroovyConsole.GROOVY_CONSOLE, null);
ProjectView.getInstance(project).refresh();
}, e.getDataContext());
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.console.actions
import com.intellij.icons.AllIcons
import com.intellij.ide.projectView.ProjectView
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.Consumer
import org.jetbrains.plugins.groovy.console.GroovyConsole
import org.jetbrains.plugins.groovy.console.GroovyConsoleStateService
import org.jetbrains.plugins.groovy.console.GroovyConsoleUtil
import org.jetbrains.plugins.groovy.util.removeUserData
class GrSelectModuleAction(
private val project: Project,
private val file: VirtualFile
) : AnAction(null, "Which module to use classpath of?", AllIcons.Nodes.Module) {
private val consoleService by lazy {
GroovyConsoleStateService.getInstance(project)
}
override fun update(e: AnActionEvent) {
val module = consoleService.getSelectedModule(file)
e.presentation.text = if (module == null || module.isDisposed) "Select module..." else GroovyConsoleUtil.getTitle(module)
}
override fun displayTextInToolbar(): Boolean {
return true
}
override fun actionPerformed(e: AnActionEvent) {
GroovyConsoleUtil.selectModuleAndRun(project, Consumer { module: Module ->
if (consoleService.getSelectedModule(file) == module) return@Consumer
file.removeUserData(GroovyConsole.GROOVY_CONSOLE)?.stop()
consoleService.setFileModule(file, module)
ProjectView.getInstance(project).refresh()
}, e.dataContext)
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.util
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.UserDataHolder
fun <T> UserDataHolder.removeUserData(key: Key<T>): T? {
val data = getUserData(key)
putUserData(key, null)
return data
}