UNPKG

shahnevis-core

Version:

**Shahnevis Core** is a lightweight and flexible library for building custom code editors. It provides essential features like syntax highlighting, a minimap, multi-cursor support, line numbering, and plugin management. This library is framework-agnostic,

146 lines (128 loc) 7.26 kB
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /* This file is part of Shahnevis Core. Copyright (C) 2024 shahrooz saneidarani (github.com/shahroozD) Shahnevis Core is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Shahnevis Core is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ export function updateIndentationGuides(editor, minimapContent, lineNumbers, foldingManager) { var foldedBlocks = foldingManager.getFoldedBlocksById(); function countTotalFoldedLines(startLine) { var keys = Object.keys(foldedBlocks).map(Number).sort(function (a, b) { return a - b; }); var total = foldedBlocks[startLine].length; // console.log("=---------------------------------"); // console.log(foldedBlocks); // console.log(keys); var _iterator = _createForOfIteratorHelper(keys), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var key = _step.value; // console.log("key: ", key); // console.log("startLine: ", startLine); // console.log(foldedBlocks[key.toString()]); // console.log(foldedBlocks[key.toString()].length); // console.log(total); // console.log(key, parseInt(startLine)); if (key <= startLine) continue; if (key < total + startLine) { total += foldedBlocks[key.toString()].length; } else { break; } } // console.log("total: ", total); } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return total; } var codeLines = _toConsumableArray(editor.value.split('\n')); // const lineNumbers = document.getElementById('line-numbers'); lineNumbers.innerHTML = ''; // Clear previous lines var indentationLevels = []; var hiddenLineCount = 0; var currentVisibleLine = 0; var i = 0; // Iterate through code lines codeLines.forEach(function (line, index) { var _codeLines; var trimmedLine = line.trim(); // If there are hidden lines, append placeholders if (hiddenLineCount) { appendHiddenLines(lineNumbers, hiddenLineCount); currentVisibleLine += countTotalFoldedLines(currentVisibleLine - 1) + 1; hiddenLineCount = 0; } else { currentVisibleLine++; } var trimmedNextLine = (_codeLines = codeLines[index + 1]) === null || _codeLines === void 0 ? void 0 : _codeLines.trim(); var isFolded = !!foldedBlocks[currentVisibleLine - 1]; // Create line number element with indentations var lineNumberElement = createLineNumberElement(trimmedLine, currentVisibleLine, indentationLevels, index); // Add fold/expand buttons lineNumberElement.appendChild(foldingManager.foldingButtons(line, index, currentVisibleLine - 1, editor, minimapContent, lineNumbers, foldingManager)); lineNumberElement.appendChild(foldingManager.expandButtons(line, index, currentVisibleLine - 1, editor, minimapContent, lineNumbers, foldingManager)); lineNumbers.appendChild(lineNumberElement); // Adjust indentation based on code structure handleIndentation(trimmedLine, trimmedNextLine, indentationLevels, isFolded); // Handle folded lines if (isFolded) { hiddenLineCount = foldedBlocks[currentVisibleLine - 1].length; indentationLevels.pop(); } }); } // Helper function to append hidden lines function appendHiddenLines(lineNumbers, count) { for (var i = 0; i < count; i++) { var hiddenLineNumberElement = document.createElement('div'); hiddenLineNumberElement.className = "line-number"; hiddenLineNumberElement.style.visibility = 'hidden'; lineNumbers.appendChild(hiddenLineNumberElement); } } // Helper function to create the line number element function createLineNumberElement(trimmedLine, visibleLineNumber, indentationLevels, index) { var lineNumberElement = document.createElement('div'); lineNumberElement.className = "line-number"; var indentations = getIndentationGuides(indentationLevels); lineNumberElement.innerHTML = indentations + "<span>".concat(visibleLineNumber, "</span>"); return lineNumberElement; } // Helper function to get indentation guides function getIndentationGuides(levels) { var indentations = ''; levels.forEach(function (level) { indentations += "<span class=\"indentation-guide line-indentation-".concat(level, "\"></span>"); }); return indentations; } // Helper function to handle indentation logic function handleIndentation(trimmedLine, nextLine, indentationLevels, isFolded) { if (trimmedLine.endsWith('{') && !isFolded || trimmedLine.endsWith('(')) { indentationLevels.push(indentationLevels.length + 1); // New block } if (nextLine !== null && nextLine !== void 0 && nextLine.startsWith('}') || nextLine !== null && nextLine !== void 0 && nextLine.startsWith(')')) { indentationLevels.pop(); // End of block } } //#endregion