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));
                }
            }
        /// <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()));
        }
        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));
        }
        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);
        }
        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
		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
        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
        public Document GetOpenDocumentInCurrentContextWithChanges(ITextSnapshot snapshot)
        {
            var text = snapshot.AsText();

            return(text.GetOpenDocumentInCurrentContextWithChanges());
        }
示例#17
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)
 {
     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
 public Document GetOpenDocumentInCurrentContextWithChanges(ITextSnapshot snapshot)
 {
     var text = snapshot.AsText();
     return text.GetOpenDocumentInCurrentContextWithChanges();
 }
示例#22
0
 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
 SourceText ISourceTextHelper.AsText(ITextSnapshot textSnapshot) => textSnapshot.AsText();