Search code examples
javaintellij-ideaintellij-plugin

Intellij Plugin set text of current Document


Is there a way to change the whole code in a file from a string? I mean there is a way to read everything in the Document but can I also replace the whole text?

The way with which you can read the Code in the Document:

SourceCodeString = e.getData(LangDataKeys.EDITOR).getDocument().getText();

So I'm basically trying to do the opposite of that.


Solution

  • Check the related documentation Safely Replacing Selected Text in the Document:

    // Replace the selection with a fixed string.
    // Must do this document change in a write action context.
    WriteCommandAction.runWriteCommandAction(project, () ->
        document.replaceString(start, end, "editor_basics")
    );