Skip to content

Commit

Permalink
option to control re-indenting on paste
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Oct 2, 2012
1 parent cd97017 commit f53f127
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Expand Up @@ -97,6 +97,11 @@ public PrefValue<Boolean> insertMatching()
{
return bool("insert_matching", true);
}

public PrefValue<Boolean> reindentOnPaste()
{
return bool("reindent_on_paste", true);
}

public PrefValue<Boolean> softWrapRFiles()
{
Expand Down
Expand Up @@ -39,6 +39,7 @@ public EditingPreferencesPane(UIPrefs prefs)
add(checkboxPref("Enable vim editing mode", prefs_.useVimMode()));
add(checkboxPref("Blinking cursor", prefs_.blinkingCursor()));
add(checkboxPref("Insert matching parens/quotes", prefs_.insertMatching()));
add(checkboxPref("Automatically reindent pasted code", prefs_.reindentOnPaste()));
add(checkboxPref("Soft-wrap R source files", prefs_.softWrapRFiles()));
add(checkboxPref("Focus console after executing from source", prefs_.focusConsoleAfterExec()));
add(checkboxPref("Show syntax highlighting in console input", prefs_.syntaxColorConsole()));
Expand Down
Expand Up @@ -246,13 +246,35 @@ public void onKeyDown(KeyDownEvent event)
break;
case 'Y':
event.preventDefault();
Position start = getSelectionStart();
InputEditorUtil.pasteYanked(AceEditor.this);
indentPastedRange(Range.fromPoints(start,
getSelectionEnd()));
break;
}
}

}
});

addPasteHandler(new PasteEvent.Handler()
{
@Override
public void onPaste(PasteEvent event)
{
final Position start = getSelectionStart();

Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
@Override
public void execute()
{
Range range = Range.fromPoints(start, getSelectionEnd());
indentPastedRange(range);
}
});
}
});

// handle click events
addAceClickHandler(new AceClickEvent.Handler()
Expand Down Expand Up @@ -287,6 +309,31 @@ public void onCursorChanged(CursorChangedEvent event)

}

private void indentPastedRange(Range range)
{
if (fileType_ == null ||
!fileType_.canAutoIndent() ||
!RStudioGinjector.INSTANCE.getUIPrefs().reindentOnPaste().getValue())
{
return;
}

String firstLinePrefix = getSession().getTextRange(
Range.fromPoints(Position.create(range.getStart().getRow(), 0),
range.getStart()));

if (firstLinePrefix.trim().length() != 0)
{
Position newStart = Position.create(range.getStart().getRow() + 1, 0);
if (newStart.compareTo(range.getEnd()) >= 0)
return;

range = Range.fromPoints(newStart, range.getEnd());
}

getSession().reindent(range);
}

@Inject
void initialize(CodeToolsServerOperations server)
{
Expand Down

0 comments on commit f53f127

Please sign in to comment.