Wednesday, November 13, 2013

How to make Xamarin Studio sharper

One of the most demanded features of Xamarin Studio/MonoDevelop is static source analysis like ReSharper (if you're a VS.NET user and haven't heard about that tool - just buy it - you won't regret that decision).

What most Xamarin Studio users don't know is that they already have static code analysis. It is possible to get the same development experience in Xamarin Studio as well without buying any 3rd party addin. We had that feature since some releases but it's shaping up quite well now. With the latest 4.2 release it's really worth turning it on. You just need to:
  • Get the latest Xamarin Studio (or MonoDevelop) release - newer is always better
  • Enable background source analysis 
Not all users are familiar with source analysis is and how it works. Once used to that you'll never be able to program without it - it's almost as helpful as having code completion.
Background source analysis is like pair programming. That means that that two programmers work at one computer. And while the you write code the other will observe what you write any review each line you type.
The IDE takes the part of the observer and reviews the written source code on the fly (but without eating your pizza).
That's a big deal - it's possible to track many problems the compiler doesn't find. Xamarin Studio looks over your code and helps to eliminate 'bad' code before it becomes a problem. It will reduce development time dramatically and shows problems before compilation. And it'll make your code look really professional by giving good hints.

Enable it through the IDE options:

Now after that notice that the scroll bar has been replaced with a new control:

If Xamarin Studio finds a possible issue in your code you can see it in that control. And directly jump to the location (or you can use the button at the top of the control - this button quickly shows the status of the file - if there are any errors/warnings etc. - a click on that button takes you to the most urgent issue in that file).
The issues are marked in the text editor as well. Most issues are underlined - but there are other markers for example redundant code is grayed out:

Generally code issues in Xamarin Studio provide a resolution to fix them. Even for most common errors fixes are available. That's dramatically reducing development time.

A little marker appears when the caret is at the marked code. This indicates that a refactoring action is available - either press alt+enter to open the context sensitive refactoring menu or look in the context menu to perform the action.
Action marker over a code issue

But the IDE does even more: Quick fixes

If you're not at a code issue that marker appears for various constructs - it shows that refactoring actions are available which let you easily change code without typing.

There are hundreds of code actions and code issues available which make the daily work with Xamarin Studio a charm.

Thursday, September 5, 2013

How I set up MonoDevelop on Linux

Mono and Linux is a complex story - currently the fact is that no mainstream Linux distribution gives you the most recent .NET development tools (yes I know about arch - I can recommend using arch or manjaro if you want 'the easy way'). Most of us Linux users can be happy if your Linux distribution has a mono run time from this decade.

MonoDevelop runs on Linux quite well (in case you use a light theme - see MonoDevelop 4.0 and dark themes). Many developers from Xamarin are Linux users and run MonoDevelop on Linux every day (but without a dark theme :)).

It is possible to get a very good looking monodevelop environment on Linux as well. I'll try to explain how I do it and show a screenshot as 'proof of concept' :)

Parallel Mono Environment

To get the latest mono you'll need to build a parallel mono environment. This is far easier than it sounds and wont affect the mono your distribution has. (But you need to install a mono in your distribution for building a mono from the repositories)

First you need a script for 'switching' to the parallel environment.  I use this script for doing it:

export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig
export MONO_TLS_PROVIDER=legacy


The best way is to build mono, gtk# (gtk-sharp-2-12-branch) and gnome-sharp from the sources found on github(I hope you're familiar with git):

For building mono & gtk-sharp your distribution needs some prerequisites - either you follow what the configure/bootstrap scripts tell you or try something like sudo apt-get build-dep monodevelop (Note: On current ubuntu/monodevelop you'll need more packages like libtool but the build scripts will tell you what's needed).
You need to run the configure/bootstrap scripts with the option --prefix=/opt/mono - and then it installs 'as usual' with make/make install.

Building MonoDevelop

Get monodevelop from then switch to your parallel mono environment and type inside the monodevelop directory: ./configure ; make

And just run it with "make run" - that's all.

Some tips: Adjust the fonts (I use Source Code Pro for the text editor and Source Sans Pro almost everywhere else).

Get the Xamarin gtk theme from (you may need to set the prefix here to --prefix=/usr) and enable it in Environment->Language.

The end result should like this:

Ok I agree that there is a drawing error in the tab bar - but that's due a graphic device driver bug (it should render a horizontal linear gradient - chrome tabs suffer from the same bug on my machine).


It's hard to get a .NET environment running on Linux these days - but it's doable :). Btw. we could need help improving the monodevelop Linux client - esp. dark theme support is lacking a bit :)

For the Lazy

Lately I've been reinstalling Ubuntu 17.10 and I did following (may be helpful):

sudo apt-get install autoconf libtool-bin libglade2-dev libgnomeui-dev cmake libssh-dev libssh2-1* nuget mono-devel git intltool curl

git clone
cd mono
./ --prefix=/opt/mono
make get-monolite-latest
make; sudo make install; cd ..

git clone --branch gtk-sharp-2-12-branch
cd gtk-sharp
./bootstrap-2.12 --prefix=/opt/mono
make; sudo make install; cd ..

git clone
cd gnome-sharp
# needed to edit Settings.cs atom ./sample/gconf/Settings.cs
./bootstrap-2.24 --prefix=/opt/mono
make; sudo make install; cd ..

git clone
cd xamarin-gtk-theme
 ./ --prefix=/usr
make; sudo make install; cd ..

# Building monodevelop (git clone may be different for you)
git clone 
cd monodevelop
make run 

# This is broken:
git clone
cd msbuild
git checkout -b xplat-master origin/xplat-master
./ --scope Compile --target Mono --host Mono

# Alternative - get an os x box grab msbuild from there and install and install it # in opt/mono/lib/mono/msbuild/15.0/bin/MSBuild.dll

sudo apt-key adv --keyserver hkp:// --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb xenial main" | sudo tee /etc/apt/sources.list.d/mono-official.list

sudo apt-get update
sudo apt-get install msbuild
sudo cp -R /usr/lib/mono/msbuild/15.0 /opt/mono/lib/mono/msbuild
sudo cp /usr/bin/msbuild /opt/mono/bin

sudo mozroots --import --machine --sync
sudo certmgr -ssl -m
sudo certmgr -ssl -m
sudo certmgr -ssl -m

git clone
cd fsharp
./ --prefix=/opt/mono
make; sudo make install; cd ..

# install PCL
# go to windows C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable
# the portable libraries should end up in /opt/mono/lib/mono/xbuild-frameworks/.NETPortable
# for me it looks like:
# /opt/mono/lib/mono/xbuild-frameworks/.NETPortable$ ls
# v4.0  v4.5  v4.6

# Building monodevelop (git clone may be different for you)
git clone
cd monodevelop
make run

Friday, August 2, 2013

Unit test text editor integration

Writing unit tests should be easy and fun.
What bugs me about writing tests in Xamarin Studio is that testing requires a context switch.

I write my test and then I need to compile the test project and trying to search the test in the unit test pad (which doesn't support search - but that's another story :)). 

This disturbs my workflow.

What I really want is to be able to just run the test I'm working on. I don't need to run the full suite (that's what continuos integration is for) - just the current test. 

I've been working on the solution: Unit test text editor integration. 

This feature is not finished but it's already in the public repositories and I think Xamarin Studio 4.2 will contain it. It's a bit hidden in the options - but once it's activated it makes life much easier: 


 After it's active a new margin appears in the text editor: 

This margin parses the current file and marks all unit tests. 
Clicking on one of these icons let the user choose to Run or Debug the tests. No write->build->search test in pad->run test->change test->repeat cycle anymore - just write->run test->repeat. This feature has made my life much more easier. See it in action:  Demo video

Saturday, March 30, 2013

Code formatting with NRefactory

NRefactory is a library for parsing and analyzing C# source code hosted at GitHub. It is a bit like Roslyn from Microsoft, but fully open source and free to use (MIT X11). It's possible to parse and analyze individual C# files or whole projects. Mainly it was developed for IDEs therefore it has many high level features suited for development environments like type semantic highlighting, refactorings or code completion.
It provides almost everything needed for working with C# code.

NRefactory get's used in SharpDevelop 5, MonoDevelop and Xamarin Studio for providing high level C# features. Even VIM got a C# plugin providing high level C# features: Omnisharp. I've seen many uses of the library in various non IDE releated projects like the Saltarelle C#->JavaScript compiler.

But I've never seen anyone using the C# formatter from nrefactory. The formatter provides rich formatting features and is very easy to use. Let's take an example.
Today I wrote a piece of code using a text editor:

As you see there is a space after the 'null' that shouldn't be there. I could've just removed the space and continue my work - but what would happen if I do it again ? Wouldn't it be nice if it could be auto formatted like in a IDE. Just make the code format itself!

First step - make the program print itself. For that I added some code around the if that repeats the program and prints it:

Now running the program on the console gives:

The first step is done. Now we need to call the formatter. The project needs to reference NRefactory + NRefactory.CSharp and a using to the ICSharpCode.NRefactory.CSharp namespace needs to be added. The WriteLine call should use the CSharpFormatter to print a formatted version of itself:

Now running this will correctly format the if statement:

The self formatting source code - easy and without any external IDE.
The wrong space is finally gone. But the formatter has done something with the brackets. That's because of the options I gave to the formatter.
The C# formatter needs an options object that specifies how the C# code should be formatted. The CSharpFormattingOptions class provides a large set of very fine grained formatting options. The factory gives easy access to the most common indenting styles (see wikipedia). The Allman style is used in Visual Studio .NET which puts all braces on it's own line. That's why the formatter has altered the if brackets.
That's just one way to call the formatter. It was built for in IDE use.
The formatter can generate the minimum change set of replace operations required to do the formatting. Furthermore it's possible to format only a portion of the text inside a document. In monodevelop the on the fly formatter uses this functionality to provide formatting.
The formatting engine in NRefactory is mighty and easy to use - let me know if you find uses for it or have improvements.

Monday, February 25, 2013

Upcoming color importer

Many people requested it and in the next Version of Xamarin Studio it will become a reality:

The Visual Studio color importer !

Defining own color schemes is very painful it is much easier to use a pre defined scheme from Studio Styles. Now it is easy to do as well importing a visual studio color scheme. Now finally what once has been an April's fool joke can now become a reality:

You surely want such a pretty user interface for the IDE yourself and ask how to achieve this. It's easy to do see the little demo video I made to demonstrate how to import a color scheme (recorded on my linux desktop):

This feature is already pushed to the monodevelop git repository. In case you want to try it out you have to built MonoDevelop from the source. The Xamarin Studio users need to wait a bit longer we release a new version when it's ready.

We import all C#/HTML/XML/ASP.NET colors as well as some colors not defined by Studio Styles. It required some changes to our color scheme system which means that old scheme files are broken again. But I promise that it will be more stable in the future. The new format supports versioning and it's easier to extend it with new colors. The new color scheme system uses json to store the color schemes. Newer versions will be able to load old style files without breaking.

Visual Studio .NET schemes are imported on the fly and remain in the VS.NET format. This way improvements to the importer engine directly benefit previous imported color schemes. However when the user changes them in our scheme editor the format will get converted.

I would appreciate if I would get some feedback about this feature. I want it as perfect as possible.