本文整理汇总了C#中ITextBuffer.CreateEdit方法的典型用法代码示例。如果您正苦于以下问题:C# ITextBuffer.CreateEdit方法的具体用法?C# ITextBuffer.CreateEdit怎么用?C# ITextBuffer.CreateEdit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITextBuffer
的用法示例。
在下文中一共展示了ITextBuffer.CreateEdit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ApplyChange
/// Takes current text buffer and new text then builds list of changed
/// regions and applies them to the buffer. This way we can avoid
/// destruction of bookmarks and other markers. Complete
/// buffer replacement deletes all markers which causes
/// loss of bookmarks, breakpoints and other similar markers.
public static void ApplyChange(
ITextBuffer textBuffer,
int position,
int length,
string newText,
string transactionName,
ISelectionTracker selectionTracker,
int maxMilliseconds) {
var snapshot = textBuffer.CurrentSnapshot;
int oldLength = Math.Min(length, snapshot.Length - position);
string oldText = snapshot.GetText(position, oldLength);
var changes = TextChanges.BuildChangeList(oldText, newText, maxMilliseconds);
if (changes != null && changes.Count > 0) {
using (var selectionUndo = new SelectionUndo(selectionTracker, transactionName, automaticTracking: false)) {
using (ITextEdit edit = textBuffer.CreateEdit()) {
// Replace ranges in reverse so relative positions match
for (int i = changes.Count - 1; i >= 0; i--) {
TextChange tc = changes[i];
edit.Replace(tc.Position + position, tc.Length, tc.NewText);
}
edit.Apply();
}
}
}
}
示例2: UpdateText
private static void UpdateText(ITextBuffer textBuffer, string text)
{
using (var edit = textBuffer.CreateEdit())
{
edit.Replace(0, textBuffer.CurrentSnapshot.Length, text);
edit.Apply();
}
}
示例3: ApplyEdits
private static void ApplyEdits(ITextBuffer textBuffer, IList<Edit> edits)
{
using (var vsEdit = textBuffer.CreateEdit())
{
foreach (var edit in edits)
vsEdit.Replace(edit.Start, edit.Length, edit.Text);
vsEdit.Apply();
}
}
示例4: ApplyChanges
private static void ApplyChanges(ITextBuffer buffer, IList<TextChange> changes)
{
using (var edit = buffer.CreateEdit())
{
foreach (var change in changes)
{
edit.Replace(change.Span.ToSpan(), change.NewText);
}
edit.Apply();
}
}
示例5: ApplyChangeByTokens
/// <summary>
/// Incrementally applies whitespace change to the buffer
/// having old and new tokens produced from the 'before formatting'
/// and 'after formatting' versions of the same text.
/// </summary>
/// <param name="textBuffer">Text buffer to apply changes to</param>
/// <param name="newTextProvider">Text provider of the text fragment before formatting</param>
/// <param name="newTextProvider">Text provider of the formatted text</param>
/// <param name="oldTokens">Tokens from the 'before' text fragment</param>
/// <param name="newTokens">Tokens from the 'after' text fragment</param>
/// <param name="formatRange">Range that is being formatted in the text buffer</param>
/// <param name="transactionName">Name of the undo transaction to open</param>
/// <param name="selectionTracker">Selection tracker object that will save, track
/// <param name="additionalAction">Action to perform after changes are applies by undo unit is not yet closed.</param>
/// and restore selection after changes have been applied.</param>
public static void ApplyChangeByTokens(
ITextBuffer textBuffer,
ITextProvider oldTextProvider,
ITextProvider newTextProvider,
IReadOnlyList<ITextRange> oldTokens,
IReadOnlyList<ITextRange> newTokens,
ITextRange formatRange,
string transactionName,
ISelectionTracker selectionTracker,
IEditorShell editorShell,
Action additionalAction = null) {
Debug.Assert(oldTokens.Count == newTokens.Count);
if (oldTokens.Count == newTokens.Count) {
ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
using (CreateSelectionUndo(selectionTracker, editorShell, transactionName)) {
using (ITextEdit edit = textBuffer.CreateEdit()) {
if (oldTokens.Count > 0) {
// Replace whitespace between tokens in reverse so relative positions match
int oldEnd = oldTextProvider.Length;
int newEnd = newTextProvider.Length;
string oldText, newText;
for (int i = newTokens.Count - 1; i >= 0; i--) {
oldText = oldTextProvider.GetText(TextRange.FromBounds(oldTokens[i].End, oldEnd));
newText = newTextProvider.GetText(TextRange.FromBounds(newTokens[i].End, newEnd));
if (oldText != newText) {
edit.Replace(formatRange.Start + oldTokens[i].End, oldEnd - oldTokens[i].End, newText);
}
oldEnd = oldTokens[i].Start;
newEnd = newTokens[i].Start;
}
newText = newTextProvider.GetText(TextRange.FromBounds(0, newEnd));
edit.Replace(formatRange.Start, oldEnd, newText);
} else {
string newText = newTextProvider.GetText(TextRange.FromBounds(0, newTextProvider.Length));
edit.Replace(formatRange.Start, formatRange.Length, newText);
}
edit.Apply();
additionalAction?.Invoke();
}
}
}
}
示例6: RemoveTrailingWhitespace
private void RemoveTrailingWhitespace(ITextBuffer buffer)
{
using (ITextEdit edit = buffer.CreateEdit())
{
ITextSnapshot snap = edit.Snapshot;
foreach (ITextSnapshotLine line in snap.Lines)
{
string text = line.GetText();
int length = text.Length;
while (--length >= 0 && Char.IsWhiteSpace(text[length])) ;
if (length < text.Length - 1)
{
int start = line.Start.Position;
edit.Delete(start + length + 1, text.Length - length - 1);
}
}
edit.Apply();
}
}
示例7: ApplyChanges
private static ITextSnapshot ApplyChanges(ITextBuffer buffer, params TextChange[] changes)
{
using (var edit = buffer.CreateEdit())
{
foreach (var change in changes)
{
edit.Replace(change.Start, change.Length, change.Text);
}
return edit.Apply();
}
}
示例8: 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();
}
}
示例9: RunCodeCleanupGeneric
/// <summary>
/// Attempts to run code cleanup on the specified generic document.
/// </summary>
/// <param name="document">The document for cleanup.</param>
/// <param name="textBuffer">The text buffer for the document.</param>
private void RunCodeCleanupGeneric(Document document, ITextBuffer textBuffer)
{
ITextDocument textDocument;
var doc = (TextDocument)document.Object("TextDocument");
textBuffer.Properties.TryGetProperty(typeof(ITextDocument), out textDocument);
var path = doc.Parent.FullName;
FileConfiguration settings;
if (!ConfigLoader.TryLoad(path, out settings))
return;
using (ITextEdit edit = textBuffer.CreateEdit())
{
ITextSnapshot snapshot = edit.Snapshot;
if (settings.Charset != null && textDocument != null)
FixDocumentCharset(textDocument, settings.Charset.Value);
if (settings.TryKeyAsBool("trim_trailing_whitespace"))
TrimTrailingWhitespace(snapshot, edit);
if (settings.TryKeyAsBool("insert_final_newline"))
InsertFinalNewline(snapshot, edit, settings.EndOfLine());
var eol = settings.EndOfLine();
FixLineEndings(snapshot, edit, eol);
edit.Apply();
}
}
示例10: 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();
}
}
示例11: GetFormattedText
private static string GetFormattedText(ITextBuffer buffer, IList<TextChange> changes)
{
using (var edit = buffer.CreateEdit())
{
foreach (var change in changes)
{
edit.Replace(change.Span.ToSpan(), change.NewText);
}
edit.Apply();
}
return buffer.CurrentSnapshot.GetText();
}
示例12: AppendLineNoPromptInjection
private void AppendLineNoPromptInjection(ITextBuffer buffer)
{
using (var edit = buffer.CreateEdit(EditOptions.None, null, SuppressPromptInjectionTag))
{
edit.Insert(buffer.CurrentSnapshot.Length, _lineBreakString);
edit.Apply();
}
}
示例13: 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();
}
}
示例14: ReplaceAll
public bool ReplaceAll(ITextBuffer tb) {
Contract.Assume(tb != null);
var tra = new TacticReplacerActor(tb);
var isMoreMembers = tra.NextMemberInTld();
var replaceStatus = TacticReplaceStatus.Success;
var tedit = tb.CreateEdit();
try
{
while (isMoreMembers && (replaceStatus == TacticReplaceStatus.Success || replaceStatus == TacticReplaceStatus.NoTactic))
{
var isMoreTactics = tra.NextTacticCallInMember();
while (isMoreTactics && (replaceStatus == TacticReplaceStatus.Success || replaceStatus == TacticReplaceStatus.NoTactic))
{
replaceStatus = tra.ReplaceSingleTacticCall(tedit);
isMoreTactics = tra.NextTacticCallInMember();
}
isMoreMembers = tra.NextMemberInTld();
}
if(replaceStatus==TacticReplaceStatus.Success || replaceStatus == TacticReplaceStatus.NoTactic)
{ tedit.Apply();} else { tedit.Dispose();}
} catch { tedit.Dispose(); }
return NotifyOfReplacement(replaceStatus);
}
示例15: UpdateFromImport
private static void UpdateFromImport(
PythonAst curAst,
ITextBuffer buffer,
FromImportStatement fromImport,
string name
) {
NameExpression[] names = new NameExpression[fromImport.Names.Count + 1];
NameExpression[] asNames = fromImport.AsNames == null ? null : new NameExpression[fromImport.AsNames.Count + 1];
NameExpression newName = new NameExpression(name);
for (int i = 0; i < fromImport.Names.Count; i++) {
names[i] = fromImport.Names[i];
}
names[fromImport.Names.Count] = newName;
if (asNames != null) {
for (int i = 0; i < fromImport.AsNames.Count; i++) {
asNames[i] = fromImport.AsNames[i];
}
}
var newImport = new FromImportStatement((ModuleName)fromImport.Root, names, asNames, fromImport.IsFromFuture, fromImport.ForceAbsolute);
curAst.CopyAttributes(fromImport, newImport);
var newCode = newImport.ToCodeString(curAst);
var span = fromImport.GetSpan(curAst);
int leadingWhiteSpaceLength = (fromImport.GetLeadingWhiteSpace(curAst) ?? "").Length;
using (var edit = buffer.CreateEdit()) {
edit.Delete(span.Start.Index - leadingWhiteSpaceLength, span.Length + leadingWhiteSpaceLength);
edit.Insert(span.Start.Index, newCode);
edit.Apply();
}
}