use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class TextEditingTarget method onExecuteFromCurrentLine.
@Handler
void onExecuteFromCurrentLine() {
docDisplay_.focus();
int startRow = docDisplay_.getSelectionStart().getRow();
int startColumn = 0;
Position start = Position.create(startRow, startColumn);
codeExecution_.executeRange(Range.fromPoints(start, endPosition()));
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position 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);
}
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class TextEditingTarget method updateStatusBarPosition.
private void updateStatusBarPosition() {
Position pos = docDisplay_.getCursorPosition();
statusBar_.getPosition().setValue((pos.getRow() + 1) + ":" + (pos.getColumn() + 1));
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class TextEditingTarget method getSelectionAsSourceLocation.
private SourceLocation getSelectionAsSourceLocation(boolean fromClick) {
// get doc path (bail if the document is unsaved)
String file = docUpdateSentinel_.getPath();
if (file == null)
return null;
Position selPos = docDisplay_.getSelectionStart();
int line = selPos.getRow() + 1;
int column = selPos.getColumn() + 1;
return SourceLocation.create(file, line, column, fromClick);
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class TextEditingTarget method onGoToStartOfCurrentScope.
@Handler
void onGoToStartOfCurrentScope() {
docDisplay_.focus();
Scope scope = docDisplay_.getCurrentScope();
if (scope != null) {
Position position = Position.create(scope.getBodyStart().getRow(), scope.getBodyStart().getColumn() + 1);
docDisplay_.setCursorPosition(position);
}
}
Aggregations