problem inserting text from menu-action

Hi..

this is probably simple; I'm trying to insert text from a menu-item action;

public class InsertAction extends AnAction
{
public void actionPerformed( AnActionEvent event )
{
DataContext dataContext = event.getDataContext();
final Editor editor = (Editor) dataContext.getData(
DataConstants.EDITOR );

ApplicationManager.getApplication().runWriteAction( new Runnable()
{
public void run()
{
EditorModificationUtil.insertStringAtCaret( editor, "test" );
}
} );
}

but get an exception;
ERROR - m.intellij.openapi.command.b.p - Assertion failed:
Undoable actions allowed inside commands only (see
com.intellij.openapi.command.CommandProcessor.executeCommand())

the doc for executeCommand() doesn't say much..

How should I do instead?

thanks for any help!

regards / Ole


3 comments
Comment actions Permalink

You should also wrap your writeAction into a runnable under
CommandProcessor.getInstance().executeCommand();

But it would be much easier and safe if you use EditorAction instead of
AnAction. This would like as:

public class InsertAction extends EditorAction {
public InsertAction() {
super(new Handler());
}

public static class Handler extends EditorWriteActionHandler {
public void executeWriteAction(Editor editor, DataContext dataContext) {
EditorModificationUtil.insertStringAtCaret( editor, "test" );
}
}
}

This construct will run all writeActions, commands for you and also check if
file is read-only and will prompt user for VSS action if needed.

--

Best regards,
Maxim Shafirov
JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"


"Ole Matzura" <ole@eviware.com> wrote in message
news:apb5rn$rto$1@is.intellij.net...

Hi..

>

this is probably simple; I'm trying to insert text from a menu-item

action;
>

public class InsertAction extends AnAction
{
public void actionPerformed( AnActionEvent event )
{
DataContext dataContext = event.getDataContext();
final Editor editor = (Editor) dataContext.getData(
DataConstants.EDITOR );

>

ApplicationManager.getApplication().runWriteAction( new Runnable()
{
public void run()
{
EditorModificationUtil.insertStringAtCaret( editor, "test" );
}
} );
}

>

but get an exception;
ERROR - m.intellij.openapi.command.b.p - Assertion failed:
Undoable actions allowed inside commands only (see
com.intellij.openapi.command.CommandProcessor.executeCommand())

>

the doc for executeCommand() doesn't say much..

>

How should I do instead?

>

thanks for any help!

>

regards / Ole

>
>


0
Comment actions Permalink

Thx! works like a charm!

Is there any way I can get IDEA to apply code formatting to the inserted
text? Maybe I could invoke the Ctrl-Alt-L feature programatically?

regards!

/Ole

"Maxim Shafirov" <max@intellij.net> wrote in message
news:apb76u$t7d$1@is.intellij.net...

You should also wrap your writeAction into a runnable under
CommandProcessor.getInstance().executeCommand();

>

But it would be much easier and safe if you use EditorAction instead of
AnAction. This would like as:

>

public class InsertAction extends EditorAction {
public InsertAction() {
super(new Handler());
}

>

public static class Handler extends EditorWriteActionHandler {
public void executeWriteAction(Editor editor, DataContext dataContext)

{

EditorModificationUtil.insertStringAtCaret( editor, "test" );
}
}
}

>

This construct will run all writeActions, commands for you and also check

if

file is read-only and will prompt user for VSS action if needed.

>

--

>

Best regards,
Maxim Shafirov
JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"

>
>

"Ole Matzura" <ole@eviware.com> wrote in message
news:apb5rn$rto$1@is.intellij.net...

Hi..

>

this is probably simple; I'm trying to insert text from a menu-item

action;
>

public class InsertAction extends AnAction
{
public void actionPerformed( AnActionEvent event )
{
DataContext dataContext = event.getDataContext();
final Editor editor = (Editor) dataContext.getData(
DataConstants.EDITOR );

>

ApplicationManager.getApplication().runWriteAction( new Runnable()
{
public void run()
{
EditorModificationUtil.insertStringAtCaret( editor,

"test" );

}
} );
}

>

but get an exception;
ERROR - m.intellij.openapi.command.b.p - Assertion failed:
Undoable actions allowed inside commands only (see
com.intellij.openapi.command.CommandProcessor.executeCommand())

>

the doc for executeCommand() doesn't say much..

>

How should I do instead?

>

thanks for any help!

>

regards / Ole

>
>

>
>


0
Comment actions Permalink

Yes, you can invoke action with id "ReformatCode" but one will launch a
prompt dialog.

--

Best regards,
Maxim Shafirov
JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"


"Ole Matzura" <ole@eviware.com> wrote in message
news:apb851$u4f$1@is.intellij.net...

Thx! works like a charm!

>

Is there any way I can get IDEA to apply code formatting to the inserted
text? Maybe I could invoke the Ctrl-Alt-L feature programatically?

>

regards!

>

/Ole

>

"Maxim Shafirov" <max@intellij.net> wrote in message
news:apb76u$t7d$1@is.intellij.net...

You should also wrap your writeAction into a runnable under
CommandProcessor.getInstance().executeCommand();

>

But it would be much easier and safe if you use EditorAction instead of
AnAction. This would like as:

>

public class InsertAction extends EditorAction {
public InsertAction() {
super(new Handler());
}

>

public static class Handler extends EditorWriteActionHandler {
public void executeWriteAction(Editor editor, DataContext

dataContext)

{

EditorModificationUtil.insertStringAtCaret( editor, "test" );
}
}
}

>

This construct will run all writeActions, commands for you and also

check

if

file is read-only and will prompt user for VSS action if needed.

>

--

>

Best regards,
Maxim Shafirov
JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"

>
>

"Ole Matzura" <ole@eviware.com> wrote in message
news:apb5rn$rto$1@is.intellij.net...

Hi..

>

this is probably simple; I'm trying to insert text from a menu-item

action;
>

public class InsertAction extends AnAction
{
public void actionPerformed( AnActionEvent event )
{
DataContext dataContext = event.getDataContext();
final Editor editor = (Editor) dataContext.getData(
DataConstants.EDITOR );

>

ApplicationManager.getApplication().runWriteAction( new

Runnable()

{
public void run()
{
EditorModificationUtil.insertStringAtCaret( editor,

"test" );

}
} );
}

>

but get an exception;
ERROR - m.intellij.openapi.command.b.p - Assertion failed:
Undoable actions allowed inside commands only (see
com.intellij.openapi.command.CommandProcessor.executeCommand())

>

the doc for executeCommand() doesn't say much..

>

How should I do instead?

>

thanks for any help!

>

regards / Ole

>
>

>
>

>
>


0

Please sign in to leave a comment.