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

Decouple Formatter.GetFormattedTextChanges from Workspaces to allow using it in an analyzer #8268

Closed
HellBrick opened this issue Jan 31, 2016 · 2 comments

Comments

@HellBrick
Copy link

This is basically a continuation of #7929. The use case here is I want to be able to check if the code formatting satisfies the specific formatting settings and provide warning + code fix if it doesn't. Formatter.GetFormattedTextChanges seems like the way to go here, but unfortunately it's part of Microsoft.CodeAnalysis.Workspaces, which can't be used in an analyzer without producing tons of warnings on build.

As discussed in the issue I've mentioned, I'd like to suggest refactoring out a version of this method that doesn't need Workspace, operates only on SyntaxNode and an instance of formatting settings and resides in an assembly that can be used safely both during build and in VS.

@JackJCSN
Copy link

Maybe you can try like this:

var oraginalText = context.Tree.GetText(cancellationToken);
var formatedText = context.Tree.GetRoot(cancellationToken).NormalizeWhitespace().GetText();

var changes = formatedText.GetTextChanges(oraginalText);
foreach (var change in changes)
{
    var diagnostic = Diagnostic.Create(Rule, context.Tree.GetLocation(change.Span));
    context.ReportDiagnostic(diagnostic);
}

@CyrusNajmabadi
Copy link
Member

Needs to have a full API request that can go through teh API process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants