Drafts Action Directory

Select 1 more word (Right)

Posted by @ComplexPoint, Last updated: 2018-07-04 06:53:22 UTC

Select current word,
or extend the selection Rightward by one more word.

Install

Steps

  • script

    (() => {
        'use strict';
    
        // main :: IO ()
        const main = () =>
            expandSelnByWord(true, false);
    
    
        // GENERIC FUNCTIONS ----------------------------------
    
        // Tuple (,) :: a -> b -> (a, b)
        const Tuple = (a, b) => ({
            type: 'Tuple',
            '0': a,
            '1': b,
            length: 2
        });
    
        // concatMap :: (a -> [b]) -> [a] -> [b]
        const concatMap = (f, xs) => [].concat.apply([], xs.map(f));
    
        // splitAt :: Int -> [a] -> ([a],[a])
        const splitAt = (n, xs) => Tuple(xs.slice(0, n), xs.slice(n));
    
    
        // SELECTION EXTENSIONS  ------------------------------
    
        // expandSelnByWord :: () -> IO ()
        const expandSelnByWord = (blnMultiWord, blnLeft) => {
            const
                e = editor,
                tplSeln = e.getSelectedRange(),
                tplLine = e.getSelectedLineRange(),
                strLine = e.getTextInRange(...tplLine),
                intPosn = tplSeln[0],
                xy = splitAt(
                    intPosn - tplLine[0],
                    strLine
                ),
                [dl, dr] = concatMap(
                    x => x !== null ? (
                        [x[0].length]
                    ) : [0], //
                    [/\b[\S]*$/.exec(xy[0]), /^[\S]*\b/.exec(xy[1])]
                );
            return (tplSeln[1] === 0 || dl > 0 && dr > 0) ? (
                e.setSelectedRange(intPosn - dl, dl + dr),
                'extended'
            ) : blnMultiWord ? (
                additionalWord(
                    blnLeft, tplSeln, tplLine, strLine
                )
            ) : 'No further';
        };
    
        // additionalWord :: Bool -> (Int, Int) -> (Int, Int)
        //                                          -> String -> IO ()
        const additionalWord = (blnLeft, tplSeln, tplLine, strLine) => {
            const
                e = editor,
                s = blnLeft ? (
                    strLine.slice(0, tplSeln[0] - tplLine[0])
                ) : strLine.slice(tplSeln[0] - tplLine[0] + tplSeln[1]),
                m = blnLeft ? (
                    /\b\S+\s*$/.exec(s)
                ) : /^\s*\S+\b/.exec(s),
                d = m ? m[0].length : 1;
    
            return (
                e.setSelectedRange(
                    tplSeln[0] - (blnLeft ? d : 0),
                    tplSeln[1] + d
                ),
                m ? 'extending' : (() => {
                    const
                        tplNew = e.getSelectedRange(),
                        blnFinished = blnLeft ? (
                            tplNew[0] < 1
                        ) : tplNew[0] + tplNew[1] >= e.getText().length;
                    return blnFinished ? (
                        'Finished'
                    ) : expandSelnByWord(true, blnLeft);
                })()
            );
        };
    
        // MAIN ---
        return main();
    })();
    

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.