document.Replace(..,..); "JetBrains.Util.LoggerException: Document can be modified inside a command scope only"

Hi,
i,m trying to replace some text in text editor but i keep getting this exception:
JetBrains.Util.LoggerException: Document can be modified inside a command scope only

In my Execute method i'm doing something like this:
[ActionHandler("ResharperPlugin1.FirstPlugin")]
    public class FirstPluginAction : IActionHandler
    {
      public void Execute(IDataContext context, DelegateExecute nextExecute)
      {
            ISolution solution = context.GetData(JetBrains.IDE.DataConstants.SOLUTION);
            IProjectFile projectFile = context.GetData(JetBrains.IDE.DataConstants.PROJECT_MODEL_ELEMENT) as IProjectFile;
            DocumentManager documentManager = DocumentManager.GetInstance(solution);
            IDocument document = documentManager.GetDocument(projectFile);
            TextRange t = new TextRange(3, 10);
            document.ReplaceText(t, "This is a new text");

5 comments

i'll just say that at the end the text is replaced but the resharper exception window pops up.
What an i doing wrong? what is command scope and how can i avoid this exception.
Best regards
Daro

0

Daro,

you have to wrap your document modification with using(
CommandCookie.Create("command name for undo") )

--
Sergey V. Coox
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0

Thanks for quick answer, however now during invoking
using (var cookie = CommandCookie.Create("UndoChangeComment"))
                        {
                            document.ReplaceText(t, result);
                        }
on the line document.ReplaceText i get this exception:
JetBrains.Util.LoggerException: ModificationCookie is not currently held for the file.

Shall i add anything more?
Thanks a lot.
Daro

0

ok i think i got it:
                        using (ModificationCookie cookie = textControl.Document.EnsureWritable())
                        {
                            if (cookie.EnsureWritableResult != EnsureWritableResult.SUCCESS)
                            {
                                throw new Exception("not writable");
                            }

                            using (CommandCookie.Create("Add Exceptions List"))
                            {
                                document.ReplaceText(t, result);
                            }
                        }

0

Hello ,

You figured it quite right, but you shouldn't be throwing exception if result
was not success. For example, if modifying the file requires checkout from
source control and user clicked Cancel to the request from source control,
like he decided to not change file this way, or checkout was unsuccesful
because source server is not reachable, or whatever else. Fail gracefully
by cancelling action.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


IR> ok i think i got it:
IR> using (ModificationCookie cookie =
IR> textControl.Document.EnsureWritable())
IR> {
IR> if (cookie.EnsureWritableResult !=
IR> EnsureWritableResult.SUCCESS)
IR> {
IR> throw new Exception("not writable");
IR> }
IR> using (CommandCookie.Create("Add
IR> Exceptions List"))
IR> {
IR> document.ReplaceText(t, result);
IR> }
IR> }
IR> ---
IR> Original message URL:
IR> http://www.jetbrains.net/devnet/message/5232428#5232428


0

Please sign in to leave a comment.