Search in sources :

Example 11 with Position

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

the class Source method doOpenSourceFile.

private void doOpenSourceFile(final FileSystemItem file, final TextFileType fileType, final FilePosition position, final String pattern, final int navMethod, final boolean forceHighlightMode) {
    // if the navigation should happen in another window, do that instead
    NavigationResult navResult = windowManager_.navigateToFile(file, position, navMethod);
    // we navigated externally, just skip this
    if (navResult.getType() == NavigationResult.RESULT_NAVIGATED)
        return;
    // we're about to open in this window--if it's the main window, focus it
    if (SourceWindowManager.isMainSourceWindow() && Desktop.isDesktop())
        Desktop.getFrame().bringMainFrameToFront();
    final boolean isDebugNavigation = navMethod == NavigationMethods.DEBUG_STEP || navMethod == NavigationMethods.DEBUG_END;
    final CommandWithArg<EditingTarget> editingTargetAction = new CommandWithArg<EditingTarget>() {

        @Override
        public void execute(EditingTarget target) {
            if (position != null) {
                SourcePosition endPosition = null;
                if (isDebugNavigation) {
                    DebugFilePosition filePos = (DebugFilePosition) position.cast();
                    endPosition = SourcePosition.create(filePos.getEndLine() - 1, filePos.getEndColumn() + 1);
                    if (Desktop.isDesktop() && navMethod != NavigationMethods.DEBUG_END)
                        Desktop.getFrame().bringMainFrameToFront();
                }
                navigate(target, SourcePosition.create(position.getLine() - 1, position.getColumn() - 1), endPosition);
            } else if (pattern != null) {
                Position pos = target.search(pattern);
                if (pos != null) {
                    navigate(target, SourcePosition.create(pos.getRow(), 0), null);
                }
            }
        }

        private void navigate(final EditingTarget target, final SourcePosition srcPosition, final SourcePosition srcEndPosition) {
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                @Override
                public void execute() {
                    if (navMethod == NavigationMethods.DEBUG_STEP) {
                        target.highlightDebugLocation(srcPosition, srcEndPosition, true);
                    } else if (navMethod == NavigationMethods.DEBUG_END) {
                        target.endDebugHighlighting();
                    } else {
                        // force highlight mode if requested
                        if (forceHighlightMode)
                            target.forceLineHighlighting();
                        // now navigate to the new position
                        boolean highlight = navMethod == NavigationMethods.HIGHLIGHT_LINE && !uiPrefs_.highlightSelectedLine().getValue();
                        target.navigateToPosition(srcPosition, false, highlight);
                    }
                }
            });
        }
    };
    if (navResult.getType() == NavigationResult.RESULT_RELOCATE) {
        server_.getSourceDocument(navResult.getDocId(), new ServerRequestCallback<SourceDocument>() {

            @Override
            public void onResponseReceived(final SourceDocument doc) {
                editingTargetAction.execute(addTab(doc, OPEN_REPLAY));
            }

            @Override
            public void onError(ServerError error) {
                globalDisplay_.showErrorMessage("Document Tab Move Failed", "Couldn't move the tab to this window: \n" + error.getMessage());
            }
        });
        return;
    }
    final CommandWithArg<FileSystemItem> action = new CommandWithArg<FileSystemItem>() {

        @Override
        public void execute(FileSystemItem file) {
            openFile(file, fileType, editingTargetAction);
        }
    };
    // highlight in place.
    if (isDebugNavigation) {
        setPendingDebugSelection();
        for (int i = 0; i < editors_.size(); i++) {
            EditingTarget target = editors_.get(i);
            String path = target.getPath();
            if (path != null && path.equalsIgnoreCase(file.getPath())) {
                // the file's open; just update its highlighting 
                if (navMethod == NavigationMethods.DEBUG_END) {
                    target.endDebugHighlighting();
                } else {
                    view_.selectTab(i);
                    editingTargetAction.execute(target);
                }
                return;
            }
        }
        // open a file just to turn off debug highlighting in the file!
        if (navMethod == NavigationMethods.DEBUG_END)
            return;
    }
    // Warning: event.getFile() can be null (e.g. new Sweave document)
    if (file != null && file.getLength() < 0) {
        // If the file has no size info, stat the file from the server. This
        // is to prevent us from opening large files accidentally.
        server_.stat(file.getPath(), new ServerRequestCallback<FileSystemItem>() {

            @Override
            public void onResponseReceived(FileSystemItem response) {
                action.execute(response);
            }

            @Override
            public void onError(ServerError error) {
                // Couldn't stat the file? Proceed anyway. If the file doesn't
                // exist, we'll let the downstream code be the one to show the
                // error.
                action.execute(file);
            }
        });
    } else {
        action.execute(file);
    }
}
Also used : SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ServerError(org.rstudio.studio.client.server.ServerError) TextEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget) EditingTarget(org.rstudio.studio.client.workbench.views.source.editors.EditingTarget) DataEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget) CodeBrowserEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget) SourceDocument(org.rstudio.studio.client.workbench.views.source.model.SourceDocument) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString) FileSystemItem(org.rstudio.core.client.files.FileSystemItem) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) NavigationResult(org.rstudio.studio.client.workbench.views.source.SourceWindowManager.NavigationResult)

Example 12 with Position

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

the class AceEditorIdleCommands method onPreviewLatex.

private void onPreviewLatex(TextEditingTarget target, DocUpdateSentinel sentinel, IdleState state) {
    Position position = resolvePosition(target.getDocDisplay(), state);
    Range range = MathJaxUtil.getLatexRange(target.getDocDisplay(), position);
    if (range == null)
        return;
    String pref = prefs_.showLatexPreviewOnCursorIdle().getValue();
    // document
    if (sentinel.getBoolProperty(TextEditingTargetNotebook.CONTENT_PREVIEW_ENABLED, pref != UIPrefsAccessor.LATEX_PREVIEW_SHOW_NEVER)) {
        target.renderLatex(range, true);
    }
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 13 with Position

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

the class AceEditorWidget method toggleBreakpointAtCursor.

public void toggleBreakpointAtCursor() {
    Position pos = editor_.getSession().getSelection().getCursor();
    toggleBreakpointAtPosition(Position.create(pos.getRow(), 0));
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Example 14 with Position

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

the class AceEditorWidget method toggleBreakpointAtPosition.

private void toggleBreakpointAtPosition(Position pos) {
    // rows are 0-based, but debug line numbers are 1-based
    int lineNumber = lineFromRow(pos.getRow());
    int breakpointIdx = getBreakpointIdxByLine(lineNumber);
    // if there's already a breakpoint on that line, remove it
    if (breakpointIdx >= 0) {
        Breakpoint breakpoint = breakpoints_.get(breakpointIdx);
        removeBreakpointMarker(breakpoint);
        fireEvent(new BreakpointSetEvent(lineNumber, breakpoint.getBreakpointId(), false));
        breakpoints_.remove(breakpointIdx);
    } else // if there's no breakpoint on that line yet, create a new unset
    // breakpoint there (the breakpoint manager will pick up the new
    // breakpoint and attempt to set it on the server)
    {
        try {
            // non-whitespace, non-comment token
            if (editor_.getSession().getMode().getCodeModel() != null) {
                Position tokenPos = editor_.getSession().getMode().getCodeModel().findNextSignificantToken(pos);
                if (tokenPos != null) {
                    lineNumber = lineFromRow(tokenPos.getRow());
                    if (getBreakpointIdxByLine(lineNumber) >= 0) {
                        return;
                    }
                } else {
                    // set a breakpoint
                    return;
                }
            }
        } catch (Exception e) {
        // If we failed at any point to fast-forward to the next line with
        // a statement, we'll try to set a breakpoint on the line the user
        // originally clicked. 
        }
        fireEvent(new BreakpointSetEvent(lineNumber, BreakpointSetEvent.UNSET_BREAKPOINT_ID, true));
    }
}
Also used : Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

Example 15 with Position

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

the class AceEditorWidget method updateBreakpoints.

private void updateBreakpoints(AceDocumentChangeEventNative changeEvent) {
    // if there are no breakpoints, don't do any work to move them about
    if (breakpoints_.size() == 0) {
        return;
    }
    // see if we need to move any breakpoints around in response to 
    // this change to the document's text
    String action = changeEvent.getAction();
    Range range = changeEvent.getRange();
    Position start = range.getStart();
    Position end = range.getEnd();
    // in a way that could change lines, we can't have moved anything
    if (start.getRow() == end.getRow() || (!action.equals("insertText") && !action.equals("insertLines") && !action.equals("removeText") && !action.equals("removeLines"))) {
        return;
    }
    int shiftedBy = 0;
    int shiftStartRow = 0;
    // compute how many rows to shift
    if (action == "insertText" || action == "insertLines") {
        shiftedBy = end.getRow() - start.getRow();
    } else {
        shiftedBy = start.getRow() - end.getRow();
    }
    // compute where to start shifting
    shiftStartRow = start.getRow() + ((action == "insertText" && start.getColumn() > 0) ? 1 : 0);
    // make a pass through the breakpoints and move them as appropriate:
    // remove all the breakpoints after the row where the change
    // happened, and add them back at their new position if they were
    // not part of a deleted range. 
    ArrayList<Breakpoint> movedBreakpoints = new ArrayList<Breakpoint>();
    for (int idx = 0; idx < breakpoints_.size(); idx++) {
        Breakpoint breakpoint = breakpoints_.get(idx);
        int breakpointRow = rowFromLine(breakpoint.getEditorLineNumber());
        if (breakpointRow >= shiftStartRow) {
            // remove the breakpoint from its old position
            movedBreakpoints.add(breakpoint);
            removeBreakpointMarker(breakpoint);
        }
    }
    for (Breakpoint breakpoint : movedBreakpoints) {
        // calculate the new position of the breakpoint
        int oldBreakpointPosition = rowFromLine(breakpoint.getEditorLineNumber());
        int newBreakpointPosition = oldBreakpointPosition + shiftedBy;
        // breakpoint there
        if (oldBreakpointPosition >= end.getRow() && !(oldBreakpointPosition == end.getRow() && shiftedBy < 0) && getBreakpointIdxByLine(lineFromRow(newBreakpointPosition)) < 0) {
            breakpoint.moveToLineNumber(lineFromRow(newBreakpointPosition));
            placeBreakpointMarker(breakpoint);
            fireEvent(new BreakpointMoveEvent(breakpoint.getBreakpointId()));
        } else {
            breakpoints_.remove(breakpoint);
            fireEvent(new BreakpointSetEvent(breakpoint.getEditorLineNumber(), breakpoint.getBreakpointId(), false));
        }
    }
}
Also used : Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ArrayList(java.util.ArrayList) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) AnchoredRange(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AnchoredRange) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

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