// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.IO;
using ICSharpCode.Core;
namespace ICSharpCode.SharpDevelop.Editor
{
///
/// Allows add-ins to attach language/project-specific features to ITextEditor (one instance per ITextEditor is created).
///
public interface ITextEditorExtension
{
///
/// Callback function for backend bindings to add services to ITextEditor.
/// This is called when the file name of an ITextEditor changes.
///
void Attach(ITextEditor editor);
///
/// Callback function for backend bindings to remove all added services from ITextEditor.
/// This is called when the file name of an ITextEditor changes, to unload all added
/// features properly.
///
void Detach();
}
///
/// Creates ITextEditorExtension objects for the code editor.
///
///
/// Semicolon-separated list of file extensions that are handled by the text editor extension (e.g. .xaml)
///
///
/// Name of the ITextEditorExtension class.
///
/// Only in /SharpDevelop/ViewContent/TextEditor/Extensions
///
/// The ILanguageBinding object.
///
public class TextEditorExtensionDoozer : IDoozer
{
public bool HandleConditions {
get {
return false;
}
}
public object BuildItem(BuildItemArgs args)
{
ITextEditor editor = (ITextEditor)args.Parameter;
string[] extensions = args.Codon["extensions"].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (CanAttach(extensions, editor.FileName)) {
return args.AddIn.CreateObject(args.Codon["class"]);
} else {
return null;
}
}
static bool CanAttach(string[] extensions, string fileName)
{
// always attach when no extensions were given
if (extensions.Length == 0)
return true;
if (string.IsNullOrEmpty(fileName))
return false;
string fileExtension = Path.GetExtension(fileName);
foreach (string ext in extensions) {
if (string.Equals(ext, fileExtension, StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
}
}