/// <summary> /// Adds global edit that will create instance of current type definition. /// </summary> /// <param name="editName">Name of the edit.</param> /// <param name="variableNameProvider">Provider of name of created instance.</param> /// <param name="argumentsProvider">Provider of arguments used in constructor.</param> /// <returns>Edit.</returns> protected Edit AddCreationEdit(string editName, NameProvider variableNameProvider, ArgumentsProvider argumentsProvider = null) { var creationTransformation = new AddCallTransformation((v) => { var entryContext = v.EntryBlock.Call; var name = variableNameProvider(this, entryContext); if (name == null) { v.Abort("Name hasn't been selected"); return(null); } var args = argumentsProvider == null ? null : argumentsProvider(v); if (args == null) { args = new object[0]; } if (v.IsAborted) { return(null); } var call = new CallEditInfo(TypeInfo, Naming.CtorName, args); call.ReturnName = name; return(call); }); var edit = new Edit(null, null, null, editName, creationTransformation); _globalEdits.Add(edit); return(edit); }
public override Transformation AppendCall(CallEditInfo call) { return(new SourceTransformation((view, source) => { source.AppendCall(view, _line, call); }, _source)); }
/// <summary> /// Generate code for C# call. /// </summary> /// <param name="call">Call which code will be generated.</param> /// <returns>Generated code.</returns> private string callToCSharp(CallEditInfo call) { var rawThisObj = call.IsExtensionCall ? call.CallArguments[0] : call.ThisObj; var rawArguments = call.IsExtensionCall ? call.CallArguments.Skip(1) : call.CallArguments; string thisObj; string callFormat; if (call.CallName == Naming.CtorName) { callFormat = "new {0}({2})"; thisObj = toCSharpType(rawThisObj as InstanceInfo); } else { callFormat = "{0}.{1}({2})"; thisObj = toCSharp(rawThisObj); } var args = (from arg in rawArguments select toCSharp(arg)).ToArray(); var argsList = string.Join(",", args); var callRepresentation = string.Format(callFormat + ";\n", thisObj, call.CallName, argsList); if (call.ReturnName != null) { //assign to desired variable callRepresentation = string.Format("var {0} = {1}", call.ReturnName, callRepresentation); } return(callRepresentation); }
/// <summary> /// Append call at new line after lineNode. /// </summary> /// <param name="view">View where transformation is processed.</param> /// <param name="lineNode">Node with line where call will be appended.</param> /// <param name="call">Prepended call.</param> internal void PrependCall(ExecutionView view, INodeAST lineNode, CallEditInfo call) { var callRepresentation = callToCSharp(call); ensureNamespaces(view, call); var beforeLineOffset = getBeforeOffset(lineNode); write(view, beforeLineOffset, callRepresentation); }
/// <summary> /// Add component into composition point. /// </summary> /// <param name="component">The component to add.</param> /// <param name="v">View of composition point.</param> /// <returns>Edit info for component adding.</returns> private CallEditInfo addComponent(ComponentInfo component, ExecutionView v) { var call = new CallEditInfo(component.ComponentType, Naming.CtorName); var name = TypeSystem.Dialogs.VariableName.GetName(component.ComponentType, _currentResult.EntryContext); if (name == null) { v.Abort("User aborted component adding"); return(null); } call.ReturnName = name; return(call); }
/// <summary> /// Ensure that required namespaces are present. /// </summary> /// <param name="view">The view.</param> /// <param name="call">Call which namespaces are required.</param> private void ensureNamespaces(ExecutionView view, CallEditInfo call) { if (!call.IsExtensionCall) { return; } var typeSignature = PathInfo.GetSignature(call.ThisObj as TypeDescriptor); var lastDot = typeSignature.LastIndexOf('.'); if (lastDot <= 0) { //there is no required namespace return; } var ns = typeSignature.Substring(0, lastDot); EditContext(view).EnsureNamespace(ns); }