Skip to content

Commit

Permalink
Merge branch 'corani-code-folding'
Browse files Browse the repository at this point in the history
  • Loading branch information
aumayr committed Mar 15, 2016
2 parents 016db2b + 564f507 commit 01a6582
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 107 deletions.
134 changes: 29 additions & 105 deletions fava/static/javascript/ace-mode-beancount.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var BeancountHighlightRules = function() {
token: "comment.line.shebang",
regex: /^#!.*/,
comment: "Shebangs"
}, {
token: "section.line.beancount",
regex: /^\*+ .+$/,
comment: "Section marker"
}, {
token: "string.quoted.triple.beancount",
regex: /"""/,
Expand Down Expand Up @@ -172,139 +176,59 @@ oop.inherits(BeancountHighlightRules, TextHighlightRules);
exports.BeancountHighlightRules = BeancountHighlightRules;
});

define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
define("ace/mode/folding/orgstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
"use strict";

var oop = require("../../lib/oop");
var Range = require("../../range").Range;
var BaseFoldMode = require("./fold_mode").FoldMode;

var FoldMode = exports.FoldMode = function(commentRegex) {
if (commentRegex) {
this.foldingStartMarker = new RegExp(
this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
);
this.foldingStopMarker = new RegExp(
this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
);
}
};
var FoldMode = exports.FoldMode = function() {};
oop.inherits(FoldMode, BaseFoldMode);

(function() {
this.foldingStartMarker = /^(\*+) (.+)$/;
this.foldingStopMarker;

this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
this._getFoldWidgetBase = this.getFoldWidget;
this.getFoldWidget = function(session, foldStyle, row) {
var line = session.getLine(row);

if (this.singleLineBlockCommentRe.test(line)) {
if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
return "";
if (!this.foldingStartMarker.test(line)) {
return "";
}

var fw = this._getFoldWidgetBase(session, foldStyle, row);

if (!fw && this.startRegionRe.test(line))
return "start"; // lineCommentRegionStart
if (!fw && this.foldingStartMarker.test(line))
return "start";

return fw;
};

this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
this.getFoldWidgetRange = function(session, foldStyle, row) {
var line = session.getLine(row);

if (this.startRegionRe.test(line))
return this.getCommentRegionBlock(session, line, row);

var match = line.match(this.foldingStartMarker);
if (match) {
var i = match.index;

if (match[1])
return this.openingBracketBlock(session, match[1], row, i);

var range = session.getCommentFoldRange(row, i + match[0].length, 1);

if (range && !range.isMultiLine()) {
if (forceMultiline) {
range = this.getSectionRange(session, row);
} else if (foldStyle != "all")
range = null;
console.log(match);
var startColumn = line.length;
var maxRow = session.getLength();
var startRow = row;
var level = match[1].length

while (++row < maxRow) {
line = session.getLine(row);
var m = this.foldingStartMarker.exec(line);
if (!m) continue;
if (m[1].length <= level) break;
}

return range;
}

if (foldStyle === "markbegin")
return;

var match = line.match(this.foldingStopMarker);
if (match) {
var i = match.index + match[0].length;

if (match[1])
return this.closingBracketBlock(session, match[1], row, i);

return session.getCommentFoldRange(row, i, -1);
}
};

this.getSectionRange = function(session, row) {
var line = session.getLine(row);
var startIndent = line.search(/\S/);
var startRow = row;
var startColumn = line.length;
row = row + 1;
var endRow = row;
var maxRow = session.getLength();
while (++row < maxRow) {
line = session.getLine(row);
var indent = line.search(/\S/);
if (indent === -1)
continue;
if (startIndent > indent)
break;
var subRange = this.getFoldWidgetRange(session, "all", row);

if (subRange) {
if (subRange.start.row <= startRow) {
break;
} else if (subRange.isMultiLine()) {
row = subRange.end.row;
} else if (startIndent == indent) {
break;
}
var endRow = row;
if (endRow > startRow) {
if (endRow < maxRow) endRow--;
return new Range(startRow, startColumn, endRow, line.length);
}
endRow = row;
}

return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
};
this.getCommentRegionBlock = function(session, line, row) {
var startColumn = line.search(/\s*$/);
var maxRow = session.getLength();
var startRow = row;

var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
var depth = 1;
while (++row < maxRow) {
line = session.getLine(row);
var m = re.exec(line);
if (!m) continue;
if (m[1]) depth--;
else depth++;

if (!depth) break;
}

var endRow = row;
if (endRow > startRow) {
return new Range(startRow, startColumn, endRow, line.length);
}
};

Expand All @@ -318,7 +242,7 @@ define("ace/mode/beancount",["require","exports","module","ace/lib/oop","ace/mod
var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var BeancountHighlightRules = require("./beancount_highlight_rules").BeancountHighlightRules;
var FoldMode = require("./folding/cstyle").FoldMode;
var FoldMode = require("./folding/orgstyle").FoldMode;

var Mode = function() {
this.HighlightRules = BeancountHighlightRules;
Expand Down
2 changes: 1 addition & 1 deletion fava/static/javascript/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $(document).ready(function() {
fontSize: "13px",
fontFamily: "monospace",
useSoftTabs: true,
showFoldWidgets: false
showFoldWidgets: true
};

if ($('.editor').attr('data-print-margin-column')) {
Expand Down
12 changes: 11 additions & 1 deletion fava/static/sass/_editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ div.editor-wrapper {
}

.ace_gutter-cell {
padding: 0 6px;
padding: 0 13px;
}

.ace_section {
color: $color_editor_comment;
font-weight: 700;
border: solid 1px $color_editor_comment;
border-radius: 2px;
padding-right: 10px;
}

.ace_line .ace_fold { border-color: transparent; }

.ace_comment {
color: $color_editor_comment;
font-style: italic;
Expand Down

0 comments on commit 01a6582

Please sign in to comment.