File indexing completed on 2024-05-19 04:00:09

0001 var katescript = {
0002     "name": "LilyPond",
0003     "author": "Wilbert Berendsen <info@wilbertberendsen.nl>",
0004     "license": "LGPL",
0005     "revision": 3,
0006     "kate-version": "5.1",
0007     "required-syntax-style": "lilypond",
0008     "indent-languages": ["lilypond"]
0009 }; // kate-script-header, must be at the start of the file without comments, pure json
0010 
0011 /*
0012     SPDX-FileCopyrightText: 2008 Wilbert Berendsen <info@wilbertberendsen.nl>
0013 
0014     SPDX-License-Identifier: LGPL-2.0-or-later
0015 */
0016 
0017 // required katepart js libraries
0018 require ("range.js");
0019 
0020 var triggerCharacters = "}>%;";
0021 
0022 function dbg(s) {
0023   // debug to the term in blue so that it's easier to make out amongst all
0024   // of Kate's other debug output.
0025   debug("\u001B[34m" + s + "\u001B[0m");
0026 }
0027 
0028 reCloser = /\}|\>\>/g;
0029 reStartClosers = /^(\s*([%#]?\}|\>\>))+/
0030 reSkipLine = /^\s*$|^%(?![{}])|^;/;
0031 reFullCommentLine = /^\s*(;;;|%%%)/;
0032 
0033 function indent(line, indentWidth, ch)
0034 {
0035   // not necessary to indent the first line
0036   if (line == 0)
0037     return -2;
0038 
0039   var c = document.line(line); // current line
0040 
0041   // no indent for triple commented lines
0042   if (c.match(reFullCommentLine))
0043     return 0;
0044 
0045   // Search backwards for first non-space, non-comment line.
0046   var prev = line;
0047   while (prev--) {
0048     if (!document.line(prev).match(reSkipLine)) {
0049       var p = document.line(prev); // previous non-space line
0050       var prevIndent = document.firstVirtualColumn(prev);
0051       var pos = 0;
0052       var end = document.lineLength(prev);
0053       // Discard first closers: } and >> as they already influenced the indent.
0054       if (m = p.match(reStartClosers))
0055         var pos = m[0].length;
0056 
0057       var delta = 0;    // count normal openers/closers: { << } >>
0058       var level = 0;    // count opened Scheme parens
0059       var paren = [];   // save their positions
0060       // Walk over openers and closers in the remainder of the previous line.
0061       while (pos < end) {
0062         if (!document.isString(prev, pos)) {
0063           var one = document.charAt(prev, pos);
0064           var two = one + (pos+1 < end ? document.charAt(prev, pos+1) : "");
0065           if (two == "%{" || two == "#{" || two == "<<")
0066             ++delta, ++pos;
0067           else if (two == "%}" || two == "#}" || two == ">>")
0068             --delta, ++pos;
0069           else if (one == "%" || one == ";")
0070             break; // discard rest of line
0071           else if (one == "{")
0072             ++delta;
0073           else if (one == "}")
0074             --delta;
0075           // match parens only if they are Scheme code (not LilyPond slurs)
0076           else if (document.attribute(prev, pos) == 25 ||
0077                    document.attribute(prev, pos) == 26) {
0078             if (one == "(") {
0079               // save position of first
0080               if (level >= 0 && paren[level] == null)
0081                 paren[level] = pos;
0082               ++level, ++delta;
0083             }
0084             else if (one == ")") {
0085               --level, --delta;
0086               // is this the final closing paren of a Scheme expression?
0087               isLast = document.attribute(prev, pos) == 26
0088               // have we seen an opening parenthesis in this line?
0089               if (level >= 0 && paren[level] != null && !isLast) {
0090                 delta = 0;
0091                 prevIndent = document.toVirtualColumn(prev, paren[level]);
0092               }
0093               else {
0094                 var cur = document.anchor(prev, pos, "(");
0095                 if (cur.isValid()) {
0096                   delta = 0;
0097                   if (isLast)
0098                     prevIndent = document.firstVirtualColumn(cur.line);
0099                   else
0100                     prevIndent = document.toVirtualColumn(cur.line, cur.column);
0101                 }
0102               }
0103             }
0104           }
0105         }
0106         ++pos;
0107       }
0108       // now count the number of closers in the beginning of the current line.
0109       if (m = c.match(reStartClosers))
0110         delta -= m[0].match(reCloser).length;
0111       return Math.max(0, prevIndent + delta * indentWidth);
0112     }
0113   }
0114   return 0;
0115 }