UNPKG

lobo

Version:
226 lines (225 loc) 8.7 kB
"use strict"; exports.__esModule = true; var ElmCodeHelperImp = /** @class */ (function () { function ElmCodeHelperImp(code) { this.delimitersFunction = [" ", "\n", "\"", ":", ",", "=", "[", "]", "{", "}", "(", ")", "\\"]; this.delimitersTypeList = [" ", "\n", ",", "(", ")"]; this.code = code; this.maxIndex = code.length - 1; this.commentMap = this.buildCommentMap(this.code); } ElmCodeHelperImp.prototype.buildCommentMap = function (code) { var commentMap = []; var from; var fromLine; var blockCount = 0; for (var i = 0; i <= code.length; i++) { if (code[i] === "{" && code[i + 1] === "-") { if (from === undefined) { from = i; } blockCount++; } else if (from !== undefined && code[i] === "-" && code[i + 1] === "}") { if (blockCount === 1) { commentMap.push({ fromIndex: from, toIndex: i + 1 }); from = undefined; } blockCount--; } else if (from === undefined && fromLine === undefined && code[i] === "-" && code[i + 1] === "-") { fromLine = i; } else if (from === undefined && fromLine !== undefined && code[i] === "\n") { commentMap.push({ fromIndex: fromLine, toIndex: i - 1 }); fromLine = undefined; } } if (from !== undefined) { commentMap.push({ fromIndex: from, toIndex: code.length - 1 }); } else if (fromLine !== undefined) { commentMap.push({ fromIndex: fromLine, toIndex: code.length - 1 }); } return commentMap; }; ElmCodeHelperImp.prototype.codeBetween = function (startIndex, endIndex) { return this.code.substring(startIndex, endIndex + 1); }; ElmCodeHelperImp.prototype.exists = function (index, searchTerms) { for (var i = 0; i < searchTerms.length; i++) { if (this.existsAt(index, searchTerms[i])) { return i; } } return -1; }; ElmCodeHelperImp.prototype.existsAt = function (index, searchTerm) { for (var i = 0; i < searchTerm.length; i++) { if (this.code[index + i] !== searchTerm[i]) { return false; } } return true; }; ElmCodeHelperImp.prototype.findIncludingComments = function (startIndex, isMatch) { var index = startIndex; while (index <= this.maxIndex) { var result = isMatch(index); if (result) { return result; } index++; } return undefined; }; ElmCodeHelperImp.prototype.findExcludingComments = function (startIndex, isMatch) { var commentIndex = 0; var maxCommentMapIndex = this.commentMap.length - 1; while (commentIndex <= maxCommentMapIndex) { if (this.commentMap[commentIndex].toIndex > startIndex) { break; } commentIndex++; } var index = startIndex; while (index <= this.maxIndex) { if (commentIndex <= maxCommentMapIndex && this.commentMap[commentIndex].fromIndex <= index && this.commentMap[commentIndex].toIndex >= index) { index = this.commentMap[commentIndex].toIndex + 1; commentIndex++; } else { var result = isMatch(index); if (result) { return result; } index++; } } return undefined; }; ElmCodeHelperImp.prototype.findChar = function (startIndex, searchChar, includeComments) { var _this = this; if (includeComments === void 0) { includeComments = false; } var isMatch = function (index) { if (_this.existsAt(index, searchChar)) { return index; } else { return undefined; } }; if (includeComments) { return this.findIncludingComments(startIndex, isMatch); } return this.findExcludingComments(startIndex, isMatch); }; ElmCodeHelperImp.prototype.findClose = function (startIndex, open, close, includeComments) { var _this = this; if (includeComments === void 0) { includeComments = false; } var contextCount = 0; var isMatch = function (index) { if (_this.existsAt(index, close)) { contextCount--; if (contextCount <= 0) { return index; } } else if (_this.existsAt(index, open)) { contextCount++; } return undefined; }; if (includeComments) { return this.findIncludingComments(startIndex, isMatch); } return this.findExcludingComments(startIndex, isMatch); }; ElmCodeHelperImp.prototype.findEndComment = function (wordResult) { if (wordResult.word[0] === "{" && wordResult.word[1] === "-") { var endBlockIndex = this.findClose(wordResult.nextIndex - wordResult.word.length - 1, "{-", "-}", true); if (!endBlockIndex) { return this.maxIndex; } return endBlockIndex + 1; } var endLineIndex = this.findChar(wordResult.nextIndex, "\n", true); if (!endLineIndex) { return this.maxIndex; } return endLineIndex - 1; }; ElmCodeHelperImp.prototype.findNextWord = function (startIndex, skipComments, delimiters) { var _this = this; if (skipComments === void 0) { skipComments = true; } if (delimiters === void 0) { delimiters = [" ", "\n", ":", "="]; } var isMatch = function (index) { if (_this.exists(index, delimiters) >= 0) { if (startIndex === index) { return { nextIndex: index + 1, word: _this.codeBetween(startIndex, index) }; } else { return { nextIndex: index, word: _this.codeBetween(startIndex, index - 1) }; } } else if (index === _this.maxIndex) { return { nextIndex: index + 1, word: _this.codeBetween(startIndex, index) }; } return undefined; }; var result; if (skipComments) { result = this.findExcludingComments(startIndex, isMatch); if (result && this.isWordComment(result.word)) { var endIndex = this.findEndComment(result); return this.findNextWord(endIndex + 1, skipComments, delimiters); } } else { result = this.findIncludingComments(startIndex, isMatch); if (result && this.isWordComment(result.word)) { var endIndex = this.findEndComment(result); return { nextIndex: endIndex + 1, word: this.codeBetween(startIndex, endIndex) }; } } if (result) { return result; } return { nextIndex: this.maxIndex + 1, word: this.codeBetween(startIndex, this.maxIndex) }; }; ElmCodeHelperImp.prototype.findUntilEndOfBlock = function (startIndex, wordResult) { var _this = this; var currentStartIndex = startIndex; var endIndex = startIndex; var isMatch = function (index) { if (_this.code[index] === "\n") { if (_this.code[index - 1] !== "\n") { endIndex = index - 1; } if (_this.code[index + 1] !== "\n" && _this.code[index + 1] !== " ") { return { nextIndex: endIndex, word: wordResult.word }; } } return undefined; }; var result = this.findExcludingComments(currentStartIndex, isMatch); if (result) { return result; } return { nextIndex: this.maxIndex, word: wordResult.word }; }; ElmCodeHelperImp.prototype.isWordComment = function (word) { if (!word || word.length < 2) { return false; } return word[1] === "-" && (word[0] === "{" || word[0] === "-"); }; return ElmCodeHelperImp; }()); exports.ElmCodeHelperImp = ElmCodeHelperImp; function makeElmCodeHelper(code) { return new ElmCodeHelperImp(code); } exports.makeElmCodeHelper = makeElmCodeHelper;