use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class DomUtils method trimLines.
private static int trimLines(NodeList<Node> nodes, final int linesToTrim) {
if (nodes == null || nodes.getLength() == 0 || linesToTrim == 0)
return 0;
int linesLeft = linesToTrim;
Node node = nodes.getItem(0);
while (node != null && linesLeft > 0) {
switch(node.getNodeType()) {
case Node.ELEMENT_NODE:
if (((Element) node).getTagName().equalsIgnoreCase("br")) {
linesLeft--;
node = removeAndGetNext(node);
continue;
} else {
int trimmed = trimLines(node.getChildNodes(), linesLeft);
linesLeft -= trimmed;
if (!node.hasChildNodes())
node = removeAndGetNext(node);
continue;
}
case Node.TEXT_NODE:
String text = ((Text) node).getData();
Match lastMatch = null;
Match match = NEWLINE.match(text, 0);
while (match != null && linesLeft > 0) {
lastMatch = match;
linesLeft--;
match = match.nextMatch();
}
if (linesLeft > 0 || lastMatch == null) {
node = removeAndGetNext(node);
continue;
} else {
int index = lastMatch.getIndex() + 1;
if (text.length() == index)
node.removeFromParent();
else
((Text) node).deleteData(0, index);
break;
}
}
}
return linesToTrim - linesLeft;
}
use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class FileSystemItem method getIcon.
// RStudio-specific code should use FileTypeRegistry.getIconForFile() instead
public final ImageResource getIcon() {
if (isDirectory()) {
if (isPublicFolder())
return new ImageResource2x(RES.iconPublicFolder2x());
else
return new ImageResource2x(RES.iconFolder2x());
}
Match m = EXT_PATTERN.match(getName(), 0);
if (m == null)
return new ImageResource2x(RES.iconText2x());
String lowerExt = m.getValue().toLowerCase();
if (lowerExt.equals(".csv")) {
return new ImageResource2x(RES.iconCsv2x());
} else if (lowerExt.equals(".pdf")) {
return new ImageResource2x(RES.iconPdf2x());
} else if (lowerExt.equals(".jpg") || lowerExt.equals(".jpeg") || lowerExt.equals(".gif") || lowerExt.equals(".bmp") || lowerExt.equals(".tiff") || lowerExt.equals(".tif") || lowerExt.equals(".png")) {
return new ImageResource2x(RES.iconPng2x());
} else {
return new ImageResource2x(RES.iconText2x());
}
}
use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class VirtualConsole method submit.
public void submit(String data, String clazz) {
if (CONTROL.match(data, 0) == null) {
text(data, clazz);
return;
}
int tail = 0;
Match match = CONTROL.match(data, 0);
while (match != null) {
int pos = match.getIndex();
// character, add it.
if (tail != pos)
text(data.substring(tail, pos), clazz);
tail = pos + 1;
switch(data.charAt(pos)) {
case '\r':
carriageReturn();
break;
case '\b':
backspace();
break;
case '\n':
newline(clazz);
break;
case '\f':
formfeed();
break;
default:
assert false : "Unknown control char, please check regex";
text(data.charAt(pos) + "", clazz);
break;
}
match = match.nextMatch();
}
// If there was any plain text after the last control character, add it
if (tail < data.length())
text(data.substring(tail), clazz);
}
use of org.rstudio.core.client.regex.Match 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.core.client.regex.Match in project rstudio by rstudio.
the class RTokenizer method eatUntil.
private String eatUntil(String regex, boolean eatAllOnFailure) {
int start = pos_;
Match match = Pattern.create(regex).match(data_, pos_);
if (match == null) {
if (eatAllOnFailure) {
pos_ = data_.length();
return data_.substring(start);
} else {
return null;
}
} else {
pos_ = match.getIndex();
return data_.substring(start, pos_);
}
}
Aggregations