Roslyn ImportAdderService doesn't visit trivia

228 views Asked by At

The Microsoft.CodeAnalysis.Editing.ImportAdderService.AddImportsAsync method only visits non-trivia syntax nodes, and therefore doesn't add namespace imports for them. This leads to the results, that cref's in xml documentation comments cannot be simplified to their short names, when using the Microsoft.CodeAnalysis.Simplification.Simplifier.

The biggest problem that I have with this, is that the simplification can lead to inconsistent results. Assume that you have multiple cref's, and for one cref, you have a namespace import (introduced due to another type, that is actually used in a non-trivia node). Then, this one cref's gets shortened, and the others don't.

For clarification.

Before applying ImportAdderService and Simplifier:

namespace Namespace
{
  /// <summary>
  /// <see cref=""System.Collections.IEnumerable"" />
  /// <see cref=""System.Collections.Generic.IEnumerable{T}"" />.
  /// </summary>
  internal class MyDeclaration
  {
    System.Collections.IEnumerable Field;
  }
}

After applying ImportAdderService and Simplifier:

using System.Collections;

namespace Namespace
{
  /// <summary>
  /// <see cref=""IEnumerable"" />
  /// <see cref=""System.Collections.Generic.IEnumerable{T}"" />.
  /// </summary>
  internal class MyDeclaration
  {
    IEnumerable Field;
  }
}

Question

Can I work around this issue? We definitely want to add usings for all types referenced in cref tags.

Or is this even by design?

I guess it is indeed a missing feature, to tell the ImportAdderService to visit trivia syntax nodes.

0

There are 0 answers