コード例 #1
0
 public async Task <ImmutableArray <RazorMappedSpanResult> > MapSpansAsync(
     Document document,
     IEnumerable <TextSpan> spans,
     CancellationToken cancellationToken)
 {
     return(await MapSpansAsync(spans, _textSnapshot.AsText(), _documentSnapshot.Snapshot.AsText(), cancellationToken).ConfigureAwait(false));
 }
コード例 #2
0
            static SyntaxTree CreateSyntaxTreeFromSnapshot(ITextSnapshot snapshot, CancellationToken cancellationToken)
            {
                var sourceText = snapshot.AsText();
                var syntaxTree = CSharpSyntaxTree.ParseText(sourceText, cancellationToken: cancellationToken);

                return(syntaxTree);
            }
コード例 #3
0
            static async Task <NavigationBarModel?> ComputeModelAsync(ITextSnapshot textSnapshot, CancellationToken cancellationToken)
            {
                // When computing items just get the partial semantics workspace.  This will ensure we can get data for this
                // file, and hopefully have enough loaded to get data for other files in the case of partial types.  In the
                // event the other files aren't available, then partial-type information won't be correct.  That's ok though
                // as this is just something that happens during solution load and will pass once that is over.  By using
                // partial semantics, we can ensure we don't spend an inordinate amount of time computing and using full
                // compilation data (like skeleton assemblies).
                var document = textSnapshot.AsText().GetDocumentWithFrozenPartialSemantics(cancellationToken);

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

                var itemService = document.GetLanguageService <INavigationBarItemService>();

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

                using (Logger.LogBlock(FunctionId.NavigationBar_ComputeModelAsync, cancellationToken))
                {
                    var items = await itemService.GetItemsAsync(document, textSnapshot.Version, cancellationToken).ConfigureAwait(false);

                    return(new NavigationBarModel(itemService, items));
                }
            }
コード例 #4
0
        /// <summary>
        /// Gets block comments by parsing the text for comment markers.
        /// </summary>
        protected override Task <ImmutableArray <TextSpan> > GetBlockCommentsInDocumentAsync(Document document, ITextSnapshot snapshot,
                                                                                             TextSpan linesContainingSelections, CommentSelectionInfo commentInfo, CancellationToken cancellationToken)
        {
            var allText        = snapshot.AsText();
            var commentedSpans = ArrayBuilder <TextSpan> .GetInstance();

            var openIdx = 0;

            while ((openIdx = allText.IndexOf(commentInfo.BlockCommentStartString, openIdx, caseSensitive: true)) >= 0)
            {
                // Retrieve the first closing marker located after the open index.
                var closeIdx = allText.IndexOf(commentInfo.BlockCommentEndString, openIdx + commentInfo.BlockCommentStartString.Length, caseSensitive: true);
                // If an open marker is found without a close marker, it's an unclosed comment.
                if (closeIdx < 0)
                {
                    closeIdx = allText.Length - commentInfo.BlockCommentEndString.Length;
                }

                var blockCommentSpan = new TextSpan(openIdx, closeIdx + commentInfo.BlockCommentEndString.Length - openIdx);
                commentedSpans.Add(blockCommentSpan);
                openIdx = closeIdx;
            }

            return(Task.FromResult(commentedSpans.ToImmutableAndFree()));
        }
コード例 #5
0
        private async Task<Document> DetermineNewDocumentAsync(MemberInsertionCompletionItem completionItem, ITextSnapshot textSnapshot, CancellationToken cancellationToken)
        {
            // The span we're going to replace
            var line = textSnapshot.GetLineFromLineNumber(completionItem.Line);

            var sourceText = textSnapshot.AsText();
            var document = sourceText.GetOpenDocumentInCurrentContextWithChanges();
            Contract.ThrowIfNull(document);

            // Annotate the line we care about so we can find it after adding usings
            var tree = document.GetSyntaxTreeAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var token = GetToken(completionItem, tree, cancellationToken);
            var annotatedRoot = tree.GetRoot(cancellationToken).ReplaceToken(token, token.WithAdditionalAnnotations(_otherAnnotation));
            document = document.WithSyntaxRoot(annotatedRoot);

            var memberContainingDocument = await GenerateMemberAndUsingsAsync(document, completionItem, line, cancellationToken).ConfigureAwait(false);

            var insertionRoot = PrepareTreeForMemberInsertion(memberContainingDocument, cancellationToken);
            var insertionText = GenerateInsertionText(memberContainingDocument, cancellationToken);

            var destinationSpan = ComputeDestinationSpan(insertionRoot, insertionText);

            var finalText = insertionRoot.GetText(sourceText.Encoding).Replace(destinationSpan, insertionText.Trim());

            document = document.WithText(finalText);
            var newRoot = document.GetSyntaxRootAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var declaration = GetSyntax(newRoot.FindToken(destinationSpan.End));

            document = document.WithSyntaxRoot(newRoot.ReplaceNode(declaration, declaration.WithAdditionalAnnotations(_annotation)));
            return Formatter.FormatAsync(document, _annotation, cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);
        }
コード例 #6
0
        private async Task <Document> DetermineNewDocumentAsync(MemberInsertionCompletionItem completionItem, ITextSnapshot textSnapshot, CancellationToken cancellationToken)
        {
            // The span we're going to replace
            var line = textSnapshot.GetLineFromLineNumber(completionItem.Line);

            var sourceText = textSnapshot.AsText();
            var document   = sourceText.GetOpenDocumentInCurrentContextWithChanges();

            Contract.ThrowIfNull(document);

            // Annotate the line we care about so we can find it after adding usings
            var tree          = document.GetSyntaxTreeAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var token         = GetToken(completionItem, tree, cancellationToken);
            var annotatedRoot = tree.GetRoot(cancellationToken).ReplaceToken(token, token.WithAdditionalAnnotations(_otherAnnotation));

            document = document.WithSyntaxRoot(annotatedRoot);

            var memberContainingDocument = await GenerateMemberAndUsingsAsync(document, completionItem, line, cancellationToken).ConfigureAwait(false);

            var insertionRoot = PrepareTreeForMemberInsertion(memberContainingDocument, cancellationToken);
            var insertionText = GenerateInsertionText(memberContainingDocument, cancellationToken);

            var destinationSpan = ComputeDestinationSpan(insertionRoot, insertionText);

            var finalText = insertionRoot.GetText(sourceText.Encoding).Replace(destinationSpan, insertionText.Trim());

            document = document.WithText(finalText);
            var newRoot     = document.GetSyntaxRootAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var declaration = GetSyntax(newRoot.FindToken(destinationSpan.End));

            document = document.WithSyntaxRoot(newRoot.ReplaceNode(declaration, declaration.WithAdditionalAnnotations(_annotation)));
            return(Formatter.FormatAsync(document, _annotation, cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken));
        }
コード例 #7
0
        protected AbstractPasteProcessor(
            string newLine,
            string indentationWhitespace,
            ITextSnapshot snapshotBeforePaste,
            ITextSnapshot snapshotAfterPaste,
            Document documentBeforePaste,
            Document documentAfterPaste,
            ExpressionSyntax stringExpressionBeforePaste)
        {
            NewLine = newLine;
            IndentationWhitespace = indentationWhitespace;

            SnapshotBeforePaste = snapshotBeforePaste;
            SnapshotAfterPaste  = snapshotAfterPaste;

            TextBeforePaste = SnapshotBeforePaste.AsText();
            TextAfterPaste  = SnapshotAfterPaste.AsText();

            DocumentBeforePaste = documentBeforePaste;
            DocumentAfterPaste  = documentAfterPaste;

            StringExpressionBeforePaste     = stringExpressionBeforePaste;
            StringExpressionBeforePasteInfo = StringInfo.GetStringInfo(TextBeforePaste, stringExpressionBeforePaste);
            TextContentsSpansAfterPaste     = StringExpressionBeforePasteInfo.ContentSpans.SelectAsArray(MapSpanForward);

            Contract.ThrowIfTrue(StringExpressionBeforePasteInfo.ContentSpans.IsEmpty);
        }
コード例 #8
0
        public static string Compile(string code, ITextSnapshot snapshot)
        {
            var roslynSourceText = snapshot.AsText();

            return($@"
#region generated code
// yaha code:
//{code.Replace("\n", "\n//")}
#endregion
			"            );
        }
コード例 #9
0
ファイル: CompletionInfo.cs プロジェクト: manojdjoshi/dnSpy
		public static CompletionInfo? Create(ITextSnapshot snapshot) {
			if (snapshot == null)
				throw new ArgumentNullException(nameof(snapshot));
			var sourceText = snapshot.AsText();
			var document = sourceText.GetOpenDocumentInCurrentContextWithChanges();
			if (document == null)
				return null;
			var completionService = CompletionService.GetService(document);
			if (completionService == null)
				return null;
			return new CompletionInfo(completionService, document, sourceText, snapshot);
		}
コード例 #10
0
        public bool BeginUndoTransaction(ITextSnapshot snapshot)
        {
            var sourceText = snapshot?.AsText();

            if (sourceText != null)
            {
                _transactions.TryGetValue(sourceText, out var transaction);
                if (transaction != null)
                {
                    return(transaction.Begin(_undoHistoryRegistry?.GetHistory(snapshot.TextBuffer)));
                }
            }

            return(false);
        }
コード例 #11
0
        public static SignatureHelpInfo?Create(ITextSnapshot snapshot)
        {
            if (snapshot is null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }
            var sourceText = snapshot.AsText();
            var document   = sourceText.GetOpenDocumentInCurrentContextWithChanges();

            if (document is null)
            {
                return(null);
            }
            var signatureHelpService = SignatureHelpService.GetService(document);

            if (signatureHelpService is null)
            {
                return(null);
            }
            return(new SignatureHelpInfo(signatureHelpService, document, sourceText, snapshot));
        }
コード例 #12
0
        public static CompletionInfo?Create(ITextSnapshot snapshot)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }
            var sourceText = snapshot.AsText();
            var document   = sourceText.GetOpenDocumentInCurrentContextWithChanges();

            if (document == null)
            {
                return(null);
            }
            var completionService = CompletionService.GetService(document);

            if (completionService == null)
            {
                return(null);
            }
            return(new CompletionInfo(completionService, document, sourceText, snapshot));
        }
コード例 #13
0
ファイル: QuickInfoState.cs プロジェクト: haise0/dnSurgeon
        public static QuickInfoState?Create(ITextSnapshot snapshot)
        {
            if (snapshot is null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }
            var sourceText = snapshot.AsText();
            var document   = sourceText.GetOpenDocumentInCurrentContextWithChanges();

            if (document is null)
            {
                return(null);
            }
            var quickInfoService = QuickInfoService.GetService(document);

            if (quickInfoService is null)
            {
                return(null);
            }
            return(new QuickInfoState(quickInfoService, document, sourceText, snapshot));
        }
コード例 #14
0
 /// <summary>
 /// Gets the <see cref="Document"/>s from the corresponding <see cref="Workspace.CurrentSolution"/> that are associated with the <see cref="ITextSnapshot"/>'s buffer,
 /// updated to contain the same text as the snapshot if necessary. There may be multiple <see cref="Document"/>s associated with the buffer
 /// if the file is linked into multiple projects or is part of a Shared Project.
 /// </summary>
 public static IEnumerable <Document> GetRelatedDocumentsWithChanges(
     this ITextSnapshot text
     ) => text.AsText().GetRelatedDocumentsWithChanges();
コード例 #15
0
 public Task <Document> GetDocumentAsync(ITextSnapshot snapshot, CancellationToken cancellationToken)
 {
     AssertIsBackground();
     return(Task.FromResult(snapshot.AsText().GetDocumentWithFrozenPartialSemantics(cancellationToken)));
 }
コード例 #16
0
ファイル: IDocumentProvider.cs プロジェクト: yonifra/roslyn
        public Document GetOpenDocumentInCurrentContextWithChanges(ITextSnapshot snapshot)
        {
            var text = snapshot.AsText();

            return(text.GetOpenDocumentInCurrentContextWithChanges());
        }
コード例 #17
0
ファイル: Extensions.cs プロジェクト: fjsnogueira/roslyn-1
 /// <summary>
 /// Gets the <see cref="Document"/> from the corresponding <see cref="Workspace.CurrentSolution"/> that is associated with the <see cref="ITextSnapshot"/>'s buffer
 /// in its current project context, updated to contain the same text as the snapshot if necessary. There may be multiple <see cref="Document"/>s
 /// associated with the buffer if it is linked into multiple projects or is part of a Shared Project. In this case, the <see cref="Workspace"/>
 /// is responsible for keeping track of which of these <see cref="Document"/>s is in the current project context.
 /// </summary>
 public static Document GetOpenDocumentInCurrentContextWithChanges(this ITextSnapshot text)
 {
     return(text.AsText().GetOpenDocumentInCurrentContextWithChanges());
 }
コード例 #18
0
 /// <summary>
 /// get Document corresponding to the snapshot
 /// </summary>
 public static bool TryGetDocument(this ITextSnapshot snapshot, out Document document)
 {
     document = snapshot.AsText().GetRelatedDocumentsWithChanges().FirstOrDefault();
     return(document != null);
 }
コード例 #19
0
 public Document GetDocument(ITextSnapshot snapshot, CancellationToken cancellationToken)
 {
     AssertIsBackground();
     return(snapshot.AsText().GetDocumentWithFrozenPartialSemantics(cancellationToken));
 }
コード例 #20
0
 /// <summary>
 /// Gets the <see cref="Document"/> from the corresponding <see cref="Workspace.CurrentSolution"/> that is associated with the <see cref="ITextSnapshot"/>'s buffer
 /// in its current project context, updated to contain the same text as the snapshot if necessary. There may be multiple <see cref="Document"/>s
 /// associated with the buffer if it is linked into multiple projects or is part of a Shared Project. In this case, the <see cref="Workspace"/>
 /// is responsible for keeping track of which of these <see cref="Document"/>s is in the current project context.
 /// </summary>
 public static Document?GetOpenDocumentInCurrentContextWithChanges(
     this ITextSnapshot text
     ) => text.AsText().GetOpenDocumentInCurrentContextWithChanges();
コード例 #21
0
ファイル: IDocumentProvider.cs プロジェクト: Rickinio/roslyn
 public Document GetOpenDocumentInCurrentContextWithChanges(ITextSnapshot snapshot)
 {
     var text = snapshot.AsText();
     return text.GetOpenDocumentInCurrentContextWithChanges();
 }
コード例 #22
0
ファイル: IDocumentProvider.cs プロジェクト: Rickinio/roslyn
 public Task<Document> GetDocumentAsync(ITextSnapshot snapshot, CancellationToken cancellationToken)
 {
     AssertIsBackground();
     return snapshot.AsText().GetDocumentWithFrozenPartialSemanticsAsync(cancellationToken);
 }
コード例 #23
0
 public Document GetDocument(ITextSnapshot snapshot, CancellationToken cancellationToken)
 {
     _threadingContext.ThrowIfNotOnBackgroundThread();
     return(snapshot.AsText().GetDocumentWithFrozenPartialSemantics(cancellationToken));
 }
コード例 #24
0
ファイル: SourceTextHelper.cs プロジェクト: 331564533/dnSpy
 SourceText ISourceTextHelper.AsText(ITextSnapshot textSnapshot) => textSnapshot.AsText();