use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class TextEditingTargetReformatHelper method getAlignmentRanges.
private ArrayList<Pair<Integer, Integer>> getAlignmentRanges() {
int selectionStart = docDisplay_.getSelectionStart().getRow();
int selectionEnd = docDisplay_.getSelectionEnd().getRow();
ArrayList<Pair<Integer, Integer>> ranges = new ArrayList<Pair<Integer, Integer>>();
for (int i = selectionStart; i <= selectionEnd; i++) {
String line = docDisplay_.getLine(i);
String masked = StringUtil.maskStrings(line);
Match match = DELIM_PATTERN.match(masked, 0);
if (match != null) {
String delimiter = match.getGroup(2);
int rangeStart = i;
while (i++ <= selectionEnd) {
line = docDisplay_.getLine(i);
masked = StringUtil.maskStrings(line);
// Allow empty lines, and comments, to live within the range.
if (masked.matches("^\\s*$") || masked.matches("^\\s*#.*$"))
continue;
// If this line doesn't match, bail
match = DELIM_PATTERN.match(masked, 0);
if (match == null || !match.getGroup(2).equals(delimiter))
break;
}
// But don't allow comments or whitespaces to exist at the
// end of a range.
int rangeEnd = i - 1;
line = docDisplay_.getLine(rangeEnd);
while (line.matches("^\\s*$") || line.matches("^\\s*#.*$")) {
rangeEnd--;
line = docDisplay_.getLine(rangeEnd);
}
ranges.add(new Pair<Integer, Integer>(rangeStart, rangeEnd));
}
}
return ranges;
}
use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class TextEditingTargetReformatHelper method doAlignAssignment.
private void doAlignAssignment(int startRow, int endRow) {
docDisplay_.setSelectionRange(Range.fromPoints(Position.create(startRow, 0), Position.create(endRow, docDisplay_.getLine(endRow).length())));
String[] splat = docDisplay_.getSelectionValue().split("\n");
ArrayList<String> starts = new ArrayList<String>();
ArrayList<String> delimiters = new ArrayList<String>();
ArrayList<String> ends = new ArrayList<String>();
for (int i = 0; i < splat.length; i++) {
String line = splat[i];
String masked = StringUtil.maskStrings(splat[i]);
Match match = DELIM_PATTERN.match(masked, 0);
if (match == null) {
starts.add(line);
delimiters.add("");
ends.add("");
} else {
String start = line.substring(0, match.getGroup(1).length());
start = start.replaceAll("\\s*$", "");
starts.add(start);
delimiters.add(match.getGroup(2));
int endOfDelim = match.getGroup(1).length() + match.getGroup(2).length();
String end = line.substring(endOfDelim);
end = end.replaceAll("^\\s*", "");
ends.add(end);
}
}
// Transform the ends if they appear numeric-y -- we want to
// right-align numbers, e.g.
//
// x = 1,
// y = 10,
// z = 100
//
ArrayList<Integer> endPrefixes = new ArrayList<Integer>();
boolean success = true;
for (int i = 0; i < ends.size(); i++) {
String current = ends.get(i).replaceAll("[\\s,\\);]*", "");
try {
endPrefixes.add(("" + Integer.parseInt(current)).length());
} catch (Exception e) {
success = false;
break;
}
}
if (success) {
int maxLength = 0;
for (int i = 0; i < endPrefixes.size(); i++) maxLength = Math.max(maxLength, endPrefixes.get(i));
for (int i = 0; i < ends.size(); i++) ends.set(i, StringUtil.repeat(" ", maxLength - endPrefixes.get(i)) + ends.get(i).replaceAll("^\\s*", ""));
}
// Pad the 'start's with whitespace, to align the delimiter.
int maxLength = 0;
for (int i = 0; i < starts.size(); i++) maxLength = Math.max(maxLength, starts.get(i).replaceAll("\\s*$", "").length());
for (int i = 0; i < starts.size(); i++) starts.set(i, starts.get(i) + StringUtil.repeat(" ", maxLength - starts.get(i).length()));
// Build a new selection by concatenating the (transformed)
// pieces.
StringBuilder newSelectionBuilder = new StringBuilder();
for (int i = 0; i < starts.size(); i++) {
newSelectionBuilder.append(starts.get(i));
newSelectionBuilder.append(" " + delimiters.get(i) + " ");
newSelectionBuilder.append(ends.get(i));
if (i < starts.size() - 1)
newSelectionBuilder.append("\n");
}
docDisplay_.replaceSelection(newSelectionBuilder.toString());
}
use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class TextEditingTargetCompilePdfHelper method getRnwOptionsStart.
@Override
public int getRnwOptionsStart(String line, int cursorPos) {
Pattern pattern = docDisplay_.getFileType().getRnwStartPatternBegin();
if (pattern == null)
return -1;
String linePart = line.substring(0, cursorPos);
Match match = pattern.match(linePart, 0);
if (match == null)
return -1;
// See if the cursor is already past the end of the chunk header,
// for example <<foo>>=[CURSOR].
Pattern patternEnd = docDisplay_.getFileType().getRnwStartPatternEnd();
if (patternEnd != null && patternEnd.match(linePart, 0) != null)
return -1;
return match.getValue().length();
}
use of org.rstudio.core.client.regex.Match 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.Match 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.");
}
Aggregations