JS Outdent

Posted by RoyRogers, Last update 24 days ago

Outdents single or mutliple lines.
Removes two spaces or one tab

Steps

  • script

    // JS Outdent
    // RV 2020-03-06 at 18:36 EST
    
    // Outdents single or mutliple lines.
    // Removes two spaces or tab.
    
    var lnRange = editor.getSelectedLineRange();
    var text = editor.getTextInRange(lnRange[0],lnRange[1]);
    var selRange = editor.getSelectedRange();
    var charsAdded = 0;
    var newText = '';
    var lnCount = text.split('\n').length;
    
    main: {
      // Blank line or no indents:
      if (lnRange[1] == 1 || text.match(/^\S/)) break main; 
    
      newText = text.replace(/^\t|^  |^ /gm, '')
      charsAdded = newText.length - text.length;
    
      // update text and set new selection:
      editor.setTextInRange(lnRange[0],lnRange[1], newText);
    
      if (lnCount > 2) {
        editor.setSelectedRange(lnRange[0], lnRange[1] + charsAdded);
      }
      else {
        let offset = 2;
        if (selRange[0] - lnRange[0] == 0) offset = 0;
    
        editor.setSelectedRange(selRange[0] - offset, selRange[1]);
      }
    }
    
    

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.