Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

Roslyn CTP: Three Introductory Projects

Rate me:
Please Sign up or sign in to vote.
4.92/5 (21 votes)
8 May 2012CPOL18 min read 75.7K   1K   42  
An introduction to the Roslyn CTP
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Services.Editor;
using Roslyn.Services;
using Roslyn.Compilers.CSharp;
using Roslyn.Compilers;
using RoslynCTPLibrary.Attributes;
using RoslynCTPLibrary.Extensions;
using Roslyn.Compilers.Common;
using System.Threading;

namespace EventImplementer {
    [ExportSyntaxNodeOutliner(typeof(PropertyDeclarationSyntax))]
    class Outliner : ISyntaxOutliner {
        public IEnumerable<OutliningSpan> GetOutliningSpans(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken) {
            var property = (PropertyDeclarationSyntax)node;

            if (!property.Attributes.Any(ad => ad.Attributes.Any(a => a.Name.PlainName == typeof(FiresEventOnChange).Name || a.Name.PlainName == typeof(FiresEventOnQuery).Name))) yield break;

            var field = property.GetOwnerType().Members.OfType<FieldDeclarationSyntax>().FirstOrDefault(f => Constants.FieldPrefix + property.Identifier.ValueText == f.Declaration.Variables.First().Identifier.ValueText);
            if (field == null) yield break;

            var whiteSpace = "";
            foreach (var ws in property.GetLeadingTrivia().Where(t => t.Kind == SyntaxKind.WhitespaceTrivia))
                for (int i = 0; i < ws.Width; i++)
                    whiteSpace += " ";

            yield return new OutliningSpan(TextSpan.FromBounds(property.Identifier.Span.End + 2, field.Span.End), whiteSpace + "{ get; set; }", true);
        }

        #region Unimplemented ISyntaxOutliner members

        public IEnumerable<OutliningSpan> GetOutliningSpans(IDocument document, CommonSyntaxTrivia trivia, CancellationToken cancellationToken) {
            throw new NotImplementedException();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Hungary Hungary
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions