/// <summary> /// Adds the property. /// </summary> /// <param name="codeClass">The code class.</param> /// <param name="var">The var.</param> /// <returns></returns> public static CodeProperty AddProperty(CodeClass codeClass, CodeVariable var) { CodeProperty prop = null; try { prop = codeClass.AddProperty( FormatPropertyName(var.Name), FormatPropertyName(var.Name), var.Type.AsFullName, -1, vsCMAccess.vsCMAccessPublic, null); EditPoint editPoint = prop.Getter.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); //Delete return default(int); added by codeClass.AddProperty editPoint.Delete(editPoint.LineLength); editPoint.Indent(null, 4); editPoint.Insert(string.Format(CultureInfo.InvariantCulture, "return {0};", var.Name)); editPoint = prop.Setter.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); editPoint.Indent(null, 1); editPoint.Insert(string.Format(CultureInfo.InvariantCulture, "{0} = value;", var.Name)); editPoint.SmartFormat(editPoint); return(prop); } catch { //Property already exists return(null); } }
void CreateCodeVariable(string code) { AddCodeFile("class.cs", code); ITypeDefinition typeDefinition = assemblyModel.TopLevelTypeDefinitions.Single().Resolve(); codeVariable = new CodeVariable(codeModelContext, typeDefinition.Fields.First()); }
/// <summary> /// Gets the access attribute value for given variable depending on the options. /// Returns 'true' if new access value is different than the currently stored one. /// Otherwise no action may be taken as variable doesn't need to be changed. /// </summary> public static bool GetVariableAttributes(CodeVariable var, PropertyGeneratorOptions options, out vsCMAccess access) { switch (options & PropertyGeneratorOptions.ForceVarMask) { case PropertyGeneratorOptions.ForceVarDontChange: access = vsCMAccess.vsCMAccessDefault; return(false); case PropertyGeneratorOptions.ForceVarPublic: access = vsCMAccess.vsCMAccessPublic; break; case PropertyGeneratorOptions.ForceVarInternal: access = vsCMAccess.vsCMAccessAssemblyOrFamily; break; case PropertyGeneratorOptions.ForceVarProtected: access = vsCMAccess.vsCMAccessProtected; break; case PropertyGeneratorOptions.ForceVarProtectedInternal: access = vsCMAccess.vsCMAccessProjectOrProtected; break; case PropertyGeneratorOptions.ForceVarPrivate: access = vsCMAccess.vsCMAccessPrivate; break; default: // by default decrease to private: access = vsCMAccess.vsCMAccessPrivate; break; } // validate if real change has been made: return(var.Access != access); }
/// <summary> /// Adds the property. /// </summary> /// <param name="codeClass">The code class.</param> /// <param name="var">The var.</param> /// <returns></returns> public static CodeProperty AddProperty(CodeClass codeClass, CodeVariable var) { CodeProperty prop = null; try { prop = codeClass.AddProperty( FormatPropertyName(var.Name), FormatPropertyName(var.Name), var.Type.AsFullName, -1, vsCMAccess.vsCMAccessPublic, null); EditPoint editPoint = prop.Getter.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); //Delete return default(int); added by codeClass.AddProperty editPoint.Delete(editPoint.LineLength); editPoint.Indent(null, 4); editPoint.Insert(string.Format(CultureInfo.InvariantCulture, "return {0};", var.Name)); editPoint = prop.Setter.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); editPoint.Indent(null, 1); editPoint.Insert(string.Format(CultureInfo.InvariantCulture, "{0} = value;", var.Name)); editPoint.SmartFormat(editPoint); return prop; } catch { //Property already exists return null; } }
private void ParseRafyProperty(CodeVariable codeVariable) { var name = codeVariable.Name; var typeFullName = codeVariable.Type.AsFullName; //名字与类型中都有 Property 字样的属性,才是托管属性。 if (name.EndsWith(PropertyToken) && typeFullName.Contains(PropertyToken)) { var propertyName = name.Substring(0, name.Length - PropertyToken.Length);//去除后缀 Property var handled = ParseReference(codeVariable, propertyName, typeFullName); if (handled) { return; } handled = ParseChild(codeVariable, propertyName, typeFullName); if (handled) { return; } ParseValueProperty(codeVariable, propertyName, typeFullName); } }
/// <summary> /// Implements the variable. /// </summary> /// <param name="instance">The instance.</param> /// <param name="name">The name.</param> /// <param name="type">The type.</param> /// <param name="isReadOnly">if set to <c>true</c> [is read only].</param> /// <returns>The Code Variable. </returns> public static CodeVariable ImplementVariable( this CodeClass instance, string name, string type, bool isReadOnly) { TraceService.WriteLine("CodeClassExtensions::ImplementVariable name=" + name + " type=" + type); CodeVariable codeVariable = instance.AddVariable(name, type, 0, vsCMAccess.vsCMAccessPrivate, 0); codeVariable.DocComment = "<doc><summary>\r\nBacking field for " + name + ".\r\n</summary></doc>"; codeVariable.GetEndPoint().CreateEditPoint().InsertNewLine(); if (isReadOnly) { CodeVariable2 codeVariable2 = codeVariable as CodeVariable2; if (codeVariable2 != null) { codeVariable2.ConstKind = vsCMConstKind.vsCMConstKindReadOnly; } } return(codeVariable); }
/// <summary> /// Creates <see cref="TypeMethodInfo" /> for given element. /// </summary> /// <param name="element">Element which <see cref="TypeMethodInfo" /> is created.</param> /// <returns>Created <see cref="TypeMethodInfo" />.</returns> internal TypeMethodInfo BuildFrom(CodeVariable element) { var isShared = element.IsShared; var isAbstract = false; //variables cannot be abstract var declaringType = CreateDescriptor(element.Parent as CodeClass); var variableType = CreateDescriptor(element.Type); //variables cannot have type arguments var methodTypeArguments = TypeDescriptor.NoDescriptors; TypeDescriptor returnType; ParameterTypeInfo[] parameters; var buildGetter = RequiredName.StartsWith(Naming.GetterPrefix); if (buildGetter) { returnType = variableType; parameters = ParameterTypeInfo.NoParams; } else { returnType = TypeDescriptor.Void; parameters = new[] { ParameterTypeInfo.Create("value", variableType) }; } var methodInfo = new TypeMethodInfo( declaringType, RequiredName, returnType, parameters, isShared, methodTypeArguments, isAbstract ); return(methodInfo); }
/// <summary> /// Implements the mock variable. /// </summary> /// <param name="instance">The instance.</param> /// <param name="name">The name.</param> /// <param name="type">The type.</param> /// <param name="mockVariableDeclaration">The mock variable declaration.</param> /// <returns>The code variable.</returns> public static CodeVariable ImplementMockVariable( this CodeClass instance, string name, string type, string mockVariableDeclaration) { TraceService.WriteLine("CodeClassExtensions::ImplementMockVariable file=" + instance.Name); CodeVariable codeVariable = instance.AddVariable(name, type, 0, vsCMAccess.vsCMAccessPrivate, 0); string typeDescriptor = name.Substring(4, 1).ToUpper() + name.Substring(5); codeVariable.DocComment = "<doc><summary>\r\nMock " + typeDescriptor + ".\r\n</summary></doc>"; EditPoint startPoint = codeVariable.StartPoint.CreateEditPoint(); EditPoint endPoint = codeVariable.EndPoint.CreateEditPoint(); //// if we are Moq then we change the variable declaration. if (string.IsNullOrEmpty(mockVariableDeclaration) == false) { string substitution = mockVariableDeclaration.Replace("%TYPE%", type); string text = startPoint.GetText(endPoint); string newText = text.Replace("private " + type, "private " + substitution); startPoint.ReplaceText(endPoint, newText, 0); } // get the new endpoint before inserting new line. endPoint = codeVariable.EndPoint.CreateEditPoint(); endPoint.InsertNewLine(); return(codeVariable); }
public static void Write(CodeTerm codeTerm, int indentation, TextWriter wtr) { CodeCompoundTerm codeCompoundTerm = codeTerm as CodeCompoundTerm; if (codeCompoundTerm != null) { Write(codeCompoundTerm, indentation, wtr); return; } CodeVariable codeVariable = codeTerm as CodeVariable; if (codeVariable != null) { Write(codeVariable, indentation, wtr); return; } CodeValue codeValue = codeTerm as CodeValue; if (codeValue != null) { Write(codeValue, indentation, wtr); return; } }
/// <summary> /// Checks element for local declaration scope. /// </summary> /// <param name="variable">CodeVariable instance.</param> protected void CheckLocalDeclaration(CodeVariable variable) { if (variable != null) { IsLocalDeclaration = variable.Access == vsCMAccess.vsCMAccessPrivate; } }
//Returns the first class in the file public CodeClass GetClass(ProjectItem activeDocument) { ThreadHelper.ThrowIfNotOnUIThread(); CodeElements fileCodeModel = activeDocument.FileCodeModel.CodeElements; TextSelection textSelection = Dte.ActiveDocument.Selection as TextSelection; VirtualPoint point = textSelection.ActivePoint; CodeElement codeElement = point.CodeElement[vsCMElement.vsCMElementClass]; if (codeElement != null) { if (codeElement.Kind == vsCMElement.vsCMElementClass) //verify if it's a class { return(codeElement as CodeClass); } else if (codeElement.Kind == vsCMElement.vsCMElementVariable) { CodeVariable codeVariable = codeElement as CodeVariable; codeElement = codeVariable.Parent as CodeElement; return(codeElement as CodeClass); } else if (codeElement.Kind == vsCMElement.vsCMElementFunction) { CodeFunction codeFunction = codeElement as CodeFunction; codeElement = codeFunction.Parent as CodeElement; return(codeElement as CodeClass); } } ErrorHandler.ShowMessageBox("Please select a class element."); return(null); }
/// <summary> /// Implements the mock variable. /// </summary> /// <param name="instance">The instance.</param> /// <param name="name">The name.</param> /// <param name="type">The type.</param> /// <returns>The code variable.</returns> public static CodeVariable ImplementMockVariable( this CodeClass instance, string name, string type) { TraceService.WriteLine("CodeClassExtensions::ImplementMockVariable file=" + instance.Name); CodeVariable codeVariable = instance.AddVariable(name, type, 0, vsCMAccess.vsCMAccessPrivate, 0); string typeDescriptor = name.Substring(4, 1).ToUpper() + name.Substring(5); codeVariable.DocComment = "<doc><summary>\r\nMock " + typeDescriptor + ".\r\n</summary></doc>"; EditPoint startPoint = codeVariable.StartPoint.CreateEditPoint(); EditPoint endPoint = codeVariable.EndPoint.CreateEditPoint(); string text = startPoint.GetText(endPoint); string newText = text.Replace(type, "Mock<" + type + ">"); startPoint.ReplaceText(endPoint, newText, 0); // get the new endpoint before inserting new line. endPoint = codeVariable.EndPoint.CreateEditPoint(); endPoint.InsertNewLine(); return(codeVariable); }
/// <summary> /// <seealso cref="IAction.Execute"/> /// </summary> public override void Execute() { this.Field = this.CodeClass.AddVariable(this.FieldName, this.FieldType, this.Position, this.Access, null); }
void CreateFieldEditPoint() { var codeVariable = new CodeVariable(fieldHelper.Field, documentLoader); TextPoint startPoint = codeVariable.GetStartPoint(); endPoint = codeVariable.GetEndPoint(); editPoint = startPoint.CreateEditPoint(); }
public async Task Parent() { CodeVariable testObject = await GetCodeVariableAsync("A", "intC"); CodeClass testObjectParent = testObject.Parent as CodeClass; Assert.Equal("A", testObjectParent.Name); }
public void DocComment() { CodeVariable testObject = GetCodeVariable("A", "intB"); string expected = "<doc>\r\n<summary>\r\nThis is a summary.\r\n</summary>\r\n</doc>"; Assert.Equal(expected, testObject.DocComment); }
public async Task DocComment() { CodeVariable testObject = await GetCodeVariableAsync("A", "intB"); string expected = "<doc>\r\n<summary>\r\nThis is a summary.\r\n</summary>\r\n</doc>"; Assert.Equal(expected, testObject.DocComment); }
private static string GetVariableType(CodeVariable n) { if (n.Type.TypeKind == vsCMTypeRef.vsCMTypeRefArray) { return(GenericNameMangler.MangleParameterName(getArray(n.Type))); } return(GenericNameMangler.MangleParameterName(n.Type.AsFullName)); }
public void Parent() { CodeVariable testObject = GetCodeVariable("A", "intC"); CodeClass testObjectParent = testObject.Parent as CodeClass; Assert.Equal("A", testObjectParent.Name); }
public MemberNode(CodeVariable cv, CodeModelEditorForm context) : base(CodeModelEditor.BrowseKind.ClassMember, context) { base.Tag = cv; base.ImageKey = "Member"; base.SelectedImageKey = "Member"; SetText(cv.Name); }
private void ParseValueProperty(CodeVariable codeVariable, string propertyName, string typeFullName) { //如果匹配以下格式,说明此属性是一个一般属性。 //例如:Rafy.Domain.Property<System.Nullable<System.Double>> var match = Regex.Match(typeFullName, @"Property<(?<name>.+)>"); var propertyType = match.Groups["name"].Value; var property = new ValueProperty(); property.CodeElement = codeVariable; property.Name = propertyName; //System.Nullable<System.Double> if (propertyType.Contains(NullableTypeToken)) { property.Nullable = true; propertyType = UnwrapGenericType(propertyType);//去除 System.Nullable< 及 > } switch (propertyType) { case "System.String": property.PropertyType = ValuePropertyType.String; break; case "System.Boolean": property.PropertyType = ValuePropertyType.Boolean; break; case "System.Int32": property.PropertyType = ValuePropertyType.Int; break; case "System.Double": property.PropertyType = ValuePropertyType.Double; break; case "System.Decimal": property.PropertyType = ValuePropertyType.Decimal; break; case "System.DateTime": property.PropertyType = ValuePropertyType.DateTime; break; case "System.Byte[]": property.PropertyType = ValuePropertyType.Bytes; break; default: property.PropertyType = ValuePropertyType.Unknown; property[PropertyTypeFullNameProperty] = propertyType; _unknownProperties.Add(property); break; } _currentType.ValueProperties.Add(property); }
public void GetEndPoint_Navigate() { CodeVariable testObject = GetCodeVariable("A", "intC"); TextPoint endPoint = testObject.GetEndPoint(vsCMPart.vsCMPartNavigate); Assert.Equal(14, endPoint.Line); Assert.Equal(21, endPoint.LineCharOffset); }
public async Task GetEndPoint_AttributesWithDelimiter() { CodeVariable testObject = await GetCodeVariableAsync("A", "intC"); TextPoint endPoint = testObject.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter); Assert.Equal(13, endPoint.Line); Assert.Equal(19, endPoint.LineCharOffset); }
private void ParseEnumValue(CodeVariable codeVariable) { _currentEnum.Items.Add(new EnumItem { CodeElement = codeVariable, Name = codeVariable.Name, //Value = codeVariable }); }
public async Task GetEndPoint_WholeWithAttributes() { CodeVariable testObject = await GetCodeVariableAsync("A", "intC"); TextPoint endPoint = testObject.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes); Assert.Equal(14, endPoint.Line); Assert.Equal(26, endPoint.LineCharOffset); }
public async Task GetEndPoint_Navigate() { CodeVariable testObject = await GetCodeVariableAsync("A", "intC"); TextPoint endPoint = testObject.GetEndPoint(vsCMPart.vsCMPartNavigate); Assert.Equal(14, endPoint.Line); Assert.Equal(21, endPoint.LineCharOffset); }
public void EndPoint() { CodeVariable testObject = GetCodeVariable("A", "intC"); TextPoint endPoint = testObject.EndPoint; Assert.Equal(14, endPoint.Line); Assert.Equal(26, endPoint.LineCharOffset); }
public void StartPoint() { CodeVariable testObject = GetCodeVariable("A", "intC"); TextPoint startPoint = testObject.StartPoint; Assert.Equal(13, startPoint.Line); Assert.Equal(5, startPoint.LineCharOffset); }
public async Task StartPoint() { CodeVariable testObject = await GetCodeVariableAsync("A", "intC"); TextPoint startPoint = testObject.StartPoint; Assert.Equal(13, startPoint.Line); Assert.Equal(5, startPoint.LineCharOffset); }
public void GetStartPoint_WholeWithAttributes() { CodeVariable testObject = GetCodeVariable("A", "intC"); TextPoint startPoint = testObject.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes); Assert.Equal(13, startPoint.Line); Assert.Equal(5, startPoint.LineCharOffset); }
public async Task EndPoint() { CodeVariable testObject = await GetCodeVariableAsync("A", "intC"); TextPoint endPoint = testObject.EndPoint; Assert.Equal(14, endPoint.Line); Assert.Equal(26, endPoint.LineCharOffset); }
public CodeVariable Lookup(WamVariable wamVariable) { if (wamVariable == null) { throw new ArgumentNullException("wamVariable"); } CodeVariable codeVariable; if (!_variables.TryGetValue(wamVariable, out codeVariable)) { ++_count; codeVariable = new CodeVariable(string.Format("V{0}", _count)); _variables.Add(wamVariable, codeVariable); } return codeVariable; }
private static string GetVariableType(CodeVariable n) { if (n.Type.TypeKind == vsCMTypeRef.vsCMTypeRefArray) return GenericNameMangler.MangleParameterName(getArray(n.Type)); return GenericNameMangler.MangleParameterName(n.Type.AsFullName); }
/// <summary> /// Checks element for local declaration scope. /// </summary> /// <param name="variable">CodeVariable instance.</param> protected void CheckLocalDeclaration(CodeVariable variable) { if (variable != null) IsLocalDeclaration = variable.Access == vsCMAccess.vsCMAccessPrivate; }
protected virtual void VisitVariable(CodeVariable codeVariable) { }
private bool ParseReference(CodeVariable codeVariable, string propertyName, string typeFullName) { if (typeFullName.Contains(RefIdProperty)) { _currentReference = new Reference { CodeElement = codeVariable, IdProperty = propertyName, }; var initExp = codeVariable.InitExpression.ToString(); if (initExp.Contains("ReferenceType.Parent")) { _currentReference.ReferenceType = ReferenceType.Parent; } else if (initExp.Contains("ReferenceType.Child")) { _currentReference.ReferenceType = ReferenceType.Child; } //Nullable 放到 VisitProperty 中处理。 //... _currentType.References.Add(_currentReference); return true; } if (typeFullName.Contains(RefEntityProperty)) { var refEntityTypeFullName = UnwrapGenericType(typeFullName); _currentReference[RefEntityTypeFullNameProperty] = refEntityTypeFullName; _currentReference.EntityProperty = propertyName; _currentReference = null; return true; } return false; }
private bool TryGetEnumMember(CodeVariable variable, TypeContext typeContext, int index, out TypeScriptEnumMember member) { var values = GetMemberValues(variable, typeContext); member = new TypeScriptEnumMember { Name = values.Name, FullName = variable.FullName, Ignore = values.Ignore, Value = variable.InitExpression == null ? index : Int32.Parse(variable.InitExpression.ToString()), Comment = variable.Comment, DocComment = variable.DocComment }; if (member.Name == null) { // The property is not explicit marked with TypeScriptMemberAttribute if (variable.Access != vsCMAccess.vsCMAccessPublic) // remove non-public default properties return false; member.Name = variable.Name; } if (member.Ignore) { return false; } if (values.CamelCase && values.Name == null) member.Name = member.Name.Substring(0, 1).ToLowerInvariant() + member.Name.Substring(1); return true; }
private TypeScriptMemberAttributeValues GetMemberValues(CodeVariable variable, TypeContext typeContext) { bool? attributeOptional = null; bool? attributeCamelCase = null; bool attributeIgnore = false; string attributeName = null; string attributeType = null; CodeAttribute attribute; // By default ignore properties marked with MemberIgnoreAttributes if (Settings.MemberIgnoreAttributes.Any(a => TryGetAttribute(variable.Attributes, a, out attribute, true))) { attributeIgnore = true; } if (TryGetAttribute(variable.Attributes, MemberAttributeFullName, out attribute)) { var values = GetAttributeValues(attribute); if (values.ContainsKey("Optional")) attributeOptional = values["Optional"] == "true"; if (values.ContainsKey("CamelCase")) attributeCamelCase = values["CamelCase"] == "true"; if (values.ContainsKey("Ignore")) attributeIgnore = values["Ignore"] == "true"; values.TryGetValue("Name", out attributeName); values.TryGetValue("Type", out attributeType); } return new TypeScriptMemberAttributeValues { Optional = attributeOptional.HasValue ? attributeOptional.Value : Settings.DefaultOptional, Name = attributeName, Type = attributeType, CamelCase = attributeCamelCase ?? Settings.DefaultCamelCaseMemberNames, Ignore = attributeIgnore }; }
private void ParseRafyProperty(CodeVariable codeVariable) { var name = codeVariable.Name; var typeFullName = codeVariable.Type.AsFullName; //名字与类型中都有 Property 字样的属性,才是托管属性。 if (name.EndsWith(PropertyToken) && typeFullName.Contains(PropertyToken)) { var propertyName = name.Substring(0, name.Length - PropertyToken.Length);//去除后缀 Property var handled = ParseReference(codeVariable, propertyName, typeFullName); if (handled) return; handled = ParseChild(codeVariable, propertyName, typeFullName); if (handled) return; ParseValueProperty(codeVariable, propertyName, typeFullName); } }
/// <summary> /// Gets the declaration of the specified code field as a string. /// </summary> /// <param name="codeField">The code field.</param> /// <returns>The string declaration.</returns> internal static string GetFieldDeclaration(CodeVariable codeField) { // Get the start point at the end of the attributes if there are any (vsCMPartHeader is // not available for fields). var startPoint = codeField.Attributes.Count > 0 ? codeField.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter) : codeField.StartPoint; return TextDocumentHelper.GetTextToFirstMatch(startPoint, @"[,;]"); }
private void Get(CodeVariable codeVariable, WamInstructionRegister sourceRegister) { WamInstructionRegister targetRegister = GetRegisterAssignment(codeVariable.Name); if (targetRegister.IsUnused) { targetRegister = GetNextPermanentRegister(codeVariable.Name); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.GetUnboundVariable, sourceRegister, targetRegister)); } else { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.GetBoundVariable, sourceRegister, targetRegister)); } }
private bool ParseChild(CodeVariable codeVariable, string propertyName, string typeFullName) { if (typeFullName.Contains(ListProperty)) { var initExp = codeVariable.InitExpression as string; if (!initExp.Contains(ListAsAggregationToken)) { var child = new Child { CodeElement = codeVariable, Name = propertyName, ListTypeFullName = UnwrapGenericType(typeFullName) }; _currentType.Children.Add(child); return true; } } return false; }
public static void Write(CodeVariable codeVariable, int indentation, TextWriter wtr) { wtr.WriteLine("{0}{1} - CodeVariable", Indentation(indentation), codeVariable.Name); }
protected override void VisitVariable(CodeVariable codeVariable) { if (_currentType != null) { //只读取静态字段。 if (codeVariable.IsShared) { ParseRafyProperty(codeVariable); } } else if (_currentEnum != null) { ParseEnumValue(codeVariable); } base.VisitVariable(codeVariable); }
private static string GetSummary(CodeVariable property) { return GetSummary(property.InfoLocation, property.DocComment, property.Comment, property.FullName); }
public static void Rule(ListTailItem lhs, Variable variable) { var codeVariable = new CodeVariable(variable.Text); lhs.CodeTerm = codeVariable; }