use of org.rstudio.core.client.regex.Match 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.Match in project rstudio by rstudio.
the class AceEditor method moveSelectionToNextLine.
public boolean moveSelectionToNextLine(boolean skipBlankLines) {
int curRow = getSession().getSelection().getCursor().getRow();
while (++curRow < getSession().getLength()) {
String line = getSession().getLine(curRow);
Pattern pattern = Pattern.create("[^\\s]");
Match match = pattern.match(line, 0);
if (skipBlankLines && match == null)
continue;
int col = (match != null) ? match.getIndex() : 0;
getSession().getSelection().moveCursorTo(curRow, col, false);
getSession().unfold(curRow, true);
scrollCursorIntoViewIfNecessary();
return true;
}
return false;
}
use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class TextEditingTarget method extractIndentation.
private String extractIndentation(String code) {
Pattern leadingWhitespace = Pattern.create("^(\\s*)");
Match match = leadingWhitespace.match(code, 0);
return match == null ? "" : match.getGroup(1);
}
use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class TextEditingTarget method isRChunk.
private boolean isRChunk(Scope scope) {
String labelText = docDisplay_.getLine(scope.getPreamble().getRow());
Pattern reEngine = Pattern.create(".*engine\\s*=\\s*['\"]([^'\"]*)['\"]");
Match match = reEngine.match(labelText, 0);
if (match == null)
return true;
String engine = match.getGroup(1).toLowerCase();
// collect those here.
return engine.equals("r");
}
use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.
the class TextEditingTarget method reflowComments.
private void reflowComments(String commentPrefix, final boolean multiParagraphIndent, InputEditorSelection selection, final InputEditorPosition cursorPos) {
String code = docDisplay_.getCode(selection);
String[] lines = code.split("\n");
String prefix = StringUtil.getCommonPrefix(lines, true, false);
Pattern pattern = Pattern.create("^\\s*" + commentPrefix + "+('?)\\s*");
Match match = pattern.match(prefix, 0);
// Selection includes non-comments? Abort.
if (match == null)
return;
prefix = match.getValue();
final boolean roxygen = match.hasGroup(1);
int cursorRowIndex = 0;
int cursorColIndex = 0;
if (cursorPos != null) {
cursorRowIndex = selectionToPosition(cursorPos).getRow() - selectionToPosition(selection.getStart()).getRow();
cursorColIndex = Math.max(0, cursorPos.getPosition() - prefix.length());
}
final WordWrapCursorTracker wwct = new WordWrapCursorTracker(cursorRowIndex, cursorColIndex);
int maxLineLength = prefs_.printMarginColumn().getValue() - prefix.length();
WordWrap wordWrap = new WordWrap(maxLineLength, false) {
@Override
protected boolean forceWrapBefore(String line) {
String trimmed = line.trim();
if (roxygen && trimmed.startsWith("@") && !trimmed.startsWith("@@")) {
// Roxygen tags always need to be at the start of a line. If
// there is content immediately following the roxygen tag, then
// content should be wrapped until the next roxygen tag is
// encountered.
indent_ = "";
if (TAG_WITH_CONTENTS.match(line, 0) != null) {
indentRestOfLines_ = true;
}
return true;
} else // empty line disables indentation
if (!multiParagraphIndent && (line.trim().length() == 0)) {
indent_ = "";
indentRestOfLines_ = false;
}
return super.forceWrapBefore(line);
}
@Override
protected void onChunkWritten(String chunk, int insertionRow, int insertionCol, int indexInOriginalString) {
if (indentRestOfLines_) {
indentRestOfLines_ = false;
// TODO: Use real indent from settings
indent_ = " ";
}
wwct.onChunkWritten(chunk, insertionRow, insertionCol, indexInOriginalString);
}
private boolean indentRestOfLines_ = false;
private Pattern TAG_WITH_CONTENTS = Pattern.create("@\\w+\\s+[^\\s]");
};
for (String line : lines) {
String content = line.substring(Math.min(line.length(), prefix.length()));
if (content.matches("^\\s*\\@examples\\b.*$"))
wordWrap.setWrappingEnabled(false);
else if (content.trim().startsWith("@"))
wordWrap.setWrappingEnabled(true);
wwct.onBeginInputRow();
wordWrap.appendLine(content);
}
String wrappedString = wordWrap.getOutput();
StringBuilder finalOutput = new StringBuilder();
for (String line : StringUtil.getLineIterator(wrappedString)) finalOutput.append(prefix).append(line).append("\n");
// Remove final \n
if (finalOutput.length() > 0)
finalOutput.deleteCharAt(finalOutput.length() - 1);
String reflowed = finalOutput.toString();
docDisplay_.setSelection(selection);
if (!reflowed.equals(code)) {
docDisplay_.replaceSelection(reflowed);
}
if (cursorPos != null) {
if (wwct.getResult() != null) {
int row = wwct.getResult().getY();
int col = wwct.getResult().getX();
row += selectionToPosition(selection.getStart()).getRow();
col += prefix.length();
Position pos = Position.create(row, col);
docDisplay_.setSelection(docDisplay_.createSelection(pos, pos));
} else {
docDisplay_.collapseSelection(false);
}
}
}
Aggregations