openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

indent-fold.js (1673B)


      1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
      2 // Distributed under an MIT license: http://codemirror.net/LICENSE
      3 
      4 (function(mod) {
      5   if (typeof exports == "object" && typeof module == "object") // CommonJS
      6     mod(require("../../lib/codemirror"));
      7   else if (typeof define == "function" && define.amd) // AMD
      8     define(["../../lib/codemirror"], mod);
      9   else // Plain browser env
     10     mod(CodeMirror);
     11 })(function(CodeMirror) {
     12 "use strict";
     13 
     14 function lineIndent(cm, lineNo) {
     15   var text = cm.getLine(lineNo)
     16   var spaceTo = text.search(/\S/)
     17   if (spaceTo == -1 || /\bcomment\b/.test(cm.getTokenTypeAt(CodeMirror.Pos(lineNo, spaceTo + 1))))
     18     return -1
     19   return CodeMirror.countColumn(text, null, cm.getOption("tabSize"))
     20 }
     21 
     22 CodeMirror.registerHelper("fold", "indent", function(cm, start) {
     23   var myIndent = lineIndent(cm, start.line)
     24   if (myIndent < 0) return
     25   var lastLineInFold = null
     26 
     27   // Go through lines until we find a line that definitely doesn't belong in
     28   // the block we're folding, or to the end.
     29   for (var i = start.line + 1, end = cm.lastLine(); i <= end; ++i) {
     30     var indent = lineIndent(cm, i)
     31     if (indent == -1) {
     32     } else if (indent > myIndent) {
     33       // Lines with a greater indent are considered part of the block.
     34       lastLineInFold = i;
     35     } else {
     36       // If this line has non-space, non-comment content, and is
     37       // indented less or equal to the start line, it is the start of
     38       // another block.
     39       break;
     40     }
     41   }
     42   if (lastLineInFold) return {
     43     from: CodeMirror.Pos(start.line, cm.getLine(start.line).length),
     44     to: CodeMirror.Pos(lastLineInFold, cm.getLine(lastLineInFold).length)
     45   };
     46 });
     47 
     48 });