Browse Source

Comments are now part of the dom.

newNRvisualizers
Mike Krüger 15 years ago
parent
commit
3fd114cb96
  1. 2
      ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs
  2. 5
      ICSharpCode.NRefactory/CSharp/Dom/DomVisitor.cs
  3. 85
      ICSharpCode.NRefactory/CSharp/Dom/GeneralScope/Comment.cs
  4. 39
      ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs
  5. 1
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

2
ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs

@ -229,6 +229,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -229,6 +229,8 @@ namespace ICSharpCode.NRefactory.CSharp
public const int TypeParameter = 64;
public const int Constraint = 65;
public const int Comment = 66;
}
}
}

5
ICSharpCode.NRefactory/CSharp/Dom/DomVisitor.cs

@ -45,6 +45,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -45,6 +45,11 @@ namespace ICSharpCode.NRefactory.CSharp
return VisitChildren (unit, data);
}
public virtual S VisitComment (Comment comment, T data)
{
return default (S);
}
public virtual S VisitFullTypeName (FullTypeName fullTypeName, T data)
{
return default (S);

85
ICSharpCode.NRefactory/CSharp/Dom/GeneralScope/Comment.cs

@ -0,0 +1,85 @@ @@ -0,0 +1,85 @@
//
// Comment.cs
//
// Author:
// Mike Krüger <mkrueger@novell.com>
//
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace ICSharpCode.NRefactory.CSharp
{
public enum CommentType {
SingleLine,
MultiLine,
Documentation
}
public class Comment : DomNode
{
public override NodeType NodeType {
get {
return NodeType.Unknown;
}
}
public CommentType CommentType {
get;
set;
}
public bool StartsLine {
get;
set;
}
public string Content {
get;
set;
}
DomLocation startLocation;
public override DomLocation StartLocation {
get {
return startLocation;
}
}
DomLocation endLocation;
public override DomLocation EndLocation {
get {
return endLocation;
}
}
public Comment (CommentType commentType, DomLocation startLocation, DomLocation endLocation)
{
this.CommentType = commentType;
this.startLocation = startLocation;
this.endLocation = endLocation;
}
public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data)
{
return visitor.VisitComment (this, data);
}
}
}

39
ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

@ -2328,6 +2328,44 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2328,6 +2328,44 @@ namespace ICSharpCode.NRefactory.CSharp
set { throw new NotImplementedException(); }
}
void InsertComment (DomNode node, Comment comment)
{
if (node.EndLocation < comment.StartLocation) {
node.AddChild (comment);
return;
}
foreach (var child in node.Children) {
if (child.StartLocation < comment.StartLocation && comment.StartLocation < child.EndLocation) {
InsertComment (child, comment);
return;
}
if (comment.StartLocation < child.StartLocation) {
node.InsertChildBefore (child, comment, DomNode.Roles.Comment);
return;
}
}
node.AddChild (comment);
}
void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor)
{
foreach (var special in top.SpecialsBag.Specials) {
var comment = special as SpecialsBag.Comment;
if (comment != null) {
var type = (CommentType)comment.CommentType;
var start = new DomLocation (comment.Line, comment.Col);
var end = new DomLocation (comment.EndLine, comment.EndCol);
var domComment = new Comment (type, start, end);
domComment.StartsLine = comment.StartsLine;
domComment.Content = comment.Content;
InsertComment (conversionVisitor.Unit, domComment);
}
}
}
public CompilationUnit Parse (TextReader reader)
{
// TODO: can we optimize this to avoid the text->stream->text roundtrip?
@ -2352,6 +2390,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2352,6 +2390,7 @@ namespace ICSharpCode.NRefactory.CSharp
return null;
CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (top.LocationsBag);
top.UsingsBag.Global.Accept (conversionVisitor);
InsertComments (top, conversionVisitor);
return conversionVisitor.Unit;
}
}

1
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -311,6 +311,7 @@ @@ -311,6 +311,7 @@
<Compile Include="CSharp\Dom\TypeMembers\Accessor.cs" />
<Compile Include="CSharp\Parser\mcs\assembly.cs" />
<Compile Include="CSharp\Dom\TypeMembers\EnumMemberDeclaration.cs" />
<Compile Include="CSharp\Dom\GeneralScope\Comment.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="CSharp\" />

Loading…
Cancel
Save