Search in sources :

Example 1 with AceEditorNative

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative in project rstudio by rstudio.

the class DesktopApplicationHeader method isSelectionEmpty.

public static boolean isSelectionEmpty() {
    Element activeElement = DomUtils.getActiveElement();
    AceEditorNative editor = AceEditorNative.getEditor(activeElement);
    if (editor != null) {
        Selection selection = editor.getSession().getSelection();
        return selection.isEmpty();
    }
    // has focus)
    return false;
}
Also used : AceEditorNative(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative) Selection(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Selection) Element(com.google.gwt.dom.client.Element)

Example 2 with AceEditorNative

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative in project rstudio by rstudio.

the class ShortcutManager method handleKeyDown.

private boolean handleKeyDown(NativeEvent event) {
    // generated by Qt when commands executed from menu.
    if (activeEditEventType_ != EditEvent.TYPE_NONE) {
        final int type = activeEditEventType_;
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {

            @Override
            public void execute() {
                events_.fireEvent(new EditEvent(false, type));
            }
        });
        activeEditEventType_ = EditEvent.TYPE_NONE;
        keyBuffer_.clear();
        return false;
    }
    // modal dialogs)
    if (!isEnabled())
        return false;
    // Don't dispatch on bare modifier keypresses.
    if (KeyboardHelper.isModifierKey(event.getKeyCode()))
        return false;
    // practice, this is better than breaking copy + paste everywhere else...
    if (isEditKeyCombination(event)) {
        Element target = Element.as(event.getEventTarget());
        AceEditorNative editor = AceEditorNative.getEditor(target);
        if (editor == null) {
            keyBuffer_.clear();
            return false;
        }
    }
    // Escape key should always clear the keybuffer.
    if (event.getKeyCode() == KeyCodes.KEY_ESCAPE) {
        keyBuffer_.clear();
        return false;
    }
    int keyCode = event.getKeyCode();
    int modifier = KeyboardShortcut.getModifierValue(event);
    // since we have code wired to that expectation
    if (keyCode == 173)
        keyCode = 189;
    KeyCombination keyCombination = new KeyCombination(keyCode, modifier);
    // is focused.
    if (keyCombination.getKeyCode() == KeyCodes.KEY_F && keyCombination.getModifier() == KeyboardShortcut.CTRL) {
        Element target = Element.as(event.getEventTarget());
        AceEditorNative editor = AceEditorNative.getEditor(target);
        if (editor != null && editor.isVimModeOn()) {
            keyBuffer_.clear();
            return false;
        }
    }
    // Bail if this is an ignored key combination.
    if (isIgnoredKeyCombination(keyCombination)) {
        keyBuffer_.clear();
        return false;
    }
    keyBuffer_.add(keyCombination);
    // Loop through all active key maps, and attempt to find an active
    // binding. 'pending' is used to indicate whether there are any bindings
    // following the current state of the keybuffer.
    boolean pending = false;
    for (Map.Entry<KeyMapType, KeyMap> entry : keyMaps_.entrySet()) {
        KeyMap map = entry.getValue();
        CommandBinding binding = map.getActiveBinding(keyBuffer_);
        if (binding != null) {
            keyBuffer_.clear();
            if (XTermWidget.isXTerm(Element.as(event.getEventTarget()))) {
                if (binding.getId() == "consoleClear") {
                    // special case; we expect users will try to use Ctrl+L to
                    // clear the terminal, and don't want that to actually
                    // clear the currently hidden console instead
                    event.stopPropagation();
                    commands_.clearTerminalScrollbackBuffer().execute();
                    return false;
                } else if (binding.getId() == "closeSourceDoc") {
                    // when focus is in the terminal and let terminal see keys
                    return false;
                }
                if (binding.getContext() != AppCommand.Context.Workbench && binding.getContext() != AppCommand.Context.Addin && binding.getContext() != AppCommand.Context.PackageDevelopment) {
                    // Let terminal see the keyboard input and don't execute command.
                    return false;
                }
            }
            event.stopPropagation();
            binding.execute();
            return true;
        }
        if (map.isPrefix(keyBuffer_))
            pending = true;
    }
    if (!(pending || isPrefixForEditor(keyCombination, event)))
        keyBuffer_.clear();
    // 'I', and some other cases)
    if (!keyBuffer_.isEmpty()) {
        KeyCombination keys = keyBuffer_.get(keyBuffer_.size() - 1);
        if (keys.getModifier() == KeyboardShortcut.NONE)
            keyBuffer_.clear();
    }
    return false;
}
Also used : Element(com.google.gwt.dom.client.Element) KeyCombination(org.rstudio.core.client.command.KeyboardShortcut.KeyCombination) CommandBinding(org.rstudio.core.client.command.KeyMap.CommandBinding) AceEditorNative(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) EditEvent(org.rstudio.studio.client.events.EditEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) KeyMapType(org.rstudio.core.client.command.KeyMap.KeyMapType)

Example 3 with AceEditorNative

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative in project rstudio by rstudio.

the class ShortcutManager method isPrefixForEditor.

// TODO: In a perfect world, this function does not exist and
// instead we populate an editor key map based on the current state
// of the Ace editor, which we could check for prefix matches.
private boolean isPrefixForEditor(KeyCombination keys, NativeEvent event) {
    // Check to see if the event target was Ace.
    Element target = Element.as(event.getEventTarget());
    AceEditorNative editor = AceEditorNative.getEditor(target);
    if (editor == null)
        return false;
    if (editor.isEmacsModeOn()) {
        if (keys.isCtrlPressed()) {
            int keyCode = keys.getKeyCode();
            return keyCode == KeyCodes.KEY_C || keyCode == KeyCodes.KEY_X;
        }
    }
    return false;
}
Also used : AceEditorNative(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative) Element(com.google.gwt.dom.client.Element)

Example 4 with AceEditorNative

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative in project rstudio by rstudio.

the class SignatureToolTipManager method resolveActiveFunctionAndDisplayToolTip.

public void resolveActiveFunctionAndDisplayToolTip() {
    if (suppressed_) {
        suppressed_ = false;
        return;
    }
    if (docDisplay_.isPopupVisible())
        return;
    AceEditor editor = (AceEditor) docDisplay_;
    if (editor == null)
        return;
    final Position position = getLookupPosition();
    final boolean isMouseEvent = isMouseDrivenEvent();
    // Ensure that the mouse target is actually the active editor
    if (isMouseEvent) {
        Element el = DomUtils.elementFromPoint(coordinates_.getMouseX(), coordinates_.getMouseY());
        AceEditorNative nativeEditor = AceEditorNative.getEditor(el);
        if (nativeEditor != null && nativeEditor != editor.getWidget().getEditor())
            return;
    }
    // and then later moves the mouse away)
    if (isMouseEvent)
        toolTip_.hide();
    TokenCursor cursor = editor.getSession().getMode().getRCodeModel().getTokenCursor();
    if (!cursor.moveToPosition(position, true))
        return;
    // position is not changed.
    if (!isMouseEvent && uiPrefs_.showFunctionTooltipOnIdle().getGlobalValue() && !cursor.valueEquals("(")) {
        cursor.findOpeningBracket("(", false);
    }
    Token lookahead = cursor.peekFwd(1);
    if (lookahead.valueEquals("::") || lookahead.valueEquals(":::"))
        if (!cursor.moveToNextToken())
            return;
    if (cursor.valueEquals("::") || cursor.valueEquals(":::"))
        if (!cursor.moveToNextToken())
            return;
    if (cursor.valueEquals("("))
        if (!cursor.moveToPreviousToken())
            return;
    // then bail.
    if (toolTip_.isShowing() && cursor.currentPosition().isEqualTo(completionPosition_)) {
        return;
    }
    completionPosition_ = cursor.currentPosition();
    // be an opening paren.
    if (!cursor.hasType("identifier"))
        return;
    if (!cursor.nextValue().equals("("))
        return;
    String callString = cursor.currentValue();
    if (isBoringFunction(callString))
        return;
    // If this is a namespaced function call, then append that context.
    Token lookbehind = cursor.peekBwd(1);
    if (lookbehind.valueEquals("::") || lookbehind.valueEquals(":::")) {
        // Do-while loop just to allow 'break' for control flow
        do {
            TokenCursor clone = cursor.cloneCursor();
            if (!clone.moveToPreviousToken())
                break;
            if (!clone.moveToPreviousToken())
                break;
            if (!clone.hasType("identifier"))
                break;
            callString = clone.currentValue() + "::" + callString;
        } while (false);
    }
    // Set anchor (so we can dismiss popup when cursor moves outside
    // of anchored region)
    setAnchor(cursor.cloneCursor());
    final String fnString = callString;
    server_.getArgs(fnString, "", "", new ServerRequestCallback<String>() {

        @Override
        public void onResponseReceived(String arguments) {
            if (StringUtil.isNullOrEmpty(arguments))
                return;
            final String signature = fnString + arguments;
            resolvePositionAndShow(signature, position);
        }

        @Override
        public void onError(ServerError error) {
            Debug.logError(error);
        }
    });
}
Also used : AceEditorNative(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative) TokenCursor(org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ServerError(org.rstudio.studio.client.server.ServerError) Element(com.google.gwt.dom.client.Element) AceEditor(org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor) Token(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)

Aggregations

Element (com.google.gwt.dom.client.Element)4 AceEditorNative (org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative)4 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CommandBinding (org.rstudio.core.client.command.KeyMap.CommandBinding)1 KeyMapType (org.rstudio.core.client.command.KeyMap.KeyMapType)1 KeyCombination (org.rstudio.core.client.command.KeyboardShortcut.KeyCombination)1 EditEvent (org.rstudio.studio.client.events.EditEvent)1 ServerError (org.rstudio.studio.client.server.ServerError)1 AceEditor (org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor)1 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)1 Selection (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Selection)1 Token (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)1 TokenCursor (org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor)1