Skip to content

Commit

Permalink
fix: improve yaml folding
Browse files Browse the repository at this point in the history
  • Loading branch information
akoreman committed Oct 25, 2023
1 parent cf18975 commit 5c80e3c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
90 changes: 90 additions & 0 deletions src/mode/folding/yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"use strict";

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

var FoldMode = exports.FoldMode = function() {};
oop.inherits(FoldMode, BaseFoldMode);

(function() {
this.getFoldWidgetRange = function(session, foldStyle, row) {
var re = /\S/;
var line = session.getLine(row);
var startLevel = line.search(re);
if (startLevel == -1)
return;

var startColumn = line.length;
var maxRow = session.getLength();
var startRow = row;
var endRow = row;

while (++row < maxRow) {
line = session.getLine(row);
var level = line.search(re);

if (level == -1 && row !== maxRow - 1) {
endRow = row;
break;
}

if ((level == -1 && row !== maxRow - 1) || line[level] == "-" || level > startLevel)
continue;

if (level <= startLevel) {
var token = session.getTokenAt(row, 0);
if (!token || token.type !== "string")
break;
}
}

if (endRow > startRow) {
var endColumn = session.getLine(endRow).length;
return new Range(startRow, startColumn, endRow, endColumn);
}
};

// must return "" if there's no fold, to enable caching
this.getFoldWidget = function(session, foldStyle, row) {
var line = session.getLine(row);
var indent = line.search(/[^\s]/);
var next = session.getLine(row + 1);
var prev = session.getLine(row - 1);
var prevIndent = prev.search(/[^\s]/);
var nextIndent = next.search(/[^\s]/);

var lineStartsWithDash = line.search(/^\-/) !== -1;

if (indent == -1) {
session.foldWidgets[row - 1] = prevIndent!= -1 && prevIndent < nextIndent ? "start" : "";
return "";
}

// documentation comments
if (prevIndent == -1) {
if (indent == nextIndent && line[indent] == "#" && next[indent] == "#") {
session.foldWidgets[row - 1] = "";
session.foldWidgets[row + 1] = "";
return "start";
}
} else if (prevIndent == indent && line[indent] == "#" && prev[indent] == "#") {
if (session.getLine(row - 2).search(/[^\s]/) == -1) {
session.foldWidgets[row - 1] = "start";
session.foldWidgets[row + 1] = "";
return "";
}
}

if (prevIndent!= -1 && (prevIndent < indent || lineStartsWithDash))
session.foldWidgets[row - 1] = "start";
else
session.foldWidgets[row - 1] = "";

if (indent < nextIndent)
return "start";
else
return "";
};

}).call(FoldMode.prototype);
2 changes: 1 addition & 1 deletion src/mode/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var YamlHighlightRules = require("./yaml_highlight_rules").YamlHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var FoldMode = require("./folding/coffee").FoldMode;
var FoldMode = require("./folding/yaml").FoldMode;
var WorkerClient = require("../worker/worker_client").WorkerClient;

var Mode = function() {
Expand Down

0 comments on commit 5c80e3c

Please sign in to comment.