Example #1
0
        public void FindLocalReferences(ParseInformation parseInfo, ITextSource fileContent, IVariable variable, ICompilation compilation, Action <SearchResultMatch> callback, CancellationToken cancellationToken)
        {
            var csParseInfo = parseInfo as CSharpFullParseInformation;

            if (csParseInfo == null)
            {
                throw new ArgumentException("Parse info does not have SyntaxTree");
            }

            ReadOnlyDocument document    = null;
            IHighlighter     highlighter = null;

            new FindReferences().FindLocalReferences(
                variable, csParseInfo.UnresolvedFile, csParseInfo.SyntaxTree, compilation,
                delegate(AstNode node, ResolveResult result) {
                if (document == null)
                {
                    document    = new ReadOnlyDocument(fileContent, parseInfo.FileName);
                    highlighter = SD.EditorControlService.CreateHighlighter(document);
                    highlighter.BeginHighlighting();
                }
                var region           = new DomRegion(parseInfo.FileName, node.StartLocation, node.EndLocation);
                int offset           = document.GetOffset(node.StartLocation);
                int length           = document.GetOffset(node.EndLocation) - offset;
                var builder          = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter);
                var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null;
                callback(new SearchResultMatch(parseInfo.FileName, node.StartLocation, node.EndLocation, offset, length, builder, defaultTextColor));
            }, cancellationToken);

            if (highlighter != null)
            {
                highlighter.Dispose();
            }
        }
Example #2
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         documentHighlighter.Dispose();
     }
 }
 /// <summary>
 /// This method is called when a text view is removed from this HighlightingColorizer,
 /// and also when the TextDocument on any associated text view changes.
 /// </summary>
 protected virtual void DeregisterServices(TextView textView)
 {
     if (highlighter != null)
     {
         if (isInHighlightingGroup)
         {
             highlighter.EndHighlighting();
             isInHighlightingGroup = false;
         }
         highlighter.HighlightingStateChanged -= OnHighlightStateChanged;
         // remove highlighter if it is registered
         if (textView.Services.GetService(typeof(IHighlighter)) == highlighter)
         {
             textView.Services.RemoveService(typeof(IHighlighter));
         }
         if (!isFixedHighlighter)
         {
             if (highlighter != null)
             {
                 highlighter.Dispose();
             }
             highlighter = null;
         }
     }
 }
        SearchedFile SearchForIssues(FileName fileName, ITextSource fileContent, IEnumerable <IssueManager.IssueProvider> providers, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var context = SDRefactoringContext.Create(fileName, fileContent, TextLocation.Empty, cancellationToken);
            ReadOnlyDocument document    = null;
            IHighlighter     highlighter = null;
            var results = new List <SearchResultMatch>();

            foreach (var provider in providers)
            {
                cancellationToken.ThrowIfCancellationRequested();
                foreach (var issue in provider.GetIssues(context))
                {
                    if (document == null)
                    {
                        document    = new ReadOnlyDocument(fileContent, fileName);
                        highlighter = SD.EditorControlService.CreateHighlighter(document);
                        highlighter.BeginHighlighting();
                    }
                    results.Add(SearchResultMatch.Create(document, issue.Start, issue.End, highlighter));
                }
            }
            if (highlighter != null)
            {
                highlighter.Dispose();
            }
            if (results.Count > 0)
            {
                return(new SearchedFile(fileName, results));
            }
            else
            {
                return(null);
            }
        }
        void FindReferencesInFile(SymbolSearchArgs searchArguments, ISymbol entity, FileName fileName, Action <SearchedFile> callback, CancellationToken cancellationToken)
        {
            ITextSource textSource = searchArguments.ParseableFileContentFinder.Create(fileName);

            if (textSource == null)
            {
                return;
            }
            int offset = textSource.IndexOf(entity.Name, 0, textSource.TextLength, StringComparison.Ordinal);

            if (offset < 0)
            {
                return;
            }

            var parseInfo = SD.ParserService.Parse(fileName, textSource) as XamlFullParseInformation;

            if (parseInfo == null)
            {
                return;
            }
            ReadOnlyDocument         document    = null;
            IHighlighter             highlighter = null;
            List <SearchResultMatch> results     = new List <SearchResultMatch>();
            XamlAstResolver          resolver    = new XamlAstResolver(compilation, parseInfo);

            do
            {
                if (document == null)
                {
                    document    = new ReadOnlyDocument(textSource, fileName);
                    highlighter = SD.EditorControlService.CreateHighlighter(document);
                    highlighter.BeginHighlighting();
                }
                var result = resolver.ResolveAtLocation(document.GetLocation(offset + entity.Name.Length / 2 + 1), cancellationToken);
                int length = entity.Name.Length;
                if ((result is TypeResolveResult && ((TypeResolveResult)result).Type.Equals(entity)) || (result is MemberResolveResult && ((MemberResolveResult)result).Member.Equals(entity)))
                {
                    var region  = new DomRegion(fileName, document.GetLocation(offset), document.GetLocation(offset + length));
                    var builder = SearchResultsPad.CreateInlineBuilder(region.Begin, region.End, document, highlighter);
                    results.Add(new SearchResultMatch(fileName, document.GetLocation(offset), document.GetLocation(offset + length), offset, length, builder, highlighter.DefaultTextColor));
                }
                offset = textSource.IndexOf(entity.Name, offset + length, textSource.TextLength - offset - length, StringComparison.OrdinalIgnoreCase);
            } while (offset > 0);
            if (highlighter != null)
            {
                highlighter.Dispose();
            }
            if (results.Count > 0)
            {
                callback(new SearchedFile(fileName, results));
            }
        }
            SearchedFile SearchFile(FileName fileName)
            {
                ITextSource source = fileFinder.Create(fileName);

                if (source == null)
                {
                    return(null);
                }

                ThrowIfCancellationRequested();
                ReadOnlyDocument document    = null;
                IHighlighter     highlighter = null;
                int offset = 0;
                int length = source.TextLength;

                if (Target == SearchTarget.CurrentSelection && Selection != null)
                {
                    offset = Selection.Offset;
                    length = Selection.Length;
                }
                List <SearchResultMatch> results = new List <SearchResultMatch>();

                foreach (var result in strategy.FindAll(source, offset, length))
                {
                    ThrowIfCancellationRequested();
                    if (document == null)
                    {
                        document    = new ReadOnlyDocument(source, fileName);
                        highlighter = SD.EditorControlService.CreateHighlighter(document);
                        highlighter.BeginHighlighting();
                    }
                    var start   = document.GetLocation(result.Offset);
                    var end     = document.GetLocation(result.Offset + result.Length);
                    var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter);
                    results.Add(new AvalonEditSearchResultMatch(fileName, start, end, result.Offset, result.Length, builder, highlighter.DefaultTextColor, result));
                }
                if (highlighter != null)
                {
                    highlighter.Dispose();
                }
                if (results.Count > 0)
                {
                    return(new SearchedFile(fileName, results));
                }
                else
                {
                    return(null);
                }
            }
        void RenameReferencesInFile(SymbolRenameArgs args, IFindReferenceSearchScope searchScope, FileName fileName, Action <PatchedFile> callback, Action <Error> errorCallback, bool isNameValid, CancellationToken cancellationToken)
        {
            ITextSource textSource = args.ParseableFileContentFinder.Create(fileName);

            if (textSource == null)
            {
                return;
            }
            if (searchScope.SearchTerm != null)
            {
                if (textSource.IndexOf(searchScope.SearchTerm, 0, textSource.TextLength, StringComparison.Ordinal) < 0)
                {
                    return;
                }
            }

            var parseInfo = SD.ParserService.Parse(fileName, textSource) as CSharpFullParseInformation;

            if (parseInfo == null)
            {
                return;
            }
            ReadOnlyDocument         document    = null;
            IHighlighter             highlighter = null;
            List <RenameResultMatch> results     = new List <RenameResultMatch>();

            // Grab the unresolved file matching the compilation version
            // (this may differ from the version created by re-parsing the project)
            CSharpUnresolvedFile unresolvedFile = null;
            IProjectContent      pc             = compilation.MainAssembly.UnresolvedAssembly as IProjectContent;

            if (pc != null)
            {
                unresolvedFile = pc.GetFile(fileName) as CSharpUnresolvedFile;
            }

            CSharpAstResolver resolver = new CSharpAstResolver(compilation, parseInfo.SyntaxTree, unresolvedFile);

            fr.RenameReferencesInFile(
                new[] { searchScope }, args.NewName, resolver,
                delegate(RenameCallbackArguments callbackArgs) {
                var node       = callbackArgs.NodeToReplace;
                string newCode = callbackArgs.NewNode.ToString();
                if (document == null)
                {
                    document = new ReadOnlyDocument(textSource, fileName);

                    if (args.ProvideHighlightedLine)
                    {
                        highlighter = SD.EditorControlService.CreateHighlighter(document);
                        highlighter.BeginHighlighting();
                    }
                }
                var startLocation = node.StartLocation;
                var endLocation   = node.EndLocation;
                int offset        = document.GetOffset(startLocation);
                int length        = document.GetOffset(endLocation) - offset;
                if (args.ProvideHighlightedLine)
                {
                    var builder          = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter);
                    var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null;
                    results.Add(new RenameResultMatch(fileName, startLocation, endLocation, offset, length, newCode, builder, defaultTextColor));
                }
                else
                {
                    results.Add(new RenameResultMatch(fileName, startLocation, endLocation, offset, length, newCode));
                }
            },
                errorCallback, cancellationToken);
            if (highlighter != null)
            {
                highlighter.Dispose();
            }
            if (results.Count > 0)
            {
                if (!isNameValid)
                {
                    errorCallback(new Error(ErrorType.Error, string.Format("The name '{0}' is not valid in the current context!", args.NewName),
                                            new DomRegion(fileName, results[0].StartLocation)));
                    return;
                }
                IDocument changedDocument = new TextDocument(document);
                var       oldVersion      = changedDocument.Version;
                foreach (var result in results.OrderByDescending(m => m.StartOffset))
                {
                    changedDocument.Replace(result.StartOffset, result.Length, result.NewCode);
                }
                callback(new PatchedFile(fileName, results, oldVersion, changedDocument.Version));
            }
        }
        void FindReferencesInFile(SymbolSearchArgs args, IFindReferenceSearchScope searchScope, FileName fileName, Action <SearchedFile> callback, CancellationToken cancellationToken)
        {
            ITextSource textSource = args.ParseableFileContentFinder.Create(fileName);

            if (textSource == null)
            {
                return;
            }
            if (searchScope.SearchTerm != null)
            {
                if (textSource.IndexOf(searchScope.SearchTerm, 0, textSource.TextLength, StringComparison.Ordinal) < 0)
                {
                    return;
                }
            }

            var parseInfo = SD.ParserService.Parse(fileName, textSource) as CSharpFullParseInformation;

            if (parseInfo == null)
            {
                return;
            }
            ReadOnlyDocument         document    = null;
            IHighlighter             highlighter = null;
            List <SearchResultMatch> results     = new List <SearchResultMatch>();

            // Grab the unresolved file matching the compilation version
            // (this may differ from the version created by re-parsing the project)
            CSharpUnresolvedFile unresolvedFile = null;
            IProjectContent      pc             = compilation.MainAssembly.UnresolvedAssembly as IProjectContent;

            if (pc != null)
            {
                unresolvedFile = pc.GetFile(fileName) as CSharpUnresolvedFile;
            }

            fr.FindReferencesInFile(
                searchScope, unresolvedFile, parseInfo.SyntaxTree, compilation,
                delegate(AstNode node, ResolveResult result) {
                if (document == null)
                {
                    document    = new ReadOnlyDocument(textSource, fileName);
                    highlighter = SD.EditorControlService.CreateHighlighter(document);
                    highlighter.BeginHighlighting();
                }
                Identifier identifier = node.GetChildByRole(Roles.Identifier);
                if (!identifier.IsNull)
                {
                    node = identifier;
                }
                var region           = new DomRegion(fileName, node.StartLocation, node.EndLocation);
                int offset           = document.GetOffset(node.StartLocation);
                int length           = document.GetOffset(node.EndLocation) - offset;
                var builder          = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter);
                var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null;
                results.Add(new SearchResultMatch(fileName, node.StartLocation, node.EndLocation, offset, length, builder, defaultTextColor));
            }, cancellationToken);
            if (highlighter != null)
            {
                highlighter.Dispose();
            }
            if (results.Count > 0)
            {
                callback(new SearchedFile(fileName, results));
            }
        }
        void RenameReferencesInFile(SymbolRenameArgs args, FileName fileName, Action <PatchedFile> callback, Action <Error> errorCallback, bool isNameValid, CancellationToken cancellationToken)
        {
            ITextSource textSource = args.ParseableFileContentFinder.Create(fileName);

            if (textSource == null)
            {
                return;
            }
            int offset = textSource.IndexOf(entity.Name, 0, textSource.TextLength, StringComparison.Ordinal);

            if (offset < 0)
            {
                return;
            }

            var parseInfo = SD.ParserService.Parse(fileName, textSource) as XamlFullParseInformation;

            if (parseInfo == null)
            {
                return;
            }
            ReadOnlyDocument         document    = null;
            IHighlighter             highlighter = null;
            List <RenameResultMatch> results     = new List <RenameResultMatch>();
            XamlAstResolver          resolver    = new XamlAstResolver(compilation, parseInfo);
            string newCode = args.NewName;

            do
            {
                if (document == null)
                {
                    document    = new ReadOnlyDocument(textSource, fileName);
                    highlighter = SD.EditorControlService.CreateHighlighter(document);
                    highlighter.BeginHighlighting();
                }
                var result = resolver.ResolveAtLocation(document.GetLocation(offset + entity.Name.Length / 2 + 1), cancellationToken);
                int length = entity.Name.Length;
                if ((result is TypeResolveResult && ((TypeResolveResult)result).Type.Equals(entity)) || (result is MemberResolveResult && ((MemberResolveResult)result).Member.Equals(entity)))
                {
                    var region  = new DomRegion(fileName, document.GetLocation(offset), document.GetLocation(offset + length));
                    var builder = SearchResultsPad.CreateInlineBuilder(region.Begin, region.End, document, highlighter);
                    results.Add(new RenameResultMatch(fileName, document.GetLocation(offset), document.GetLocation(offset + length), offset, length, newCode, builder, highlighter.DefaultTextColor));
                }
                offset = textSource.IndexOf(entity.Name, offset + length, textSource.TextLength - offset - length, StringComparison.OrdinalIgnoreCase);
            } while (offset > 0);
            if (highlighter != null)
            {
                highlighter.Dispose();
            }
            if (results.Count > 0)
            {
                if (!isNameValid)
                {
                    errorCallback(new Error(ErrorType.Error, string.Format("The name '{0}' is not valid in the current context!", args.NewName),
                                            new DomRegion(fileName, results[0].StartLocation)));
                    return;
                }
                IDocument changedDocument = new TextDocument(document);
                var       oldVersion      = changedDocument.Version;
                foreach (var result in results.OrderByDescending(m => m.StartOffset))
                {
                    changedDocument.Replace(result.StartOffset, result.Length, result.NewCode);
                }
                callback(new PatchedFile(fileName, results, oldVersion, changedDocument.Version));
            }
        }
 public void Dispose()
 {
     baseHighlighter.Dispose();
 }