Search in sources :

Example 1 with CopyPastePreProcessor

use of com.intellij.codeInsight.editorActions.CopyPastePreProcessor in project intellij-community by JetBrains.

the class QuickEditHandler method altCommitToOriginal.

private void altCommitToOriginal(@NotNull DocumentEvent e) {
    final PsiFile origPsiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myOrigDocument);
    String newText = myNewDocument.getText();
    // prepare guarded blocks
    LinkedHashMap<String, String> replacementMap = new LinkedHashMap<>();
    int count = 0;
    for (RangeMarker o : ContainerUtil.reverse(((DocumentEx) myNewDocument).getGuardedBlocks())) {
        String replacement = o.getUserData(REPLACEMENT_KEY);
        String tempText = "REPLACE" + (count++) + Long.toHexString(StringHash.calc(replacement));
        newText = newText.substring(0, o.getStartOffset()) + tempText + newText.substring(o.getEndOffset());
        replacementMap.put(tempText, replacement);
    }
    // run preformat processors
    final int hostStartOffset = myAltFullRange.getStartOffset();
    myEditor.getCaretModel().moveToOffset(hostStartOffset);
    for (CopyPastePreProcessor preProcessor : Extensions.getExtensions(CopyPastePreProcessor.EP_NAME)) {
        newText = preProcessor.preprocessOnPaste(myProject, origPsiFile, myEditor, newText, null);
    }
    myOrigDocument.replaceString(hostStartOffset, myAltFullRange.getEndOffset(), newText);
    // replace temp strings for guarded blocks
    for (String tempText : replacementMap.keySet()) {
        int idx = CharArrayUtil.indexOf(myOrigDocument.getCharsSequence(), tempText, hostStartOffset, myAltFullRange.getEndOffset());
        myOrigDocument.replaceString(idx, idx + tempText.length(), replacementMap.get(tempText));
    }
    // JAVA: fix occasional char literal concatenation
    fixDocumentQuotes(myOrigDocument, hostStartOffset - 1);
    fixDocumentQuotes(myOrigDocument, myAltFullRange.getEndOffset());
    // reformat
    PsiDocumentManager.getInstance(myProject).commitDocument(myOrigDocument);
    try {
        CodeStyleManager.getInstance(myProject).reformatRange(origPsiFile, hostStartOffset, myAltFullRange.getEndOffset(), true);
    } catch (IncorrectOperationException e1) {
    //LOG.error(e);
    }
    PsiElement newInjected = InjectedLanguageManager.getInstance(myProject).findInjectedElementAt(origPsiFile, hostStartOffset);
    DocumentWindow documentWindow = newInjected == null ? null : InjectedLanguageUtil.getDocumentWindow(newInjected);
    if (documentWindow != null) {
        myEditor.getCaretModel().moveToOffset(documentWindow.injectedToHost(e.getOffset()));
        myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    }
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) CopyPastePreProcessor(com.intellij.codeInsight.editorActions.CopyPastePreProcessor) IncorrectOperationException(com.intellij.util.IncorrectOperationException) RelativePoint(com.intellij.ui.awt.RelativePoint) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Aggregations

CopyPastePreProcessor (com.intellij.codeInsight.editorActions.CopyPastePreProcessor)1 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 RelativePoint (com.intellij.ui.awt.RelativePoint)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 LinkedHashMap (com.intellij.util.containers.hash.LinkedHashMap)1