Action

(Re)number lines

Last update over 6 years ago

(Re)number (selected, or all) lines
(right-justified line number followed by a colon and two spaces)

Aims to either update existing line numbers of this pattern,
or add numbering to lines which lack it.

Steps

  • script

    (() => {
        'use strict';
    
        // MAIN ------------------------------------------------
        // Line numbering (followed by colon and space) updated.
        const main = () =>
            draftOrLinesUpdated(
                xs => {
                    const
                        iLast = xs.length - 1,
                        intDigits = iLast.toString().length,
                        rgxNum = /^(\s*\d+:\s*|)(.*)$/;
                    return map(
                        (x, i) => justifyRight(
                            intDigits, ' ',
                            (1 + i).toString(),
                        ) + ':  ' + rgxNum.exec(x)[2],
                        xs
                    );
                }
            );
    
        // REUSABLE FUNCTION FOR DRAFTS:
        //
        // Update the editor with selected (or otherwise all) lines
        // transformed by a function on a list of (line) strings
        // which returns another list of strings.
    
        // draftOrLinesUpdated :: IO () -> ([String] -> [String]) -> IO ()
        const draftOrLinesUpdated = f => {
            const
                functionName = f.name, // Used for logging.
                // Selected lines or whole of draft.
                [intStart, intLength] = editor.getSelectedLineRange(),
                [intFrom, intChars] = (
                    editor.getSelectedText()
                    .length > 0 ? (
                        [intStart, intLength]
                    ) : [0, editor.getText().length]
                ),
                // Concatenated result of applying f to the list of lines
                strUpdated = unlines(f(lines(
                    editor.getTextInRange(intFrom, intChars)
                )));
            return (
                // Various channels, for testing and use.
                editor.setTextInRange(intFrom, intChars, strUpdated),
                console.log(functionName, strUpdated),
                strUpdated
            );
        };
    
        // GENERIC FUNCTIONS -----------------------------
    
        // justifyRight :: Int -> Char -> String -> String
        const justifyRight = (n, cFiller, strText) =>
            n > strText.length ? (
                (cFiller.repeat(n) + strText)
                .slice(-n)
            ) : strText;
    
        // lines :: String -> [String]
        const lines = s => s.split(/[\r\n]/);
    
        // map :: (a -> b) -> [a] -> [b]
        const map = (f, xs) => xs.map(f);
    
        // unlines :: [String] -> String
        const unlines = xs => xs.join('\n');
    
        // MAIN ------------------------------
        return main();
    })();
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.