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);
}
}
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);
}
}
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));
}
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));
}
}
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));
}
}
}
Aggregations