Create missing parameter

Answered

Hi.

public static void Test()
{
  File.ReadAllLines();
}

With the cursor between ( and ) in ReadAllLines(). I want to generate a new path parameter like this

public static void Test(string path)
{
File.ReadAllLines(path);
}

What's the fastest way using Resharper? I've looking into Live Templates, without any luck

0
6 comments

Hello Karsten Kousgaard, thank you for your question! I think the fastest way is to do this: type "path" parameter, Alt+Enter on the "path", and then select "Create parameter path in containing method". Please let me know if you have any questions. Thank you!

0

Ok, that's what I already do. I was for hoping for a way with less typing.

Thank you for your reply

0

Oh I forgot to mention this, sorry. The point is that the parameter for ReadAllLines is named path, so I want to create a new parameter with the same name as the missing parameter in ReadAllLines.

I hope it makes more sense now.

0

Karsten Kousgaard, thanks for the explanation. I found a similar feature request in our bug tracker: https://youtrack.jetbrains.com/issue/RSRP-479400. Please comment or vote for it to monitor status changes. So far, the only workaround is the one I described above. Thank you and have a nice day!

0

Yes, that's the same feature I'm looking for. Thanks for showing me.

0

I've been spending some time and have found another tool Visual Commander (https://vlasovstudio.com/visual-commander/index.html), which can help with this

I have changed one of the examples and made a command, that works for me.

using Microsoft.CodeAnalysis;

using Microsoft.CodeAnalysis.Text;

using System.Linq;

public class C : VisualCommanderExt.ICommand

{

    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)

    {

        serviceProvider = package as System.IServiceProvider;

        Microsoft.VisualStudio.Text.Editor.IWpfTextView textView = GetTextView();

        Microsoft.VisualStudio.Text.SnapshotPoint caretPosition = textView.Caret.Position.BufferPosition;

        Microsoft.CodeAnalysis.Document document = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

        Microsoft.CodeAnalysis.CSharp.Syntax.InvocationExpressionSyntax invocationExpressionNode =

            document.GetSyntaxRootAsync().Result.FindToken(caretPosition).Parent.AncestorsAndSelf().

                OfType<Microsoft.CodeAnalysis.CSharp.Syntax.InvocationExpressionSyntax>().FirstOrDefault();

        Microsoft.CodeAnalysis.CSharp.Syntax.ObjectCreationExpressionSyntax objectCreationExpressionSyntax =

            document.GetSyntaxRootAsync().Result.FindToken(caretPosition).Parent.AncestorsAndSelf().

                OfType<Microsoft.CodeAnalysis.CSharp.Syntax.ObjectCreationExpressionSyntax>().FirstOrDefault();

        Microsoft.CodeAnalysis.SemanticModel semanticModel = document.GetSemanticModelAsync().Result;

        Microsoft.CodeAnalysis.IMethodSymbol methodSymbol;

        if (invocationExpressionNode != null)

        {

            methodSymbol = ToMethod(semanticModel.GetSymbolInfo(invocationExpressionNode));

        }

        else if (objectCreationExpressionSyntax != null)

        {

            methodSymbol = ToMethod(semanticModel.GetSymbolInfo(objectCreationExpressionSyntax));

        }

        else

        {

            System.Windows.MessageBox.Show("Method not found");

            return;

        }

        if (methodSymbol == null)

        {

            System.Windows.MessageBox.Show("objectCreationExpressionSyntax or invocationExpressionNode not found");

            return;

        }

        textView.TextBuffer.Insert(caretPosition.Position, ParametersList(methodSymbol.Parameters));

    }

    private Microsoft.CodeAnalysis.IMethodSymbol ToMethod(SymbolInfo s)

    {

        Microsoft.CodeAnalysis.IMethodSymbol result = s.Symbol as Microsoft.CodeAnalysis.IMethodSymbol;

        if (result != null)

            return result;

        if (s.CandidateSymbols.Length > 0)

        {

            result = s.CandidateSymbols[0] as Microsoft.CodeAnalysis.IMethodSymbol;

            if (result != null)

                return result;

        }

        return null;

    }

    private string ParametersList(System.Collections.Immutable.ImmutableArray<IParameterSymbol> parameters)

    {

        string result = "";

        foreach (IParameterSymbol p in parameters)

        {

            if (result.Length > 0)

                result += ", ";

            result += p.Name;

        }

        return result;

    }

    private Microsoft.VisualStudio.Text.Editor.IWpfTextView GetTextView()

    {

        Microsoft.VisualStudio.TextManager.Interop.IVsTextManager textManager =

            (Microsoft.VisualStudio.TextManager.Interop.IVsTextManager)serviceProvider.GetService(

                typeof(Microsoft.VisualStudio.TextManager.Interop.SVsTextManager));

        Microsoft.VisualStudio.TextManager.Interop.IVsTextView textView;

        textManager.GetActiveView(1, null, out textView);

        return GetEditorAdaptersFactoryService().GetWpfTextView(textView);

    }

    private Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService GetEditorAdaptersFactoryService()

    {

        Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel =

            (Microsoft.VisualStudio.ComponentModelHost.IComponentModel)serviceProvider.GetService(

                typeof(Microsoft.VisualStudio.ComponentModelHost.SComponentModel));

        return componentModel.GetService<Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService>();

    }

    private System.IServiceProvider serviceProvider;

}

1

Please sign in to leave a comment.