Ejemplo n.º 1
0
		/// <summary>
		/// Creates a new <see cref="FixedHighlighter"/> for a copy of a portion
		/// of the input document (including the original highlighting).
		/// </summary>
		public static FixedHighlighter CreateView(IHighlighter highlighter, int offset, int endOffset)
		{
			var oldDocument = highlighter.Document;
			// ReadOnlyDocument would be better; but displaying the view in AvalonEdit
			// requires a TextDocument
			var newDocument = new TextDocument(oldDocument.CreateSnapshot(offset, endOffset - offset));
			
			var oldStartLine = oldDocument.GetLineByOffset(offset);
			var oldEndLine = oldDocument.GetLineByOffset(endOffset);
			int oldStartLineNumber = oldStartLine.LineNumber;
			HighlightedLine[] newLines = new HighlightedLine[oldEndLine.LineNumber - oldStartLineNumber + 1];
			highlighter.BeginHighlighting();
			try {
				for (int i = 0; i < newLines.Length; i++) {
					HighlightedLine oldHighlightedLine = highlighter.HighlightLine(oldStartLineNumber + i);
					IDocumentLine newLine = newDocument.GetLineByNumber(1 + i);
					HighlightedLine newHighlightedLine = new HighlightedLine(newDocument, newLine);
					MoveSections(oldHighlightedLine.Sections, -offset, newLine.Offset, newLine.EndOffset, newHighlightedLine.Sections);
					newHighlightedLine.ValidateInvariants();
					newLines[i] = newHighlightedLine;
				}
			} finally {
				highlighter.EndHighlighting();
			}
			return new FixedHighlighter(newDocument, newLines);
		}
Ejemplo n.º 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();
            }
        }
        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);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new <see cref="FixedHighlighter"/> for a copy of a portion
        /// of the input document (including the original highlighting).
        /// </summary>
        public static FixedHighlighter CreateView(IHighlighter highlighter, int offset, int endOffset)
        {
            var oldDocument = highlighter.Document;
            // ReadOnlyDocument would be better; but displaying the view in AvalonEdit
            // requires a TextDocument
            var newDocument = new TextDocument(oldDocument.CreateSnapshot(offset, endOffset - offset));

            var oldStartLine       = oldDocument.GetLineByOffset(offset);
            var oldEndLine         = oldDocument.GetLineByOffset(endOffset);
            int oldStartLineNumber = oldStartLine.LineNumber;

            HighlightedLine[] newLines = new HighlightedLine[oldEndLine.LineNumber - oldStartLineNumber + 1];
            highlighter.BeginHighlighting();
            try {
                for (int i = 0; i < newLines.Length; i++)
                {
                    HighlightedLine oldHighlightedLine = highlighter.HighlightLine(oldStartLineNumber + i);
                    IDocumentLine   newLine            = newDocument.GetLineByNumber(1 + i);
                    HighlightedLine newHighlightedLine = new HighlightedLine(newDocument, newLine);
                    MoveSections(oldHighlightedLine.Sections, -offset, newLine.Offset, newLine.EndOffset, newHighlightedLine.Sections);
                    newHighlightedLine.ValidateInvariants();
                    newLines[i] = newHighlightedLine;
                }
            } finally {
                highlighter.EndHighlighting();
            }
            return(new FixedHighlighter(newDocument, newLines));
        }
Ejemplo n.º 5
0
        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));
            }
        }
Ejemplo n.º 6
0
            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 textView_VisualLineConstructionStarting(object sender, VisualLineConstructionStartEventArgs e)
 {
     if (highlighter != null)
     {
         // Force update of highlighting state up to the position where we start generating visual lines.
         // This is necessary in case the document gets modified above the FirstLineInView so that the highlighting state changes.
         // We need to detect this case and issue a redraw (through OnHighlightStateChanged)
         // before the visual line construction reuses existing lines that were built using the invalid highlighting state.
         lineNumberBeingColorized = e.FirstLineInView.LineNumber - 1;
         if (!isInHighlightingGroup)
         {
             // avoid opening group twice if there was an exception during the previous visual line construction
             // (not ideal, but better than throwing InvalidOperationException "group already open"
             // without any way of recovering)
             highlighter.BeginHighlighting();
             isInHighlightingGroup = true;
         }
         highlighter.UpdateHighlightingState(lineNumberBeingColorized);
         lineNumberBeingColorized = 0;
     }
 }
Ejemplo n.º 8
0
        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));
            }
        }
Ejemplo n.º 9
0
        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));
            }
        }
Ejemplo n.º 10
0
        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 BeginHighlighting()
 {
     baseHighlighter.BeginHighlighting();
 }