From ff0cc12efcead0eecf12ff4cd41931c7de5bc799 Mon Sep 17 00:00:00 2001 From: sevin7676 Date: Sat, 18 Apr 2015 06:37:29 -0400 Subject: [PATCH] folding partially working.. --- lib/ace/mode/folding/sqlserver.js | 92 ++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 21 deletions(-) diff --git a/lib/ace/mode/folding/sqlserver.js b/lib/ace/mode/folding/sqlserver.js index b0a81d92..69b5851a 100644 --- a/lib/ace/mode/folding/sqlserver.js +++ b/lib/ace/mode/folding/sqlserver.js @@ -48,8 +48,10 @@ oop.inherits(FoldMode, BaseFoldMode); // -- LEFT OFF TRYING TO ADD ADVANCED FOLDING... UNCOMMENT TO CONTINUE - // this.foldingStartMarker = /\bCASE\b|\bBEGIN\b/i; - // this.foldingStopMarker = /\bEND\b/i; + //this.foldingStartMarker = /\bCASE\b|\bBEGIN\b/i; + this.foldingStartMarker = /(\bCASE\b|\bBEGIN\b)|^\s*(\/\*)/; + this.foldingStopMarker = /\bEND\b/i; + this.startRegionRe = /^\s*(\/\*|--)#region\b/; @@ -58,39 +60,43 @@ oop.inherits(FoldMode, BaseFoldMode); this.getFoldWidget = function(session, foldStyle, row) { var line = session.getLine(row); - //var fw = this._getFoldWidgetBase(session, foldStyle, row); + var fw = this._getFoldWidgetBase(session, foldStyle, row); - if (/*!fw && */this.startRegionRe.test(line)) + if (!fw && this.startRegionRe.test(line)) return "start"; return fw; }; - /*this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) { + //this is called when a fold widget is clicked + this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) { 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; + var match = line.match(this.foldingStartMarker); + if (match) { + var i = match.index; - // if (match[1]) return this.openingBracketBlock(session, match[1], row, i); + //if first capturing group, then match is for bracket, either '{' or '[', which match[1] will contain + if (match[1]) return this.getEndBlock(session, row, i, match[1]); + //return this.openingBracketBlock(session, match[1], row, i); - // var range = session.getCommentFoldRange(row, i + match[0].length, 1); + //still going: match is for second capturing group, which is blockcomment + 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; - // } + if (range && !range.isMultiLine()) { + if (forceMultiline) { + range = this.getSectionRange(session, row); + } + else if (foldStyle != "all") range = null; + } - // return range; - // } + return range; + } if (foldStyle === "markbegin") return; - + /* this is when end fold has marker, and user clicks it, in which case we want to find the opening braket var match = line.match(this.foldingStopMarker); if (match) { var i = match.index + match[0].length; @@ -98,10 +104,54 @@ oop.inherits(FoldMode, BaseFoldMode); if (match[1]) return this.closingBracketBlock(session, match[1], row, i); return session.getCommentFoldRange(row, i, - 1); - } + }*/ return; - };*/ + }; + + + /** + * finds the next 'END' for closing a fold range + * @param {string} matchSequence - the sequence of charaters that started the fold widget, which should remain visible when the fold widget is folded + */ + this.getEndBlock = function(session, row, column, matchSequence) { + var start = { + row: row, + column: column + matchSequence.length + }; + var maxRow = session.getLength(); + var line; + + var depth = 1; + var re = /\bEND\b/i; ///^\s*(?:\/\*|--)#(end)?region\b/; + while (++row < maxRow) { + line = session.getLine(row); + var m = re.exec(line); + if (!m) continue; + if (m[0]) depth--; + else depth++; + + if (!depth) break; + } + var endRow = row; + if (endRow > start.row) { + return new Range(start.row, start.column, endRow, line.length); + } + + + /* var end = session.$findClosingBracket(bracket, start, typeRe); + if (!end) return; + + var fw = session.foldWidgets[end.row]; + if (fw == null) fw = session.getFoldWidget(end.row); + + if (fw == "start" && end.row > start.row) { + end.row--; + end.column = session.getLine(end.row).length; + } + return Range.fromPoints(start, end);*/ + }; + this.getCommentRegionBlock = function(session, line, row) { var startColumn = line.search(/\s*$/);