use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range 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.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 RoxygenHelper method amendExistingRoxygenBlock.
private void amendExistingRoxygenBlock(int row, String objectName, JsArrayString argNames, JsArrayString argTypes, String tagName, Pattern pattern) {
// Get the range encompassing this Roxygen block.
Range range = getRoxygenBlockRange(row);
// Extract that block (as an array of strings)
JsArrayString block = extractRoxygenBlock(editor_.getWidget().getEditor(), range);
// bail.
for (int i = 0; i < block.length(); i++) {
if (RE_ROXYGEN_NONLOCAL.test(block.get(i))) {
view_.showWarningBar("Cannot automatically update roxygen blocks " + "that are not self-contained.");
return;
}
}
String roxygenDelim = RE_ROXYGEN.match(block.get(0), 0).getGroup(1);
// The replacement block (we build by munging parts of
// the old block
JsArrayString replacement = JsArray.createArray().cast();
// Scan through the block to get the names of
// pre-existing parameters.
JsArrayString params = listParametersInRoxygenBlock(block, pattern);
// Figure out what parameters need to be removed, and remove them.
// Any parameter not mentioned in the current function's argument list
// should be stripped out.
JsArrayString paramsToRemove = setdiff(params, argNames);
int blockLength = block.length();
for (int i = 0; i < blockLength; i++) {
// If we encounter a param we don't want to extract, then
// move over it.
Match match = pattern.match(block.get(i), 0);
if (match != null && contains(paramsToRemove, match.getGroup(1))) {
i++;
while (i < blockLength && !RE_ROXYGEN_WITH_TAG.test(block.get(i))) i++;
i--;
continue;
}
replacement.push(block.get(i));
}
// Now, add example roxygen for any parameters that are
// present in the function prototype, but not present
// within the roxygen block.
int insertionPosition = findParamsInsertionPosition(replacement, pattern);
JsArrayInteger indices = setdiffIndices(argNames, params);
// NOTE: modifies replacement
insertNewTags(replacement, argNames, argTypes, indices, roxygenDelim, tagName, insertionPosition);
// Ensure space between final param and next tag
ensureSpaceBetweenFirstParamAndPreviousEntry(replacement, roxygenDelim, pattern);
ensureSpaceBetweenFinalParamAndNextTag(replacement, roxygenDelim, pattern);
// Apply the replacement.
editor_.getSession().replace(range, replacement.join("\n") + "\n");
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class MathJaxRenderQueue method renderNext.
// Private Methods ----
private boolean renderNext() {
Range range = ranges_.poll();
if (range == null) {
isRunning_ = false;
return false;
}
isRunning_ = true;
mathjax_.renderLatex(range, false, callback_);
return true;
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class MathJaxUtil method isSelectionWithinLatexChunk.
public static boolean isSelectionWithinLatexChunk(DocDisplay docDisplay) {
Range range = getLatexRange(docDisplay);
if (range == null)
return false;
Token startToken = docDisplay.getTokenAt(range.getStart().getRow(), 0);
if (startToken == null || !startToken.getValue().equals("$$"))
return false;
Token endToken = docDisplay.getTokenAt(range.getEnd().getRow(), 0);
if (endToken == null || !endToken.getValue().equals("$$"))
return false;
return true;
}
Aggregations