Skip to content

Commit

Permalink
Merge pull request #59180 from CyrusNajmabadi/rawStringAnalyzerFixer
Browse files Browse the repository at this point in the history
Add refactoring to convert normal (or verbatim) strings to raw strings.
  • Loading branch information
CyrusNajmabadi authored Feb 2, 2022
2 parents 62ef6c0 + babe127 commit aa97465
Show file tree
Hide file tree
Showing 23 changed files with 1,363 additions and 47 deletions.
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

0 comments on commit aa97465

Please sign in to comment.