Browse Source

Fix #391: XML documentation comment auto-generation incomplete on delegates.

pull/478/head
Andreas Weizel 12 years ago
parent
commit
076f1c2eb7
  1. 13
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs
  2. 74
      src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp.Refactoring/CodeIssues/Custom/XmlDocIssue.cs

13
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

@ -358,8 +358,17 @@ namespace CSharpBinding.FormattingStrategy
sb.Append(indentation); sb.Append(indentation);
sb.Append("/// </summary>"); sb.Append("/// </summary>");
IUnresolvedMethod method = null;
if (member is IUnresolvedMethod) { if (member is IUnresolvedMethod) {
IUnresolvedMethod method = (IUnresolvedMethod)member; method = (IUnresolvedMethod)member;
} else if (member is IUnresolvedTypeDefinition) {
IUnresolvedTypeDefinition type = (IUnresolvedTypeDefinition) member;
if (type.Kind == TypeKind.Delegate) {
method = type.Methods.FirstOrDefault(m => m.Name == "Invoke");
}
}
if (method != null) {
for (int i = 0; i < method.Parameters.Count; ++i) { for (int i = 0; i < method.Parameters.Count; ++i) {
sb.Append(terminator); sb.Append(terminator);
sb.Append(indentation); sb.Append(indentation);
@ -376,8 +385,8 @@ namespace CSharpBinding.FormattingStrategy
} }
} }
} }
textArea.Document.Insert(cursorOffset, sb.ToString());
textArea.Document.Insert(cursorOffset, sb.ToString());
textArea.Caret.Offset = cursorOffset + indentation.Length + "/// ".Length + " <summary>".Length + terminator.Length; textArea.Caret.Offset = cursorOffset + indentation.Length + "/// ".Length + " <summary>".Length + terminator.Length;
} }
} }

74
src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp.Refactoring/CodeIssues/Custom/XmlDocIssue.cs

@ -38,25 +38,25 @@ using ICSharpCode.NRefactory.CSharp.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
[IssueDescription("Validate Xml documentation", [IssueDescription("Validate Xml documentation",
Description = "Validate Xml docs", Description = "Validate Xml docs",
Category = IssueCategories.CompilerWarnings, Category = IssueCategories.CompilerWarnings,
Severity = Severity.Warning)] Severity = Severity.Warning)]
public class XmlDocIssue : GatherVisitorCodeIssueProvider public class XmlDocIssue : GatherVisitorCodeIssueProvider
{ {
protected override IGatherVisitor CreateVisitor(BaseRefactoringContext context) protected override IGatherVisitor CreateVisitor(BaseRefactoringContext context)
{ {
return new GatherVisitor(context); return new GatherVisitor(context);
} }
class GatherVisitor : GatherVisitorBase<XmlDocIssue> class GatherVisitor : GatherVisitorBase<XmlDocIssue>
{ {
readonly List<Comment> storedXmlComment = new List<Comment>(); readonly List<Comment> storedXmlComment = new List<Comment>();
public GatherVisitor(BaseRefactoringContext ctx) public GatherVisitor(BaseRefactoringContext ctx)
: base (ctx) : base (ctx)
{ {
} }
void InvalideXmlComments() void InvalideXmlComments()
{ {
if (storedXmlComment.Count == 0) if (storedXmlComment.Count == 0)
@ -77,37 +77,37 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
)); ));
storedXmlComment.Clear(); storedXmlComment.Clear();
} }
public override void VisitComment(Comment comment) public override void VisitComment(Comment comment)
{ {
if (comment.CommentType == CommentType.Documentation) if (comment.CommentType == CommentType.Documentation)
storedXmlComment.Add(comment); storedXmlComment.Add(comment);
} }
public override void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration) public override void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration)
{ {
InvalideXmlComments(); InvalideXmlComments();
base.VisitNamespaceDeclaration(namespaceDeclaration); base.VisitNamespaceDeclaration(namespaceDeclaration);
} }
public override void VisitUsingDeclaration(UsingDeclaration usingDeclaration) public override void VisitUsingDeclaration(UsingDeclaration usingDeclaration)
{ {
InvalideXmlComments(); InvalideXmlComments();
base.VisitUsingDeclaration(usingDeclaration); base.VisitUsingDeclaration(usingDeclaration);
} }
public override void VisitUsingAliasDeclaration(UsingAliasDeclaration usingDeclaration) public override void VisitUsingAliasDeclaration(UsingAliasDeclaration usingDeclaration)
{ {
InvalideXmlComments(); InvalideXmlComments();
base.VisitUsingAliasDeclaration(usingDeclaration); base.VisitUsingAliasDeclaration(usingDeclaration);
} }
public override void VisitExternAliasDeclaration(ExternAliasDeclaration externAliasDeclaration) public override void VisitExternAliasDeclaration(ExternAliasDeclaration externAliasDeclaration)
{ {
InvalideXmlComments(); InvalideXmlComments();
base.VisitExternAliasDeclaration(externAliasDeclaration); base.VisitExternAliasDeclaration(externAliasDeclaration);
} }
TextLocation TranslateOffset(int offset) TextLocation TranslateOffset(int offset)
{ {
int line = storedXmlComment.First().StartLocation.Line; int line = storedXmlComment.First().StartLocation.Line;
@ -120,7 +120,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
return TextLocation.Empty; return TextLocation.Empty;
} }
void AddXmlIssue(int offset, int length, string str) void AddXmlIssue(int offset, int length, string str)
{ {
var textLocation = TranslateOffset(offset); var textLocation = TranslateOffset(offset);
@ -150,14 +150,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
foreach (var cmt in storedXmlComment) foreach (var cmt in storedXmlComment)
xml.Append(cmt.Content + "\n"); xml.Append(cmt.Content + "\n");
xml.Append("</root>\n"); xml.Append("</root>\n");
var doc = new AXmlParser().Parse(new StringTextSource(xml.ToString())); var doc = new AXmlParser().Parse(new StringTextSource(xml.ToString()));
var stack = new Stack<AXmlObject>(); var stack = new Stack<AXmlObject>();
stack.Push(doc); stack.Push(doc);
foreach (var err in doc.SyntaxErrors) foreach (var err in doc.SyntaxErrors)
AddXmlIssue(err.StartOffset - firstline.Length, err.EndOffset - err.StartOffset, err.Description); AddXmlIssue(err.StartOffset - firstline.Length, err.EndOffset - err.StartOffset, err.Description);
while (stack.Count > 0) { while (stack.Count > 0) {
var cur = stack.Pop(); var cur = stack.Pop();
var el = cur as AXmlElement; var el = cur as AXmlElement;
@ -183,6 +183,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var m = member as IParameterizedMember; var m = member as IParameterizedMember;
if (m != null && m.Parameters.Any(p => p.Name == name.Value)) if (m != null && m.Parameters.Any(p => p.Name == name.Value))
break; break;
var dtype = member as ITypeDefinition;
if ((dtype != null) && (dtype.Kind == TypeKind.Delegate)) {
var invokeMethod = dtype.Methods.FirstOrDefault(method => method.Name == "Invoke");
if ((invokeMethod != null) && invokeMethod.Parameters.Any(p => p.Name == name.Value))
break;
}
if (name.Value == "value" && member != null && (member.SymbolKind == SymbolKind.Property || member.SymbolKind == SymbolKind.Indexer || member.SymbolKind == SymbolKind.Event) && el.Name == "paramref") if (name.Value == "value" && member != null && (member.SymbolKind == SymbolKind.Property || member.SymbolKind == SymbolKind.Indexer || member.SymbolKind == SymbolKind.Event) && el.Name == "paramref")
break; break;
AddXmlIssue(name.ValueSegment.Offset - firstline.Length + 1, name.ValueSegment.Length - 2, string.Format(ctx.TranslateString("Parameter '{0}' not found"), name.Value)); AddXmlIssue(name.ValueSegment.Offset - firstline.Length + 1, name.ValueSegment.Length - 2, string.Format(ctx.TranslateString("Parameter '{0}' not found"), name.Value));
@ -204,7 +210,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
trctx = trctx.WithUsingScope(state.CurrentUsingScope); trctx = trctx.WithUsingScope(state.CurrentUsingScope);
var cdc = new CSharpDocumentationComment (emptySource, trctx); var cdc = new CSharpDocumentationComment (emptySource, trctx);
var entity = cdc.ResolveCref(cref.Value); var entity = cdc.ResolveCref(cref.Value);
if (entity == null) { if (entity == null) {
AddXmlIssue(cref.ValueSegment.Offset - firstline.Length + 1, cref.ValueSegment.Length - 2, string.Format(ctx.TranslateString("Cannot find reference '{0}'"), cref.Value)); AddXmlIssue(cref.ValueSegment.Offset - firstline.Length + 1, cref.ValueSegment.Length - 2, string.Format(ctx.TranslateString("Cannot find reference '{0}'"), cref.Value));
} }
@ -212,15 +218,15 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
AddXmlIssue(cref.ValueSegment.Offset - firstline.Length + 1, cref.ValueSegment.Length - 2, string.Format(ctx.TranslateString("Reference parsing error '{0}'."), e.Message)); AddXmlIssue(cref.ValueSegment.Offset - firstline.Length + 1, cref.ValueSegment.Length - 2, string.Format(ctx.TranslateString("Reference parsing error '{0}'."), e.Message));
} }
break; break;
} }
} }
foreach (var child in cur.Children) foreach (var child in cur.Children)
stack.Push(child); stack.Push(child);
} }
storedXmlComment.Clear(); storedXmlComment.Clear();
} }
protected virtual void VisitXmlChildren(AstNode node, Action checkDocumentationAction) protected virtual void VisitXmlChildren(AstNode node, Action checkDocumentationAction)
{ {
AstNode next; AstNode next;
@ -231,7 +237,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
child = next; child = next;
} }
checkDocumentationAction(); checkDocumentationAction();
for (; child != null; child = next) { for (; child != null; child = next) {
// Store next to allow the loop to continue // Store next to allow the loop to continue
// if the visitor removes/replaces child. // if the visitor removes/replaces child.
@ -240,47 +246,47 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
InvalideXmlComments(); InvalideXmlComments();
} }
protected virtual void VisitXmlChildren(AstNode node) protected virtual void VisitXmlChildren(AstNode node)
{ {
VisitXmlChildren(node, () => CheckXmlDoc(node)); VisitXmlChildren(node, () => CheckXmlDoc(node));
} }
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration) public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
{ {
VisitXmlChildren(typeDeclaration); VisitXmlChildren(typeDeclaration);
} }
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
{ {
VisitXmlChildren(methodDeclaration); VisitXmlChildren(methodDeclaration);
} }
public override void VisitDelegateDeclaration(DelegateDeclaration delegateDeclaration) public override void VisitDelegateDeclaration(DelegateDeclaration delegateDeclaration)
{ {
VisitXmlChildren(delegateDeclaration); VisitXmlChildren(delegateDeclaration);
} }
public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration) public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
{ {
VisitXmlChildren(constructorDeclaration); VisitXmlChildren(constructorDeclaration);
} }
public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration) public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration)
{ {
VisitXmlChildren(eventDeclaration); VisitXmlChildren(eventDeclaration);
} }
public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration) public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration)
{ {
VisitXmlChildren(destructorDeclaration); VisitXmlChildren(destructorDeclaration);
} }
public override void VisitEnumMemberDeclaration(EnumMemberDeclaration enumMemberDeclaration) public override void VisitEnumMemberDeclaration(EnumMemberDeclaration enumMemberDeclaration)
{ {
VisitXmlChildren(enumMemberDeclaration); VisitXmlChildren(enumMemberDeclaration);
} }
public override void VisitEventDeclaration(EventDeclaration eventDeclaration) public override void VisitEventDeclaration(EventDeclaration eventDeclaration)
{ {
VisitXmlChildren(eventDeclaration, () => { VisitXmlChildren(eventDeclaration, () => {
@ -289,7 +295,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
}); });
} }
public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration) public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration)
{ {
VisitXmlChildren(fieldDeclaration, () => { VisitXmlChildren(fieldDeclaration, () => {
@ -298,17 +304,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
}); });
} }
public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration) public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration)
{ {
VisitXmlChildren(indexerDeclaration); VisitXmlChildren(indexerDeclaration);
} }
public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration) public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
{ {
VisitXmlChildren(propertyDeclaration); VisitXmlChildren(propertyDeclaration);
} }
public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration) public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration)
{ {
VisitXmlChildren(operatorDeclaration); VisitXmlChildren(operatorDeclaration);

Loading…
Cancel
Save