forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITextBufferExtensions.cs
88 lines (74 loc) · 3.58 KB
/
ITextBufferExtensions.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.VisualStudio.Text;
namespace Microsoft.CodeAnalysis.Editor.Shared.Extensions
{
internal static partial class ITextBufferExtensions
{
internal static bool IsInLspEditorContext(this ITextBuffer buffer)
{
if (buffer.TryGetWorkspace(out var workspace))
{
var workspaceContextService = workspace.Services.GetRequiredService<IWorkspaceContextService>();
return workspaceContextService.IsInLspEditorContext();
}
return false;
}
internal static bool TryGetWorkspace(this ITextBuffer buffer, [NotNullWhen(true)] out Workspace? workspace)
=> Workspace.TryGetWorkspace(buffer.AsTextContainer(), out workspace);
/// <summary>
/// Checks if a buffer supports refactorings.
/// </summary>
internal static bool SupportsRefactorings(this ITextBuffer buffer)
=> TryGetSupportsFeatureService(buffer, out var service) && service.SupportsRefactorings(buffer);
/// <summary>
/// Checks if a buffer supports rename.
/// </summary>
internal static bool SupportsRename(this ITextBuffer buffer)
=> TryGetSupportsFeatureService(buffer, out var service) && service.SupportsRename(buffer);
/// <summary>
/// Checks if a buffer supports code fixes.
/// </summary>
internal static bool SupportsCodeFixes(this ITextBuffer buffer)
=> TryGetSupportsFeatureService(buffer, out var service) && service.SupportsCodeFixes(buffer);
/// <summary>
/// Checks if a buffer supports navigation.
/// </summary>
internal static bool SupportsNavigationToAnyPosition(this ITextBuffer buffer)
=> TryGetSupportsFeatureService(buffer, out var service) && service.SupportsNavigationToAnyPosition(buffer);
private static bool TryGetSupportsFeatureService(ITextBuffer buffer, [NotNullWhen(true)] out ITextBufferSupportsFeatureService? service)
{
service = null;
if (buffer.TryGetWorkspace(out var workspace))
{
service = workspace.Services.GetService<ITextBufferSupportsFeatureService>();
}
return service != null;
}
public static ITextSnapshot ApplyChange(this ITextBuffer buffer, TextChange change)
{
using var edit = buffer.CreateEdit(EditOptions.DefaultMinimalChange, reiteratedVersionNumber: null, editTag: null);
edit.Replace(change.Span.ToSpan(), change.NewText);
return edit.Apply();
}
public static ITextSnapshot ApplyChanges(this ITextBuffer buffer, IEnumerable<TextChange> changes)
{
using var edit = buffer.CreateEdit(EditOptions.DefaultMinimalChange, reiteratedVersionNumber: null, editTag: null);
return ApplyChanges(edit, changes);
}
public static ITextSnapshot ApplyChanges(this ITextEdit edit, IEnumerable<TextChange> changes)
{
foreach (var change in changes)
{
edit.Replace(change.Span.ToSpan(), change.NewText);
}
return edit.Apply();
}
}
}