From 3fd114cb96e58e73d68075d5a213248ec42ca6a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Wed, 24 Nov 2010 09:44:46 +0100 Subject: [PATCH] Comments are now part of the dom. --- ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs | 2 + .../CSharp/Dom/DomVisitor.cs | 5 ++ .../CSharp/Dom/GeneralScope/Comment.cs | 85 +++++++++++++++++++ .../CSharp/Parser/CSharpParser.cs | 39 +++++++++ .../ICSharpCode.NRefactory.csproj | 1 + 5 files changed, 132 insertions(+) create mode 100644 ICSharpCode.NRefactory/CSharp/Dom/GeneralScope/Comment.cs diff --git a/ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs b/ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs index 41a929036b..1a2239b9af 100644 --- a/ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs +++ b/ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs @@ -229,6 +229,8 @@ namespace ICSharpCode.NRefactory.CSharp public const int TypeParameter = 64; public const int Constraint = 65; + + public const int Comment = 66; } } } diff --git a/ICSharpCode.NRefactory/CSharp/Dom/DomVisitor.cs b/ICSharpCode.NRefactory/CSharp/Dom/DomVisitor.cs index a2b466b031..9ff7cc05ad 100644 --- a/ICSharpCode.NRefactory/CSharp/Dom/DomVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/Dom/DomVisitor.cs @@ -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); diff --git a/ICSharpCode.NRefactory/CSharp/Dom/GeneralScope/Comment.cs b/ICSharpCode.NRefactory/CSharp/Dom/GeneralScope/Comment.cs new file mode 100644 index 0000000000..c43132a7cc --- /dev/null +++ b/ICSharpCode.NRefactory/CSharp/Dom/GeneralScope/Comment.cs @@ -0,0 +1,85 @@ +// +// Comment.cs +// +// Author: +// Mike Krüger +// +// 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 (DomVisitor visitor, T data) + { + return visitor.VisitComment (this, data); + } + } +} + diff --git a/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs b/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs index f9fdeaf897..1f093a3f50 100644 --- a/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs +++ b/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs @@ -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 return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (top.LocationsBag); top.UsingsBag.Global.Accept (conversionVisitor); + InsertComments (top, conversionVisitor); return conversionVisitor.Unit; } } diff --git a/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj b/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj index 43614dcb97..da94b9b146 100644 --- a/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj +++ b/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj @@ -311,6 +311,7 @@ +