本文整理汇总了C#中RefactoringOptions.GetParser方法的典型用法代码示例。如果您正苦于以下问题:C# RefactoringOptions.GetParser方法的具体用法?C# RefactoringOptions.GetParser怎么用?C# RefactoringOptions.GetParser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RefactoringOptions
的用法示例。
在下文中一共展示了RefactoringOptions.GetParser方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PerformChanges
public override List<Change> PerformChanges (RefactoringOptions options, object properties)
{
TextEditorData data = options.GetTextEditorData ();
int start, end;
MonoDevelop.Refactoring.IntroduceConstant.IntroduceConstantRefactoring.SearchString (data, '"', out start, out end);
LineSegment line = data.Document.GetLineByOffset (start);
int closingTagLength = 1; // length of the closing "
if (end > line.Offset + line.EditableLength) { // assume missing closing tag
end = line.Offset + line.EditableLength;
closingTagLength = 0;
}
INRefactoryASTProvider provider = options.GetASTProvider ();
List<Expression> args = new List<Expression> ();
IExpressionFinder expressionFinder = options.GetParser ().CreateExpressionFinder (options.Dom);
int expressionStart = start - 1;
while (expressionStart > 0) {
if (data.Document.GetCharAt (expressionStart) == '(') {
expressionStart--;
break;
}
expressionStart--;
}
// Add parameter to existing string.format call
ExpressionResult expressionResult = expressionFinder.FindFullExpression (options.Document.TextEditor.Text, expressionStart);
InvocationExpression formatCall = null;
if (expressionResult != null) {
InvocationExpression possibleFormatCall = provider.ParseExpression (expressionResult.Expression) as InvocationExpression;
if (possibleFormatCall != null && possibleFormatCall.TargetObject is MemberReferenceExpression && ((MemberReferenceExpression)possibleFormatCall.TargetObject).MemberName == "Format") {
PrimitiveExpression expr = possibleFormatCall.Arguments[0] as PrimitiveExpression;
if (expr != null) {
string str = data.Document.GetTextBetween (start + 1, data.SelectionRange.Offset) +
"{" + (possibleFormatCall.Arguments.Count - 1) + "}" +
data.Document.GetTextBetween (data.SelectionRange.EndOffset, end);
expr.Value = str;
expr.StringValue = '"' + str + '"';
possibleFormatCall.Arguments.Add (new PrimitiveExpression (data.Document.GetTextAt (data.SelectionRange)));
formatCall = possibleFormatCall;
start = data.Document.LocationToOffset (expressionResult.Region.Start.Line - 1, expressionResult.Region.Start.Column - 1);
end = data.Document.LocationToOffset (expressionResult.Region.End.Line - 1, expressionResult.Region.End.Column - 1) - 1;
}
}
}
// insert new format call
if (formatCall == null) {
string formattedString = UnescapeString (data.Document.GetTextBetween (start + 1, data.SelectionRange.Offset) + "{0}" + data.Document.GetTextBetween (data.SelectionRange.EndOffset, end));
args.Add (new PrimitiveExpression (formattedString));
args.Add (new PrimitiveExpression (data.Document.GetTextAt (data.SelectionRange)));
TypeReference typeRef = new TypeReference ("System.String");
typeRef.IsKeyword = true;
MemberReferenceExpression stringFormat = new MemberReferenceExpression (new TypeReferenceExpression (typeRef), "Format");
formatCall = new InvocationExpression (stringFormat, args);
}
List<Change> changes = new List<Change> ();
TextReplaceChange change = new TextReplaceChange ();
change.FileName = options.Document.FileName;
change.Offset = start;
change.RemovedChars = end - start + closingTagLength;
change.InsertedText = provider.OutputNode (options.Dom, formatCall);
change.MoveCaretToReplace = true;
changes.Add (change);
return changes;
}