Skip to content

Commit

Permalink
Implement block reindenting
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed Aug 16, 2011
1 parent 2e2ad36 commit c04bc34
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/gwt/acesupport/acemode/r_code_model.js
Expand Up @@ -321,6 +321,9 @@ var RCodeModel = function(doc, tokenizer, statePattern) {

this.getNextLineIndent = function(lastRow, line, endState, tab, tabSize)
{
if (endState == "qstring" || endState == "qqstring")
return "";

// This lineOverrides nonsense is necessary because the line has not
// changed in the real document yet. We need to simulate it by replacing
// the real line with the `line` param, and when we finish with this
Expand Down
42 changes: 41 additions & 1 deletion src/gwt/acesupport/loader.js
Expand Up @@ -26,7 +26,7 @@ var EventEmitter = require("pilot/event_emitter").EventEmitter;
var Editor = require("ace/editor").Editor;
var EditSession = require("ace/edit_session").EditSession;
var UndoManager = require("ace/undomanager").UndoManager;

var Range = require("ace/range").Range;

var RStudioEditor = function(renderer, session) {
Editor.call(this, renderer, session);
Expand Down Expand Up @@ -69,6 +69,46 @@ oop.inherits(RStudioEditSession, EditSession);
return EditSession.prototype.insert.call(this, position, text);
}
};
this.reindent = function() {
var mode = this.getMode();
if (!mode.getNextLineIndent)
return;
var range = this.getSelection().getRange();
var start = range.start.row;
var end = range.end.row;
for (var i = start; i <= end; i++) {
// First line is always unindented
if (i == 0) {
this.applyIndent(i, "");
}
else {
var state = this.getState(i-1);
if (state == 'qstring' || state == 'qqstring')
continue;
var line = this.getLine(i-1);
var newline = this.getLine(i);

var shouldOutdent = mode.checkOutdent(state, " ", newline);

var newIndent = mode.getNextLineIndent(state,
line,
this.getTabString(),
this.getTabSize(),
i-1);

this.applyIndent(i, newIndent);

if (shouldOutdent) {
mode.autoOutdent(state, this, i);
}
}
}
};
this.applyIndent = function(lineNum, indent) {
var line = this.getLine(lineNum);
var matchLen = line.match(/^\s*/g)[0].length;
this.replace(new Range(lineNum, 0, lineNum, matchLen), indent);
};
}).call(RStudioEditSession.prototype);


Expand Down
Expand Up @@ -111,6 +111,7 @@ public HashSet<AppCommand> getSupportedCommands(Commands commands)
results.add(commands.executeCode());
results.add(commands.extractFunction());
results.add(commands.commentUncomment());
results.add(commands.reindent());
}
if (canExecuteAllCode())
{
Expand Down
Expand Up @@ -93,6 +93,7 @@ well as menu structures (for main menu and popup menus).
<separator/>
<cmd refid="extractFunction"/>
<cmd refid="commentUncomment"/>
<cmd refid="reindent"/>
<separator/>
<cmd refid="consoleClear"/>
</menu>
Expand Down Expand Up @@ -253,6 +254,7 @@ well as menu structures (for main menu and popup menus).
<shortcut refid="extractFunction" value="Cmd+Shift+U"/>
<shortcut refid="executeLastCode" value="Cmd+Shift+P"/>
<shortcut refid="commentUncomment" value="Cmd+/"/>
<shortcut refid="reindent" value="Cmd+I"/>
<shortcut refid="consoleClear" value="Ctrl+L"/>
<shortcut refid="goToFileFunction" value="Cmd+."/>
<shortcut refid="activateSource" value="Ctrl+1"/>
Expand Down Expand Up @@ -473,6 +475,9 @@ well as menu structures (for main menu and popup menus).
<cmd id="commentUncomment"
menuLabel="C_omment/Uncomment Lines"
desc="Comment or uncomment the current line/selection"/>
<cmd id="reindent"
menuLabel="_Reindent Lines"
desc="Reindent the current line/selection"/>

<cmd id="compilePDF"
buttonLabel="Compile PDF"
Expand Down
Expand Up @@ -51,6 +51,7 @@
public abstract AppCommand findReplace();
public abstract AppCommand extractFunction();
public abstract AppCommand commentUncomment();
public abstract AppCommand reindent();
public abstract AppCommand setWorkingDirToActiveDoc();

// Projects
Expand Down
Expand Up @@ -178,6 +178,7 @@ public Source(Commands commands,
dynamicCommands_.add(commands.findReplace());
dynamicCommands_.add(commands.extractFunction());
dynamicCommands_.add(commands.commentUncomment());
dynamicCommands_.add(commands.reindent());
dynamicCommands_.add(commands.jumpToFunction());
dynamicCommands_.add(commands.setWorkingDirToActiveDoc());
for (AppCommand command : dynamicCommands_)
Expand Down
Expand Up @@ -583,6 +583,12 @@ public boolean moveSelectionToNextLine(boolean skipBlankLines)
return false;
}

@Override
public void reindent()
{
getSession().reindent();
}

public ChangeTracker getChangeTracker()
{
return new EventBasedChangeTracker<Void>(this);
Expand Down
Expand Up @@ -120,6 +120,7 @@ public interface AnchoredSelection
String getCurrentLine();
void replaceSelection(String code);
boolean moveSelectionToNextLine(boolean skipBlankLines);
void reindent();
ChangeTracker getChangeTracker();

String getCode(Position start, Position end);
Expand Down Expand Up @@ -1230,6 +1231,13 @@ void onCommentUncomment()
docDisplay_.replaceSelection(selection);
}

@Handler
void onReindent()
{
docDisplay_.reindent();
docDisplay_.focus();
}

@Handler
void onExecuteCode()
{
Expand Down
Expand Up @@ -158,6 +158,7 @@ private Widget createCodeTransformMenuButton()
ToolbarPopupMenu menu = new ToolbarPopupMenu();
menu.addItem(commands_.extractFunction().createMenuItem(false));
menu.addItem(commands_.commentUncomment().createMenuItem(false));
menu.addItem(commands_.reindent().createMenuItem(false));
codeTransform_ = new ToolbarButton("", icon, menu);
codeTransform_.setTitle("Code Tools");
}
Expand Down
Expand Up @@ -94,4 +94,8 @@ public native final Document getDocument() /*-{
public native final void setNewLineMode(String type) /*-{
this.setNewLineMode(type);
}-*/;

public native final void reindent() /*-{
this.reindent();
}-*/;
}

0 comments on commit c04bc34

Please sign in to comment.