use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.
the class FindReplace method replace.
private void replace() {
String searchString = display_.getFindValue().getValue();
if (searchString.length() == 0)
return;
Pattern pattern = createPattern();
String line = editor_.getCurrentLine();
Match m = pattern.match(line, editor_.getSelectionStart().getColumn());
if (m != null && m.getIndex() == editor_.getSelectionStart().getColumn() && m.getValue().length() == editor_.getSelectionValue().length()) {
String replacement = display_.getReplaceValue().getValue();
editor_.replaceSelection(display_.getRegex().getValue() ? substitute(m, replacement, line) : replacement);
if (targetSelection_ != null)
targetSelection_.syncMarker();
}
find(defaultForward_ ? FindType.Forward : FindType.Reverse);
}
use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.
the class FindReplace method replaceAll.
private void replaceAll() {
String code = null;
if (targetSelection_ != null) {
Range range = targetSelection_.getRange();
code = editor_.getCode(range.getStart(), range.getEnd());
} else {
code = editor_.getCode();
}
boolean regex = display_.getRegex().getValue();
String find = display_.getFindValue().getValue();
String repl = display_.getReplaceValue().getValue();
int occurrences = 0;
if (find.length() > 0) {
Pattern pattern = createPattern();
StringBuilder result = new StringBuilder();
// pointer into original string
int pos = 0;
for (Match m = pattern.match(code, 0); m != null; m = m.nextMatch()) {
occurrences++;
// Add everything between the end of the last match, and this one
int index = m.getIndex();
result.append(code, pos, index);
// Add the replacement value
if (regex)
result.append(substitute(m, repl, code));
else
result.append(repl);
// Point to the end of this match
pos = index + m.getValue().length();
// we'll loop forever (see case 4191). Bail out.
if (m.getValue().length() == 0) {
break;
}
}
result.append(code, pos, code.length());
String newCode = result.toString();
// either replace all or replace just the target range
if (targetSelection_ != null) {
// restore and then replace the selection
editor_.setSelectionRange(targetSelection_.getRange());
editor_.replaceSelection(newCode, false);
// reset the target selection
resetTargetSelection();
} else {
editor_.replaceCode(newCode);
}
}
globalDisplay_.showMessage(GlobalDisplay.MSG_INFO, errorCaption_, occurrences + " occurrences replaced.");
}
use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.
the class DefaultChunkOptionsPopupPanel method parseChunkHeader.
private void parseChunkHeader(String line, HashMap<String, String> chunkOptions) {
String modeId = display_.getModeId();
Pattern pattern = null;
if (modeId.equals("mode/rmarkdown"))
pattern = RegexUtil.RE_RMARKDOWN_CHUNK_BEGIN;
else if (modeId.equals("mode/sweave"))
pattern = RegexUtil.RE_SWEAVE_CHUNK_BEGIN;
else if (modeId.equals("mode/rhtml"))
pattern = RegexUtil.RE_RHTML_CHUNK_BEGIN;
if (pattern == null)
return;
Match match = pattern.match(line, 0);
if (match == null)
return;
String extracted = match.getGroup(1);
chunkPreamble_ = extractChunkPreamble(extracted, modeId);
String chunkLabel = extractChunkLabel(extracted);
if (!StringUtil.isNullOrEmpty(chunkLabel))
tbChunkLabel_.setText(extractChunkLabel(extracted));
// if we had a chunk label, then we want to navigate our cursor to
// the first comma in the chunk header; otherwise, we start at the
// first space. this is done to accept chunk headers of the form
//
// ```{r message=FALSE}
//
// ie, those with no comma after the engine used
int argsStartIdx = StringUtil.isNullOrEmpty(chunkLabel) ? extracted.indexOf(' ') : extracted.indexOf(',');
String arguments = extracted.substring(argsStartIdx + 1);
TextCursor cursor = new TextCursor(arguments);
// consume commas and whitespace if needed
cursor.consumeUntilRegex("[^\\s,]");
int startIndex = 0;
do {
if (!cursor.fwdToCharacter('=', false))
break;
int equalsIndex = cursor.getIndex();
int endIndex = arguments.length();
if (cursor.fwdToCharacter(',', true) || cursor.fwdToCharacter(' ', true)) {
endIndex = cursor.getIndex();
}
chunkOptions.put(arguments.substring(startIndex, equalsIndex).trim(), arguments.substring(equalsIndex + 1, endIndex).trim());
startIndex = cursor.getIndex() + 1;
} while (cursor.moveToNextCharacter());
}
use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.
the class RequestLogEntry method getRequestMethodName.
public String getRequestMethodName() {
if (requestData_.equals("[REDACTED]"))
return requestData_;
Pattern p = Pattern.create("\\\"method\\\":\\s*\\\"([^\"]+)\\\"");
Match match = p.match(requestData_, 0);
if (match == null)
return null;
return match.getGroup(1);
}
use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.
the class DomUtils method countLinesInternal.
private static int countLinesInternal(Text textNode, boolean pre) {
if (!pre)
return 0;
String value = textNode.getData();
Pattern pattern = Pattern.create("\\n");
int count = 0;
Match m = pattern.match(value, 0);
while (m != null) {
count++;
m = m.nextMatch();
}
return count;
}
Aggregations