use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token 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.Token in project rstudio by rstudio.
the class ImagePreviewer method onPreviewLink.
public static void onPreviewLink(DocDisplay display, DocUpdateSentinel sentinel, UIPrefs prefs, Position position) {
Token token = display.getTokenAt(position);
if (token == null)
return;
if (!token.hasType("href"))
return;
Range tokenRange = Range.fromPoints(Position.create(position.getRow(), token.getColumn()), Position.create(position.getRow(), token.getColumn() + token.getValue().length()));
String href = token.getValue();
if (ImagePreviewer.isImageHref(href)) {
// extract HTML attributes from line for markdown links, e.g.
//
// ![](plot.png){width=400 height=400}
//
String attributes = null;
String line = display.getLine(position.getRow());
if (isStandaloneMarkdownLink(line)) {
int startBraceIdx = line.indexOf("){");
int endBraceIdx = line.lastIndexOf("}");
if (startBraceIdx != -1 && endBraceIdx != -1 && endBraceIdx > startBraceIdx) {
attributes = line.substring(startBraceIdx + 2, endBraceIdx).trim();
}
}
onPreviewImage(display, sentinel, prefs, href, attributes, position, tokenRange);
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token in project rstudio by rstudio.
the class EditingTargetCodeExecution method executeInlineChunk.
private boolean executeInlineChunk() {
if (!docDisplay_.getSelection().isEmpty())
return false;
Token token = docDisplay_.getTokenAt(docDisplay_.getCursorPosition());
if (token == null || !token.hasType("inline_r_chunk"))
return false;
// construct range to execute, trimming off the "`r ...`" boundaries
int row = docDisplay_.getCursorPosition().getRow();
int startColumn = token.getColumn() + 3;
int endColumn = token.getColumn() + token.getValue().length() - 1;
Range range = Range.create(row, startColumn, row, endColumn);
inlineChunkExecutor_.execute(range);
return true;
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token in project rstudio by rstudio.
the class SignatureToolTipManager method setAnchor.
// Sets an anchored range for a cursor currently lying
// on an identifier before a '(' (a function call).
private void setAnchor(TokenCursor cursor) {
if (anchor_ != null) {
anchor_.detach();
anchor_ = null;
}
TokenCursor endCursor = cursor.cloneCursor();
if (!endCursor.moveToNextToken())
return;
if (!endCursor.valueEquals("("))
return;
// this function?
if (!endCursor.fwdToMatchingToken())
return;
Position endPos = endCursor.currentPosition();
TokenCursor startCursor = cursor.cloneCursor();
Token lookbehind = startCursor.peekBwd(1);
if (lookbehind.valueEquals("::") || lookbehind.valueEquals(":::")) {
if (!startCursor.moveToPreviousToken())
return;
if (!startCursor.moveToPreviousToken())
return;
}
Position startPos = startCursor.currentPosition();
anchor_ = docDisplay_.createAnchoredSelection(startPos, endPos);
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token in project rstudio by rstudio.
the class MathJaxUtil method getLatexRange.
public static Range getLatexRange(DocDisplay docDisplay, Position pos) {
if (pos == null)
pos = docDisplay.getCursorPosition();
// find start of latex block
TokenIterator startIt = docDisplay.createTokenIterator();
// avoid case where token iterator moves back across lines
// to discover a latex block
Token startToken = startIt.moveToPosition(pos);
if (startToken != null && startToken.hasAllTypes("latex", "end") && startIt.getCurrentTokenRow() != pos.getRow()) {
return null;
}
for (Token token = startIt.moveToPosition(pos); token != null; token = startIt.stepBackward()) {
if (!token.hasType("latex"))
return null;
if (token.hasType("begin"))
break;
}
// find end of latex block
TokenIterator endIt = docDisplay.createTokenIterator();
for (Token token = endIt.moveToPosition(pos); token != null; token = endIt.stepForward()) {
if (!token.hasType("latex"))
return null;
if (token.hasType("end"))
break;
}
Token lhsToken = startIt.getCurrentToken();
if (lhsToken == null || !lhsToken.hasAllTypes("latex", "begin"))
return null;
Token rhsToken = endIt.getCurrentToken();
if (rhsToken == null || !rhsToken.hasAllTypes("latex", "end"))
return null;
Position startPos = startIt.getCurrentTokenPosition();
Position endPos = endIt.getCurrentTokenPosition();
endPos.setColumn(endPos.getColumn() + endIt.getCurrentToken().getValue().length());
return Range.fromPoints(startPos, endPos);
}
Aggregations