Browse Source

Fix #1052: Use IDocumentationProvider in the CSharpDecompiler API

pull/1030/head
Siegfried Pammer 7 years ago
parent
commit
85c83b8e6f
  1. 19
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 14
      ICSharpCode.Decompiler/CSharp/Transforms/AddXmlDocumentationTransform.cs
  3. 2
      ICSharpCode.Decompiler/DecompileRun.cs
  4. 8
      ICSharpCode.Decompiler/Documentation/XmlDocumentationProvider.cs

19
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -344,6 +344,11 @@ namespace ICSharpCode.Decompiler.CSharp @@ -344,6 +344,11 @@ namespace ICSharpCode.Decompiler.CSharp
return typeSystemAstBuilder;
}
IDocumentationProvider CreateDefaultDocumentationProvider()
{
return XmlDocLoader.LoadDocumentation(typeSystem.ModuleDefinition);
}
void RunTransforms(AstNode rootNode, DecompileRun decompileRun, ITypeResolveContext decompilationContext)
{
var typeSystemAstBuilder = CreateAstBuilder(decompilationContext);
@ -369,6 +374,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -369,6 +374,7 @@ namespace ICSharpCode.Decompiler.CSharp
{
var decompilationContext = new SimpleTypeResolveContext(typeSystem.MainAssembly);
var decompileRun = new DecompileRun(settings) {
DocumentationProvider = DocumentationProvider ?? CreateDefaultDocumentationProvider(),
CancellationToken = CancellationToken
};
syntaxTree = new SyntaxTree();
@ -433,6 +439,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -433,6 +439,7 @@ namespace ICSharpCode.Decompiler.CSharp
{
var decompilationContext = new SimpleTypeResolveContext(typeSystem.MainAssembly);
var decompileRun = new DecompileRun(settings) {
DocumentationProvider = DocumentationProvider ?? CreateDefaultDocumentationProvider(),
CancellationToken = CancellationToken
};
syntaxTree = new SyntaxTree();
@ -446,7 +453,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -446,7 +453,10 @@ namespace ICSharpCode.Decompiler.CSharp
public ILTransformContext CreateILTransformContext(ILFunction function)
{
var decompileRun = new DecompileRun(settings) { CancellationToken = CancellationToken };
var decompileRun = new DecompileRun(settings) {
DocumentationProvider = DocumentationProvider ?? CreateDefaultDocumentationProvider(),
CancellationToken = CancellationToken
};
RequiredNamespaceCollector.CollectNamespaces(function.Method, typeSystem, decompileRun.Namespaces);
return new ILTransformContext(function, typeSystem, DebugInfoProvider, settings) {
CancellationToken = CancellationToken,
@ -474,6 +484,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -474,6 +484,7 @@ namespace ICSharpCode.Decompiler.CSharp
throw new ArgumentNullException(nameof(types));
var decompilationContext = new SimpleTypeResolveContext(typeSystem.MainAssembly);
var decompileRun = new DecompileRun(settings) {
DocumentationProvider = DocumentationProvider ?? CreateDefaultDocumentationProvider(),
CancellationToken = CancellationToken
};
syntaxTree = new SyntaxTree();
@ -509,6 +520,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -509,6 +520,7 @@ namespace ICSharpCode.Decompiler.CSharp
throw new InvalidOperationException($"Could not find type definition {fullTypeName} in type system.");
var decompilationContext = new SimpleTypeResolveContext(typeSystem.MainAssembly);
var decompileRun = new DecompileRun(settings) {
DocumentationProvider = DocumentationProvider ?? CreateDefaultDocumentationProvider(),
CancellationToken = CancellationToken
};
syntaxTree = new SyntaxTree();
@ -546,7 +558,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -546,7 +558,10 @@ namespace ICSharpCode.Decompiler.CSharp
throw new ArgumentNullException(nameof(definitions));
ITypeDefinition parentTypeDef = null;
syntaxTree = new SyntaxTree();
var decompileRun = new DecompileRun(settings) { CancellationToken = CancellationToken };
var decompileRun = new DecompileRun(settings) {
DocumentationProvider = DocumentationProvider ?? CreateDefaultDocumentationProvider(),
CancellationToken = CancellationToken
};
foreach (var entity in definitions) {
if (entity.IsNil)
throw new ArgumentException("definitions contains null element");

14
ICSharpCode.Decompiler/CSharp/Transforms/AddXmlDocumentationTransform.cs

@ -34,18 +34,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -34,18 +34,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
{
public void Run(AstNode rootNode, TransformContext context)
{
if (!context.Settings.ShowXmlDocumentation)
if (!context.Settings.ShowXmlDocumentation || context.DecompileRun.DocumentationProvider == null)
return;
try {
var xmldoc = XmlDocLoader.LoadDocumentation(context.TypeSystem.ModuleDefinition);
if (xmldoc == null)
return;
foreach (var entity in rootNode.DescendantsAndSelf.OfType<EntityDeclaration>()) {
if (!(entity.GetSymbol() is IEntity symbolicEntity))
var provider = context.DecompileRun.DocumentationProvider;
foreach (var entityDecl in rootNode.DescendantsAndSelf.OfType<EntityDeclaration>()) {
if (!(entityDecl.GetSymbol() is IEntity entity))
continue;
string doc = xmldoc.GetDocumentation(XmlDocKeyProvider.GetKey(symbolicEntity));
string doc = provider.GetDocumentation(entity);
if (doc != null) {
InsertXmlDocumentation(entity, new StringReader(doc));
InsertXmlDocumentation(entityDecl, new StringReader(doc));
}
}
} catch (XmlException ex) {

2
ICSharpCode.Decompiler/DecompileRun.cs

@ -3,6 +3,7 @@ using System.Collections.Generic; @@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Threading;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.Documentation;
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler
@ -13,6 +14,7 @@ namespace ICSharpCode.Decompiler @@ -13,6 +14,7 @@ namespace ICSharpCode.Decompiler
public HashSet<string> Namespaces { get; private set; } = new HashSet<string>();
public CancellationToken CancellationToken { get; set; }
public DecompilerSettings Settings { get; }
public IDocumentationProvider DocumentationProvider { get; set; }
Lazy<CSharp.TypeSystem.UsingScope> usingScope => new Lazy<CSharp.TypeSystem.UsingScope>(() => CreateUsingScope(Namespaces));
public CSharp.TypeSystem.UsingScope UsingScope => usingScope.Value;

8
ICSharpCode.Decompiler/Documentation/XmlDocumentationProvider.cs

@ -27,8 +27,16 @@ using ICSharpCode.Decompiler.TypeSystem; @@ -27,8 +27,16 @@ using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler.Documentation
{
/// <summary>
/// Provides XML documentation for type and member definitions in source code.
/// </summary>
public interface IDocumentationProvider
{
/// <summary>
/// Returns the XML documentation for the given <paramref name="entity"/>.
/// May return null, if no documentation is present for the entity.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="entity"/> is null.</exception>
string GetDocumentation(IEntity entity);
}

Loading…
Cancel
Save