Admiral Land
Admiral Land

Reputation: 2492

How to get arguments of method to make completion with Roslyn?

I try to make code completion with Roslyn and AvalonEdit. For example, user have code:

public void Completion(int i,int j) { }

And he types:

 Completion(

So, I want to get arguments of method (int i, int j) and make code completion.

I write simple code, which works with '.' and may be this code will work for '(' letter?

 public List<ICompletionData> GetCompletionData(String code,int offset,CompletionType completionType)
    {              
        var syntaxTree = SyntaxFactory.ParseSyntaxTree(code);
        var compilation = CSharpCompilation.Create("foo")
            .AddReferences(Mscorlib)
            .AddSyntaxTrees(syntaxTree);
        var semanticModel = compilation.GetSemanticModel(syntaxTree);
        var textSpan = GetTextSpan(offset,1);// '.' or '(' coordinates                               
        ITypeSymbol lhsType = null;
        if (completionType == CompletionType.DotCompletion)
        {
            var memberAccessNode = (MemberAccessExpressionSyntax)syntaxTree.GetRoot()
           .DescendantNodes(textSpan).Last();
            lhsType = semanticModel.GetTypeInfo(memberAccessNode.Expression).Type;
        }
        else if(completionType==CompletionType.ArgumentListCompletion)
        {
            var arr = syntaxTree.GetRoot().DescendantNodes(textSpan).Last();
            var argumentListMode = (ArgumentListSyntax)syntaxTree.GetRoot().DescendantNodes(textSpan).Last();
            var directive = argumentListMode.GetFirstDirective();
            var arrgs=argumentListMode.Arguments;
            //lhsType = semanticModel.GetTypeInfo(directive).Type;
            //how to get lhsType?
        }

        if (lhsType == null)
            return new List<ICompletionData>();
        List<ICompletionData> completionDataColl = new List<ICompletionData>();

        // and here I make completion data
        foreach (var symbol in lhsType.GetMembers())
        {
            if (!symbol.CanBeReferencedByName
                || symbol.DeclaredAccessibility != Accessibility.Public
                || symbol.IsStatic)
                continue;
        }
   }

The problem is, that I can not get ITypeSymbol lhsType. It is null. How to get lhsType? Or, maybe I should use another way?

Upvotes: 1

Views: 1020

Answers (1)

Gui Padua
Gui Padua

Reputation: 19

I don't know the code completion (I couldn't find this class called CompletionType) itself, but here is a way based on Roslyn only: semantic model and the method invocation, which I believe you have available (make the method call string a InvocationExpressionSyntax)

To obtain the arguments of a method you can get its SymbolInfo from the semantic model. Then you get its symbol. A symbol contains the list of parameters(arguments).

You can call SemanticModel.GetSymbolInfo()

The result will provide you a symbol or candidate symbols (if it is an overloaded method).

A method symbol will provide the list of parameters, which is the arguments of that method.

Upvotes: 0

Related Questions