use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold in project rstudio by rstudio.
the class TextEditingTarget method fixupCodeBeforeSaving.
private void fixupCodeBeforeSaving() {
int lineCount = docDisplay_.getRowCount();
if (lineCount < 1)
return;
if (docDisplay_.hasActiveCollabSession()) {
// these preferences don't apply to shared editing sessions
return;
}
if (prefs_.stripTrailingWhitespace().getValue() && !fileType_.isMarkdown() && !name_.getValue().equals("DESCRIPTION")) {
String code = docDisplay_.getCode();
Pattern pattern = Pattern.create("[ \t]+$");
String strippedCode = pattern.replaceAll(code, "");
if (!strippedCode.equals(code)) {
// Calling 'setCode' can remove folds in the document; cache the folds
// and reapply them after document mutation.
JsArray<AceFold> folds = docDisplay_.getFolds();
docDisplay_.setCode(strippedCode, true);
for (AceFold fold : JsUtil.asIterable(folds)) docDisplay_.addFold(fold.getRange());
}
}
if (prefs_.autoAppendNewline().getValue() || fileType_.isPython()) {
String lastLine = docDisplay_.getLine(lineCount - 1);
if (lastLine.length() != 0)
docDisplay_.insertCode(docDisplay_.getEnd().getEnd(), "\n");
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold in project rstudio by rstudio.
the class TextEditingTarget method onRenameInScope.
@Handler
void onRenameInScope() {
docDisplay_.focus();
// Save folds (we need to remove them temporarily for the rename helper)
final JsArray<AceFold> folds = docDisplay_.getFolds();
docDisplay_.unfoldAll();
int matches = renameHelper_.renameInScope();
if (matches <= 0) {
if (!docDisplay_.getSelectionValue().isEmpty()) {
String message = "No matches for '" + docDisplay_.getSelectionValue() + "'";
view_.getStatusBar().showMessage(message, 1000);
}
for (AceFold fold : JsUtil.asIterable(folds)) docDisplay_.addFold(fold.getRange());
return;
}
String message = "Found " + matches;
if (matches == 1)
message += " match";
else
message += " matches";
String selectedItem = docDisplay_.getSelectionValue();
message += " for " + selectedItem + ".";
docDisplay_.disableSearchHighlight();
view_.getStatusBar().showMessage(message, new HideMessageHandler() {
private boolean onRenameFinished(boolean value) {
for (AceFold fold : JsUtil.asIterable(folds)) docDisplay_.addFold(fold.getRange());
return value;
}
@Override
public boolean onNativePreviewEvent(NativePreviewEvent preview) {
int type = preview.getTypeInt();
if (docDisplay_.isPopupVisible())
return false;
// End if the user clicks somewhere
if (type == Event.ONCLICK) {
docDisplay_.exitMultiSelectMode();
docDisplay_.clearSelection();
docDisplay_.enableSearchHighlight();
return onRenameFinished(true);
} else // Otherwise, handle key events
if (type == Event.ONKEYDOWN) {
switch(preview.getNativeEvent().getKeyCode()) {
case KeyCodes.KEY_ENTER:
preview.cancel();
case KeyCodes.KEY_UP:
case KeyCodes.KEY_DOWN:
case KeyCodes.KEY_ESCAPE:
docDisplay_.exitMultiSelectMode();
docDisplay_.clearSelection();
docDisplay_.enableSearchHighlight();
return onRenameFinished(true);
}
}
return false;
}
});
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold in project rstudio by rstudio.
the class Fold method collect.
private static void collect(AceFold fold, ArrayList<Fold> results, Position parentOffset) {
results.add(fromAceFold(fold, parentOffset));
JsArray<AceFold> subFolds = fold.getSubFolds();
for (int i = 0; i < subFolds.length(); i++) {
AceFold subFold = subFolds.get(i);
Position offset = Position.create(fold.getStart().getRow() + parentOffset.getRow(), fold.getStart().getColumn() + parentOffset.getColumn());
collect(subFold, results, offset);
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold in project rstudio by rstudio.
the class TextEditingTarget method onUnfold.
@Handler
void onUnfold() {
if (useScopeTreeFolding()) {
Range range = Range.fromPoints(docDisplay_.getSelectionStart(), docDisplay_.getSelectionEnd());
if (range.isEmpty()) {
// If no selection, either:
//
// 1) Unfold a fold containing the cursor, or
// 2) Unfold the closest fold on the current row.
Position pos = docDisplay_.getCursorPosition();
AceFold containingCandidate = null;
AceFold startCandidate = null;
AceFold endCandidate = null;
for (AceFold f : JsUtil.asIterable(docDisplay_.getFolds())) {
// Check to see whether this fold contains the cursor position.
if (f.getRange().contains(pos)) {
if (containingCandidate == null || containingCandidate.getRange().contains(f.getRange())) {
containingCandidate = f;
}
}
if (startCandidate == null && f.getStart().getRow() == pos.getRow() && f.getStart().getColumn() >= pos.getColumn()) {
startCandidate = f;
}
if (startCandidate == null && f.getEnd().getRow() == pos.getRow() && f.getEnd().getColumn() <= pos.getColumn()) {
endCandidate = f;
}
}
if (containingCandidate != null) {
docDisplay_.unfold(containingCandidate);
} else if (startCandidate == null ^ endCandidate == null) {
docDisplay_.unfold(startCandidate != null ? startCandidate : endCandidate);
} else if (startCandidate != null && endCandidate != null) {
// Both are candidates; see which is closer
int startDelta = startCandidate.getStart().getColumn() - pos.getColumn();
int endDelta = pos.getColumn() - endCandidate.getEnd().getColumn();
docDisplay_.unfold(startDelta <= endDelta ? startCandidate : endCandidate);
}
} else {
// If selection, unfold the selection
docDisplay_.unfold(range);
}
} else {
int row = docDisplay_.getSelectionStart().getRow();
docDisplay_.unfold(row);
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold in project rstudio by rstudio.
the class TextEditingTarget method onFoldAll.
@Handler
void onFoldAll() {
if (useScopeTreeFolding()) {
// Fold all except anonymous braces
HashSet<Integer> rowsFolded = new HashSet<Integer>();
for (AceFold f : JsUtil.asIterable(docDisplay_.getFolds())) rowsFolded.add(f.getStart().getRow());
ScopeList scopeList = new ScopeList(docDisplay_);
scopeList.removeAll(ScopeList.ANON_BRACE);
for (Scope scope : scopeList) {
int row = scope.getFoldStart().getRow();
if (!rowsFolded.contains(row))
docDisplay_.addFoldFromRow(row);
}
} else {
docDisplay_.foldAll();
}
}
Aggregations