Example #1
0
        public static void ShowAsSearchResults(string title, List <Reference> list)
        {
            if (list == null)
            {
                return;
            }
            List <SearchResultMatch> results  = new List <SearchResultMatch>(list.Count);
            TextDocument             document = null;
            ITextBuffer        buffer         = null;
            FileName           fileName       = null;
            ISyntaxHighlighter highlighter    = null;

            foreach (Reference r in list)
            {
                var f = new FileName(r.FileName);
                if (document == null || !f.Equals(fileName))
                {
                    buffer      = ParserService.GetParseableFileContent(r.FileName);
                    document    = new TextDocument(DocumentUtilitites.GetTextSource(buffer));
                    fileName    = new FileName(r.FileName);
                    highlighter = EditorControlService.Instance.CreateHighlighter(new AvalonEditDocumentAdapter(document, null), fileName);
                }
                var start             = document.GetLocation(r.Offset).ToLocation();
                var end               = document.GetLocation(r.Offset + r.Length).ToLocation();
                var builder           = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter);
                SearchResultMatch res = new SearchResultMatch(fileName, start, end, r.Offset, r.Length, builder, highlighter.DefaultTextColor);
                results.Add(res);
            }
            SearchResultsPad.Instance.ShowSearchResults(title, results);
            SearchResultsPad.Instance.BringToFront();
        }
Example #2
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 #3
0
            SearchedFile SearchFile(FileName fileName)
            {
                ITextBuffer buffer = fileFinder.Create(fileName);

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

                ThrowIfCancellationRequested();

                var                 source      = DocumentUtilitites.GetTextSource(buffer);
                TextDocument        document    = null;
                DocumentHighlighter 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 TextDocument(source);
                        var highlighting = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName));
                        if (highlighting != null)
                        {
                            highlighter = new DocumentHighlighter(document, highlighting.MainRuleSet);
                        }
                        else
                        {
                            highlighter = null;
                        }
                    }
                    var start   = document.GetLocation(result.Offset).ToLocation();
                    var end     = document.GetLocation(result.Offset + result.Length).ToLocation();
                    var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter);
                    results.Add(new AvalonEditSearchResultMatch(fileName, start, end, result.Offset, result.Length, builder, result));
                }
                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);
                }
            }
        public static void ShowAsSearchResults(string title, List <Reference> list)
        {
            if (list == null)
            {
                return;
            }
            List <SearchResultMatch> results  = new List <SearchResultMatch>(list.Count);
            TextDocument             document = null;
            FileName     fileName             = null;
            IHighlighter highlighter          = null;

            foreach (Reference r in list)
            {
                var f = new FileName(r.FileName);
                if (document == null || !f.Equals(fileName))
                {
                    document = new TextDocument(DocumentUtilitites.GetTextSource(ParserService.GetParseableFileContent(r.FileName)));
                    fileName = new FileName(r.FileName);
                    var def = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(r.FileName));
                    if (def != null)
                    {
                        highlighter = new DocumentHighlighter(document, def.MainRuleSet);
                    }
                    else
                    {
                        highlighter = null;
                    }
                }
                var start             = document.GetLocation(r.Offset).ToLocation();
                var end               = document.GetLocation(r.Offset + r.Length).ToLocation();
                var builder           = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter);
                SearchResultMatch res = new SearchResultMatch(fileName, start, end, r.Offset, r.Length, builder);
                results.Add(res);
            }
            SearchResultsPad.Instance.ShowSearchResults(title, results);
            SearchResultsPad.Instance.BringToFront();
        }
        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));
            }
        }