This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathRazorDirectiveCompletionSourceProvider.cs
74 lines (64 loc) · 2.85 KB
/
RazorDirectiveCompletionSourceProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.ComponentModel.Composition;
using System.Linq;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
namespace Microsoft.VisualStudio.Editor.Razor
{
[System.Composition.Shared]
[Export(typeof(IAsyncCompletionSourceProvider))]
[Name("Razor directive completion provider.")]
[ContentType(RazorLanguage.CoreContentType)]
internal class RazorDirectiveCompletionSourceProvider : IAsyncCompletionSourceProvider
{
private readonly ForegroundDispatcher _foregroundDispatcher;
private readonly RazorCompletionFactsService _completionFactsService;
[ImportingConstructor]
public RazorDirectiveCompletionSourceProvider(
ForegroundDispatcher foregroundDispatcher,
RazorCompletionFactsService completionFactsService)
{
if (foregroundDispatcher == null)
{
throw new ArgumentNullException(nameof(foregroundDispatcher));
}
if (completionFactsService == null)
{
throw new ArgumentNullException(nameof(completionFactsService));
}
_foregroundDispatcher = foregroundDispatcher;
_completionFactsService = completionFactsService;
}
public IAsyncCompletionSource GetOrCreate(ITextView textView)
{
if (textView == null)
{
throw new ArgumentNullException(nameof(textView));
}
var razorBuffer = textView.BufferGraph.GetRazorBuffers().FirstOrDefault();
if (!razorBuffer.Properties.TryGetProperty(typeof(RazorDirectiveCompletionSource), out IAsyncCompletionSource completionSource) ||
completionSource == null)
{
completionSource = CreateCompletionSource(razorBuffer);
razorBuffer.Properties.AddProperty(typeof(RazorDirectiveCompletionSource), completionSource);
}
return completionSource;
}
// Internal for testing
internal IAsyncCompletionSource CreateCompletionSource(ITextBuffer razorBuffer)
{
if (!razorBuffer.Properties.TryGetProperty(typeof(VisualStudioRazorParser), out VisualStudioRazorParser parser))
{
// Parser hasn't been associated with the text buffer yet.
return null;
}
var completionSource = new RazorDirectiveCompletionSource(_foregroundDispatcher, parser, _completionFactsService);
return completionSource;
}
}
}