package com.github.kraii.intellij.textreplacer;

import com.google.common.base.Strings;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TextReplaceAction extends AnAction {

    private static final Logger LOGGER = LoggerFactory.getLogger(TextReplaceAction.class);
    private static final int START_OF_STRING = 0;

    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        LOGGER.debug("Yo");
        Editor editor = DataKeys.EDITOR.getData(anActionEvent.getDataContext());
        if (editor != null) {
            SelectionModel selectionModel = editor.getSelectionModel();
            String selectedText = selectionModel.getSelectedText();

            if (selectedText != null && !selectedText.isEmpty()) {
                replaceSelectedTextWithSpaces(editor.getDocument(), selectionModel, selectedText);
            }
        }
    }

    private void replaceSelectedTextWithSpaces(Document document, SelectionModel selectionModel, String selectedText) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            String documentText = document.getText();
            String textBeforeSelection = documentText.substring(START_OF_STRING, selectionModel.getSelectionStart());
            String textAfterSelection = documentText.substring(selectionModel.getSelectionEnd());

            document.setText(textBeforeSelection + spacesWithTheSameLengthAs(selectedText) + textAfterSelection);
        });
    }

    @NotNull
    private String spacesWithTheSameLengthAs(String selectedText) {
        return Strings.padEnd(" ", selectedText.length(), ' ');
    }
}

@@ -184,24 +184,78 @@
 ctionEvent;%0A
+import com.intellij.openapi.actionSystem.DataContext;%0A
 import com.i
@@ -349,24 +349,66 @@
 ionManager;%0A
+import com.intellij.openapi.editor.Caret;%0A
 import com.i
@@ -534,16 +534,147 @@
 nModel;%0A
+import com.intellij.openapi.editor.actionSystem.EditorAction;%0Aimport com.intellij.openapi.editor.actionSystem.EditorActionHandler;%0A
 import o
@@ -804,18 +804,22 @@
 extends 
-An
+Editor
 Action %7B
@@ -970,175 +970,229 @@
     
-@Override%0A    public void actionPerformed(AnActionEvent anActionEvent) %7B%0A        LOGGER.debug(%22Yo%22);%0A        Editor editor = DataKeys.EDITOR.getData(anActionEvent.getD
+public TextReplaceAction() %7B%0A        super(new Handler());%0A    %7D%0A%0A    private static class Handler extends EditorActionHandler %7B%0A        @Override%0A        protected void doExecute(Editor editor, Caret caret, DataContext d
 ataC
@@ -1197,29 +1197,31 @@
 aContext
-());%0A
+) %7B%0A   
         
 if (edit
@@ -1216,29 +1216,53 @@
     
-if (editor != null) %7B
+ super.doExecute(editor, caret, dataContext);
 %0A   
@@ -1462,24 +1462,74 @@
 sEmpty()) %7B%0A
+                LOGGER.debug(%22Woop gonna do it%22);%0A
             
@@ -1624,25 +1624,97 @@
            %7D
+ else %7B
 %0A
+                LOGGER.debug(%22No selected text - ignoring%22);%0A    
         %7D%0A  
@@ -1711,27 +1711,35 @@
       %7D%0A    
+ 
-%7D%0A%0A
+   %7D%0A%0A    
     private 
@@ -1854,16 +1854,20 @@
         
+   
+ 
 Applicat
@@ -1925,24 +1925,28 @@
             
+    
 String docum
@@ -1979,32 +1979,36 @@
 ();%0A            
+   
+ 
 String textBefor
@@ -2101,32 +2101,36 @@
 ));%0A            
+    
 String textAfter
@@ -2212,16 +2212,20 @@
         
+   
+ 
 document
@@ -2330,19 +2330,31 @@
     
-%7D);%0A
+    %7D);%0A    
     %7D%0A%0A
+    
     
@@ -2362,16 +2362,20 @@
 NotNull%0A
+    
     priv
@@ -2442,16 +2442,20 @@
         
+    
 return S
@@ -2506,12 +2506,24 @@
 ');%0A    
-%7D
+    %7D%0A    %7D%0A%0A
 %0A%7D%0A

Haphazardly converted to EditorAction