Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / XML

Line Counter - Writing a SharpDevelop Add-In

Rate me:
Please Sign up or sign in to vote.
4.90/5 (15 votes)
18 Jul 2006LGPL313 min read 83K   1.3K   69  
This article shows you how to start writing SharpDevelop add-ins, by porting Jon Rista's VS add-in to SharpDevelop.
/*
 * Created by SharpDevelop.
 * User: Daniel Grunwald
 * Date: 18.07.2006
 * Time: 17:08
 */

using System;
using ICSharpCode.Core;

namespace LineCounterAddin
{
	// Improvement 2
	
	public interface ICountingAlgorithm
	{
		void CountLines(LineCountInfo info);
	}

	public class CountingAlgorithmGeneric : ICountingAlgorithm {
		public void CountLines(LineCountInfo info) {
			LineCounterBrowser.CountLinesGeneric(info);
		}
	}
	public class CountingAlgorithmCStyle : ICountingAlgorithm {
		public void CountLines(LineCountInfo info) {
			LineCounterBrowser.CountLinesCStyle(info);
		}
	}
	public class CountingAlgorithmVBStyle : ICountingAlgorithm {
		public void CountLines(LineCountInfo info) {
			LineCounterBrowser.CountLinesVBStyle(info);
		}
	}
	public class CountingAlgorithmXmlStyle : ICountingAlgorithm {
		public void CountLines(LineCountInfo info) {
			LineCounterBrowser.CountLinesXMLStyle(info);
		}
	}

	public class CountingAlgorithmDoozer : IDoozer
	{
		public bool HandleConditions {
			get {
				// our doozer cannot handle conditions, let SharpDevelop
				// do that for us
				return false;
			}
		}
		
		public object BuildItem(object caller, Codon codon, System.Collections.ArrayList subItems)
		{
			return new CountingAlgorithmDescriptor(codon.AddIn,
			                                       codon.Properties["extensions"],
			                                       codon.Properties["class"]);
		}
	}

	public class CountingAlgorithmDescriptor
	{
		AddIn addIn;
		internal string[] extensions;
		string className;
		
		public CountingAlgorithmDescriptor(AddIn addIn, string extensions, string className)
		{
			this.addIn = addIn;
			this.extensions = extensions.ToLowerInvariant().Split(';');
			this.className = className;
		}
		
		public bool CanCountLines(LineCountInfo info)
		{
			return (Array.IndexOf(extensions, info.FileType.ToLowerInvariant()) >= 0);
		}
		
		ICountingAlgorithm cachedAlgorithm;
		
		public ICountingAlgorithm GetAlgorithm()
		{
			if (cachedAlgorithm == null) {
				cachedAlgorithm = (ICountingAlgorithm)addIn.CreateObject(className);
			}
			return cachedAlgorithm;
		}
	}
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Germany Germany
I am the lead developer on the SharpDevelop open source project.

Comments and Discussions