Pattern Catalog Replace swallows code

I wanted to acchieve following: Resharper should warn me on follwing statement:

Form1 form1 = new Form1();
 
        form1.Text = "test";
        form1.FormBorderStyle = FormBorderStyle.Fixed3D;
        form1.ShowDialog();     

and replace it it with:

        using (Form1 form1 = new Form1())         {             form1.Text = "test";             form1.FormBorderStyle = FormBorderStyle.Fixed3D;             form1.ShowDialog();         }

I achieved it with the Pattern Catalog I specified following search pattern.

search pattern:

$form$ $variable$ = new $form$();
$stmt$
$variable$.ShowDialog();

replace pattern:

using($form$ $variable$ = new $form$())
{
     $stmt$
     $variable$.ShowDialog();
}

variables:
$form$ is a type (of type form or inherited)
$variable$ is an identifier;
$stmt$ is a statement of minimum 0 - maximum 10 statements;

Problem:

resharper replaces with this definition:

        Form1 form1 = new Form1();         form1.Text = "test";         form1.FormBorderStyle = FormBorderStyle.Fixed3D;         form1.ShowDialog();
into:

        using (Form1 form1 = new Form1())         {              form1.ShowDialog();         }

so it forgets that the stmt has matched 2 statements;

So in my opinion this is a Bug or are I'm doing something wrong.

I attached the pattern and the sample project to reproduce it.




Attachment(s):
WindowsFormsApplication2.zip
Pattern.xml
8 comments

I tried the pattern with Resharper 6 EAP and still the same issue.

0

I found a new problem with pattern matching :-(.

The search works but the replace doesn't work.

When I search for an Identifier with a regular expression like .*Control the find works but not the replace
It gives a strange error of 1 something (don't know the exact term)  not found.
Could it be that the replace is done on the life model, and the matched term is not cached and during the replace the term becomes invalid because it is replaced?

Else the Searching works like a charm. Keep up the good work :-)

P.S.: Any news when this will be fixed? I guess It's in the 6.1 Time Frame.

0

This is the exception message

Cannot find placeholder element '1'

--- EXCEPTION #1/2 [ElementFactoryException]
Message = “Cannot find placeholder element '1'”
ExceptionPath = Root.InnerException
ClassName = JetBrains.ReSharper.Psi.ElementFactoryException
HResult = COR_E_EXCEPTION=80131500
Source = JetBrains.ReSharper.Psi.CSharp
StackTraceString = “
  at JetBrains.ReSharper.Psi.CSharp.Impl.CSharpElementFactoryImpl.SubstituteNodes(ITreeNode root, ParameterMarker[] markers, Object[] args)
     at JetBrains.ReSharper.Psi.CSharp.Impl.CSharpElementFactoryImpl.CreateTypeMemberDeclaration(String format, Object[] args)
     at JetBrains.ReSharper.Psi.Services.CSharp.StructuralSearch.CSharpStructuralSearchReplacer.DoReplace(ITreeNode elementToReplace, String replacePattern, IDictionary`2 placeholders, Func`2 getMatchedElement, IEnumerable`1 elementsToRemove, Boolean formatAfterReplace, Boolean shortenReferences)


--- Outer ---

--- EXCEPTION #2/2 [LoggerException]
Message = “Cannot find placeholder element '1'”
ExceptionPath = Root
ClassName = JetBrains.Util.LoggerException
Data.PlatformSccRevision = CompiledWithoutBuildServer
Data.ProductSccRevision = CompiledWithoutBuildServer
Data.VsVersion = 10.0.40219.1
InnerException = “Exception #1 at Root.InnerException”
HResult = COR_E_APPLICATION=80131600
StackTraceString = “
  at JetBrains.ReSharper.Psi.Services.CSharp.StructuralSearch.CSharpStructuralSearchReplacer.DoReplace(ITreeNode elementToReplace, String replacePattern, IDictionary`2 placeholders, Func`2 getMatchedElement, IEnumerable`1 elementsToRemove, Boolean formatAfterReplace, Boolean shortenReferences)
     at JetBrains.ReSharper.Psi.Services.CSharp.StructuralSearch.CSharpStructuralSearchReplacer.Replace(IStructuralMatchResult match, String replacePattern, IDictionary`2 placeholders, Boolean formatAfterReplace, Boolean shortenReferences)
     at JetBrains.ReSharper.Psi.Services.CSharp.StructuralSearch.CSharpStructuralSearchReplacer.Replace(IEnumerable`1 matches, String replacePattern, IDictionary`2 placeholders, Boolean formatAfterReplace, Boolean shortenReferences)
     at JetBrains.ReSharper.Features.StructuralSearch.StructuralSearchActionManager.<>c__DisplayClass11.<>c__DisplayClass13.<DoReplace>b__10()
     at JetBrains.Threading.ReentrancyGuard.Execute(String name, Action action)
     at JetBrains.Threading.ReentrancyGuard.ExecuteOrQueue(String name, Action action)
     at JetBrains.ReSharper.Features.StructuralSearch.StructuralSearchActionManager.<>c__DisplayClass11.<>c__DisplayClass13.<DoReplace>b__f(Object , EventArgs )
     at JetBrains.ReSharper.Features.StructuralSearch.UI.ReplaceOccurenciesForm.OnOkClick()
     at JetBrains.ReSharper.Features.StructuralSearch.UI.ReplaceOccurenciesForm.<.ctor>b__4(Object , CancelEventArgs )
     at System.Windows.Forms.Form.OnClosing(CancelEventArgs e)
     at System.Windows.Forms.Form.WmClose(Message& m)
     at System.Windows.Forms.Form.WndProc(Message& m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
     at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam)
     at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
     at System.Windows.Forms.Form.Close()
     at JetBrains.ReSharper.Features.StructuralSearch.UI.ReplaceOccurenciesForm.<>c__DisplayClass16.<InitializeFooter>b__13(Object sender, EventArgs args)
     at System.EventHandler.Invoke(Object sender, EventArgs e)
     at System.Windows.Forms.Control.OnClick(EventArgs e)
     at System.Windows.Forms.Button.OnClick(EventArgs e)
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
     at System.Windows.Forms.Control.WndProc(Message& m)
     at System.Windows.Forms.ButtonBase.WndProc(Message& m)
     at System.Windows.Forms.Button.WndProc(Message& m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
     at System.Windows.Forms.Application.ThreadContext.LocalModalMessageLoop(Form form)
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
     at System.Windows.Forms.Application.RunDialog(Form form)
     at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
     at JetBrains.UI.Options.OptionsDialogOwner.<>c__DisplayClass9.<>c__DisplayClassb.<.ctor>b__3(LifetimeDefinition lifetimeDefinitionDialog, Lifetime lifetimeDialog)
     at JetBrains.DataFlow.Lifetimes.Define(Lifetime lifetime, String id, Action`2 FAtomic)
     at JetBrains.UI.Options.OptionsDialogOwner.<>c__DisplayClass9.<>c__DisplayClassb.<.ctor>b__2()
     at JetBrains.Util.Logger.Catch(Action action)
     at JetBrains.UI.Options.OptionsDialogOwner.<>c__DisplayClass9.<.ctor>b__1(Modality modality)
     at JetBrains.UI.Options.OptionsManager.ShowOptionsModal(String pageId)
     at JetBrains.UI.Options.ShowOptionsAction.<Execute>b__0()
     at JetBrains.Util.Logger.Catch(Action action)
     at JetBrains.Threading.IThreadingEx.<>c__DisplayClass6.<>c__DisplayClass8.<ExecuteOrQueueWhenNotGuarded>b__3()
     at JetBrains.Threading.JetDispatcher.Closure.Execute()
     at JetBrains.Threading.JetDispatcher.ProcessQueue()
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
     at System.Windows.Threading.DispatcherOperation.InvokeImpl()
     at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
     at System.Threading.ExecutionContext.runTryCode(Object userData)
     at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Windows.Threading.DispatcherOperation.Invoke()
     at System.Windows.Threading.Dispatcher.ProcessQueue()
     at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
     at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
     at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
     at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

0

Hello Thomas,

Could you please export your SSR pattern into an xml file and attach it here?
Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

I found a new problem with pattern matching :-(.

The search works but the replace doesn't work.

When I search for an Identifier with a regular expression like
.*Control the find works but not the replace

It gives a strange error of 1 something (don't know the exact term)
not found.

Could it be that the replace is done on the life model, and the
matched term is not cached and during the replace the term becomes
invalid because it is replaced?

Else the Searching wokrs like a charm. Keep up the good work :)

P.S.: Any news when this will be fixed? I guess It's in the 6.1 Time
Frame.

---
Original message URL:
http://devnet.jetbrains.net/message/5312562#5312562



0

Here is the Pattern Catalog
It's the ErrorPattern:

Matches this Item
....
        /// <summary>Reaction after a node has been activated.</summary>
        protected override void AfterItemActivate()
        {
            base.AfterItemActivate();

            this.abteilungEditControl.Data = this.cache.GetAbteilung(this.GetSelectedId());
        }
....



Attachment(s):
ErrorPattern.xml
0

Thanks for repoting this issue, it's fixed in internal build and will be available in 6.1 update soon.

0

Thanks a lot for fixing it.

I will test it as soon as EAP is started.

0

Please sign in to leave a comment.