public static bool ResolveAt(Document doc, out ResolveResult resolveResult, out AstNode node, CancellationToken token = default(CancellationToken))
        {
            var parsedDocument = doc.ParsedDocument;

            resolveResult = null;
            node          = null;
            if (parsedDocument == null)
            {
                return(false);
            }
            var unit       = parsedDocument.GetAst <SyntaxTree> ();
            var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile;

            if (unit == null || parsedFile == null)
            {
                return(false);
            }
            try {
                var location = RefactoringService.GetCorrectResolveLocation(doc, doc.Editor.Caret.Location);
                resolveResult = ResolveAtLocation.Resolve(doc.Compilation, parsedFile, unit, location, out node, token);
                if (resolveResult == null || node is Statement)
                {
                    return(false);
                }
            } catch (OperationCanceledException) {
                return(false);
            } catch (Exception e) {
                Console.WriteLine("Got resolver exception:" + e);
                return(false);
            }
            return(true);
        }
        public QuickFixResponse FindDerivedMembersAsQuickFixes
            (GotoImplementationRequest request)
        {
            var res = _bufferParser.ParsedContent(request.Buffer, request.FileName);

            var loc = new TextLocation(request.Line, request.Column);

            ResolveResult resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc);

            var rctx       = new CSharpTypeResolveContext(res.Compilation.MainAssembly);
            var usingScope = res.UnresolvedFile.GetUsingScope(loc).Resolve(res.Compilation);

            rctx = rctx.WithUsingScope(usingScope);

            if (resolveResult is TypeResolveResult)
            {
                return(GetTypeResponse(rctx, resolveResult.Type.GetDefinition()));
            }

            if (resolveResult is MemberResolveResult)
            {
                return(GetMemberResponse(rctx, resolveResult as MemberResolveResult));
            }

            return(new QuickFixResponse());
        }
Esempio n. 3
0
        public static bool TryResolveAt(this Document doc, DocumentLocation loc, out ResolveResult result, out AstNode node)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }
            result = null;
            node   = null;
            var parsedDocument = doc.ParsedDocument;

            if (parsedDocument == null)
            {
                return(false);
            }

            var unit       = parsedDocument.GetAst <CompilationUnit> ();
            var parsedFile = parsedDocument.ParsedFile as CSharpParsedFile;

            if (unit == null || parsedFile == null)
            {
                return(false);
            }
            try {
                result = ResolveAtLocation.Resolve(() => doc.Compilation, parsedFile, unit, loc, out node);
                if (result == null || node is Statement)
                {
                    return(false);
                }
            } catch (Exception e) {
                Console.WriteLine("Got resolver exception:" + e);
                return(false);
            }
            return(true);
        }
        void ResolveButtonClick(object sender, EventArgs e)
        {
            IProjectContent project        = new CSharpProjectContent();
            var             unresolvedFile = syntaxTree.ToTypeSystem();

            project = project.AddOrUpdateFiles(unresolvedFile);
            //project = project.AddAssemblyReferences(builtInLibs.Value);

            ICompilation compilation = project.CreateCompilation();

            ResolveResult result;

            if (csharpTreeView.SelectedNode != null)
            {
                var selectedNode           = (AstNode)csharpTreeView.SelectedNode.Tag;
                CSharpAstResolver resolver = new CSharpAstResolver(compilation, syntaxTree, unresolvedFile);
                result = resolver.Resolve(selectedNode);
                // CSharpAstResolver.Resolve() never returns null
            }
            else
            {
                TextLocation location = GetTextLocation(csharpCodeTextBox, csharpCodeTextBox.SelectionStart);
                result = ResolveAtLocation.Resolve(compilation, unresolvedFile, syntaxTree, location);
                if (result == null)
                {
                    MessageBox.Show("Could not find a resolvable node at the caret location.");
                    return;
                }
            }
            using (var dlg = new SemanticTreeDialog(result))
                dlg.ShowDialog();
        }
Esempio n. 5
0
        public Common.DomRegion ResolveTypeByName(string typeName, string typeMemberName)
        {
            if (Project == null)
            {
                return(Common.DomRegion.Empty);
            }

            var code     = $"class dummy11111 {{ System.Type t = typeof({typeName}); }}";
            var location = new ReadOnlyDocument(code).GetLocation(code.Length - 6);

            var syntaxTree = new CSharpParser().Parse(code, "dummy11111.cs");

            syntaxTree.Freeze();
            var unresolvedFile = syntaxTree.ToTypeSystem();

            Project = Project.AddOrUpdateFiles(unresolvedFile);

            var compilation = Project.CreateCompilation();

            var result = ResolveAtLocation.Resolve(compilation, unresolvedFile, syntaxTree, location);

            var type = (result as TypeResolveResult).Type as DefaultResolvedTypeDefinition;

            if (type != null)
            {
                var asm = type.ParentAssembly;
                if (asm.UnresolvedAssembly is IUnresolvedAssembly) //referenced assembly
                {
                    FileLocation document = new Reflector().ReconstructToFile(asm, type, memberName: typeMemberName);
                    return(document.ToDomRegion().ToCommon());
                }
            }

            return(Common.DomRegion.Empty);
        }
Esempio n. 6
0
        public static bool ResolveAt(Document doc, out ResolveResult resolveResult, out AstNode node)
        {
            var parsedDocument = doc.ParsedDocument;

            resolveResult = null;
            node          = null;
            if (parsedDocument == null)
            {
                return(false);
            }
            var unit       = parsedDocument.GetAst <CompilationUnit> ();
            var parsedFile = parsedDocument.ParsedFile as CSharpParsedFile;

            if (unit == null || parsedFile == null)
            {
                return(false);
            }
            try {
                resolveResult = ResolveAtLocation.Resolve(doc.Compilation, parsedFile, unit, doc.Editor.Caret.Location, out node);
                if (resolveResult == null || node is Statement)
                {
                    return(false);
                }
            } catch (Exception e) {
                Console.WriteLine("Got resolver exception:" + e);
                return(false);
            }
            return(true);
        }
Esempio n. 7
0
        static ResolveResult GetHeuristicResult(Document doc, DocumentLocation location, ref AstNode node)
        {
            var editor = doc.Editor;

            if (editor == null || editor.Caret == null)
            {
                return(null);
            }
            int  offset = editor.Caret.Offset;
            bool wasLetter = false, wasWhitespaceAfterLetter = false;

            while (offset < editor.Length)
            {
                char ch           = editor.GetCharAt(offset);
                bool isLetter     = char.IsLetterOrDigit(ch) || ch == '_';
                bool isWhiteSpace = char.IsWhiteSpace(ch);
                bool isValidPunc  = ch == '.' || ch == '<' || ch == '>';

                if (!(wasLetter && wasWhitespaceAfterLetter) && (isLetter || isWhiteSpace || isValidPunc))
                {
                    if (isValidPunc)
                    {
                        wasWhitespaceAfterLetter = false;
                        wasLetter = false;
                    }
                    offset++;
                }
                else
                {
                    offset--;
                    while (offset > 1)
                    {
                        ch = editor.GetCharAt(offset - 1);
                        if (!(ch == '.' || char.IsWhiteSpace(ch)))
                        {
                            break;
                        }
                        offset--;
                    }
                    break;
                }

                wasLetter |= isLetter;
                if (wasLetter)
                {
                    wasWhitespaceAfterLetter |= isWhiteSpace;
                }
            }

            var unit = SyntaxTree.Parse(CreateStub(doc, offset), doc.FileName);

            return(ResolveAtLocation.Resolve(
                       doc.Compilation,
                       doc.ParsedDocument.ParsedFile as CSharpUnresolvedFile,
                       unit,
                       location,
                       out node));
        }
Esempio n. 8
0
        public ResolveResult Resolve(ParseInformation parseInfo, TextLocation location, ICompilation compilation, CancellationToken cancellationToken)
        {
            var decompiledParseInfo = parseInfo as ILSpyFullParseInformation;

            if (decompiledParseInfo == null)
            {
                throw new ArgumentException("ParseInfo does not have SyntaxTree");
            }
            return(ResolveAtLocation.Resolve(compilation, null, decompiledParseInfo.SyntaxTree, location, cancellationToken));
        }
Esempio n. 9
0
        public TypeLookupResponse GetTypeLookupResponse(TypeLookupRequest request)
        {
            var res           = _bufferParser.ParsedContent(request.Buffer, request.FileName);
            var loc           = new TextLocation(request.Line, request.Column);
            var resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc);
            var response      = new TypeLookupResponse();
            var ambience      = new CSharpAmbience()
            {
                ConversionFlags = AmbienceFlags,
            };


            if (resolveResult == null || resolveResult is NamespaceResolveResult)
            {
                response.Type = "";
            }
            else if (resolveResult != null)
            {
                response.Type = resolveResult.Type.ToString();

                if (resolveResult is CSharpInvocationResolveResult)
                {
                    var result = resolveResult as CSharpInvocationResolveResult;
                    response.Type = ambience.ConvertEntity(result.Member);
                }
                else if (resolveResult is LocalResolveResult)
                {
                    var result = resolveResult as LocalResolveResult;
                    response.Type = ambience.ConvertVariable(result.Variable);
                }
                else if (resolveResult is MemberResolveResult)
                {
                    var result = resolveResult as MemberResolveResult;
                    response.Type = ambience.ConvertEntity(result.Member);
                }
                else if (resolveResult is TypeResolveResult)
                {
                    ambience.ConversionFlags |= ConversionFlags.UseFullyQualifiedTypeNames;
                    response.Type             = ambience.ConvertType(resolveResult.Type);
                }

                if (resolveResult.Type is UnknownType)
                {
                    response.Type = "Unknown Type: " + resolveResult.Type.Name;
                }
                if (resolveResult.Type == SpecialType.UnknownType)
                {
                    response.Type = "Unknown Type";
                }
            }

            return(response);
        }
Esempio n. 10
0
		public ResolveResult Resolve(ParseInformation parseInfo, TextLocation location, ICompilation compilation, CancellationToken cancellationToken)
		{
			var csParseInfo = parseInfo as AlFullParseInformation;
			if (csParseInfo == null)
				throw new ArgumentException("Parse info does not have SyntaxTree");
			
			AlUnresolvedFile unresolvedFile = csParseInfo.UnresolvedFile;
			var projectContents = compilation.Assemblies.Select(asm => asm.UnresolvedAssembly).OfType<IProjectContent>().ToList();
			if (projectContents.All(pc => pc.GetFile(unresolvedFile.FileName) != unresolvedFile))
				unresolvedFile = null;
			return ResolveAtLocation.Resolve(compilation, unresolvedFile, csParseInfo.SyntaxTree, location, cancellationToken);
		}
Esempio n. 11
0
        public GotoDefinitionResponse GetGotoDefinitionResponse(GotoDefinitionRequest request)
        {
            var res = _bufferParser.ParsedContent(request.Buffer, request.FileName);

            var loc = new TextLocation(request.Line, request.Column);

            ResolveResult resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc);
            var           response      = new GotoDefinitionResponse();

            if (resolveResult != null)
            {
                var region = resolveResult.GetDefinitionRegion();
                response.FileName = region.FileName == null ? null : region.FileName.ApplyPathReplacementsForClient();
                response.Line     = region.BeginLine;
                response.Column   = region.BeginColumn;
            }

            return(response);
        }
Esempio n. 12
0
        ResolveResult ResolveFromPosition(string editorText, int offset, string fileName)
        {
            if (Project == null)
            {
                return(null);
            }

            var location = new ReadOnlyDocument(editorText).GetLocation(offset);

            var syntaxTree = new CSharpParser().Parse(editorText, fileName);

            syntaxTree.Freeze();
            var unresolvedFile = syntaxTree.ToTypeSystem();

            Project = Project.AddOrUpdateFiles(unresolvedFile);

            var compilation = Project.CreateCompilation();

            return(ResolveAtLocation.Resolve(compilation, unresolvedFile, syntaxTree, location));
        }
Esempio n. 13
0
        static bool TryResolveAt(Document doc, DocumentLocation loc, out ResolveResult result, out AstNode node)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }

            result = null;
            node   = null;

            var parsedDocument = doc.ParsedDocument;

            if (parsedDocument == null)
            {
                return(false);
            }

            var unit       = parsedDocument.GetAst <SyntaxTree> ();
            var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile;

            if (unit == null || parsedFile == null)
            {
                return(false);
            }

            try {
                result = ResolveAtLocation.Resolve(new Lazy <ICompilation> (() => doc.Compilation), parsedFile, unit, loc, out node);
                if (result == null || node is Statement)
                {
                    return(false);
                }
            } catch {
                return(false);
            }

            return(true);
        }
Esempio n. 14
0
        public TypeLookupResponse GetTypeLookupResponse(TypeLookupRequest request)
        {
            var res           = _bufferParser.ParsedContent(request.Buffer, request.FileName);
            var loc           = new TextLocation(request.Line, request.Column);
            var resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc);
            var response      = new TypeLookupResponse();
            var ambience      = new CSharpAmbience()
            {
                ConversionFlags = AmbienceFlags,
            };


            if (resolveResult == null || resolveResult is NamespaceResolveResult)
            {
                response.Type = "";
            }
            else
            {
                response.Type = resolveResult.Type.ToString();
                IEntity entity = null;
                if (resolveResult is CSharpInvocationResolveResult)
                {
                    var result = resolveResult as CSharpInvocationResolveResult;
                    entity        = result.Member;
                    response.Type = ambience.ConvertSymbol(result.Member);
                }
                else if (resolveResult is LocalResolveResult)
                {
                    var result = resolveResult as LocalResolveResult;
                    response.Type = ambience.ConvertVariable(result.Variable);
                }
                else if (resolveResult is MemberResolveResult)
                {
                    var result = resolveResult as MemberResolveResult;
                    entity        = result.Member;
                    response.Type = ambience.ConvertSymbol(result.Member);
                }
                else if (resolveResult is TypeResolveResult)
                {
                    ambience.ConversionFlags |= ConversionFlags.UseFullyQualifiedTypeNames;
                    response.Type             = ambience.ConvertType(resolveResult.Type);
                }

                if (resolveResult.Type is UnknownType)
                {
                    response.Type = "Unknown Type: " + resolveResult.Type.Name;
                }
                if (resolveResult.Type == SpecialType.UnknownType)
                {
                    response.Type = "Unknown Type";
                }

                if (request.IncludeDocumentation && entity != null)
                {
                    var project = _solution.ProjectContainingFile(request.FileName);
                    response.Documentation = new DocumentationFetcher().GetDocumentation(project, entity, _config);
                }
            }

            return(response);
        }
Esempio n. 15
0
        public IEnumerable <AstNode> FindUsageNodes(Request request)
        {
            var res = _parser.ParsedContent(request.Buffer, request.FileName);
            var loc = new TextLocation(request.Line, request.Column);

            _result = new ConcurrentBag <AstNode>();
            var findReferences = new FindReferences
            {
                FindCallsThroughInterface         = true,
                FindCallsThroughVirtualBaseMethod = true,
                FindTypeReferencesEvenIfAliased   = true,
            };

            ResolveResult resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc);

            if (resolveResult is LocalResolveResult)
            {
                var variable = (resolveResult as LocalResolveResult).Variable;
                findReferences.FindLocalReferences(variable, res.UnresolvedFile, res.SyntaxTree, res.Compilation,
                                                   (node, rr) => _result.Add(node.GetDefinition()), CancellationToken.None);
            }
            else
            {
                IEntity entity = null;
                IEnumerable <IList <IFindReferenceSearchScope> > searchScopes = null;
                if (resolveResult is TypeResolveResult)
                {
                    var type = (resolveResult as TypeResolveResult).Type;
                    entity = type.GetDefinition();
                    ProcessTypeResults(type);
                    searchScopes = new[] { findReferences.GetSearchScopes(entity) };
                }

                if (resolveResult is MemberResolveResult)
                {
                    entity = (resolveResult as MemberResolveResult).Member;
                    if (entity.EntityType == EntityType.Constructor)
                    {
                        // process type instead
                        var type = entity.DeclaringType;
                        entity = entity.DeclaringTypeDefinition;
                        ProcessTypeResults(type);
                        searchScopes = new[] { findReferences.GetSearchScopes(entity) };
                    }
                    else
                    {
                        ProcessMemberResults(resolveResult);
                        var members = MemberCollector.CollectMembers(_solution,
                                                                     (resolveResult as MemberResolveResult).Member);
                        searchScopes = members.Select(findReferences.GetSearchScopes);
                    }
                }

                if (entity == null)
                {
                    return(_result);
                }

                var interesting = new List <CSharpUnresolvedFile>();

                foreach (var project in _solution.Projects)
                {
                    var pctx = project.ProjectContent.CreateCompilation();
                    interesting = (from file in project.Files
                                   select(file.ParsedFile as CSharpUnresolvedFile)).ToList();

                    foreach (var file in interesting)
                    {
                        string text = _solution.GetFile(file.FileName).Content.Text;
                        var    unit = new CSharpParser().Parse(text, file.FileName);
                        foreach (var scope in searchScopes)
                        {
                            findReferences.FindReferencesInFile(scope, file, unit,
                                                                pctx,
                                                                (node, rr) => _result.Add(node.GetIdentifier()),
                                                                CancellationToken.None);
                        }
                    }
                }
            }
            return(_result);
        }