Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refactoring to convert normal (or verbatim) strings to raw strings. #59180

Merged
merged 14 commits into from
Feb 2, 2022
Merged
1 change: 1 addition & 0 deletions src/Compilers/Test/Core/Traits/Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static class Features
public const string CodeActionsConvertLocalFunctionToMethod = "CodeActions.ConvertLocalFunctionToMethod";
public const string CodeActionsConvertNumericLiteral = "CodeActions.ConvertNumericLiteral";
public const string CodeActionsConvertQueryToForEach = "CodeActions.ConvertQueryToForEach";
public const string CodeActionsConvertRegularToRawString = "CodeActions.CodeActionsConvertRegularToRawString";
public const string CodeActionsConvertSwitchStatementToExpression = "CodeActions.ConvertSwitchStatementToExpression";
public const string CodeActionsConvertToInterpolatedString = "CodeActions.ConvertToInterpolatedString";
public const string CodeActionsConvertToIterator = "CodeActions.ConvertToIterator";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,13 @@ SyntaxKind.InterpolatedSingleLineRawStringStartToken or
return false;
}

var indentation = GetPreferredIndentation(document, token, cancellationToken);
var indentation = token.GetPreferredIndentation(document, cancellationToken);

var newLine = document.Project.Solution.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);

using var transaction = CaretPreservingEditTransaction.TryCreate(
CSharpEditorResources.Split_string, textView, _undoHistoryRegistry, _editorOperationsFactoryService);

var indentService = document.GetRequiredLanguageService<IIndentationService>();

var edit = subjectBuffer.CreateEdit();

var sourceText = document.GetTextSynchronously(cancellationToken);
Expand All @@ -116,45 +114,5 @@ SyntaxKind.InterpolatedSingleLineRawStringStartToken or
transaction?.Complete();
return true;
}

private static string GetPreferredIndentation(Document document, SyntaxToken token, CancellationToken cancellationToken)
{
var sourceText = document.GetTextSynchronously(cancellationToken);
var tokenLine = sourceText.Lines.GetLineFromPosition(token.SpanStart);
var firstNonWhitespacePos = tokenLine.GetFirstNonWhitespacePosition();
Contract.ThrowIfNull(firstNonWhitespacePos);
if (firstNonWhitespacePos.Value == token.SpanStart)
{
// token was on it's own line. Start the end delimiter at the same location as it.
return tokenLine.Text!.ToString(TextSpan.FromBounds(tokenLine.Start, token.SpanStart));
}

// Token was on a line with something else. Determine where we would indent the token if it was on the next
// line and use that to determine the indentation of the final line.

var options = document.Project.Solution.Options;
var newLine = options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);

var annotation = new SyntaxAnnotation();
var newToken = token.WithAdditionalAnnotations(annotation);
newToken = newToken.WithLeadingTrivia(newToken.LeadingTrivia.Add(SyntaxFactory.EndOfLine(newLine)));

var root = document.GetRequiredSyntaxRootSynchronously(cancellationToken);
var newRoot = root.ReplaceToken(token, newToken);
var newDocument = document.WithSyntaxRoot(newRoot);
var newText = newDocument.GetTextSynchronously(cancellationToken);

var newTokenLine = newText.Lines.GetLineFromPosition(newRoot.GetAnnotatedTokens(annotation).Single().SpanStart);

var indentStyle = document.Project.Solution.Options.GetOption(FormattingOptions.SmartIndent, LanguageNames.CSharp);
var indenter = document.GetRequiredLanguageService<IIndentationService>();

var indentation = indenter.GetIndentation(newDocument, newTokenLine.LineNumber, indentStyle, cancellationToken);

return indentation.GetIndentationString(
newText,
options.GetOption(FormattingOptions.UseTabs, LanguageNames.CSharp),
options.GetOption(FormattingOptions.TabSize, LanguageNames.CSharp));
}
}
}
Loading