当前位置: 首页>>代码示例>>C#>>正文


C# Document.GetTextAsync方法代码示例

本文整理汇总了C#中Document.GetTextAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Document.GetTextAsync方法的具体用法?C# Document.GetTextAsync怎么用?C# Document.GetTextAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Document的用法示例。


在下文中一共展示了Document.GetTextAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetDesiredIndentationAsync

        public Task<IndentationResult?> GetDesiredIndentationAsync(Document document, int lineNumber, CancellationToken cancellationToken)
        {
            var root = document.GetSyntaxRootAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var sourceText = document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);

            var textSnapshot = sourceText.FindCorrespondingEditorTextSnapshot();
            if (textSnapshot == null)
            {
                // text snapshot doesn't exit. return null
                return Task.FromResult<IndentationResult?>(null);
            }

            var lineToBeIndented = textSnapshot.GetLineFromLineNumber(lineNumber);

            var formattingRules = GetFormattingRules(document, lineToBeIndented.Start);
            var optionSet = document.Project.Solution.Workspace.Options;

            // enter on a token case.
            if (ShouldUseSmartTokenFormatterInsteadOfIndenter(formattingRules, root, lineToBeIndented, optionSet, cancellationToken))
            {
                return Task.FromResult<IndentationResult?>(null);
            }

            var indenter = GetIndenter(document, lineToBeIndented, formattingRules, optionSet, cancellationToken);
            return Task.FromResult(indenter.GetDesiredIndentation());
        }
开发者ID:JackWangCUMT,项目名称:roslyn,代码行数:26,代码来源:AbstractIndentationService.cs

示例2: StartInlineSession

        public InlineRenameSessionInfo StartInlineSession(
            Document document,
            TextSpan textSpan,
            CancellationToken cancellationToken)
        {
            if (_activeRenameSession != null)
            {
                throw new InvalidOperationException(EditorFeaturesResources.AnActiveInlineRenameSessionIsActive);
            }

            var editorRenameService = document.GetLanguageService<IEditorInlineRenameService>();
            var renameInfo = editorRenameService.GetRenameInfoAsync(document, textSpan.Start, cancellationToken).WaitAndGetResult(cancellationToken);
            if (!renameInfo.CanRename)
            {
                return new InlineRenameSessionInfo(renameInfo.LocalizedErrorMessage);
            }

            var snapshot = document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken).FindCorrespondingEditorTextSnapshot();
            ActiveSession = new InlineRenameSession(
                this,
                document.Project.Solution.Workspace,
                renameInfo.TriggerSpan.ToSnapshotSpan(snapshot),
                renameInfo,
                _waitIndicator,
                _textBufferAssociatedViewService,
                _textBufferFactoryService,
                _refactorNotifyServices,
                _aggregateListener);

            return new InlineRenameSessionInfo(ActiveSession);
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:31,代码来源:InlineRenameService.cs

示例3: IsContext

        public static bool IsContext(Document document, int position, CancellationToken cancellationToken)
        {
            // Check to see if we're to the right of an $ or an @$
            var text = document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);

            var start = position - 1;
            if (start < 0)
            {
                return false;
            }

            if (text[start] == '@')
            {
                start--;

                if (start < 0)
                {
                    return false;
                }
            }

            if (text[start] != '$')
            {
                return false;
            }

            var tree = document.GetSyntaxTreeAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var token = tree.GetRoot(cancellationToken).FindTokenOnLeftOfPosition(start);

            return tree.IsExpressionContext(start, token, attributes: false, cancellationToken: cancellationToken)
                || tree.IsStatementContext(start, token, cancellationToken);
        }
开发者ID:RoryVL,项目名称:roslyn,代码行数:32,代码来源:InterpolatedStringCompletionSession.cs

示例4: GetItemsWorkerAsync

        protected override async Task<IEnumerable<CompletionItem>> GetItemsWorkerAsync(Document document, int position, CompletionTriggerInfo triggerInfo, CancellationToken cancellationToken)
        {
            var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

            if (tree.IsInNonUserCode(position, cancellationToken))
            {
                return null;
            }

            var targetToken = tree.FindTokenOnLeftOfPosition(position, cancellationToken).GetPreviousTokenIfTouchingWord(position);
            if (targetToken.IsKind(SyntaxKind.AliasKeyword) && targetToken.Parent.IsKind(SyntaxKind.ExternAliasDirective))
            {
                var compilation = await document.GetCSharpCompilationAsync(cancellationToken).ConfigureAwait(false);
                var aliases = compilation.ExternalReferences.SelectMany(r => r.Properties.Aliases).ToSet();

                if (aliases.Any())
                {
                    var root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);
                    var usedAliases = root.ChildNodes().OfType<ExternAliasDirectiveSyntax>().Where(e => !e.Identifier.IsMissing).Select(e => e.Identifier.ValueText);
                    aliases.RemoveRange(usedAliases);
                    aliases.Remove(MetadataReferenceProperties.GlobalAlias);
                    var textChangeSpan = CompletionUtilities.GetTextChangeSpan(await document.GetTextAsync(cancellationToken).ConfigureAwait(false), position);
                    return aliases.Select(e =>
                        new CompletionItem(this, e, textChangeSpan, glyph: Glyph.Namespace));
                }
            }

            return SpecializedCollections.EmptyEnumerable<CompletionItem>();
        }
开发者ID:ehsansajjad465,项目名称:roslyn,代码行数:29,代码来源:ExternAliasCompletionProvider.cs

示例5: GetChangeAsync

        public override async Task<CompletionChange> GetChangeAsync(Document document, CompletionItem item, char? commitKey = default(char?), CancellationToken cancellationToken = default(CancellationToken))
        {
            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
            var newDocument = await DetermineNewDocumentAsync(item, text, cancellationToken).ConfigureAwait(false);

            var newText = await newDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
            var newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            int? newPosition = null;

            // Attempt to find the inserted node and move the caret appropriately
            if (newRoot != null)
            {
                var caretTarget = newRoot.GetAnnotatedNodesAndTokens(_annotation).FirstOrNullable();
                if (caretTarget != null)
                {
                    var targetPosition = GetTargetCaretPosition(caretTarget.Value.AsNode());

                    // Something weird happened and we failed to get a valid position.
                    // Bail on moving the caret.
                    if (targetPosition > 0 && targetPosition <= newText.Length)
                    {
                        newPosition = targetPosition;
                    }
                }
            }

            var changes = await newDocument.GetTextChangesAsync(document, cancellationToken).ConfigureAwait(false);
            return CompletionChange.Create(ImmutableArray.CreateRange(changes), newPosition, includesCommitCharacter: true);
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:30,代码来源:AbstractMemberInsertingCompletionProvider.cs

示例6: GetTextChangesAsync

        public async Task<IEnumerable<TextChange>> GetTextChangesAsync(Document oldDocument, Document newDocument, CancellationToken cancellationToken)
        {
            var oldText = await oldDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
            var newText = await newDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var diffService = _differenceSelectorService.GetTextDifferencingService(oldDocument.Project.LanguageServices.GetService<IContentTypeLanguageService>().GetDefaultContentType())
                ?? _differenceSelectorService.DefaultTextDifferencingService;

            var differenceOptions = new StringDifferenceOptions()
            {
                DifferenceType = StringDifferenceTypes.Word
            };

            var oldTextSnapshot = oldText.FindCorrespondingEditorTextSnapshot();
            var newTextSnapshot = newText.FindCorrespondingEditorTextSnapshot();
            var useSnapshots = oldTextSnapshot != null && newTextSnapshot != null;

            var diffResult = useSnapshots
                ? diffService.DiffSnapshotSpans(oldTextSnapshot.GetFullSpan(), newTextSnapshot.GetFullSpan(), differenceOptions)
                : diffService.DiffStrings(oldText.ToString(), newText.ToString(), differenceOptions);

            return diffResult.Differences.Select(d =>
                new TextChange(
                    diffResult.LeftDecomposition.GetSpanInOriginal(d.Left).ToTextSpan(),
                    newText.GetSubText(diffResult.RightDecomposition.GetSpanInOriginal(d.Right).ToTextSpan()).ToString()));
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:26,代码来源:EditorTextDifferencingService.cs

示例7: FindBracesAsync

        public async Task<BraceMatchingResult?> FindBracesAsync(
            Document document,
            int position,
            CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var token = root.FindToken(position);

            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
            if (position < text.Length && this.IsBrace(text[position]))
            {
                if (token.RawKind == _openBrace.Kind && AllowedForToken(token))
                {
                    var leftToken = token;
                    if (TryFindMatchingToken(leftToken, out var rightToken))
                    {
                        return new BraceMatchingResult(leftToken.Span, rightToken.Span);
                    }
                }
                else if (token.RawKind == _closeBrace.Kind && AllowedForToken(token))
                {
                    var rightToken = token;
                    if (TryFindMatchingToken(rightToken, out var leftToken))
                    {
                        return new BraceMatchingResult(leftToken.Span, rightToken.Span);
                    }
                }
            }

            return null;
        }
开发者ID:GuilhermeSa,项目名称:roslyn,代码行数:31,代码来源:AbstractBraceMatcher.cs

示例8: Format

        public Document Format(Document document, IEnumerable<TextSpan> changes, CancellationToken cancellationToken)
        {
            var snapshot = document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken).FindCorrespondingEditorTextSnapshot();

            var root = document.GetSyntaxRootAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var formattingSpans = changes.Select(s => s.ToSnapshotSpan(snapshot))
                                         .Select(s => CommonFormattingHelpers.GetFormattingSpan(root, s.Span.ToTextSpan()));

            return Formatter.FormatAsync(document, formattingSpans, cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);
        }
开发者ID:RoryVL,项目名称:roslyn,代码行数:10,代码来源:AbstractCommentUncommentService.cs

示例9: CreateAsync

 internal static async Task<ItemGetter> CreateAsync(
     AbstractOverrideCompletionProvider overrideCompletionProvider,
     Document document,
     int position,
     CancellationToken cancellationToken)
 {
     var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
     var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
     var startLineNumber = text.Lines.IndexOf(position);
     var startLine = text.Lines[startLineNumber];
     return new ItemGetter(overrideCompletionProvider, document, position, text, syntaxTree, startLineNumber, startLine, cancellationToken);
 }
开发者ID:TyOverby,项目名称:roslyn,代码行数:12,代码来源:AbstractOverrideCompletionProvider.ItemGetter.cs

示例10: ItemGetter

            public ItemGetter(
                AbstractOverrideCompletionProvider overrideCompletionProvider,
                Document document, int position, CancellationToken cancellationToken)
            {
                _provider = overrideCompletionProvider;
                _document = document;
                _position = position;
                _cancellationToken = cancellationToken;

                _text = document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
                _syntaxTree = document.GetSyntaxTreeAsync(cancellationToken).WaitAndGetResult(cancellationToken);
                _startLineNumber = _text.Lines.IndexOf(position);
                _startLine = _text.Lines[_startLineNumber];
            }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:14,代码来源:AbstractOverrideCompletionProvider.ItemGetter.cs

示例11: BuildContentAsync

        protected override async Task<IDeferredQuickInfoContent> BuildContentAsync(
            Document document,
            SyntaxToken token,
            CancellationToken cancellationToken)
        {
            if (token.Kind() != SyntaxKind.CloseBraceToken)
            {
                return null;
            }

            // Don't show for interpolations
            if (token.Parent.IsKind(SyntaxKind.Interpolation) &&
                ((InterpolationSyntax)token.Parent).CloseBraceToken == token)
            {
                return null;
            }

            // Now check if we can find an open brace. 
            var parent = token.Parent;
            var openBrace = parent.ChildNodesAndTokens().FirstOrDefault(n => n.Kind() == SyntaxKind.OpenBraceToken).AsToken();
            if (openBrace.Kind() != SyntaxKind.OpenBraceToken)
            {
                return null;
            }

            // If the open brace is the first token of the node (like in the case of a block node or
            // an accessor list node), then walk up one higher so we can show more useful context
            // (like the method a block belongs to).
            if (parent.GetFirstToken() == openBrace)
            {
                parent = parent.Parent;
            }

            // Now that we know what we want to display, create a small elision buffer with that
            // span, jam it in a view and show that to the user.
            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
            var textSnapshot = text.FindCorrespondingEditorTextSnapshot();
            if (textSnapshot == null)
            {
                return null;
            }

            var span = new SnapshotSpan(textSnapshot, Span.FromBounds(parent.SpanStart, openBrace.Span.End));
            return this.CreateElisionBufferDeferredContent(span);
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:45,代码来源:SyntacticQuickInfoProvider.cs

示例12: IsTriggerOnDotAsync

        private async Task<bool?> IsTriggerOnDotAsync(Document document, int characterPosition, CancellationToken cancellationToken)
        {
            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
            if (text[characterPosition] != '.')
            {
                return null;
            }

            // don't want to trigger after a number.  All other cases after dot are ok.
            var tree = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var token = tree.FindToken(characterPosition);
            if (token.Kind() == SyntaxKind.DotToken)
            {
                token = token.GetPreviousToken();
            }

            return token.Kind() != SyntaxKind.NumericLiteralToken;
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:18,代码来源:SymbolCompletionProvider.cs

示例13: GetDesiredIndentationAsync

        public async Task<IndentationResult?> GetDesiredIndentationAsync(Document document, int lineNumber, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var lineToBeIndented = sourceText.Lines[lineNumber];

            var formattingRules = GetFormattingRules(document, lineToBeIndented.Start);

            // enter on a token case.
            if (ShouldUseSmartTokenFormatterInsteadOfIndenter(formattingRules, root, lineToBeIndented, document.Options, cancellationToken))
            {
                return null;
            }

            var indenter = await GetIndenterAsync(document, lineToBeIndented, formattingRules, document.Options, cancellationToken).ConfigureAwait(false);
            return indenter.GetDesiredIndentation();
        }
开发者ID:RoryVL,项目名称:roslyn,代码行数:18,代码来源:AbstractIndentationService.cs

示例14: GetItemsWorkerAsync

        protected override async Task<IEnumerable<CompletionItem>> GetItemsWorkerAsync(
            Document document, int position, CompletionTriggerInfo triggerInfo, CancellationToken cancellationToken)
        {
            if (document != null && document.SourceCodeKind == SourceCodeKind.Interactive)
            {
                // the provider might be invoked in non-interactive context:
                Workspace ws;
                if (Workspace.TryGetWorkspace(document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken).Container, out ws))
                {
                    var workspace = ws as InteractiveWorkspace;
                    if (workspace != null)
                    {
                        var window = workspace.Engine.CurrentWindow;
                        var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

                        if (tree.IsBeforeFirstToken(position, cancellationToken) &&
                            tree.IsPreProcessorKeywordContext(position, cancellationToken))
                        {
                            var textChangeSpan = await this.GetTextChangeSpanAsync(document, position, cancellationToken).ConfigureAwait(false);
                            var list = new List<CompletionItem>();

                            IInteractiveWindowCommands commands = window.GetInteractiveCommands();
                            if (commands != null)
                            {
                                foreach (var command in commands.GetCommands())
                                {
                                    foreach (var commandName in command.Names)
                                    {
                                        list.Add(new CSharpCompletionItem(
                                            workspace, this, commandName, textChangeSpan, c => Task.FromResult(command.Description.ToSymbolDisplayParts()), glyph: Glyph.Intrinsic));
                                    }
                                }
                            }

                            return list;
                        }
                    }
                }
            }

            return SpecializedCollections.EmptyEnumerable<CompletionItem>();
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:42,代码来源:ReplCommandCompletionProvider.cs

示例15: IsContext

        public static bool IsContext(Document document, int position, CancellationToken cancellationToken)
        {
            // First, check to see if the character to the left of the position is an open curly. If it is,
            // we shouldn't complete because the user may be trying to escape a curly.
            var text = document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var index = position - 1;
            var openCurlyCount = 0;
            while (index >= 0)
            {
                if (text[index] == '{')
                {
                    openCurlyCount++;
                }
                else
                {
                    break;
                }

                index--;
            }

            if (openCurlyCount > 0 && openCurlyCount % 2 == 1)
            {
                return false;
            }

            // Next, check to see if we're typing in an interpolated string
            var tree = document.GetSyntaxTreeAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var token = tree.GetRoot(cancellationToken).FindTokenOnLeftOfPosition(position);

            if (!token.Span.IntersectsWith(position))
            {
                return false;
            }

            return token.IsKind(
                SyntaxKind.InterpolatedStringStartToken,
                SyntaxKind.InterpolatedVerbatimStringStartToken,
                SyntaxKind.InterpolatedStringTextToken,
                SyntaxKind.InterpolatedStringEndToken);
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:41,代码来源:InterpolationCompletionSession.cs


注:本文中的Document.GetTextAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。