本文整理汇总了C#中IDocument.Replace方法的典型用法代码示例。如果您正苦于以下问题:C# IDocument.Replace方法的具体用法?C# IDocument.Replace怎么用?C# IDocument.Replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDocument
的用法示例。
在下文中一共展示了IDocument.Replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IndentSingleLine
static void IndentSingleLine(CacheIndentEngine engine, IDocument document, IDocumentLine line)
{
engine.Update(line.EndOffset);
if (engine.NeedsReindent) {
var indentation = TextUtilities.GetWhitespaceAfter(document, line.Offset);
// replacing the indentation in two steps is necessary to make the caret move accordingly.
document.Replace(indentation.Offset, indentation.Length, "");
document.Replace(indentation.Offset, 0, engine.ThisLineIndent);
engine.ResetEngineToPosition(line.Offset);
}
}
示例2: SortLines
public void SortLines(IDocument document, int startLine, int endLine, StringComparer comparer, bool removeDuplicates)
{
List<string> lines = new List<string>();
for (int i = startLine; i <= endLine; ++i) {
IDocumentLine line = document.GetLine(i);
lines.Add(document.GetText(line.Offset, line.Length));
}
lines.Sort(comparer);
if (removeDuplicates) {
lines = lines.Distinct(comparer).ToList();
}
using (document.OpenUndoGroup()) {
for (int i = 0; i < lines.Count; ++i) {
IDocumentLine line = document.GetLine(startLine + i);
document.Replace(line.Offset, line.Length, lines[i]);
}
// remove removed duplicate lines
for (int i = startLine + lines.Count; i <= endLine; ++i) {
IDocumentLine line = document.GetLine(startLine + lines.Count);
document.Remove(line.Offset, line.TotalLength);
}
}
}
示例3: ReplaceFieldDeclaration
protected override void ReplaceFieldDeclaration(IDocument document, IField oldField, string newFieldDeclaration)
{
// In VB, the field region begins at the start of the declaration
// and ends on the first column of the line following the declaration.
int startOffset = document.PositionToOffset(oldField.Region.BeginLine, 1);
int endOffset = document.PositionToOffset(oldField.Region.EndLine, 1);
document.Replace(startOffset, endOffset - startOffset, tabs + newFieldDeclaration + Environment.NewLine);
}
示例4: RemoveDiacritics
public void RemoveDiacritics(IDocument document)
{
lexerFactory()
.LexWords(document)
.Where(segment => wordProcessor.HasDiacritics(segment.Word))
.ForEach(segment => document.Replace(segment,
wordProcessor.RemoveDiacritics(segment.Word)));
}
示例5: SmartReplaceLine
/// <summary>
/// Replaces the text in a line.
/// If only whitespace at the beginning and end of the line was changed, this method
/// only adjusts the whitespace and doesn't replace the other text.
/// </summary>
public static void SmartReplaceLine(IDocument document, LineSegment line, string newLineText)
{
if (document == null)
throw new ArgumentNullException("document");
if (line == null)
throw new ArgumentNullException("line");
if (newLineText == null)
throw new ArgumentNullException("newLineText");
string newLineTextTrim = newLineText.Trim(_whitespaceChars);
string oldLineText = document.GetText(line);
if (oldLineText == newLineText)
return;
int pos = oldLineText.IndexOf(newLineTextTrim);
if (newLineTextTrim.Length > 0 && pos >= 0)
{
document.UndoStack.StartUndoGroup();
try
{
// find whitespace at beginning
int startWhitespaceLength = 0;
while (startWhitespaceLength < newLineText.Length)
{
char c = newLineText[startWhitespaceLength];
if (c != ' ' && c != '\t')
break;
startWhitespaceLength++;
}
// find whitespace at end
int endWhitespaceLength = newLineText.Length - newLineTextTrim.Length - startWhitespaceLength;
// replace whitespace sections
int lineOffset = line.Offset;
document.Replace(lineOffset + pos + newLineTextTrim.Length, line.Length - pos - newLineTextTrim.Length, newLineText.Substring(newLineText.Length - endWhitespaceLength));
document.Replace(lineOffset, pos, newLineText.Substring(0, startWhitespaceLength));
}
finally
{
document.UndoStack.EndUndoGroup();
}
}
else
{
document.Replace(line.Offset, line.Length, newLineText);
}
}
示例6: Convert
protected override void Convert(IDocument document, int startOffset, int length)
{
StringBuilder what = new StringBuilder(document.GetText(startOffset, length));
for (int i = 0; i < what.Length; ++i) {
if (!Char.IsLetter(what[i]) && i < what.Length - 1) {
what[i + 1] = Char.ToUpper(what[i + 1]);
}
}
document.Replace(startOffset, length, what.ToString());
}
示例7: RenumberOrderedList
private static void RenumberOrderedList(
IDocument document,
DocumentLine line,
int number)
{
while ((line = line.NextLine) != null)
{
number += 1;
var text = document.GetText(line.Offset, line.Length);
var match = OrderedListPattern.Match(text);
if (match.Success == false) break;
var group = match.Groups[1];
var currentNumber = int.Parse(group.Value);
if (currentNumber != number) document.Replace(line.Offset + group.Index, group.Length, number.ToString());
}
}
示例8: Convert
protected override void Convert(IDocument document, int y1, int y2)
{
for (int i = y2; i >= y1; --i) {
LineSegment line = document.GetLineSegment(i);
if(line.Length > 0) {
// count how many whitespace characters there are at the start
int whiteSpace = 0;
for(whiteSpace = 0; whiteSpace < line.Length && Char.IsWhiteSpace(document.GetCharAt(line.Offset + whiteSpace)); whiteSpace++) {
// deliberately empty
}
if(whiteSpace > 0) {
string newLine = document.GetText(line.Offset,whiteSpace);
string newPrefix = newLine.Replace("\t",new string(' ', document.TextEditorProperties.TabIndent));
document.Replace(line.Offset,whiteSpace,newPrefix);
}
}
}
}
示例9: UpdateMethodBody
public void UpdateMethodBody(IDocument document, IMethod method, string methodBody)
{
DomRegion methodRegion = GetBodyRegionInDocument(method);
int startOffset = GetStartOffset(document, methodRegion);
int endOffset = GetEndOffset(document, methodRegion);
document.Replace(startOffset, endOffset - startOffset, methodBody);
}
示例10: SwapText
/// <summary>
/// Swaps 2 ranges of text in a document.
/// </summary>
static void SwapText(IDocument document, Location start1, Location end1, Location start2, Location end2)
{
if (start1 > start2) {
Location sw;
sw = start1; start1 = start2; start2 = sw;
sw = end1; end1 = end2; end2 = sw;
}
if (end1 >= start2)
throw new InvalidOperationException("Cannot swap overlaping segments");
int offset1 = document.PositionToOffset(start1);
int len1 = document.PositionToOffset(end1) - offset1;
int offset2 = document.PositionToOffset(start2);
int len2 = document.PositionToOffset(end2) - offset2;
string text1 = document.GetText(offset1, len1);
string text2 = document.GetText(offset2, len2);
using (var undoGroup = document.OpenUndoGroup()) {
document.Replace(offset2, len2, text1);
document.Replace(offset1, len1, text2);
}
}
示例11: ModifyDocument
public static void ModifyDocument(List<Modification> modifications, IDocument doc, int offset, int length, string newName)
{
using (doc.OpenUndoGroup()) {
foreach (Modification m in modifications) {
if (m.Document == doc) {
if (m.Offset < offset)
offset += m.LengthDifference;
}
}
int lengthDifference = newName.Length - length;
doc.Replace(offset, length, newName);
if (lengthDifference != 0) {
for (int i = 0; i < modifications.Count; ++i) {
Modification m = modifications[i];
if (m.Document == doc) {
if (m.Offset > offset) {
m.Offset += lengthDifference;
modifications[i] = m; // Modification is a value type
}
}
}
modifications.Add(new Modification(doc, offset, lengthDifference));
}
}
}
示例12: Convert
protected override void Convert(IDocument document, int y1, int y2)
{
int redocounter = 0;
for (int i = y2; i >= y1; --i) {
LineSegment line = document.GetLineSegment(i);
if(line.Length > 0) {
// note: some users may prefer a more radical ConvertLeadingSpacesToTabs that
// means there can be no spaces before the first character even if the spaces
// didn't add up to a whole number of tabs
string newLine = TextUtilities.LeadingWhiteSpaceToTabs(document.GetText(line.Offset,line.Length), document.TextEditorProperties.TabIndent);
document.Replace(line.Offset,line.Length,newLine);
++redocounter;
}
}
if (redocounter > 0) {
document.UndoStack.CombineLast(redocounter); // redo the whole operation (not the single deletes)
}
}
示例13: ReplaceFieldDeclaration
/// <summary>
/// Replaces a field declaration in the source code document.
/// </summary>
/// <remarks>
/// The default implementation assumes that the field region starts at the very beginning
/// of the line of the field declaration and ends at the end of that line.
/// Override this method if that is not the case in a specific language.
/// </remarks>
protected virtual void ReplaceFieldDeclaration(IDocument document, IField oldField, string newFieldDeclaration)
{
int startOffset = document.PositionToOffset(oldField.Region.BeginLine, 1);
int endOffset = document.PositionToOffset(oldField.Region.EndLine + 1, 1);
document.Replace(startOffset, endOffset - startOffset, tabs + newFieldDeclaration + Environment.NewLine);
}
示例14: Apply
public void Apply(IDocument document)
{
using (document.OpenUndoGroup()) {
var changes = oldVersion.GetChangesTo(newVersion);
foreach (var change in changes)
document.Replace(change.Offset, change.RemovalLength, change.InsertedText);
}
}
示例15: SortLines
public void SortLines(IDocument document, int startLine, int endLine)
{
ArrayList lines = new ArrayList();
for (int i = startLine; i <= endLine; ++i) {
LineSegment line = document.GetLineSegment(i);
lines.Add(document.GetText(line.Offset, line.Length));
}
lines.Sort(new SortComparer());
bool removeDupes = PropertyService.Get(SortOptionsDialog.removeDupesOption, false);
if (removeDupes) {
for (int i = 0; i < lines.Count - 1; ++i) {
if (lines[i].Equals(lines[i + 1])) {
lines.RemoveAt(i);
--i;
}
}
}
for (int i = 0; i < lines.Count; ++i) {
LineSegment line = document.GetLineSegment(startLine + i);
document.Replace(line.Offset, line.Length, lines[i].ToString());
}
// remove removed duplicate lines
for (int i = startLine + lines.Count; i <= endLine; ++i) {
LineSegment line = document.GetLineSegment(startLine + lines.Count);
document.Remove(line.Offset, line.TotalLength);
}
}