Search in sources :

Example 21 with Position

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

the class TextEditingTarget method onExecuteFromCurrentLine.

@Handler
void onExecuteFromCurrentLine() {
    docDisplay_.focus();
    int startRow = docDisplay_.getSelectionStart().getRow();
    int startColumn = 0;
    Position start = Position.create(startRow, startColumn);
    codeExecution_.executeRange(Range.fromPoints(start, endPosition()));
}
Also used : InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) 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 22 with Position

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

the class TextEditingTarget method reflowComments.

private void reflowComments(String commentPrefix, final boolean multiParagraphIndent, InputEditorSelection selection, final InputEditorPosition cursorPos) {
    String code = docDisplay_.getCode(selection);
    String[] lines = code.split("\n");
    String prefix = StringUtil.getCommonPrefix(lines, true, false);
    Pattern pattern = Pattern.create("^\\s*" + commentPrefix + "+('?)\\s*");
    Match match = pattern.match(prefix, 0);
    // Selection includes non-comments? Abort.
    if (match == null)
        return;
    prefix = match.getValue();
    final boolean roxygen = match.hasGroup(1);
    int cursorRowIndex = 0;
    int cursorColIndex = 0;
    if (cursorPos != null) {
        cursorRowIndex = selectionToPosition(cursorPos).getRow() - selectionToPosition(selection.getStart()).getRow();
        cursorColIndex = Math.max(0, cursorPos.getPosition() - prefix.length());
    }
    final WordWrapCursorTracker wwct = new WordWrapCursorTracker(cursorRowIndex, cursorColIndex);
    int maxLineLength = prefs_.printMarginColumn().getValue() - prefix.length();
    WordWrap wordWrap = new WordWrap(maxLineLength, false) {

        @Override
        protected boolean forceWrapBefore(String line) {
            String trimmed = line.trim();
            if (roxygen && trimmed.startsWith("@") && !trimmed.startsWith("@@")) {
                // Roxygen tags always need to be at the start of a line. If
                // there is content immediately following the roxygen tag, then
                // content should be wrapped until the next roxygen tag is
                // encountered.
                indent_ = "";
                if (TAG_WITH_CONTENTS.match(line, 0) != null) {
                    indentRestOfLines_ = true;
                }
                return true;
            } else // empty line disables indentation
            if (!multiParagraphIndent && (line.trim().length() == 0)) {
                indent_ = "";
                indentRestOfLines_ = false;
            }
            return super.forceWrapBefore(line);
        }

        @Override
        protected void onChunkWritten(String chunk, int insertionRow, int insertionCol, int indexInOriginalString) {
            if (indentRestOfLines_) {
                indentRestOfLines_ = false;
                // TODO: Use real indent from settings
                indent_ = "  ";
            }
            wwct.onChunkWritten(chunk, insertionRow, insertionCol, indexInOriginalString);
        }

        private boolean indentRestOfLines_ = false;

        private Pattern TAG_WITH_CONTENTS = Pattern.create("@\\w+\\s+[^\\s]");
    };
    for (String line : lines) {
        String content = line.substring(Math.min(line.length(), prefix.length()));
        if (content.matches("^\\s*\\@examples\\b.*$"))
            wordWrap.setWrappingEnabled(false);
        else if (content.trim().startsWith("@"))
            wordWrap.setWrappingEnabled(true);
        wwct.onBeginInputRow();
        wordWrap.appendLine(content);
    }
    String wrappedString = wordWrap.getOutput();
    StringBuilder finalOutput = new StringBuilder();
    for (String line : StringUtil.getLineIterator(wrappedString)) finalOutput.append(prefix).append(line).append("\n");
    // Remove final \n
    if (finalOutput.length() > 0)
        finalOutput.deleteCharAt(finalOutput.length() - 1);
    String reflowed = finalOutput.toString();
    docDisplay_.setSelection(selection);
    if (!reflowed.equals(code)) {
        docDisplay_.replaceSelection(reflowed);
    }
    if (cursorPos != null) {
        if (wwct.getResult() != null) {
            int row = wwct.getResult().getY();
            int col = wwct.getResult().getX();
            row += selectionToPosition(selection.getStart()).getRow();
            col += prefix.length();
            Position pos = Position.create(row, col);
            docDisplay_.setSelection(docDisplay_.createSelection(pos, pos));
        } else {
            docDisplay_.collapseSelection(false);
        }
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) JsArrayString(com.google.gwt.core.client.JsArrayString) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) Match(org.rstudio.core.client.regex.Match)

Example 23 with Position

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

the class TextEditingTarget method updateStatusBarPosition.

private void updateStatusBarPosition() {
    Position pos = docDisplay_.getCursorPosition();
    statusBar_.getPosition().setValue((pos.getRow() + 1) + ":" + (pos.getColumn() + 1));
}
Also used : InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Example 24 with Position

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

the class TextEditingTarget method getSelectionAsSourceLocation.

private SourceLocation getSelectionAsSourceLocation(boolean fromClick) {
    // get doc path (bail if the document is unsaved)
    String file = docUpdateSentinel_.getPath();
    if (file == null)
        return null;
    Position selPos = docDisplay_.getSelectionStart();
    int line = selPos.getRow() + 1;
    int column = selPos.getColumn() + 1;
    return SourceLocation.create(file, line, column, fromClick);
}
Also used : InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) JsArrayString(com.google.gwt.core.client.JsArrayString) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

Example 25 with Position

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

the class TextEditingTarget method onGoToStartOfCurrentScope.

@Handler
void onGoToStartOfCurrentScope() {
    docDisplay_.focus();
    Scope scope = docDisplay_.getCurrentScope();
    if (scope != null) {
        Position position = Position.create(scope.getBodyStart().getRow(), scope.getBodyStart().getColumn() + 1);
        docDisplay_.setCursorPosition(position);
    }
}
Also used : InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) 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

Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)64 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)20 JsArrayString (com.google.gwt.core.client.JsArrayString)16 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)11 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)11 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)7 FileChangeHandler (org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)7 TokenCursor (org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor)7 SourcePosition (org.rstudio.studio.client.workbench.views.source.model.SourcePosition)7 Handler (org.rstudio.core.client.command.Handler)6 EnsureHeightHandler (org.rstudio.core.client.events.EnsureHeightHandler)6 EnsureVisibleHandler (org.rstudio.core.client.events.EnsureVisibleHandler)6 ChangeFontSizeHandler (org.rstudio.studio.client.application.events.ChangeFontSizeHandler)6 ServerError (org.rstudio.studio.client.server.ServerError)6 AceEditor (org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor)6 Token (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)6 HideMessageHandler (org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler)6 RecordNavigationPositionHandler (org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler)6 InputEditorLineWithCursorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorLineWithCursorPosition)4 CodeModel (org.rstudio.studio.client.workbench.views.source.editors.text.ace.CodeModel)4