ROSLYN

.NET Compiler Platform

Hi. I'm ns

Links

Roslyn

  • compiler/scripting API
  • syntax analysis & transformation API
  • semantic analysis API
  • workspace API

You can use it for

  • Source explorers (like source.roslyn.io) or compilers

  • Diagnostics and refactorings

  • Style checking tools

  • Project-specific solution wide analysis and refactorings (when regexps doesn't helps)

  • Integration to editors (like omnisharp.net)

  • Live coding (like comealive.io)

  • Or smth crazy

But here are problems

  • fucking ugly documentation. It's not like tutorials about MVC 4 or Rails 
  • a lot of nice stuff isn't covered with documentation or tutorials
  • a lot of good tutorials doesn't exists or are deprecated
  • source code of roslyn & existing tools is your best documentation

And here are more problems

  • API IS CODE GENERATED. 
  • API is focused on speed & thread safety, so some things are hard to code

  • AST of C# language is very complex and monsterous. 
  • some API aren't language-specific (e.g. should support both VB and C#) 
  • hard debugging

And that's not all

  • it's more easier to concatenate strings and parse them than use syntax creating API

  • it's more easier to use regexes to perform easy solution-wide refactorings

But it's a very nice and powerful tool, ok?

You should believe me, it's cool ;-)

Why it's cool?

  • It works
  • It works fast
  • You can do amazing things
  • Documentation and tutorials aren't so ugly as I described ;-)

Key stuff

  • Document consists of some metadata & SyntaxTree
  • SyntaxNodes are mapped directly to text
  • Each SyntaxNode is immutable
  • Nodes can be rewritten and transformed
  • You can create SyntaxNode with SyntaxFactory

Key stuff

  • Each node contains both AST and whitespace-trash

  • Whitespace-trash is important too :-)
  • But you can ask Roslyn to automatically reformart code

Key stuff

  • Document also has a concept of SemanticModel

  • SemanticModel is IntelliSense API

  • Some methods of SemanticModel has speculative overloads

Key stuff

There a lot of nice classes providing abstractions over SyntaxTrees & SemanticModels 

Let's start

  • Visual Studio 2015
  • .NET Compiler Platform SDK 
  • NuGet packages Microsoft.CodeAnalysis.*

T : CodeRefactoringProvider

  • It's called only when user press Ctrl+.
  • When provider is called it's got a span of user text
  • Then your code can think if it can provide something for user
  • Visual Studio tries to show & apply changes (if user chooses your CodeAction)

T : DiagnosticAnalyzer

  • On init you should register event handlers
  • Event handlers can be called twice or more for the same part of code
  • In event handler you can report that code has problems
  • Reporting doesn't means fixing
  • Yes, you can just say 'you code is bullshit' :-)

T : CodeFixProvider

  • Subscribes to specified diagnostics
  • Still called when user press Ctrl+.
  • But supports bulk codefixes
  • Can get some data from diagnostic provider

Console analyzers

Subtitle

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;

var workspace = MSBuildWorkspace.Create();
var solutionInfo = SolutionInfo.Create(
    SolutionId.CreateNewId(), 
    VersionStamp.Create(), 
    slnPath);
var solution = await workspace
    .OpenSolutionAsync(slnPath);
var documents = solution.Projects
    .SelectMany(p => p.Documents);

Useful API 

SyntaxFactory

Low level creation of SyntaxNodes

Microsoft.CodeAnalysis.CSharp

SyntaxGenerator

Microsoft.CodeAnalysis.Editing

Language-agnostic high-level beautiful API

DocumentEditor

Provides imperative-style for manipulating documents.  

Microsoft.CodeAnalysis.Editing

And another imperative stuff

  • SyntaxEditor

  • SymbolEditor

  • SolutionEditor

SymbolFinder

Microsoft.CodeAnalysis.FindSymbols

Extensions of

Semantic Model

Renamer

Microsoft.CodeAnalysis.Rename

Formatter

Microsoft.CodeAnalysis.Formatting

Simplifier

Microsoft.CodeAnalysis.Simplification

WarningAnnotation

Microsoft.CodeAnalysis.CodeActions

ConflictAnnotation

RenameAnnotation

Classifier

Microsoft.CodeAnalysis.Classification

Recommender

Microsoft.CodeAnalysis.FindSymbols

C#-specific autocomplete

Questions?

Roslyn

By Viktor Lova