Permalink
Browse files

control-enter to execute code in the console

  • Loading branch information...
jjallaire committed Mar 28, 2013
1 parent 6c6c995 commit 52f87a8ebec89a8ee7fcfe0b138cc7c13b72d488
@@ -0,0 +1,44 @@
+/*
+ * ConsoleExecutePendingInputEvent.java
+ *
+ * Copyright (C) 2009-12 by RStudio, Inc.
+ *
+ * Unless you have received this program directly from RStudio pursuant
+ * to the terms of a commercial license agreement with RStudio, then
+ * this program is licensed to you under the terms of version 3 of the
+ * GNU Affero General Public License. This program is distributed WITHOUT
+ * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
+ * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
+ *
+ */
+package org.rstudio.studio.client.workbench.views.console.events;
+
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.GwtEvent;
+
+public class ConsoleExecutePendingInputEvent extends GwtEvent<ConsoleExecutePendingInputEvent.Handler>
+{
+ public interface Handler extends EventHandler
+ {
+ void onExecutePendingInput(ConsoleExecutePendingInputEvent event);
+ }
+
+ public ConsoleExecutePendingInputEvent()
+ {
+ }
+
+ @Override
+ public Type<Handler> getAssociatedType()
+ {
+ return TYPE;
+ }
+
+ @Override
+ protected void dispatch(Handler handler)
+ {
+ handler.onExecutePendingInput(this);
+ }
+
+ public static final Type<Handler> TYPE = new Type<Handler>();
+}
@@ -62,6 +62,7 @@
ConsolePromptHandler,
ConsoleResetHistoryHandler,
ConsoleRestartRCompletedEvent.Handler,
+ ConsoleExecutePendingInputEvent.Handler,
SendToConsoleHandler
{
static interface Binder extends CommandBinder<Commands, Shell>
@@ -116,6 +117,7 @@ public Shell(ConsoleServerOperations server,
eventBus.addHandler(ConsolePromptEvent.TYPE, this);
eventBus.addHandler(ConsoleResetHistoryEvent.TYPE, this);
eventBus.addHandler(ConsoleRestartRCompletedEvent.TYPE, this);
+ eventBus.addHandler(ConsoleExecutePendingInputEvent.TYPE, this);
eventBus.addHandler(SendToConsoleEvent.TYPE, this);
final CompletionManager completionManager
@@ -329,6 +331,13 @@ public void execute()
inputAnimator_.enque(event.getCode(), finishSendToConsole);
}
}
+
+ @Override
+ public void onExecutePendingInput(ConsoleExecutePendingInputEvent event)
+ {
+ if (view_.getInputEditorDisplay().isFocused())
+ processCommandEntry();
+ }
private final class InputKeyDownHandler implements KeyDownHandler,
KeyPressHandler
@@ -32,6 +32,7 @@
Rectangle getPositionBounds(InputEditorPosition position);
Rectangle getBounds() ;
void setFocus(boolean focused) ;
+ boolean isFocused();
/**
* @param value New value
* @return Original value
@@ -634,6 +634,11 @@ public void focus()
widget_.getEditor().focus();
}
+ public boolean isFocused()
+ {
+ return widget_.getEditor().isFocused();
+ }
+
public void codeCompletion()
{
@@ -68,6 +68,7 @@
void setCode(String code, boolean preserveCursorPosition);
void insertCode(String code, boolean blockMode);
void focus();
+ boolean isFocused();
void print();
void codeCompletion();
void goToHelp();
@@ -81,6 +81,7 @@
import org.rstudio.studio.client.workbench.prefs.model.UIPrefs;
import org.rstudio.studio.client.workbench.prefs.model.UIPrefsAccessor;
import org.rstudio.studio.client.workbench.ui.FontSizeManager;
+import org.rstudio.studio.client.workbench.views.console.events.ConsoleExecutePendingInputEvent;
import org.rstudio.studio.client.workbench.views.console.events.SendToConsoleEvent;
import org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition;
import org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection;
@@ -1689,9 +1690,15 @@ else if (content.trim().startsWith("@"))
@Handler
void onExecuteCode()
- {
- docDisplay_.focus();
-
+ {
+ // allow console a chance to execute code if we aren't focused
+ if (!docDisplay_.isFocused())
+ {
+ events_.fireEvent(new ConsoleExecutePendingInputEvent());
+ return;
+ }
+
+
Range selectionRange = docDisplay_.getSelectionRange();
boolean noSelection = selectionRange.isEmpty();
if (noSelection)
@@ -78,6 +78,10 @@ public native final void toggleCommentLines() /*-{
public native final void focus() /*-{
this.focus();
}-*/;
+
+ public native final boolean isFocused() /*-{
+ return this.isFocused();
+ }-*/;
public native final void blur() /*-{
this.blur();

2 comments on commit 52f87a8

@randy3k

This comment has been minimized.

Show comment Hide comment
@randy3k

randy3k Feb 29, 2016

Contributor

@jjallaire
I just came across your commit to resolve the ctrl-enter issue listed in
https://support.rstudio.com/hc/en-us/community/posts/200633018-Make-Control-Enter-on-the-console-enter-the-console-command

However this commit didn't fix the problem completely, the ctrl-enter thing only works when there is at least one editor opened. When there is no opening editor, ctrl-enter has no response at all. I tried to look into the reverent code to fix it, but it seems it is not easy....

Contributor

randy3k replied Feb 29, 2016

@jjallaire
I just came across your commit to resolve the ctrl-enter issue listed in
https://support.rstudio.com/hc/en-us/community/posts/200633018-Make-Control-Enter-on-the-console-enter-the-console-command

However this commit didn't fix the problem completely, the ctrl-enter thing only works when there is at least one editor opened. When there is no opening editor, ctrl-enter has no response at all. I tried to look into the reverent code to fix it, but it seems it is not easy....

@jjallaire

This comment has been minimized.

Show comment Hide comment
@jjallaire

jjallaire Feb 29, 2016

Owner
Owner

jjallaire replied Feb 29, 2016

Please sign in to comment.