當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript string.default.lines方法代碼示例

本文整理匯總了TypeScript中underscore.string.default.lines方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript string.default.lines方法的具體用法?TypeScript string.default.lines怎麽用?TypeScript string.default.lines使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在underscore.string.default的用法示例。


在下文中一共展示了string.default.lines方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: getHighlightedCode

function getHighlightedCode(lscAst: ILambdaScriptAstNode, lambdaScriptCode: string) {
    const codeByLines = _str.lines(lambdaScriptCode),
        colorActions = traverse(lscAst)
            .reduce(function(colorActions: ILineColorAction[], astNode: ILambdaScriptAstNode) {
                const color = colorMap[astNode.type];

                if (color) {
                    const colorFn = chalk[color].bind(chalk),
                        locKey = alternateLocMap[astNode.type] || 'loc',

                        // I'm just using _.result to avoid TS7017
                        loc: ILoc = _.result(astNode, locKey);

                    /**
                     * Originally, I just colored as soon as I found a node. However, this does not work,
                     * because other coloring may have occurred on the line already, which will offset our
                     * column indices. And it is not sufficient just to drop all color from the line and
                     * see what the length difference is, because some of that color could be *after* our
                     * column indices and thus irrelevant. To solve this, we will gather all the colorings
                     * we want to do, and then apply them in sorted order. This makes the offset caclulation trivial.
                     */
                    return colorActions.concat({
                        // I don't know why but jison does not 0-index the line number.
                        lineIndex: loc.first_line - 1,
                        colStart: loc.first_column,
                        colEnd: loc.last_column,
                        colorFn: colorFn
                    });
                }

                return colorActions;
            }, []),

        coloredCode = _(colorActions)
            .sortByAll(['lineIndex', 'colStart'])
            .reduce(function(lines: string[], colorAction: ILineColorAction) {

                const linesClone = _.clone(lines),
                    lineToColor = linesClone[colorAction.lineIndex],

                    existingColorOffset = lineToColor.length - chalk.stripColor(lineToColor).length,

                    colStartWithOffset = colorAction.colStart + existingColorOffset,
                    colEndWithOffset = colorAction.colEnd + existingColorOffset;

                // One potential way to fail fast here would be to detect when we are coloring
                // over an existing highlight. I have yet to encounter a case where that was expected behavior.

                linesClone[colorAction.lineIndex] =
                    lineToColor.slice(0, colStartWithOffset) +
                    colorAction.colorFn(lineToColor.slice(colStartWithOffset, colEndWithOffset)) +
                    lineToColor.slice(colEndWithOffset);

                logger.debug({
                    original: lines[colorAction.lineIndex],
                    colored: linesClone[colorAction.lineIndex]
                }, 'Coloring line');

                return linesClone;
            }, codeByLines);

    return coloredCode.join(os.EOL);
}
開發者ID:NickHeiner,項目名稱:lambdascript,代碼行數:63,代碼來源:get-highlighted-code.ts


注:本文中的underscore.string.default.lines方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。