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 pathTextSnapshotProjectItem.cs
65 lines (51 loc) · 2.17 KB
/
TextSnapshotProjectItem.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
// 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.IO;
using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.VisualStudio.Text;
namespace Microsoft.VisualStudio.Editor.Razor
{
internal class TextSnapshotProjectItem : RazorProjectItem
{
private readonly ITextSnapshot _snapshot;
public TextSnapshotProjectItem(ITextSnapshot snapshot, string projectDirectory, string relativeFilePath, string filePath)
{
if (snapshot == null)
{
throw new ArgumentNullException(nameof(snapshot));
}
if (string.IsNullOrEmpty(projectDirectory))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(projectDirectory));
}
if (string.IsNullOrEmpty(relativeFilePath))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(relativeFilePath));
}
if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(filePath));
}
_snapshot = snapshot;
BasePath = projectDirectory;
FilePath = relativeFilePath;
PhysicalPath = filePath;
}
public override string BasePath { get; }
public override string FilePath { get; }
public override string PhysicalPath { get; }
public override bool Exists => true;
public override Stream Read()
{
var charArray = _snapshot.ToCharArray(0, _snapshot.Length);
// We can assume UTF8 because the call path that reads from RazorProjectItem => SourceDocument
// can't determine the encoding and always assumes Encoding.UTF8. This is something that we might
// want to revisit in the future.
var bytes = Encoding.UTF8.GetBytes(charArray);
var memoryStream = new MemoryStream(bytes);
return memoryStream;
}
}
}