Permalink
Browse files

Allow service registration using the AddInTree.

  • Loading branch information...
1 parent 46a82c7 commit c559da33362e7e92d4c1128388b35efb192df7f0 @dgrunwald dgrunwald committed Mar 24, 2012
@@ -9,6 +9,7 @@
<Runtime>
<Import assembly = ":ICSharpCode.AvalonEdit"/>
+ <Import assembly = ":ICSharpCode.SharpDevelop"/>
<Import assembly = "ICSharpCode.AvalonEdit.AddIn.dll">
<Doozer name="SyntaxMode" class="ICSharpCode.AvalonEdit.AddIn.SyntaxModeDoozer"/>
<Doozer name="ContextActionOptionPanel" class="ICSharpCode.AvalonEdit.AddIn.ContextActions.ContextActionOptionPanelDoozer"/>
@@ -29,8 +30,9 @@
<Include id="DefaultEditor" path="/SharpDevelop/ViewContent/TextEditor/ContextMenu"/>
</Path>
- <Path name = "/SharpDevelop/ViewContent/TextEditor/EditorControlService">
- <Class id="CodeEditorView" class="ICSharpCode.AvalonEdit.AddIn.AvalonEditorControlService"/>
+ <Path name = "/SharpDevelop/Services">
+ <Service id="ICSharpCode.SharpDevelop.Editor.IEditorControlService"
+ class="ICSharpCode.AvalonEdit.AddIn.AvalonEditorControlService"/>
</Path>
<!--
@@ -56,7 +56,7 @@ protected override object CreateText()
LoggingService.Debug("Creating text for search result (" + location.Line + ", " + location.Column + ") ");
TextBlock textBlock = new TextBlock();
- textBlock.FontFamily = new FontFamily(EditorControlService.GlobalOptions.FontFamily);
+ textBlock.FontFamily = new FontFamily(SD.EditorControlService.GlobalOptions.FontFamily);
textBlock.Inlines.Add("(" + location.Line + ", " + location.Column + ")\t");
@@ -41,7 +41,7 @@ public AvalonEditTextEditorAdapter(TextEditor textEditor)
public static TextEditor CreateAvalonEditInstance()
{
object editor;
- EditorControlService.CreateEditor(out editor);
+ SD.EditorControlService.CreateEditor(out editor);
if (!(editor is TextEditor))
throw new NotSupportedException("Expected text editor to be AvalonEdit");
return (TextEditor)editor;
@@ -3,93 +3,65 @@
using System;
using ICSharpCode.AvalonEdit;
+using ICSharpCode.Core;
+using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
namespace ICSharpCode.SharpDevelop.Editor
{
/// <summary>
/// Allows creating new text editor instances and accessing the default text editor options.
/// </summary>
+ [FallbackService(typeof(EditorControlServiceFallback))]
public interface IEditorControlService
{
ITextEditor CreateEditor(out object control);
ITextEditorOptions GlobalOptions { get; }
}
- /// <summary>
- /// Allows creating new text editor instances and accessing the default text editor options.
- /// </summary>
- public static class EditorControlService
+ // Fallback if AvalonEdit.AddIn is not available (e.g. some unit tests)
+ sealed class EditorControlServiceFallback : IEditorControlService, ITextEditorOptions
{
- static readonly Lazy<IEditorControlService> instance = new Lazy<IEditorControlService>(
- delegate {
- // fetch IEditorControlService that's normally implemented in AvalonEdit.AddIn
- var node = Core.AddInTree.GetTreeNode("/SharpDevelop/ViewContent/TextEditor/EditorControlService", false);
- IEditorControlService ecs = null;
- if (node != null && node.Codons.Count > 0) {
- ecs = (IEditorControlService)node.BuildChildItem(node.Codons[0], null);
- }
- return ecs ?? new DummyService();
- }
- );
-
- public static IEditorControlService Instance {
- get { return instance.Value; }
+ public ITextEditorOptions GlobalOptions {
+ get { return this; }
}
- public static ITextEditor CreateEditor(out object control)
+ public ITextEditor CreateEditor(out object control)
{
- return Instance.CreateEditor(out control);
+ TextEditor avalonedit = new TextEditor();
+ control = avalonedit;
+ return new AvalonEditTextEditorAdapter(avalonedit);
}
- public static ITextEditorOptions GlobalOptions {
- get { return Instance.GlobalOptions; }
+ public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged { add {} remove {} }
+
+ public string IndentationString {
+ get { return "\t"; }
}
- // Fallback if AvalonEdit.AddIn is not available (e.g. some unit tests)
- sealed class DummyService : IEditorControlService, ITextEditorOptions
- {
- public ITextEditorOptions GlobalOptions {
- get { return this; }
- }
-
- public ITextEditor CreateEditor(out object control)
- {
- TextEditor avalonedit = new TextEditor();
- control = avalonedit;
- return new AvalonEditTextEditorAdapter(avalonedit);
- }
-
- public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged { add {} remove {} }
-
- public string IndentationString {
- get { return "\t"; }
- }
-
- public bool AutoInsertBlockEnd {
- get { return true; }
- }
-
- public bool ConvertTabsToSpaces {
- get { return false; }
- }
-
- public int IndentationSize {
- get { return 4; }
- }
-
- public int VerticalRulerColumn {
- get { return 120; }
- }
-
- public bool UnderlineErrors {
- get { return true; }
- }
-
- public string FontFamily {
- get {
- return "Consolas";
- }
+ public bool AutoInsertBlockEnd {
+ get { return true; }
+ }
+
+ public bool ConvertTabsToSpaces {
+ get { return false; }
+ }
+
+ public int IndentationSize {
+ get { return 4; }
+ }
+
+ public int VerticalRulerColumn {
+ get { return 120; }
+ }
+
+ public bool UnderlineErrors {
+ get { return true; }
+ }
+
+ public string FontFamily {
+ get {
+ return "Consolas";
}
}
}
@@ -374,8 +374,8 @@ public void SaveFile(FileDescriptionTemplate newfile, string content, string bin
string parsedContent = StringParser.Parse(StringParser.Parse(content));
if (parsedContent != null) {
- if (EditorControlService.GlobalOptions.IndentationString != "\t") {
- parsedContent = parsedContent.Replace("\t", EditorControlService.GlobalOptions.IndentationString);
+ if (SD.EditorControlService.GlobalOptions.IndentationString != "\t") {
+ parsedContent = parsedContent.Replace("\t", SD.EditorControlService.GlobalOptions.IndentationString);
}
}
@@ -131,7 +131,7 @@ void CreateManifest()
defaultManifest = r.ReadToEnd();
}
}
- defaultManifest = defaultManifest.Replace("\t", EditorControlService.GlobalOptions.IndentationString);
+ defaultManifest = defaultManifest.Replace("\t", SD.EditorControlService.GlobalOptions.IndentationString);
File.WriteAllText(manifestFile, defaultManifest, System.Text.Encoding.UTF8);
FileService.FireFileCreated(manifestFile, false);
}
@@ -278,7 +278,7 @@ public ConsoleControl()
object tmp;
- this.editorAdapter = EditorControlService.CreateEditor(out tmp);
+ this.editorAdapter = SD.EditorControlService.CreateEditor(out tmp);
this.editor = (AvalonEdit.TextEditor)tmp;
this.editor.SetValue(Grid.ColumnProperty, 0);
@@ -86,9 +86,9 @@ void OnParserUpdateStep(object sender, ParserUpdateStepEventArgs e)
Task<ResolveResult> ResolveAtCaretAsync(ParserUpdateStepEventArgs e)
{
IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow;
- if (window == null) return null;
+ if (window == null) return Task.FromResult<ResolveResult>(null);
ITextEditorProvider provider = window.ActiveViewContent as ITextEditorProvider;
- if (provider == null) return null;
+ if (provider == null) return Task.FromResult<ResolveResult>(null);
ITextEditor editor = provider.TextEditor;
// e might be null when this is a manually triggered update
@@ -375,8 +375,8 @@ public IProject CreateProject(ProjectCreateInformation projectCreateInformation,
StreamWriter sr = new StreamWriter(File.Create(fileName), ParserService.DefaultFileEncoding);
string fileContent = StringParser.Parse(file.Content, new StringTagPair("ProjectName", projectCreateInformation.ProjectName), new StringTagPair("FileName", fileName));
fileContent = StringParser.Parse(fileContent);
- if (EditorControlService.GlobalOptions.IndentationString != "\t") {
- fileContent = fileContent.Replace("\t", EditorControlService.GlobalOptions.IndentationString);
+ if (SD.EditorControlService.GlobalOptions.IndentationString != "\t") {
+ fileContent = fileContent.Replace("\t", SD.EditorControlService.GlobalOptions.IndentationString);
}
sr.Write(fileContent);
sr.Close();
@@ -596,7 +596,7 @@ public virtual void GenerateCodeFromCodeDom(System.CodeDom.CodeCompileUnit compi
options.BlankLinesBetweenMembers = AmbienceService.CodeGenerationProperties.Get("BlankLinesBetweenMembers", true);
options.BracingStyle = AmbienceService.CodeGenerationProperties.Get("StartBlockOnSameLine", true) ? "Block" : "C";
options.ElseOnClosing = AmbienceService.CodeGenerationProperties.Get("ElseOnClosing", true);
- options.IndentString = ICSharpCode.SharpDevelop.Editor.EditorControlService.GlobalOptions.IndentationString;
+ options.IndentString = SD.EditorControlService.GlobalOptions.IndentationString;
provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
} else {
writer.WriteLine("No CodeDom provider was found for this language.");
@@ -6,6 +6,7 @@
using ICSharpCode.Core;
using ICSharpCode.Core.Implementation;
using ICSharpCode.Core.Services;
+using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Gui;
namespace ICSharpCode.SharpDevelop
@@ -32,9 +33,7 @@ public static class SD
/// </summary>
public static void InitializeForUnitTests()
{
- var container = new ThreadSafeServiceContainer();
- container.AddService(typeof(ILoggingService), new TextWriterLoggingService(new TraceTextWriter()));
- container.AddService(typeof(IMessageService), new TextWriterMessageService(Console.Out));
+ var container = new ThreadSafeServiceContainer(ServiceSingleton.FallbackServiceProvider);
PropertyService.InitializeServiceForUnitTests();
ServiceSingleton.ServiceProvider = container;
}
@@ -76,5 +75,9 @@ public static void InitializeForUnitTests()
public static IMessageService MessageService {
get { return GetRequiredService<IMessageService>(); }
}
+
+ public static IEditorControlService EditorControlService {
+ get { return GetRequiredService<IEditorControlService>(); }
+ }
}
}
@@ -519,9 +519,9 @@ public static XElement FormatXml(this XElement element, int indentationLevel)
{
StringWriter sw = new StringWriter();
using (XmlTextWriter xmlW = new XmlTextWriter(sw)) {
- if (EditorControlService.GlobalOptions.ConvertTabsToSpaces) {
+ if (SD.EditorControlService.GlobalOptions.ConvertTabsToSpaces) {
xmlW.IndentChar = ' ';
- xmlW.Indentation = EditorControlService.GlobalOptions.IndentationSize;
+ xmlW.Indentation = SD.EditorControlService.GlobalOptions.IndentationSize;
} else {
xmlW.Indentation = 1;
xmlW.IndentChar = '\t';
@@ -538,7 +538,7 @@ static string GetIndentation(int level)
{
StringBuilder indentation = new StringBuilder();
for (int i = 0; i < level; i++) {
- indentation.Append(EditorControlService.GlobalOptions.IndentationString);
+ indentation.Append(SD.EditorControlService.GlobalOptions.IndentationString);
}
return indentation.ToString();
}
@@ -573,7 +573,7 @@ public static XElement AddFirstWithIndentation(this XElement element, XElement n
while (tmp != null) {
tmp = tmp.Parent;
indentationLevel++;
- indentation.Append(EditorControlService.GlobalOptions.IndentationString);
+ indentation.Append(SD.EditorControlService.GlobalOptions.IndentationString);
}
if (!element.Nodes().Any()) {
element.Add(new XText(Environment.NewLine + GetIndentation(indentationLevel - 1)));
@@ -13,41 +13,29 @@ namespace ICSharpCode.SharpDevelop
/// </summary>
public class ThreadSafeServiceContainer : IServiceProvider, IServiceContainer, IDisposable
{
- Dictionary<Type, object> services = new Dictionary<Type, object>();
+ readonly IServiceProvider parentProvider;
+ readonly Dictionary<Type, object> services = new Dictionary<Type, object>();
public ThreadSafeServiceContainer()
{
services.Add(typeof(ThreadSafeServiceContainer), this);
services.Add(typeof(IServiceContainer), this);
}
- public object GetOrCreateService(Type type, Func<object> serviceCreator)
+ public ThreadSafeServiceContainer(IServiceProvider parentProvider) : this()
{
- lock (services) {
- object instance;
- if (!services.TryGetValue(type, out instance)) {
- instance = serviceCreator();
- services.Add(type, instance);
- }
- return instance;
- }
- }
-
- public void TryAddService(Type type, object instance)
- {
- lock (services) {
- if (!services.ContainsKey(type))
- services.Add(type, instance);
- }
+ this.parentProvider = parentProvider;
}
public object GetService(Type serviceType)
{
+ bool foundService;
object instance;
lock (services) {
- if (!services.TryGetValue(serviceType, out instance))
- return null;
+ foundService = services.TryGetValue(serviceType, out instance);
}
+ if (!foundService)
+ return parentProvider != null ? parentProvider.GetService(serviceType) : null;
ServiceCreatorCallback callback = instance as ServiceCreatorCallback;
if (callback == null)
return instance;
@@ -107,6 +107,7 @@
<Compile Include="Src\Services\AnalyticsMonitor\AnalyticsMonitorService.cs" />
<Compile Include="Src\Services\AnalyticsMonitor\IAnalyticsMonitor.cs" />
<Compile Include="Src\Services\ApplicationStateInfoService.cs" />
+ <Compile Include="Src\Services\FallbackServiceAttribute.cs" />
<Compile Include="Src\Services\FileUtility\FileName.cs" />
<Compile Include="Src\Services\FileUtility\FileNameEventHandler.cs" />
<Compile Include="Src\Services\FileUtility\FileUtility.cs" />
@@ -121,7 +122,7 @@
<Compile Include="Src\Services\RegistryService\RegistryService.cs" />
<Compile Include="Src\Services\ResourceService\ResourceNotFoundException.cs" />
<Compile Include="Src\Services\ResourceService\ResourceService.cs" />
- <Compile Include="Src\Services\ServiceManager.cs" />
+ <Compile Include="Src\Services\ServiceSingleton.cs" />
<Compile Include="Src\Services\ServiceNotFoundException.cs" />
<Compile Include="Src\Services\StringParser\IStringTagProvider.cs" />
<Compile Include="Src\Services\StringParser\PropertyObjectTagProvider.cs" />
@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
+using System.ComponentModel.Design;
using System.IO;
namespace ICSharpCode.Core
@@ -176,6 +177,11 @@ public void RunInitialization()
{
AddInTree.Load(addInFiles, disabledAddIns);
+ // perform service registration
+ var container = ServiceSingleton.ServiceProvider.GetService<IServiceContainer>();
+ if (container != null)
+ AddInTree.BuildItems<object>("/SharpDevelop/Services", container, false);
+
// run workspace autostart commands
LoggingService.Info("Running autostart commands...");
foreach (ICommand command in AddInTree.BuildItems<ICommand>("/Workspace/Autostart", null, false)) {
Oops, something went wrong.

0 comments on commit c559da3

Please sign in to comment.