Search in sources :

Example 1 with AceFold

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

the class TextEditingTarget method fixupCodeBeforeSaving.

private void fixupCodeBeforeSaving() {
    int lineCount = docDisplay_.getRowCount();
    if (lineCount < 1)
        return;
    if (docDisplay_.hasActiveCollabSession()) {
        // these preferences don't apply to shared editing sessions
        return;
    }
    if (prefs_.stripTrailingWhitespace().getValue() && !fileType_.isMarkdown() && !name_.getValue().equals("DESCRIPTION")) {
        String code = docDisplay_.getCode();
        Pattern pattern = Pattern.create("[ \t]+$");
        String strippedCode = pattern.replaceAll(code, "");
        if (!strippedCode.equals(code)) {
            // Calling 'setCode' can remove folds in the document; cache the folds
            // and reapply them after document mutation.
            JsArray<AceFold> folds = docDisplay_.getFolds();
            docDisplay_.setCode(strippedCode, true);
            for (AceFold fold : JsUtil.asIterable(folds)) docDisplay_.addFold(fold.getRange());
        }
    }
    if (prefs_.autoAppendNewline().getValue() || fileType_.isPython()) {
        String lastLine = docDisplay_.getLine(lineCount - 1);
        if (lastLine.length() != 0)
            docDisplay_.insertCode(docDisplay_.getEnd().getEnd(), "\n");
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) AceFold(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold) JsArrayString(com.google.gwt.core.client.JsArrayString) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

Example 2 with AceFold

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

the class TextEditingTarget method onRenameInScope.

@Handler
void onRenameInScope() {
    docDisplay_.focus();
    // Save folds (we need to remove them temporarily for the rename helper)
    final JsArray<AceFold> folds = docDisplay_.getFolds();
    docDisplay_.unfoldAll();
    int matches = renameHelper_.renameInScope();
    if (matches <= 0) {
        if (!docDisplay_.getSelectionValue().isEmpty()) {
            String message = "No matches for '" + docDisplay_.getSelectionValue() + "'";
            view_.getStatusBar().showMessage(message, 1000);
        }
        for (AceFold fold : JsUtil.asIterable(folds)) docDisplay_.addFold(fold.getRange());
        return;
    }
    String message = "Found " + matches;
    if (matches == 1)
        message += " match";
    else
        message += " matches";
    String selectedItem = docDisplay_.getSelectionValue();
    message += " for " + selectedItem + ".";
    docDisplay_.disableSearchHighlight();
    view_.getStatusBar().showMessage(message, new HideMessageHandler() {

        private boolean onRenameFinished(boolean value) {
            for (AceFold fold : JsUtil.asIterable(folds)) docDisplay_.addFold(fold.getRange());
            return value;
        }

        @Override
        public boolean onNativePreviewEvent(NativePreviewEvent preview) {
            int type = preview.getTypeInt();
            if (docDisplay_.isPopupVisible())
                return false;
            // End if the user clicks somewhere
            if (type == Event.ONCLICK) {
                docDisplay_.exitMultiSelectMode();
                docDisplay_.clearSelection();
                docDisplay_.enableSearchHighlight();
                return onRenameFinished(true);
            } else // Otherwise, handle key events
            if (type == Event.ONKEYDOWN) {
                switch(preview.getNativeEvent().getKeyCode()) {
                    case KeyCodes.KEY_ENTER:
                        preview.cancel();
                    case KeyCodes.KEY_UP:
                    case KeyCodes.KEY_DOWN:
                    case KeyCodes.KEY_ESCAPE:
                        docDisplay_.exitMultiSelectMode();
                        docDisplay_.clearSelection();
                        docDisplay_.enableSearchHighlight();
                        return onRenameFinished(true);
                }
            }
            return false;
        }
    });
}
Also used : AceFold(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold) NativePreviewEvent(com.google.gwt.user.client.Event.NativePreviewEvent) HideMessageHandler(org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler) JsArrayString(com.google.gwt.core.client.JsArrayString) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) Handler(org.rstudio.core.client.command.Handler) ChangeFontSizeHandler(org.rstudio.studio.client.application.events.ChangeFontSizeHandler) RecordNavigationPositionHandler(org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler) EnsureHeightHandler(org.rstudio.core.client.events.EnsureHeightHandler) EnsureVisibleHandler(org.rstudio.core.client.events.EnsureVisibleHandler) HideMessageHandler(org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler) FileChangeHandler(org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)

Example 3 with AceFold

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

the class Fold method collect.

private static void collect(AceFold fold, ArrayList<Fold> results, Position parentOffset) {
    results.add(fromAceFold(fold, parentOffset));
    JsArray<AceFold> subFolds = fold.getSubFolds();
    for (int i = 0; i < subFolds.length(); i++) {
        AceFold subFold = subFolds.get(i);
        Position offset = Position.create(fold.getStart().getRow() + parentOffset.getRow(), fold.getStart().getColumn() + parentOffset.getColumn());
        collect(subFold, results, offset);
    }
}
Also used : AceFold(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Example 4 with AceFold

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

the class TextEditingTarget method onUnfold.

@Handler
void onUnfold() {
    if (useScopeTreeFolding()) {
        Range range = Range.fromPoints(docDisplay_.getSelectionStart(), docDisplay_.getSelectionEnd());
        if (range.isEmpty()) {
            // If no selection, either:
            //
            // 1) Unfold a fold containing the cursor, or
            // 2) Unfold the closest fold on the current row.
            Position pos = docDisplay_.getCursorPosition();
            AceFold containingCandidate = null;
            AceFold startCandidate = null;
            AceFold endCandidate = null;
            for (AceFold f : JsUtil.asIterable(docDisplay_.getFolds())) {
                // Check to see whether this fold contains the cursor position.
                if (f.getRange().contains(pos)) {
                    if (containingCandidate == null || containingCandidate.getRange().contains(f.getRange())) {
                        containingCandidate = f;
                    }
                }
                if (startCandidate == null && f.getStart().getRow() == pos.getRow() && f.getStart().getColumn() >= pos.getColumn()) {
                    startCandidate = f;
                }
                if (startCandidate == null && f.getEnd().getRow() == pos.getRow() && f.getEnd().getColumn() <= pos.getColumn()) {
                    endCandidate = f;
                }
            }
            if (containingCandidate != null) {
                docDisplay_.unfold(containingCandidate);
            } else if (startCandidate == null ^ endCandidate == null) {
                docDisplay_.unfold(startCandidate != null ? startCandidate : endCandidate);
            } else if (startCandidate != null && endCandidate != null) {
                // Both are candidates; see which is closer
                int startDelta = startCandidate.getStart().getColumn() - pos.getColumn();
                int endDelta = pos.getColumn() - endCandidate.getEnd().getColumn();
                docDisplay_.unfold(startDelta <= endDelta ? startCandidate : endCandidate);
            }
        } else {
            // If selection, unfold the selection
            docDisplay_.unfold(range);
        }
    } else {
        int row = docDisplay_.getSelectionStart().getRow();
        docDisplay_.unfold(row);
    }
}
Also used : AceFold(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) Handler(org.rstudio.core.client.command.Handler) ChangeFontSizeHandler(org.rstudio.studio.client.application.events.ChangeFontSizeHandler) RecordNavigationPositionHandler(org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler) EnsureHeightHandler(org.rstudio.core.client.events.EnsureHeightHandler) EnsureVisibleHandler(org.rstudio.core.client.events.EnsureVisibleHandler) HideMessageHandler(org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler) FileChangeHandler(org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)

Example 5 with AceFold

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

the class TextEditingTarget method onFoldAll.

@Handler
void onFoldAll() {
    if (useScopeTreeFolding()) {
        // Fold all except anonymous braces
        HashSet<Integer> rowsFolded = new HashSet<Integer>();
        for (AceFold f : JsUtil.asIterable(docDisplay_.getFolds())) rowsFolded.add(f.getStart().getRow());
        ScopeList scopeList = new ScopeList(docDisplay_);
        scopeList.removeAll(ScopeList.ANON_BRACE);
        for (Scope scope : scopeList) {
            int row = scope.getFoldStart().getRow();
            if (!rowsFolded.contains(row))
                docDisplay_.addFoldFromRow(row);
        }
    } else {
        docDisplay_.foldAll();
    }
}
Also used : AceFold(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) HashSet(java.util.HashSet) Handler(org.rstudio.core.client.command.Handler) ChangeFontSizeHandler(org.rstudio.studio.client.application.events.ChangeFontSizeHandler) RecordNavigationPositionHandler(org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler) EnsureHeightHandler(org.rstudio.core.client.events.EnsureHeightHandler) EnsureVisibleHandler(org.rstudio.core.client.events.EnsureVisibleHandler) HideMessageHandler(org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler) FileChangeHandler(org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)

Aggregations

AceFold (org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold)5 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)4 Handler (org.rstudio.core.client.command.Handler)3 EnsureHeightHandler (org.rstudio.core.client.events.EnsureHeightHandler)3 EnsureVisibleHandler (org.rstudio.core.client.events.EnsureVisibleHandler)3 ChangeFontSizeHandler (org.rstudio.studio.client.application.events.ChangeFontSizeHandler)3 FileChangeHandler (org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)3 HideMessageHandler (org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler)3 RecordNavigationPositionHandler (org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler)3 JsArrayString (com.google.gwt.core.client.JsArrayString)2 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)2 NativePreviewEvent (com.google.gwt.user.client.Event.NativePreviewEvent)1 HashSet (java.util.HashSet)1 Pattern (org.rstudio.core.client.regex.Pattern)1 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)1 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)1