本文整理汇总了C#中TextEditor.ReplaceText方法的典型用法代码示例。如果您正苦于以下问题:C# TextEditor.ReplaceText方法的具体用法?C# TextEditor.ReplaceText怎么用?C# TextEditor.ReplaceText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TextEditor
的用法示例。
在下文中一共展示了TextEditor.ReplaceText方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CorrectIndentingImplementation
protected override void CorrectIndentingImplementation (PolicyContainer policyParent, TextEditor editor, int line)
{
var lineSegment = editor.GetLine (line);
if (lineSegment == null)
return;
try {
var policy = policyParent.Get<CSharpFormattingPolicy> (MimeType);
var textpolicy = policyParent.Get<TextStylePolicy> (MimeType);
var tracker = new CSharpIndentEngine (policy.CreateOptions (textpolicy));
tracker.Update (IdeApp.Workbench.ActiveDocument.Editor, lineSegment.Offset);
for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++) {
tracker.Push (editor.GetCharAt (i));
}
string curIndent = lineSegment.GetIndentation (editor);
int nlwsp = curIndent.Length;
if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && editor.GetCharAt (lineSegment.Offset + nlwsp) == '*')) {
// Possibly replace the indent
string newIndent = tracker.ThisLineIndent;
if (newIndent != curIndent)
editor.ReplaceText (lineSegment.Offset, nlwsp, newIndent);
}
} catch (Exception e) {
LoggingService.LogError ("Error while indenting", e);
}
}
示例2: FixLineStart
public bool FixLineStart (TextEditor textEditorData, ICSharpCode.NRefactory6.CSharp.IStateMachineIndentEngine stateTracker, int lineNumber)
{
if (lineNumber > 1) {
var line = textEditorData.GetLine (lineNumber);
if (line == null)
return false;
var prevLine = textEditorData.GetLine (lineNumber - 1);
if (prevLine == null)
return false;
string trimmedPreviousLine = textEditorData.GetTextAt (prevLine).TrimStart ();
//xml doc comments
//check previous line was a doc comment
//check there's a following line?
if (trimmedPreviousLine.StartsWith ("///", StringComparison.Ordinal)) {
if (textEditorData.GetTextAt (line.Offset, line.Length).TrimStart ().StartsWith ("///", StringComparison.Ordinal))
return false;
//check that the newline command actually inserted a newline
textEditorData.EnsureCaretIsNotVirtual ();
var nextLineSegment = textEditorData.GetLine (lineNumber + 1);
string nextLine = nextLineSegment != null ? textEditorData.GetTextAt (nextLineSegment).TrimStart () : "";
if (trimmedPreviousLine.Length > "///".Length || nextLine.StartsWith ("///", StringComparison.Ordinal)) {
var insertionPoint = textEditorData.CaretOffset;
textEditorData.InsertText (insertionPoint, "/// ");
textEditorData.CaretOffset = insertionPoint + "/// ".Length;
return true;
}
//multi-line comments
} else if (stateTracker.IsInsideMultiLineComment) {
if (textEditorData.GetTextAt (line.Offset, line.Length).TrimStart ().StartsWith ("*", StringComparison.Ordinal))
return false;
textEditorData.EnsureCaretIsNotVirtual ();
string commentPrefix = string.Empty;
if (trimmedPreviousLine.StartsWith ("* ", StringComparison.Ordinal)) {
commentPrefix = "* ";
} else if (trimmedPreviousLine.StartsWith ("/**", StringComparison.Ordinal) || trimmedPreviousLine.StartsWith ("/*", StringComparison.Ordinal)) {
commentPrefix = " * ";
} else if (trimmedPreviousLine.StartsWith ("*", StringComparison.Ordinal)) {
commentPrefix = "*";
}
int indentSize = line.GetIndentation (textEditorData).Length;
var insertedText = prevLine.GetIndentation (textEditorData) + commentPrefix;
textEditorData.ReplaceText (line.Offset, indentSize, insertedText);
textEditorData.CaretOffset = line.Offset + insertedText.Length;
return true;
} else if (wasInStringLiteral) {
var lexer = new ICSharpCode.NRefactory.CSharp.Completion.CSharpCompletionEngineBase.MiniLexer (textEditorData.GetTextAt (0, prevLine.EndOffset).TrimEnd ());
lexer.Parse ();
if (!lexer.IsInString)
return false;
textEditorData.EnsureCaretIsNotVirtual ();
textEditorData.InsertText (prevLine.Offset + prevLine.Length, "\" +");
int indentSize = textEditorData.CaretOffset - line.Offset;
var insertedText = prevLine.GetIndentation (textEditorData) + (trimmedPreviousLine.StartsWith ("\"", StringComparison.Ordinal) ? "" : "\t") + "\"";
textEditorData.ReplaceText (line.Offset, indentSize, insertedText);
return true;
}
}
return false;
}
示例3: SortSelectedLines
public static void SortSelectedLines (TextEditor textEditor)
{
var selectionRegion = textEditor.SelectionRegion;
var start = selectionRegion.Begin;
var end = selectionRegion.End;
var caret = textEditor.CaretLocation;
int startLine = start.Line;
int endLine = end.Line;
if (startLine == endLine)
return;
int length = 0;
var lines = new string[endLine - startLine + 1];
for (int i = startLine; i <= endLine; i++) {
//get lines *with* line endings
var lineText = textEditor.GetLineText (i, true);
lines [i - startLine] = lineText;
length += lineText.Length;
}
var linesUnsorted = new string[lines.Length];
Array.Sort (lines, StringComparer.Ordinal);
bool changed = false;
for (int i = 0; i <= lines.Length; i++) {
//can't simply use reference comparison as Array.Sort is not stable
if (string.Equals (lines [i], linesUnsorted [i], StringComparison.Ordinal)) {
continue;
}
changed = true;
break;
}
if (!changed)
return;
var sb = new StringBuilder ();
for (int i = 0; i < lines.Length; i++) {
sb.Append (lines [i]);
}
var startOffset = textEditor.LocationToOffset (startLine, 1);
textEditor.ReplaceText (startOffset, length, sb.ToString ());
textEditor.CaretLocation = LimitColumn (textEditor, caret);
textEditor.SetSelection (LimitColumn (textEditor, start), LimitColumn (textEditor, end));
}
示例4: TransposeCharacters
public static void TransposeCharacters (TextEditor textEditor)
{
// Code from Mono.TextEditor.MiscActions.TransposeCharacters
if (textEditor.CaretOffset == 0)
return;
var line = textEditor.GetLine (textEditor.CaretLine);
if (line == null)
return;
using (var undoGroup = textEditor.OpenUndoGroup ()) {
int transposeOffset = textEditor.CaretOffset - 1;
char ch;
if (textEditor.CaretColumn == 0) {
var lineAbove = textEditor.GetLine (textEditor.CaretLine - 1);
if (lineAbove.Length == 0 && line.Length == 0)
return;
if (line.Length != 0) {
ch = textEditor.GetCharAt (textEditor.CaretOffset);
textEditor.RemoveText (textEditor.CaretOffset, 1);
textEditor.InsertText (lineAbove.Offset + lineAbove.Length, ch.ToString ());
return;
}
int lastCharOffset = lineAbove.Offset + lineAbove.Length - 1;
ch = textEditor.GetCharAt (lastCharOffset);
textEditor.RemoveText (lastCharOffset, 1);
textEditor.InsertAtCaret (ch.ToString ());
return;
}
int offset = textEditor.CaretOffset;
if (textEditor.CaretColumn >= line.Length + 1) {
offset = line.Offset + line.Length - 1;
transposeOffset = offset - 1;
// case one char in line:
if (transposeOffset < line.Offset) {
var lineAbove = textEditor.GetLine (textEditor.CaretLine - 1);
transposeOffset = lineAbove.Offset + lineAbove.Length;
ch = textEditor.GetCharAt (offset);
textEditor.RemoveText (offset, 1);
textEditor.InsertText (transposeOffset, ch.ToString ());
textEditor.CaretOffset = line.Offset;
return;
}
}
ch = textEditor.GetCharAt (offset);
textEditor.ReplaceText (offset, 1, textEditor.GetCharAt (transposeOffset).ToString ());
textEditor.ReplaceText (transposeOffset, 1, ch.ToString ());
if (textEditor.CaretColumn < line.Length + 1)
textEditor.CaretOffset = offset + 1;
}
}
示例5: Format
static void Format (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, TextEditor editor, DocumentContext context, int startOffset, int endOffset, bool exact, bool formatLastStatementOnly = false, OptionSet optionSet = null)
{
TextSpan span;
if (exact) {
span = new TextSpan (startOffset, endOffset - startOffset);
} else {
span = new TextSpan (0, endOffset);
}
var analysisDocument = context.AnalysisDocument;
if (analysisDocument == null)
return;
using (var undo = editor.OpenUndoGroup (/*OperationType.Format*/)) {
try {
var syntaxTree = analysisDocument.GetSyntaxTreeAsync ().Result;
if (formatLastStatementOnly) {
var root = syntaxTree.GetRoot ();
var token = root.FindToken (endOffset);
var tokens = ICSharpCode.NRefactory6.CSharp.FormattingRangeHelper.FindAppropriateRange (token);
if (tokens.HasValue) {
span = new TextSpan (tokens.Value.Item1.SpanStart, tokens.Value.Item2.Span.End - tokens.Value.Item1.SpanStart);
} else {
var parent = token.Parent;
if (parent != null)
span = parent.FullSpan;
}
}
if (optionSet == null) {
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
var textPolicy = policyParent.Get<TextStylePolicy> (mimeTypeChain);
optionSet = policy.CreateOptions (textPolicy);
}
var doc = Formatter.FormatAsync (analysisDocument, span, optionSet).Result;
var newTree = doc.GetSyntaxTreeAsync ().Result;
var caretOffset = editor.CaretOffset;
int delta = 0;
foreach (var change in newTree.GetChanges (syntaxTree)) {
if (!exact && change.Span.Start + delta >= caretOffset)
continue;
var newText = change.NewText;
editor.ReplaceText (delta + change.Span.Start, change.Span.Length, newText);
delta = delta - change.Span.Length + newText.Length;
}
if (startOffset < caretOffset) {
var caretEndOffset = caretOffset + delta;
if (0 <= caretEndOffset && caretEndOffset < editor.Length)
editor.CaretOffset = caretEndOffset;
if (editor.CaretColumn == 1) {
if (editor.CaretLine > 1 && editor.GetLine (editor.CaretLine - 1).Length == 0)
editor.CaretLine--;
editor.CaretColumn = editor.GetVirtualIndentationColumn (editor.CaretLine);
}
}
} catch (Exception e) {
LoggingService.LogError ("Error in on the fly formatter", e);
}
}
}