/// <summary> /// Gets a list of all events which are published by models in /// the current simulations tree. /// </summary> /// <param name="model"></param> public List <NeedContextItemsArgs.ContextItem> GetEvents(IModel model) { var events = new List <NeedContextItemsArgs.ContextItem>(); List <IModel> allModels = Apsim.ChildrenRecursively(Apsim.Parent(model, typeof(Simulations))); foreach (var publisher in Events.Publisher.FindAll(allModels)) { string description = NeedContextItemsArgs.GetDescription(publisher.EventInfo); Type eventType = publisher.EventInfo.EventHandlerType; events.Add(NeedContextItemsArgs.ContextItem.NewEvent(publisher.Name, description, eventType)); } return(events); }
/// <summary> /// Gets a list of all events which are published by models in /// the current simulations tree. /// </summary> /// <param name="model"></param> public List <NeedContextItemsArgs.ContextItem> GetEvents(IModel model) { var events = new List <NeedContextItemsArgs.ContextItem>(); IEnumerable <IModel> allModels = model.FindAncestor <Simulations>().FindAllDescendants(); foreach (var publisher in Events.Publisher.FindAll(allModels)) { string description = NeedContextItemsArgs.GetDescription(publisher.EventInfo); Type eventType = publisher.EventInfo.EventHandlerType; events.Add(NeedContextItemsArgs.ContextItem.NewEvent(publisher.Name, description, eventType)); } return(events); }
/// <summary> /// Shows completion information for a method call. /// </summary> /// <param name="relativeTo">Model to be used as a reference when searching for completion data.</param> /// <param name="code">Code for which we want to generate completion data.</param> /// <param name="offset">Offset of the cursor/caret in the code.</param> public void ShowMethodCompletion(IModel relativeTo, string code, int offset, Point location) { string contentsToCursor = code.Substring(0, offset).TrimEnd('.'); // Ignore everything before the most recent comma. contentsToCursor = contentsToCursor.Substring(contentsToCursor.LastIndexOf(',') + 1); string currentLine = contentsToCursor.Split(Environment.NewLine.ToCharArray()).Last().Trim(); // Set the trigger word for later use. triggerWord = GetTriggerWord(currentLine); // Ignore everything before most recent model name in square brackets. // I'm assuming that model/node names cannot start with a number. string modelNamePattern = @"\[([A-Za-z]+[A-Za-z0-9]*)\]"; string objectName = currentLine; var matches = System.Text.RegularExpressions.Regex.Matches(code, modelNamePattern); if (matches.Count > 0) { int modelNameIndex = currentLine.LastIndexOf(matches[matches.Count - 1].Value); if (modelNameIndex >= 0) { currentLine = currentLine.Substring(modelNameIndex); int lastPeriod = currentLine.LastIndexOf('.'); objectName = lastPeriod >= 0 ? currentLine.Substring(0, lastPeriod) : currentLine; } } string methodName = triggerWord.TrimEnd('('); MethodInfo method = NeedContextItemsArgs.GetMethodInfo(relativeTo as Model, methodName, objectName); if (method == null) { return; } MethodCompletion completion = new MethodCompletion(); List <string> parameterStrings = new List <string>(); StringBuilder parameterDocumentation = new StringBuilder(); foreach (ParameterInfo parameter in method.GetParameters()) { string parameterString = string.Format("{0} {1}", parameter.ParameterType.Name, parameter.Name); if (parameter.DefaultValue != DBNull.Value) { parameterString += string.Format(" = {0}", parameter.DefaultValue.ToString()); } parameterStrings.Add(parameterString); parameterDocumentation.AppendLine(string.Format("{0}: {1}", parameter.Name, NeedContextItemsArgs.GetDescription(method, parameter.Name))); } string parameters = parameterStrings.Aggregate((a, b) => string.Format("{0}, {1}", a, b)); completion.Signature = string.Format("{0} {1}({2})", method.ReturnType.Name, method.Name, parameters); completion.Summary = NeedContextItemsArgs.GetDescription(method); completion.ParameterDocumentation = parameterDocumentation.ToString().Trim(Environment.NewLine.ToCharArray()); methodCompletionView.Completions = new List <MethodCompletion>() { completion }; methodCompletionView.Location = location; methodCompletionView.Visible = true; }