本文整理汇总了C#中Microsoft.CodeAnalysis.Text.SourceText.GetTextChanges方法的典型用法代码示例。如果您正苦于以下问题:C# SourceText.GetTextChanges方法的具体用法?C# SourceText.GetTextChanges怎么用?C# SourceText.GetTextChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Text.SourceText
的用法示例。
在下文中一共展示了SourceText.GetTextChanges方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UpdateText
public void UpdateText(SourceText newText)
{
_updatding = true;
_editor.Document.BeginUpdate();
try
{
var changes = newText.GetTextChanges(_currentText);
var offset = 0;
foreach (var change in changes)
{
_editor.Document.Replace(change.Span.Start + offset, change.Span.Length, new StringTextSource(change.NewText));
offset += change.NewText.Length - change.Span.Length;
}
_currentText = newText;
}
finally
{
_updatding = false;
_editor.Document.EndUpdate();
}
}
示例2: ApplyDocumentTextChanged
protected override void ApplyDocumentTextChanged(DocumentId document, SourceText newText)
{
if (_openDocumentId != document)
{
return;
}
ITextSnapshot appliedText;
using (var edit = _openTextContainer.GetTextBuffer().CreateEdit(EditOptions.DefaultMinimalChange, reiteratedVersionNumber: null, editTag: null))
{
var oldText = _openTextContainer.CurrentText;
var changes = newText.GetTextChanges(oldText);
foreach (var change in changes)
{
edit.Replace(change.Span.Start, change.Span.Length, change.NewText);
}
appliedText = edit.Apply();
}
this.OnDocumentTextChanged(document, appliedText.AsText(), PreservationMode.PreserveIdentity);
}
示例3: ApplyDocumentTextChanged
protected override async void ApplyDocumentTextChanged (DocumentId id, SourceText text)
{
var document = GetDocument (id);
if (document == null)
return;
bool isOpen;
var filePath = document.FilePath;
Projection projection = null;
foreach (var entry in ProjectionList) {
var p = entry.Projections.FirstOrDefault (proj => proj?.Document?.FileName != null && FilePath.PathComparer.Equals (proj.Document.FileName, filePath));
if (p != null) {
filePath = entry.File.FilePath;
projection = p;
break;
}
}
var data = TextFileProvider.Instance.GetTextEditorData (filePath, out isOpen);
// Guard against already done changes in linked files.
// This shouldn't happen but the roslyn merging seems not to be working correctly in all cases :/
if (document.GetLinkedDocumentIds ().Length > 0 && isOpen && !(text.GetType ().FullName == "Microsoft.CodeAnalysis.Text.ChangedText")) {
return;
}
SourceText formerText;
lock (changedFiles) {
if (changedFiles.TryGetValue (filePath, out formerText)) {
if (formerText.Length == text.Length && formerText.ToString () == text.ToString ())
return;
}
changedFiles [filePath] = text;
}
SourceText oldFile;
if (!isOpen || !document.TryGetText (out oldFile)) {
oldFile = await document.GetTextAsync ();
}
var changes = text.GetTextChanges (oldFile).OrderByDescending (c => c.Span.Start).ToList ();
int delta = 0;
if (!isOpen) {
delta = ApplyChanges (projection, data, changes);
var formatter = CodeFormatterService.GetFormatter (data.MimeType);
if (formatter.SupportsPartialDocumentFormatting) {
var mp = GetMonoProject (CurrentSolution.GetProject (id.ProjectId));
string currentText = data.Text;
foreach (var change in changes) {
delta -= change.Span.Length - change.NewText.Length;
var startOffset = change.Span.Start - delta;
if (projection != null) {
int originalOffset;
if (projection.TryConvertFromProjectionToOriginal (startOffset, out originalOffset))
startOffset = originalOffset;
}
string str;
if (change.NewText.Length == 0) {
str = formatter.FormatText (mp.Policies, currentText, TextSegment.FromBounds (Math.Max (0, startOffset - 1), Math.Min (data.Length, startOffset + 1)));
} else {
str = formatter.FormatText (mp.Policies, currentText, new TextSegment (startOffset, change.NewText.Length));
}
data.ReplaceText (startOffset, change.NewText.Length, str);
}
}
data.Save ();
if (projection != null) {
await UpdateProjectionsDocuments (document, data);
} else {
OnDocumentTextChanged (id, new MonoDevelopSourceText (data), PreservationMode.PreserveValue);
}
FileService.NotifyFileChanged (filePath);
} else {
var formatter = CodeFormatterService.GetFormatter (data.MimeType);
var documentContext = IdeApp.Workbench.Documents.FirstOrDefault (d => FilePath.PathComparer.Compare (d.FileName, filePath) == 0);
var root = await projectChanges.NewProject.GetDocument (id).GetSyntaxRootAsync ();
var annotatedNode = root.DescendantNodesAndSelf ().FirstOrDefault (n => n.HasAnnotation (TypeSystemService.InsertionModeAnnotation));
SyntaxToken? renameTokenOpt = root.GetAnnotatedNodesAndTokens (Microsoft.CodeAnalysis.CodeActions.RenameAnnotation.Kind)
.Where (s => s.IsToken)
.Select (s => s.AsToken ())
.Cast<SyntaxToken?> ()
.FirstOrDefault ();
if (documentContext != null) {
var editor = (TextEditor)data;
await Runtime.RunInMainThread (async () => {
using (var undo = editor.OpenUndoGroup ()) {
var oldVersion = editor.Version;
delta = ApplyChanges (projection, data, changes);
var versionBeforeFormat = editor.Version;
if (formatter.SupportsOnTheFlyFormatting) {
foreach (var change in changes) {
delta -= change.Span.Length - change.NewText.Length;
var startOffset = change.Span.Start - delta;
if (projection != null) {
int originalOffset;
if (projection.TryConvertFromProjectionToOriginal (startOffset, out originalOffset))
//.........这里部分代码省略.........
示例4: UpdateText
private static void UpdateText(SourceText newText, ITextBuffer buffer, EditOptions options)
{
using (var edit = buffer.CreateEdit(options, reiteratedVersionNumber: null, editTag: null))
{
var oldSnapshot = buffer.CurrentSnapshot;
var oldText = oldSnapshot.AsText();
var changes = newText.GetTextChanges(oldText);
Workspace workspace = null;
if (Workspace.TryGetWorkspace(oldText.Container, out workspace))
{
var undoService = workspace.Services.GetService<ISourceTextUndoService>();
undoService.BeginUndoTransaction(oldSnapshot);
}
foreach (var change in changes)
{
edit.Replace(change.Span.Start, change.Span.Length, change.NewText);
}
edit.Apply();
}
}
示例5: UpdateText
// Stolen from Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.DocumentProvider.StandardTextDocument
private static void UpdateText(SourceText newText, ITextBuffer buffer, EditOptions options)
{
using (ITextEdit textEdit = buffer.CreateEdit(options, null, null))
{
SourceText oldText = buffer.CurrentSnapshot.AsText();
foreach (var current in newText.GetTextChanges(oldText))
{
textEdit.Replace(current.Span.Start, current.Span.Length, current.NewText);
}
textEdit.Apply();
}
}
示例6: Update
internal void Update(SourceText newText)
{
var buffer = GetTextBuffer();
using (var edit = buffer.CreateEdit(EditOptions.DefaultMinimalChange, reiteratedVersionNumber: null, editTag: null))
{
var oldText = buffer.CurrentSnapshot.AsText();
var changes = newText.GetTextChanges(oldText);
foreach (var change in changes)
{
edit.Replace(change.Span.Start, change.Span.Length, change.NewText);
}
edit.Apply();
}
}
示例7: ApplyDocumentTextChanged
protected override void ApplyDocumentTextChanged (DocumentId id, SourceText text)
{
var document = GetDocument (id);
if (document == null)
return;
bool isOpen;
var filePath = document.FilePath;
var data = TextFileProvider.Instance.GetTextEditorData (filePath, out isOpen);
// Guard against already done changes in linked files.
// This shouldn't happen but the roslyn merging seems not to be working correctly in all cases :/
if (document.GetLinkedDocumentIds ().Length > 0 && isOpen && !(text.GetType ().FullName == "Microsoft.CodeAnalysis.Text.ChangedText")) {
return;
}
SourceText formerText;
if (changedFiles.TryGetValue (filePath, out formerText)) {
if (formerText.Length == text.Length && formerText.ToString () == text.ToString ())
return;
}
changedFiles [filePath] = text;
Projection projection = null;
foreach (var entry in ProjectionList) {
var p = entry.Projections.FirstOrDefault (proj => FilePath.PathComparer.Equals (proj.Document.FileName, filePath));
if (p != null) {
filePath = entry.File.FilePath;
projection = p;
break;
}
}
SourceText oldFile;
if (!isOpen || !document.TryGetText (out oldFile)) {
oldFile = new MonoDevelopSourceText (data);
}
var changes = text.GetTextChanges (oldFile).OrderByDescending (c => c.Span.Start).ToList ();
int delta = 0;
if (!isOpen) {
delta = ApplyChanges (projection, data, changes);
var formatter = CodeFormatterService.GetFormatter (data.MimeType);
var mp = GetMonoProject (CurrentSolution.GetProject (id.ProjectId));
string currentText = data.Text;
foreach (var change in changes) {
delta -= change.Span.Length - change.NewText.Length;
var startOffset = change.Span.Start - delta;
if (projection != null) {
int originalOffset;
if (projection.TryConvertFromProjectionToOriginal (startOffset, out originalOffset))
startOffset = originalOffset;
}
string str;
if (change.NewText.Length == 0) {
str = formatter.FormatText (mp.Policies, currentText, TextSegment.FromBounds (Math.Max (0, startOffset - 1), Math.Min (data.Length, startOffset + 1)));
} else {
str = formatter.FormatText (mp.Policies, currentText, new TextSegment (startOffset, change.NewText.Length));
}
data.ReplaceText (startOffset, change.NewText.Length, str);
}
data.Save ();
OnDocumentTextChanged (id, new MonoDevelopSourceText (data), PreservationMode.PreserveValue);
FileService.NotifyFileChanged (filePath);
} else {
var formatter = CodeFormatterService.GetFormatter (data.MimeType);
var documentContext = IdeApp.Workbench.Documents.FirstOrDefault (d => FilePath.PathComparer.Compare (d.FileName, filePath) == 0);
if (documentContext != null) {
var editor = (TextEditor)data;
using (var undo = editor.OpenUndoGroup ()) {
delta = ApplyChanges (projection, data, changes);
foreach (var change in changes) {
delta -= change.Span.Length - change.NewText.Length;
var startOffset = change.Span.Start - delta;
if (projection != null) {
int originalOffset;
if (projection.TryConvertFromProjectionToOriginal (startOffset, out originalOffset))
startOffset = originalOffset;
}
if (change.NewText.Length == 0) {
formatter.OnTheFlyFormat (editor, documentContext, TextSegment.FromBounds (Math.Max (0, startOffset - 1), Math.Min (data.Length, startOffset + 1)));
} else {
formatter.OnTheFlyFormat (editor, documentContext, new TextSegment (startOffset, change.NewText.Length));
}
}
}
}
OnDocumentTextChanged (id, new MonoDevelopSourceText(data.CreateDocumentSnapshot ()), PreservationMode.PreserveValue);
Runtime.RunInMainThread (() => {
if (IdeApp.Workbench != null)
foreach (var w in IdeApp.Workbench.Documents)
w.StartReparseThread ();
});
}
}
示例8: UpdateText
private static void UpdateText(SourceText newText, ITextBuffer buffer, EditOptions options)
{
using (var edit = buffer.CreateEdit(options, reiteratedVersionNumber: null, editTag: null))
{
var oldText = buffer.CurrentSnapshot.AsText();
var changes = newText.GetTextChanges(oldText);
foreach (var change in changes)
{
edit.Replace(change.Span.Start, change.Span.Length, change.NewText);
}
edit.Apply();
}
}
示例9: UpdateText
public void UpdateText(SourceText newText)
{
var subjectBuffer = (IProjectionBuffer)this.GetOpenTextBuffer();
var originalSnapshot = subjectBuffer.CurrentSnapshot;
var originalText = originalSnapshot.AsText();
var changes = newText.GetTextChanges(originalText);
IEnumerable<int> affectedVisibleSpanIndices = null;
var editorVisibleSpansInOriginal = SharedPools.Default<List<TextSpan>>().AllocateAndClear();
try
{
var originalDocument = _workspace.CurrentSolution.GetDocument(this.Id);
editorVisibleSpansInOriginal.AddRange(GetEditorVisibleSpans());
var newChanges = FilterTextChanges(originalText, editorVisibleSpansInOriginal, changes).ToList();
if (newChanges.Count == 0)
{
// no change to apply
return;
}
ApplyChanges(subjectBuffer, newChanges, editorVisibleSpansInOriginal, out affectedVisibleSpanIndices);
AdjustIndentation(subjectBuffer, affectedVisibleSpanIndices);
}
finally
{
SharedPools.Default<HashSet<int>>().ClearAndFree((HashSet<int>)affectedVisibleSpanIndices);
SharedPools.Default<List<TextSpan>>().ClearAndFree(editorVisibleSpansInOriginal);
}
}