Skip to content

Commit

Permalink
PageUp and PageDown navigate sections, chunks in Rmd/Rpres
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Aug 20, 2015
1 parent aab0b6b commit b5320e5
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/gwt/src/org/rstudio/core/client/js/JsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,39 @@ public void remove()
}
};
}

public static <T extends JavaScriptObject> Iterable<T> asReverseIterable(final JsArray<T> array)
{
return new Iterable<T>()
{
@Override
public Iterator<T> iterator()
{
return new Iterator<T>()
{
int index_ = array.length() - 1;

@Override
public boolean hasNext()
{
return index_ > 0;
}

@Override
public T next()
{
return array.get(index_--);
}

@Override
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
};
}

public static boolean areEqual(JsArrayString a, JsArrayString b)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ public HashSet<AppCommand> getSupportedCommands(Commands commands)
{
results.add(commands.toggleDocumentOutline());
}

results.add(commands.goToNextSection());
results.add(commands.goToPrevSection());
results.add(commands.findReplace());
results.add(commands.findNext());
results.add(commands.findPrevious());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ well as menu structures (for main menu and popup menus).
<shortcut refid="shrinkSelection" value="Ctrl+Shift+Down" if="!org.rstudio.core.client.BrowseCap.isMacintosh()"/>
<shortcut refid="expandSelection" value="Cmd+Alt+Shift+Up" if="org.rstudio.core.client.BrowseCap.isMacintosh()"/>
<shortcut refid="shrinkSelection" value="Cmd+Alt+Shift+Down" if="org.rstudio.core.client.BrowseCap.isMacintosh()"/>
<shortcut refid="goToNextSection" value="PageDown"/>
<shortcut refid="goToPrevSection" value="PageUp"/>
<shortcut value="Cmd+Alt+Up" title="Add cursor above current cursor"/>
<shortcut value="Cmd+Alt+Down" title="Add cursor below current cursor"/>
<shortcut value="Ctrl+Alt+Shift+Up" title="Move active cursor up"/>
Expand Down Expand Up @@ -1283,6 +1285,14 @@ well as menu structures (for main menu and popup menus).
menuLabel="Shrink Selection"
desc="Shrink selection"
context="editor"/>

<cmd id="goToNextSection"
label="Go to Next Section"
context="editor"/>

<cmd id="goToPrevSection"
label="Go to Previous Section"
context="editor"/>

<cmd id="expandRaggedSelection"
label="Expand Ragged Selection"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
public abstract AppCommand toggleDocumentOutline();
public abstract AppCommand expandSelection();
public abstract AppCommand shrinkSelection();
public abstract AppCommand goToNextSection();
public abstract AppCommand goToPrevSection();
public abstract AppCommand expandRaggedSelection();
public abstract AppCommand extractFunction();
public abstract AppCommand extractLocalVariable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ public Source(Commands commands,
dynamicCommands_.add(commands.shrinkSelection());
dynamicCommands_.add(commands.toggleDocumentOutline());
dynamicCommands_.add(commands.knitWithParameters());
dynamicCommands_.add(commands.goToNextSection());
dynamicCommands_.add(commands.goToPrevSection());
for (AppCommand command : dynamicCommands_)
{
command.setVisible(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,16 @@ public boolean isCursorInSingleLineString()
{
return StringUtil.isEndOfLineInRStringState(getCurrentLineUpToCursor());
}

public void gotoPageUp()
{
widget_.getEditor().gotoPageUp();
}

public void gotoPageDown()
{
widget_.getEditor().gotoPageDown();
}

public void scrollToBottom()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ DocDisplay.AnchoredSelection createAnchoredSelection(Position start,
void scrollCursorIntoViewIfNecessary(int rowsAround);
boolean isCursorInSingleLineString();

void gotoPageDown();
void gotoPageUp();

void ensureRowVisible(int row);

InputEditorSelection search(String needle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.rstudio.studio.client.workbench.views.source.editors.text;

import com.google.gwt.core.client.JsArray;

import org.rstudio.core.client.StringUtil;
import org.rstudio.core.client.js.JsUtil;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range;
Expand Down Expand Up @@ -104,6 +105,16 @@ public Scope[] getScopes()
{
return scopes_.toArray(new Scope[scopes_.size()]);
}

public Scope get(int index)
{
return scopes_.get(index);
}

public int size()
{
return scopes_.size();
}

public void removeAll(ScopePredicate shouldRemove)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,90 @@ public void onDocTabDragStateChanged(
});
}

private boolean moveCursorToNextSectionOrChunk()
{
Scope current = docDisplay_.getCurrentScope();
ScopeList scopes = new ScopeList(docDisplay_);
Position cursorPos = docDisplay_.getCursorPosition();

int n = scopes.size();
for (int i = 0; i < n; i++)
{
Scope scope = scopes.get(i);
if (!(scope.isChunk() || scope.isSection()))
continue;

if (scope.equals(current))
continue;

if (scope.getPreamble().isAfter(cursorPos))
{
if (scope.getPreamble().getRow() - cursorPos.getRow() < 50)
{
docDisplay_.setCursorPosition(scope.getPreamble());
return true;
}
return false;
}
}

return false;
}

private boolean moveCursorToPreviousSectionOrChunk()
{
ScopeList scopes = new ScopeList(docDisplay_);
Position cursorPos = docDisplay_.getCursorPosition();

int n = scopes.size();
for (int i = n - 1; i >= 0; i--)
{
Scope scope = scopes.get(i);
if (!(scope.isChunk() || scope.isSection()))
continue;

if (scope.getPreamble().isBefore(cursorPos))
{
if (cursorPos.getRow() - scope.getPreamble().getRow() < 50)
{
docDisplay_.setCursorPosition(scope.getPreamble());
return true;
}
return false;
}
}

return false;
}

@Handler
void onGoToNextSection()
{
if (docDisplay_.getFileType().isRmd() || docDisplay_.getFileType().isRpres())
{
if (!moveCursorToNextSectionOrChunk())
docDisplay_.gotoPageDown();
}
else
{
docDisplay_.gotoPageDown();
}
}

@Handler
void onGoToPrevSection()
{
if (docDisplay_.getFileType().isRmd() || docDisplay_.getFileType().isRpres())
{
if (!moveCursorToPreviousSectionOrChunk())
docDisplay_.gotoPageUp();
}
else
{
docDisplay_.gotoPageUp();
}
}

public void onExecuteUserCommand(final ExecuteUserCommandEvent event)
{
withSavedDoc(new Command()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,16 @@ private static native <T> JavaScriptObject addEventListenerInternal(
private static native void invokeFunctor(JavaScriptObject functor) /*-{
functor();
}-*/;

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

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

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

public final native void scrollToRow(int row) /*-{
this.scrollToRow(row);
}-*/;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,10 @@ public native final void setPosition(Position position) /*-{
this.row = position.row;
this.column = position.column;
}-*/;

public final String asString()
{
return "[" + getRow() + ", " + getColumn() + "]";
}

}

1 comment on commit b5320e5

@jjallaire
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a NEWS.md entry on this?

Please sign in to comment.