当前位置: 首页>>代码示例>>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;未经允许,请勿转载。