use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class MathJaxUtil method findLatexChunks.
public static List<Range> findLatexChunks(DocDisplay docDisplay) {
docDisplay.tokenizeDocument();
List<Range> ranges = new ArrayList<Range>();
Position startPos = null;
for (int i = 0, n = docDisplay.getRowCount(); i < n; i++) {
Position pos = Position.create(i, 0);
Token token = docDisplay.getTokenAt(Position.create(i, 0));
if (token == null)
continue;
if (token.hasAllTypes("latex", "begin") && token.getValue().equals("$$")) {
startPos = pos;
// get the length of this line to see if it could be an inline
// LaTeX chunk (e.g. $$ x = y $$)
int length = docDisplay.getLength(i);
if (length < 5)
continue;
// get the last token on the row; if it's a LaTeX end token then
// consider the row to be an inline LaTeX chunk
Token endLineToken = docDisplay.getTokenAt(Position.create(i, docDisplay.getLength(i)));
if (endLineToken != null && endLineToken.hasAllTypes("latex", "end")) {
ranges.add(Range.fromPoints(startPos, Position.create(i, length)));
}
continue;
}
if (token.hasAllTypes("latex", "end") && token.getValue().equals("$$")) {
ranges.add(Range.fromPoints(startPos, Position.create(i, 2)));
continue;
}
}
return ranges;
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range 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.Range 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));
}
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class AceEditorWidget method removeMarkersAtCursorPosition.
public void removeMarkersAtCursorPosition() {
// Defer this so other event handling can update anchors etc.
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
Position cursor = editor_.getCursorPosition();
JsArray<AceAnnotation> newAnnotations = JsArray.createArray().cast();
for (int i = 0; i < annotations_.size(); i++) {
AnchoredAceAnnotation annotation = annotations_.get(i);
int markerId = annotation.getMarkerId();
Marker marker = editor_.getSession().getMarker(markerId);
// a previous action.
if (marker == null)
continue;
Range range = marker.getRange();
if (!range.contains(cursor))
newAnnotations.push(annotation.asAceAnnotation());
else
editor_.getSession().removeMarker(markerId);
}
editor_.getSession().setAnnotations(newAnnotations);
editor_.getRenderer().renderMarkers();
}
});
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class TextEditingTarget method onInsertChunk.
private void onInsertChunk(String chunkPlaceholder, int rowOffset, int colOffset) {
String sel = null;
Range selRange = null;
// if currently in a chunk, add a blank line (for padding) and insert
// beneath it
Scope currentChunk = docDisplay_.getCurrentChunk();
if (currentChunk != null) {
// record current selection before manipulating text
sel = docDisplay_.getSelectionValue();
selRange = docDisplay_.getSelectionRange();
docDisplay_.setCursorPosition(currentChunk.getEnd());
docDisplay_.insertCode("\n");
docDisplay_.moveCursorForward(1);
}
Position pos = moveCursorToNextInsertLocation();
InsertChunkInfo insertChunkInfo = docDisplay_.getInsertChunkInfo();
if (insertChunkInfo != null) {
// inject the chunk skeleton
docDisplay_.insertCode(chunkPlaceholder, false);
// if we had text selected, inject it into the chunk
if (!StringUtil.isNullOrEmpty(sel)) {
Position contentPosition = insertChunkInfo.getContentPosition();
Position docContentPos = Position.create(pos.getRow() + contentPosition.getRow(), contentPosition.getColumn());
Position endPos = Position.create(docContentPos.getRow(), docContentPos.getColumn());
// move over newline if selected
if (sel.endsWith("\n"))
endPos.setRow(endPos.getRow() + 1);
docDisplay_.replaceRange(Range.fromPoints(docContentPos, endPos), sel);
docDisplay_.replaceRange(selRange, "");
}
Position cursorPosition = insertChunkInfo.getCursorPosition();
docDisplay_.setCursorPosition(Position.create(pos.getRow() + cursorPosition.getRow() + rowOffset, colOffset));
docDisplay_.focus();
} else {
assert false : "Mode did not have insertChunkInfo available";
}
}
Aggregations