Skip to content

Commit

Permalink
Folding and scope tree support for sections
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed Mar 31, 2012
1 parent 5a7ed3f commit 491da24
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
42 changes: 40 additions & 2 deletions src/gwt/acesupport/acemode/r_code_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,18 @@ var RCodeModel = function(doc, tokenizer, statePattern) {
//console.log(" Token: " + tokenCursor.currentValue() + " [" + tokenCursor.currentPosition().row + "x" + tokenCursor.currentPosition().column + "]");

var tokenType = tokenCursor.currentToken().type;
if (/\bcodebegin\b/.test(tokenType))
if (/\bsectionhead\b/.test(tokenType))
{
var sectionHeadMatch = /^#+'?[-=#\s]*(.*?)\s*[-=#]+\s*$/.exec(
tokenCursor.currentValue());

var label = "Section: " + sectionHeadMatch[1];
if (label.length > 50)
label = label.substring(0, 50) + "...";

this.$scopes.onSectionHead(label, tokenCursor.currentPosition());
}
else if (/\bcodebegin\b/.test(tokenType))
{
var chunkStartPos = tokenCursor.currentPosition();
var chunkPos = {row: chunkStartPos.row + 1, column: 0};
Expand Down Expand Up @@ -348,6 +359,9 @@ var RCodeModel = function(doc, tokenizer, statePattern) {

var rowTokens = this.$tokens[row];

if (rowTokens.length == 1 && /\bsectionhead\b/.test(rowTokens[0].type))
return rowTokens[0];

var depth = 0;
var unmatchedOpen = null;
var unmatchedClose = null;
Expand Down Expand Up @@ -404,6 +418,8 @@ var RCodeModel = function(doc, tokenizer, statePattern) {
return "start";
else if (/\bcodeend\b/.test(foldToken.type))
return "end";
else if (/\bsectionhead\b/.test(foldToken.type))
return "start";

return "";
};
Expand Down Expand Up @@ -457,6 +473,25 @@ var RCodeModel = function(doc, tokenizer, statePattern) {
}
return;
}
else if (/\bsectionhead\b/.test(foldToken.type)) {
var match = /([-=#])\1+\s*$/.exec(foldToken.value);
if (!match)
return; // this would be surprising

pos.column += match.index - 1; // Not actually sure why -1 is needed
var tokenIterator3 = new TokenIterator(session, row, 0);
var lastRow = row;
for (var tok3; tok3 = tokenIterator3.stepForward(); ) {
if (/\bsectionhead\b/.test(tok3.type)) {
break;
}
lastRow = tokenIterator3.getCurrentTokenRow();
}

return Range.fromPoints(
pos,
{row: lastRow, column: session.getLine(lastRow).length});
}

return;
};
Expand All @@ -470,7 +505,7 @@ var RCodeModel = function(doc, tokenizer, statePattern) {
return "";
this.$buildScopeTreeUpToRow(position.row);

var scopePath = this.$scopes.findScope(position);
var scopePath = this.$scopes.getActiveScopes(position);
if (scopePath)
{
for (var i = scopePath.length-1; i >= 0; i--) {
Expand Down Expand Up @@ -1009,6 +1044,9 @@ var RCodeModel = function(doc, tokenizer, statePattern) {
if (/\bcode(?:begin|end)\b/.test(token.type))
return false;

if (/\bsectionhead\b/.test(token.type))
return false;

return /^\s*$/.test(token.value) ||
token.type.match(/\b(?:ace_virtual-)?comment\b/);
}
Expand Down
5 changes: 5 additions & 0 deletions src/gwt/acesupport/acemode/r_highlight_rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ define("mode/r_highlight_rules", function(require, exports, module)

this.$rules = {
"start" : [
{
// Roxygen
token : "comment.sectionhead",
regex : "#+(?!').*(?:----|====|####)\\s*$"
},
{
// Roxygen
token : "comment",
Expand Down
16 changes: 15 additions & 1 deletion src/gwt/acesupport/acemode/r_scope_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ define('mode/r_scope_tree', function(require, exports, module) {

(function() {

this.onSectionHead = function(sectionLabel, sectionPos) {
var existingScopes = this.getActiveScopes(sectionPos);
if (existingScopes.length == 2 && existingScopes[1].isSection()) {
this.$root.closeScope(sectionPos, ScopeNode.TYPE_SECTION);
}
else if (existingScopes.length != 1)
return;

this.$root.addNode(new ScopeNode(sectionLabel, sectionPos, sectionPos,
ScopeNode.TYPE_SECTION));
};

this.onChunkStart = function(chunkLabel, label, chunkStartPos, chunkPos) {
// Starting a chunk means closing the previous chunk, if any
var prev = this.$root.closeScope(chunkStartPos, ScopeNode.TYPE_CHUNK);
Expand Down Expand Up @@ -89,7 +101,7 @@ define('mode/r_scope_tree', function(require, exports, module) {
return closed;
};

this.findScope = function(pos) {
this.getActiveScopes = function(pos) {
return this.$root.findNode(pos);
};

Expand Down Expand Up @@ -144,12 +156,14 @@ define('mode/r_scope_tree', function(require, exports, module) {
ScopeNode.TYPE_ROOT = 1; // document root
ScopeNode.TYPE_BRACE = 2; // curly brace
ScopeNode.TYPE_CHUNK = 3; // Sweave chunk
ScopeNode.TYPE_SECTION = 4; // Section header

(function() {

this.isRoot = function() { return this.scopeType == ScopeNode.TYPE_ROOT; };
this.isBrace = function() { return this.scopeType == ScopeNode.TYPE_BRACE; };
this.isChunk = function() { return this.scopeType == ScopeNode.TYPE_CHUNK; };
this.isSection = function() { return this.scopeType == ScopeNode.TYPE_SECTION; };

this.addNode = function(node) {
assert(!node.end, "New node is already closed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public native final boolean isChunk() /*-{
return this.isChunk();
}-*/;

public native final boolean isSection() /*-{
return this.isSection();
}-*/;

public native final Position getPreamble() /*-{
return this.preamble;
}-*/;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,8 @@ public void execute()
(fileType_.isRnw() && function.isTopLevel());
if (useChunk)
statusBar_.setScopeType(StatusBar.SCOPE_CHUNK);
else if (function.isSection())
statusBar_.setScopeType(StatusBar.SCOPE_SECTION);
else
statusBar_.setScopeType(StatusBar.SCOPE_FUNCTION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

public interface StatusBar
{
public static final int SCOPE_SECTION = 3;
public static final int SCOPE_CHUNK = 2;
public static final int SCOPE_FUNCTION = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public void setScopeType(int type)
scopeIcon_.setResource(RES.function());
else if (type == StatusBar.SCOPE_CHUNK)
scopeIcon_.setResource(RES.chunk());
else if (type == StatusBar.SCOPE_SECTION)
scopeIcon_.setResource(RES.section());
}

@UiField
Expand Down

0 comments on commit 491da24

Please sign in to comment.