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 pathDefaultTextBufferProvider.cs
71 lines (60 loc) · 2.23 KB
/
DefaultTextBufferProvider.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
// 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;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Projection;
namespace Microsoft.VisualStudio.Editor.Razor
{
[System.Composition.Shared]
[Export(typeof(RazorTextBufferProvider))]
internal class DefaultTextBufferProvider : RazorTextBufferProvider
{
private readonly IBufferGraphFactoryService _bufferGraphService;
[ImportingConstructor]
public DefaultTextBufferProvider(IBufferGraphFactoryService bufferGraphService)
{
if (bufferGraphService == null)
{
throw new ArgumentNullException(nameof(bufferGraphService));
}
_bufferGraphService = bufferGraphService;
}
public override bool TryGetFromDocument(TextDocument document, out ITextBuffer textBuffer)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
textBuffer = null;
if (!document.TryGetText(out var sourceText))
{
// Could not retrieve source text from the document. We have no way have locating an ITextBuffer.
return false;
}
var container = sourceText.Container;
ITextBuffer buffer;
try
{
buffer = container.GetTextBuffer();
}
catch (ArgumentException)
{
// The source text container was not built from an ITextBuffer.
return false;
}
var bufferGraph = _bufferGraphService.CreateBufferGraph(buffer);
var razorBuffer = bufferGraph.GetRazorBuffers().FirstOrDefault();
if (razorBuffer == null)
{
// Could not find a text buffer associated with the text document.
return false;
}
textBuffer = razorBuffer;
return true;
}
}
}