public override Task ProvideCompletionsAsync(CompletionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // FilePath will be null when the editor is open for cases where we don't have a file on disk (C# interactive window and others).
            if (context.Document?.FilePath == null ||
                !context.Document.FilePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
            {
                // Not a Razor file.
                return(Task.CompletedTask);
            }

            if (!_textBufferProvider.TryGetFromDocument(context.Document, out var textBuffer) ||
                !_asyncCompletionBroker.IsCompletionSupported(textBuffer.ContentType))
            {
                // Completion is not supported.
                return(Task.CompletedTask);
            }

            var result = AddCompletionItems(context);

            return(result);
        }
Beispiel #2
0
        public override bool TryGetFromDocument(TextDocument document, out RazorCodeDocument codeDocument)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (!_bufferProvider.TryGetFromDocument(document, out var textBuffer))
            {
                // Could not find a Razor buffer associated with the document.
                codeDocument = null;
                return(false);
            }

            foreach (var codeDocumentProvider in _codeDocumentProviders)
            {
                if (codeDocumentProvider.TryGetFromBuffer(textBuffer, out codeDocument))
                {
                    return(true);
                }
            }

            // A Razor code document has not yet been associated with the buffer yet.
            codeDocument = null;
            return(false);
        }
        private Task AddCompletionItems(CompletionContext context)
        {
            if (!_textBufferProvider.TryGetFromDocument(context.Document, out var textBuffer) ||
                _dependencies.Value.AsyncCompletionBroker.IsCompletionSupported(textBuffer.ContentType))
            {
                // Async completion is supported that code path will handle completion.
                return(Task.CompletedTask);
            }

            if (!_codeDocumentProvider.Value.TryGetFromDocument(context.Document, out var codeDocument))
            {
                // A Razor code document has not yet been associated with the document.
                return(Task.CompletedTask);
            }

            var syntaxTree = codeDocument.GetSyntaxTree();

            if (syntaxTree == null)
            {
                // No syntax tree has been computed for the current document.
                return(Task.CompletedTask);
            }

            if (!TryGetRazorSnapshotPoint(context, out var razorSnapshotPoint))
            {
                // Could not find associated Razor location.
                return(Task.CompletedTask);
            }

            var location             = new SourceSpan(razorSnapshotPoint.Position, 0);
            var razorCompletionItems = _dependencies.Value.CompletionFactsService.GetCompletionItems(syntaxTree, location);

            foreach (var razorCompletionItem in razorCompletionItems)
            {
                if (razorCompletionItem.Kind != RazorCompletionItemKind.Directive)
                {
                    // Don't support any other types of completion kinds other than directives.
                    continue;
                }

                var propertyDictionary = new Dictionary <string, string>(StringComparer.Ordinal);
                if (!string.IsNullOrEmpty(razorCompletionItem.Description))
                {
                    propertyDictionary[DescriptionKey] = razorCompletionItem.Description;
                }

                var completionItem = CompletionItem.Create(
                    razorCompletionItem.InsertText,
                    // This groups all Razor directives together
                    sortText: "_RazorDirective_",
                    rules: CompletionItemRules.Create(formatOnCommit: false),
                    tags: ImmutableArray.Create(WellKnownTags.Intrinsic),
                    properties: propertyDictionary.ToImmutableDictionary());

                context.AddItem(completionItem);
            }

            return(Task.CompletedTask);
        }