diff options
author | Lluis Sanchez Gual <lluis@xamarin.com> | 2015-04-20 19:07:51 +0300 |
---|---|---|
committer | Lluis Sanchez Gual <lluis@xamarin.com> | 2015-04-20 19:07:51 +0300 |
commit | 95408b1a41b31b645f3a8c3323ab7d3ca4bff26b (patch) | |
tree | f8f137cddcb9a40499d303be95722fd2c6081bce /main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.TypeSystem/ParsedDocument.cs | |
parent | b70e4e04b4e41b8ec28d7e4b220b401ecf467e7a (diff) | |
parent | de043d330a8bf5855d4d983c804cd36727cb2406 (diff) |
Merge remote-tracking branch 'origin/roslyn' into new-project-model
Diffstat (limited to 'main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.TypeSystem/ParsedDocument.cs')
-rw-r--r-- | main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.TypeSystem/ParsedDocument.cs | 347 |
1 files changed, 84 insertions, 263 deletions
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.TypeSystem/ParsedDocument.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.TypeSystem/ParsedDocument.cs index 0fdb4a13cd..dddd7641f3 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.TypeSystem/ParsedDocument.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.TypeSystem/ParsedDocument.cs @@ -29,10 +29,8 @@ using System.Collections.Generic; using System.Text; using System.Linq; using System.Threading; -using ICSharpCode.NRefactory; -using ICSharpCode.NRefactory.Semantics; -using Mono.TextEditor; - +using MonoDevelop.Ide.Editor; +using System.Threading.Tasks; namespace MonoDevelop.Ide.TypeSystem { @@ -43,7 +41,6 @@ namespace MonoDevelop.Ide.TypeSystem NonSerializable = 1 } - public abstract class ParsedDocument { DateTime lastWriteTimeUtc = DateTime.UtcNow; @@ -51,63 +48,8 @@ namespace MonoDevelop.Ide.TypeSystem get { return lastWriteTimeUtc; } set { lastWriteTimeUtc = value; } } - - [NonSerialized] - List<Comment> comments = new List<Comment> (); - - public virtual IUnresolvedFile ParsedFile { - get { return null; } - set { throw new InvalidOperationException (); } - } - public IList<Comment> Comments { - get { - return comments; - } - } - /// <summary> - /// Gets or sets a value indicating whether this instance is invalid and needs to be reparsed. - /// </summary> - public bool IsInvalid { - get; - set; - } - - List<Tag> tagComments = new List<Tag> (); - public IList<Tag> TagComments { - get { - return tagComments; - } - } - - IEnumerable<FoldingRegion> foldings = null; - public virtual IEnumerable<FoldingRegion> Foldings { - get { - return foldings ?? Enumerable.Empty<FoldingRegion> (); - } - } - - public IEnumerable<FoldingRegion> UserRegions { - get { - return Foldings.Where (f => f.Type == FoldType.UserRegion); - } - } - - List<PreProcessorDefine> defines = new List<PreProcessorDefine> (); - public IList<PreProcessorDefine> Defines { - get { - return defines; - } - } - - List<ConditionalRegion> conditionalRegions = new List<ConditionalRegion> (); - public IList<ConditionalRegion> ConditionalRegions { - get { - return conditionalRegions; - } - } - [NonSerialized] ParsedDocumentFlags flags; public ParsedDocumentFlags Flags { @@ -128,16 +70,30 @@ namespace MonoDevelop.Ide.TypeSystem fileName = value; } } - - public virtual IList<Error> Errors { - get { - return new Error[0]; - } + + /// <summary> + /// Gets or sets a value indicating whether this instance is invalid and needs to be reparsed. + /// </summary> + public bool IsInvalid { + get; + set; } - + + public abstract Task<IReadOnlyList<Comment>> GetCommentsAsync (CancellationToken cancellationToken = default(CancellationToken)); + public abstract Task<IReadOnlyList<Tag>> GetTagCommentsAsync (CancellationToken cancellationToken = default(CancellationToken)); + public abstract Task<IReadOnlyList<FoldingRegion>> GetFoldingsAsync (CancellationToken cancellationToken = default(CancellationToken)); + + public async Task<IEnumerable<FoldingRegion>> GetUserRegionsAsync (CancellationToken cancellationToken = default(CancellationToken)) + { + var foldings = await GetFoldingsAsync (cancellationToken).ConfigureAwait (false); + return foldings.Where (f => f.Type == FoldType.UserRegion); + } + + public abstract Task<IReadOnlyList<Error>> GetErrorsAsync (CancellationToken cancellationToken = default(CancellationToken)); + public bool HasErrors { get { - return Errors.Any (e => e.ErrorType == ErrorType.Error); + return GetErrorsAsync ().Result.Any (e => e.ErrorType == ErrorType.Error); } } @@ -162,236 +118,84 @@ namespace MonoDevelop.Ide.TypeSystem { this.fileName = fileName; } - - - public void Add (Comment comment) - { - comments.Add (comment); - } - - public void Add (Tag tagComment) - { - tagComments.Add (tagComment); - } - - public void Add (PreProcessorDefine define) - { - defines.Add (define); - } - - public void Add (ConditionalRegion region) + } + + public class DefaultParsedDocument : ParsedDocument + { + public DefaultParsedDocument (string fileName) : base (fileName) { - conditionalRegions.Add (region); + Flags |= ParsedDocumentFlags.NonSerializable; } - List<FoldingRegion> EnsureFoldingList () - { - if (this.foldings == null || !(foldings is List<FoldingRegion>)) - this.foldings = new List<FoldingRegion> (); - return (List<FoldingRegion>)foldings; - } - - public void Add (FoldingRegion region) + List<Comment> comments = new List<Comment> (); + + public void Add (Comment comment) { - EnsureFoldingList ().Add (region); + comments.Add (comment); } - - public void Add (IEnumerable<Comment> comments) + + public void AddRange (IEnumerable<Comment> comments) { this.comments.AddRange (comments); } - - public void Add (IEnumerable<Tag> tagComments) - { - this.tagComments.AddRange (tagComments); - } - - public void Add (IEnumerable<PreProcessorDefine> defines) - { - this.defines.AddRange (defines); - } - - public void Add (IEnumerable<FoldingRegion> folds) - { - if (foldings == null) { - this.foldings = folds; - return; - } - if (foldings != null && !(foldings is List<FoldingRegion>)) - EnsureFoldingList ().AddRange (foldings); - EnsureFoldingList ().AddRange (folds); - } - - public void Add (IEnumerable<ConditionalRegion> conditionalRegions) - { - this.conditionalRegions.AddRange (conditionalRegions); - } - - #region IUnresolvedFile delegation - public virtual IUnresolvedTypeDefinition GetTopLevelTypeDefinition (TextLocation location) + + public override Task<IReadOnlyList<Comment>> GetCommentsAsync (CancellationToken cancellationToken = default(CancellationToken)) { - return null; + return Task.FromResult<IReadOnlyList<Comment>> (comments); } - - public virtual IUnresolvedTypeDefinition GetInnermostTypeDefinition (TextLocation location) + + List<Tag> tagComments = new List<Tag> (); + + public void Add (Tag tagComment) { - return null; + tagComments.Add (tagComment); } - public virtual IUnresolvedMember GetMember (TextLocation location) + public void AddRange (IEnumerable<Tag> tagComments) { - return null; + this.tagComments.AddRange (tagComments); } - public virtual IList<IUnresolvedTypeDefinition> TopLevelTypeDefinitions { - get { - return new List<IUnresolvedTypeDefinition> (); - } + public override Task<IReadOnlyList<Tag>> GetTagCommentsAsync (CancellationToken cancellationToken = default(CancellationToken)) + { + return Task.FromResult<IReadOnlyList<Tag>> (tagComments); } - #endregion + List<FoldingRegion> foldingRegions = new List<FoldingRegion> (); - public Func<MonoDevelop.Ide.Gui.Document, CancellationToken, IRefactoringContext> CreateRefactoringContext; - public Func<TextEditorData, object, CancellationToken, IRefactoringContext> CreateRefactoringContextWithEditor; - } - - public class DefaultParsedDocument : ParsedDocument, IUnresolvedFile - { - - public override IUnresolvedFile ParsedFile { - get { return this; } - } - - List<Error> errors = new List<Error> (); - - public override IList<Error> Errors { - get { - return errors; - } - } - - public DefaultParsedDocument (string fileName) : base (fileName) + public void Add (FoldingRegion foldingRegion) { - Flags |= ParsedDocumentFlags.NonSerializable; - } - - #region IUnresolvedFile implementation - public override IUnresolvedTypeDefinition GetTopLevelTypeDefinition(TextLocation location) - { - return TopLevelTypeDefinitions.FirstOrDefault (t => t.Region.IsInside (location)); + foldingRegions.Add (foldingRegion); } - - public override IUnresolvedTypeDefinition GetInnermostTypeDefinition(TextLocation location) + + public void AddRange (IEnumerable<FoldingRegion> foldingRegions) { - IUnresolvedTypeDefinition parent = null; - var type = GetTopLevelTypeDefinition(location); - while (type != null) { - parent = type; - type = parent.NestedTypes.FirstOrDefault (t => t.Region.IsInside (location)); - } - return parent; + this.foldingRegions.AddRange (foldingRegions); } - - public override IUnresolvedMember GetMember(TextLocation location) + + public override Task<IReadOnlyList<FoldingRegion>> GetFoldingsAsync (CancellationToken cancellationToken = default(CancellationToken)) { - var type = GetInnermostTypeDefinition(location); - if (type == null) - return null; - return type.Members.FirstOrDefault (e => e.Region.IsInside(location)); - } - - List<IUnresolvedTypeDefinition> types = new List<IUnresolvedTypeDefinition> (); - public override IList<IUnresolvedTypeDefinition> TopLevelTypeDefinitions { - get { - return types; - } - } - - List<IUnresolvedAttribute> attributes = new List<IUnresolvedAttribute> (); - public IList<IUnresolvedAttribute> AssemblyAttributes { - get { - return attributes; - } + return Task.FromResult<IReadOnlyList<FoldingRegion>> (foldingRegions); } - public IList<IUnresolvedAttribute> ModuleAttributes { - get { - return new List<IUnresolvedAttribute> (); - } - } - #endregion - + List<Error> errors = new List<Error> (); + public void Add (Error error) { errors.Add (error); } - - public void Add (IEnumerable<Error> errors) - { - this.errors.AddRange (errors); - } - - #region IUnresolvedFile implementation - DateTime? IUnresolvedFile.LastWriteTime { - get { - return LastWriteTimeUtc; - } - set { - LastWriteTimeUtc = value.HasValue ? value.Value : DateTime.UtcNow; - } - } - #endregion - } - - [Serializable] - public class ParsedDocumentDecorator : ParsedDocument - { - IUnresolvedFile parsedFile; - - public override IUnresolvedFile ParsedFile { - get { return parsedFile; } - set { parsedFile = value; FileName = parsedFile.FileName; } - } - - public override IList<Error> Errors { - get { - return parsedFile.Errors; - } - } - - public ParsedDocumentDecorator (IUnresolvedFile parsedFile) : base (parsedFile.FileName) - { - this.parsedFile = parsedFile; - } - - public ParsedDocumentDecorator () : base ("") - { - } - - #region IUnresolvedFile implementation - public override IUnresolvedTypeDefinition GetTopLevelTypeDefinition (TextLocation location) - { - return parsedFile.GetTopLevelTypeDefinition (location); - } - public override IUnresolvedTypeDefinition GetInnermostTypeDefinition (TextLocation location) + public void AddRange (IEnumerable<Error> errors) { - return parsedFile.GetInnermostTypeDefinition (location); + this.errors.AddRange (errors); } - public override IUnresolvedMember GetMember (TextLocation location) + public override Task<IReadOnlyList<Error>> GetErrorsAsync (CancellationToken cancellationToken = default(CancellationToken)) { - return parsedFile.GetMember (location); - } - - public override IList<IUnresolvedTypeDefinition> TopLevelTypeDefinitions { - get { - return parsedFile.TopLevelTypeDefinitions; - } + return Task.FromResult<IReadOnlyList<Error>> (errors); } - #endregion } - + public static class FoldingUtilities { static bool IncompleteOrSingleLine (DomRegion region) @@ -399,7 +203,7 @@ namespace MonoDevelop.Ide.TypeSystem return region.BeginLine <= 0 || region.EndLine <= region.BeginLine; } - public static IEnumerable<FoldingRegion> ToFolds (this IList<Comment> comments) + public static IEnumerable<FoldingRegion> ToFolds (this IReadOnlyList<Comment> comments) { for (int i = 0; i < comments.Count; i++) { Comment comment = comments [i]; @@ -449,7 +253,7 @@ namespace MonoDevelop.Ide.TypeSystem curLine = curComment.Region.BeginLine; } - if (j - i > 1) { + if (j - i > 1 || (comment.IsDocumentation && comment.Region.BeginLine < comment.Region.EndLine)) { string txt; if (comment.IsDocumentation) { txt = "/// ..."; @@ -483,7 +287,7 @@ namespace MonoDevelop.Ide.TypeSystem } yield return new FoldingRegion (txt, - new DomRegion (comment.Region.Begin, end), + new DocumentRegion (comment.Region.Begin, end), FoldType.Comment); i = j - 1; } @@ -550,6 +354,23 @@ namespace MonoDevelop.Ide.TypeSystem } return false; } + + static bool IsInsideMember (this DocumentRegion region, IUnresolvedTypeDefinition cl) + { + if (region.IsEmpty || cl == null || !cl.BodyRegion.IsInside (region.Begin.Line, region.Begin.Column)) + return false; + foreach (var member in cl.Members) { + if (member.BodyRegion.IsEmpty) + continue; + if (member.BodyRegion.IsInside (region.Begin.Line, region.Begin.Column) && member.BodyRegion.IsInside (region.End.Line, region.End.Column)) + return true; + } + foreach (var inner in cl.NestedTypes) { + if (region.IsInsideMember (inner)) + return true; + } + return false; + } } } |