public void Flush()
 {
     foreach (var i in _cache.Values)
     {
         var sourceInfoFileName = i.FileName;
         var c = File.Exists(sourceInfoFileName)
             ? File.ReadAllText(sourceInfoFileName)
             : "";
         var allContent = EndCodeEmbedder.Append(c, i.File.GetCode(true),
                                                 "autogenerated code");
         CodeFileUtils.SaveIfDifferent(allContent, sourceInfoFileName, false);
     }
 }
Beispiel #2
0
        private async Task <List <QuickFix> > QueryCodeSearchForSymbolRefsAsync(FindUsagesRequest request, ISymbol symbol,
                                                                                SourceText sourceText, int positionInSourceText)
        {
            // OnlyThisFile is supplied for CodeLense requests which won't benefit from code search service data.
            // If symbol is available then avoid querying code search service data for private symbols because all their references will be already loaded in Roslyn's workspace.
            if (request.OnlyThisFile || (symbol != null && symbol.DeclaredAccessibility == Accessibility.Private))
            {
                return(new List <QuickFix>());
            }

            // Try to get symbol text from Roslyn's symbol object - this is the most presize option
            string symbolText;

            if (symbol != null && symbol.Locations != null && symbol.Locations.Any())
            {
                Location location = symbol.Locations.First();
                symbolText = (await location.SourceTree.GetTextAsync()).ToString(location.SourceSpan);
            }
            // Try to get symbol text from Roslyn's in-memory text which might be different from what is stored on disk
            else if (sourceText != null)
            {
                int symboldStartPosition = positionInSourceText;
                do
                {
                    symboldStartPosition--;
                } while (symboldStartPosition > 0 && char.IsLetter(sourceText[symboldStartPosition]));
                symboldStartPosition++;

                int symbolEndPosition = positionInSourceText;
                while (symbolEndPosition < sourceText.Length && char.IsLetterOrDigit(sourceText[symbolEndPosition]))
                {
                    symbolEndPosition++;
                }

                symbolText = sourceText.ToString(new TextSpan(symboldStartPosition, symbolEndPosition - symboldStartPosition));
            }
            else
            {
                CodeFileUtils.TryGetSymbolTextFromFile(request.FileName, request.Line, request.Column, out symbolText);
            }

            if (string.IsNullOrWhiteSpace(symbolText) || !char.IsLetter(symbolText.ElementAt(0)))
            {
                return(new List <QuickFix>());
            }

            return(await _codeSearchServiceProvider.CodeSearchService.QueryAsync(
                       symbolText, Constants.MaxCodeSearchResults, Constants.QueryCodeSearchTimeout, exactMatch : true, CodeSearchQueryType.FindReferences));
        }
Beispiel #3
0
        private async Task <GotoDefinitionResponse> GetDefinitionFromCodeSearchAsync(GotoDefinitionRequest request)
        {
            var response = new GotoDefinitionResponse();

            if (!CodeFileUtils.TryGetSymbolTextFromFile(request.FileName, request.Line, request.Column, out string symbolText))
            {
                return(response);
            }

            List <QuickFix> hits = await _codeSearchServiceProvider.CodeSearchService.QueryAsync(
                symbolText, maxResults : 1, Constants.QueryCodeSearchTimeout, exactMatch : true, CodeSearchQueryType.FindDefinitions);

            if (hits.Count != 0)
            {
                response.FileName = hits[0].FileName;
                response.Column   = hits[0].Column;
                response.Line     = hits[0].Line;
            }
            return(response);
        }