string AddDelegateHandlers (CompletionDataList completionList, IType delegateType, bool addSemicolon = true, bool addDefault = true)
{
IMethod delegateMethod = delegateType.Methods.First ();
string delegateEndString = Document.Editor.EolMarker + stateTracker.Engine.ThisLineIndent + "}" + (addSemicolon ? ";" : "");
bool containsDelegateData = completionList.Any (d => d.DisplayText.StartsWith ("delegate("));
if (addDefault)
completionList.Add ("delegate", "md-keyword", GettextCatalog.GetString ("Creates anonymous delegate."), "delegate {" + Document.Editor.EolMarker + stateTracker.Engine.ThisLineIndent + TextEditorProperties.IndentString + "|" + delegateEndString);
StringBuilder sb = new StringBuilder ("(");
StringBuilder sbWithoutTypes = new StringBuilder ("(");
for (int k = 0; k < delegateMethod.Parameters.Count; k++) {
if (k > 0) {
sb.Append (", ");
sbWithoutTypes.Append (", ");
}
IType parameterType = dom.GetType (delegateMethod.Parameters [k].ReturnType);
IReturnType returnType = parameterType != null ? new DomReturnType (parameterType) : delegateMethod.Parameters [k].ReturnType;
sb.Append (CompletionDataCollector.ambience.GetString (Document.CompilationUnit.ShortenTypeName (returnType, textEditorData.Caret.Line, textEditorData.Caret.Column), OutputFlags.ClassBrowserEntries | OutputFlags.UseFullName | OutputFlags.UseFullInnerTypeName));
sb.Append (" ");
sb.Append (delegateMethod.Parameters [k].Name);
sbWithoutTypes.Append (delegateMethod.Parameters [k].Name);
}
sb.Append (")");
sbWithoutTypes.Append (")");
completionList.Add ("delegate" + sb, "md-keyword", GettextCatalog.GetString ("Creates anonymous delegate."), "delegate" + sb + " {" + Document.Editor.EolMarker + stateTracker.Engine.ThisLineIndent + TextEditorProperties.IndentString + "|" + delegateEndString);
completionList.Add (sbWithoutTypes.ToString (), "md-keyword", GettextCatalog.GetString ("Creates lambda expression."), sbWithoutTypes + " => |" + (addSemicolon ? ";" : ""));
// It's needed to temporarly disable inserting auto matching bracket because the anonymous delegates are selectable with '('
// otherwise we would end up with () => )
if (!containsDelegateData) {
var savedValue = MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket;
MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket = false;
completionList.CompletionListClosed += delegate {
MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket = savedValue;
};
}
return sb.ToString ();
}