Action

Tab Indent

Last update over 4 years ago - Unlisted

Indent selection by one tab

Steps

  • script

    //Initialise some hopefully unique placeholder values we'll use to mark selection areas
    let SEL_START = uuidv4();
    let SEL_END = uuidv4();
    
    //Add some placeholders to the currently selected range of text and extend the selection to include them
    let rngSelected = editor.getSelectedRange();
    let strSelected = editor.getSelectedText();
    editor.setTextInRange(rngSelected[0], rngSelected[1], SEL_START + strSelected + SEL_END);
    editor.setSelectedRange(rngSelected[0] + SEL_START.length, rngSelected[1]);
    
    //Get the text for the lines that are selected and prefix them with tabs
    let rngLines = editor.getSelectedLineRange();
    let strLines = editor.getTextInRange(rngLines[0], rngLines[1]);
    let astrLines = strLines.split("\n");
    astrLines = astrLines.map(
    	function tabIn(p_strLine)
    	{
    		if(p_strLine == "") return p_strLine
    		else return "	" + p_strLine;
    	}
    );
    strLines = astrLines.join("\n");
    
    //Update the line range for the selection with the tabbed text
    editor.setTextInRange(rngLines[0], rngLines[1], strLines);
    
    //At this point our placeholders will have moved overall, and importantly, may have moved in relation to each other. That second part is the real reason why we're using placeholders to simplify things here
    
    //Find our original placeholders and select them and the content between
    let intStart = editor.getText().indexOf(SEL_START);
    let intEnd = editor.getText().indexOf(SEL_END);
    editor.setSelectedRange(intStart, intEnd - intStart + SEL_END.length);
    
    //Get the selected content and its position
    rngSelected = editor.getSelectedRange();
    strSelected = editor.getSelectedText();
    
    //Replace it with the content between the placeholders, effectively stripping out the placeholders
    editor.setSelectedText(strSelected.substring(SEL_START.length, strSelected.length - SEL_END.length));
    //This should leave us with the same effective text selection as before (though maybe with some extra tabs in it)
    
    //A function to generate our selection placeholders
    function uuidv4() {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
      });
    }

Options

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