// Copyright (c) Microsoft. 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.Collections.Generic;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
namespace dnSpy.Roslyn.EditorFeatures.Extensions
{
static partial class Extensions2
{
public static SourceTextContainer AsTextContainer(this ITextBuffer buffer)
=> Text.Extensions.AsTextContainer(buffer);
public static ITextBuffer GetTextBuffer(this SourceTextContainer textContainer)
=> TryGetTextBuffer(textContainer) ?? throw new ArgumentException();
public static ITextBuffer TryGetTextBuffer(this SourceTextContainer textContainer)
=> Text.Extensions.TryFindEditorTextBuffer(textContainer);
///
/// Returns the ITextSnapshot behind this SourceText, or null if it wasn't created from one.
///
/// Note that multiple ITextSnapshots may map to the same SourceText instance if
/// ITextSnapshot.Version.ReiteratedVersionNumber doesn't change.
///
/// The underlying ITextSnapshot.
public static ITextSnapshot FindCorrespondingEditorTextSnapshot(this SourceText text)
=> Text.Extensions.TryFindEditorSnapshot(text);
internal static ITextImage TryFindCorrespondingEditorTextImage(this SourceText text)
=> Text.Extensions.TryFindEditorTextImage(text);
internal static TextLine AsTextLine(this ITextSnapshotLine line)
=> line.Snapshot.AsText().Lines[line.LineNumber];
public static SourceText AsText(this ITextSnapshot textSnapshot)
=> Text.Extensions.AsText(textSnapshot);
//internal static SourceText AsRoslynText(this ITextSnapshot textSnapshot, Encoding encoding)
// => new SnapshotSourceText.ClosedSnapshotSourceText(((ITextSnapshot2)textSnapshot).TextImage, encoding);
///
/// Gets the workspace corresponding to the text buffer.
///
public static Workspace GetWorkspace(this ITextBuffer buffer)
{
var container = buffer.AsTextContainer();
if (Workspace.TryGetWorkspace(container, out var workspace))
{
return workspace;
}
return null;
}
///
/// Gets the s from the corresponding that are associated with the 's buffer,
/// updated to contain the same text as the snapshot if necessary. There may be multiple s associated with the buffer
/// if the file is linked into multiple projects or is part of a Shared Project.
///
public static IEnumerable GetRelatedDocumentsWithChanges(this ITextSnapshot text)
=> text.AsText().GetRelatedDocumentsWithChanges();
///
/// Gets the from the corresponding that is associated with the 's buffer
/// in its current project context, updated to contain the same text as the snapshot if necessary. There may be multiple s
/// associated with the buffer if it is linked into multiple projects or is part of a Shared Project. In this case, the
/// is responsible for keeping track of which of these s is in the current project context.
///
public static Document GetOpenDocumentInCurrentContextWithChanges(this ITextSnapshot text)
=> text.AsText().GetOpenDocumentInCurrentContextWithChanges();
///
/// Gets the s from the corresponding that are associated with the .
/// There may be multiple s associated with the buffer if it is linked into multiple projects or is part of a Shared Project.
///
public static IEnumerable GetRelatedDocuments(this ITextBuffer buffer)
=> buffer.AsTextContainer().GetRelatedDocuments();
///
/// Tries to get the document corresponding to the text from the current partial solution
/// associated with the text's container. If the document does not contain the exact text a document
/// from a new solution containing the specified text is constructed. If no document is associated
/// with the specified text's container, or the text's container isn't associated with a workspace,
/// then the method returns false.
///
internal static Document GetDocumentWithFrozenPartialSemantics(this SourceText text, CancellationToken cancellationToken)
{
var document = text.GetOpenDocumentInCurrentContextWithChanges();
return document?.WithFrozenPartialSemantics(cancellationToken);
}
internal static bool CanApplyChangeDocumentToWorkspace(this ITextBuffer buffer)
=> Workspace.TryGetWorkspace(buffer.AsTextContainer(), out var workspace) &&
workspace.CanApplyChange(ApplyChangesKind.ChangeDocument);
///
/// Get the encoding used to load this if possible.
///
/// Note that this will return if the
/// didn't come from an , or if the
/// is already closed.
///
///
internal static Encoding GetEncodingOrUTF8(this ITextBuffer textBuffer)
=> textBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument textDocument)
? textDocument.Encoding
: Encoding.UTF8;
}
}