How insert a string into the active editor ?

Hi Friends,

I am very new to writing plugin for intellij. I am trying to write a basic plugin which will insert a string into the active editor. Tried the following code:

Editor editor = DataKeys.EDITOR.getData(e.getDataContext());
int offset = editor.getCaretModel().getOffset();
Document document = editor.getDocument();
if(document.isWritable())
document.fireReadOnlyModificationAttempt();
document.insertString(offset, "Some New String");

But got an error saying i have to use commands for undoable actions.

I thought i should be using CommandInterpreter. Am i right ? if yes, How should i use it ?

Thanks in advance

2 comments
Comment actions Permalink

Hello Rayala,

I am very new to writing plugin for intellij. I am trying to write a
basic plugin which will insert a string into the active editor. Tried
the following code:

Editor editor = DataKeys.EDITOR.getData(e.getDataContext());
int offset = editor.getCaretModel().getOffset();
Document document = editor.getDocument();
if(document.isWritable())
document.fireReadOnlyModificationAttempt();
document.insertString(offset, "Some New String");
But got an error saying i have to use commands for undoable actions.

I thought i should be using CommandInterpreter. Am i right ? if yes,
How should i use it ?


No, you should wrap the modification in CommandProcessor.executeCommand().
There is no CommandInterpreter class in IntelliJ IDEA.

You can find lots of samples for using this class in open-source plugins
for IntelliJ IDEA.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hi Dmitry,

First of all, Thanks for the reply.

I have found the following function being used in an plugin source code.

EditorModificationUtil.insertStringAtCaret(editor, "Inserted");

The text would have got inserted at the current caret position But i am getting the following exception printed in the console,

ERROR - plication.impl.ApplicationImpl - Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())

And i am using this method inside a subclass of AnAction.

And i couldnt find how to use CommandProcessor.executeCommand(). I would be very helpful if some one give an example of how to use it.

0

Please sign in to leave a comment.