public override object VisitInterface_declaration([NotNull] CSharpParser.Interface_declarationContext context) { var typeParameters = _serviceFile.ServiceDeclaration.TypeParameters; bool isTypeParamsMatch = true; var variantTypeParameters = context?.variant_type_parameter_list()?.variant_type_parameter(); if (!(variantTypeParameters is null && (typeParameters is null || typeParameters.Count == 0))) { if ((variantTypeParameters?.Length ?? 0) != (typeParameters?.Count ?? 0)) { isTypeParamsMatch = false; } else { for (int i = 0; i < variantTypeParameters.Length; ++i) { if (variantTypeParameters[i].identifier().GetText() != typeParameters[i].TypeParam) { isTypeParamsMatch = false; break; } } } } if (context.identifier().GetText() == _serviceClassInterfaceName && GetCurrentNamespace() == _serviceFile.ServiceNamespace && isTypeParamsMatch) { _hasServiceInterface = true; var preclassWhitespace = Tokens.GetHiddenTokensToLeft(context.Start.TokenIndex, Lexer.Hidden); int tabLevels = 1 + ((preclassWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels(preclassWhitespace[0]?.Text ?? string.Empty, _tabString) : 0); int?finalMethod = null; int?finalProperty = null; var members = context?.interface_body()?.interface_member_declaration(); foreach (var member in members) { if (member.interface_method_declaration() != null) { finalMethod = member?.interface_method_declaration()?.Stop?.TokenIndex; } else if (member.interface_property_declaration() != null) { finalProperty = member?.interface_property_declaration()?.Stop?.TokenIndex; } } int propertyStopIndex = finalProperty ?? context.interface_body().OPEN_BRACE().Symbol.TokenIndex; var propertyString = _cSharpParserService.GeneratePropertyDeclarations( _serviceFile.ServiceDeclaration.Body.PropertyDeclarations, tabLevels, _tabString, true); int?methodStopIndex = finalMethod; var methodString = _cSharpParserService.GenerateMethodDeclarations( _serviceFile.ServiceDeclaration.Body.MethodDeclarations, tabLevels, _tabString, true); if (methodStopIndex is null) { propertyString += methodString; } else { if (methodString.Length > 0) { IsModified = true; Rewriter.InsertAfter(Tokens.Get(methodStopIndex ?? -1), methodString); } } if (propertyString.Length > 0) { IsModified = true; Rewriter.InsertAfter(propertyStopIndex, propertyString); } } VisitChildren(context); return(null); }
public override object VisitClass_declaration([NotNull] CSharpParser.Class_declarationContext context) { _currentClass.Push(context.identifier().GetText()); var typeParameters = _serviceFile.ServiceDeclaration.TypeParameters; var currentNamespace = GetCurrentNamespace(); bool isTypeParamsMatch = true; var typeParams = context?.type_parameter_list()?.type_parameter(); if (!(typeParams is null && (typeParameters is null || typeParameters.Count == 0))) { if ((typeParams?.Length ?? 0) != (typeParameters?.Count ?? 0)) { isTypeParamsMatch = false; } else { for (int i = 0; i < typeParams.Length; ++i) { if (typeParams[i].identifier().GetText() != typeParameters[i].TypeParam) { isTypeParamsMatch = false; break; } } } } if (GetCurrentClass() == _serviceClassInterfaceName && currentNamespace == _serviceFile.ServiceNamespace && isTypeParamsMatch) { _isCorrectClass.Push(true); _hasServiceClass = true; } else { _isCorrectClass.Push(false); } VisitChildren(context); if (_isCorrectClass.Peek()) { _hasServiceClass = true; var preclassWhitespace = Tokens.GetHiddenTokensToLeft(context.Start.TokenIndex, Lexer.Hidden); int tabLevels = 1 + ((preclassWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels(preclassWhitespace[0]?.Text ?? string.Empty, _tabString) : 0); int?finalProperty = null; int?finalMethod = null; int?finalField = null; int?finalConstantOrField = null; int?finalConstructorOrDestructor = null; var members = context?.class_body()?.class_member_declarations()?.class_member_declaration(); if (members != null) { foreach (var member in members) { if (member.method_declaration() != null) { finalMethod = member.method_declaration().Stop.TokenIndex; } else if (member.property_declaration() != null) { finalProperty = member.property_declaration().Stop.TokenIndex; } else if (member.constant_declaration() != null) { finalConstantOrField = member.constant_declaration().Stop.TokenIndex; } else if (member.field_declaration() != null) { finalConstantOrField = member.field_declaration().Stop.TokenIndex; finalField = member.field_declaration().Stop.TokenIndex; } else if (member.constructor_declaration() != null) { finalConstructorOrDestructor = member.constructor_declaration().Stop.TokenIndex; } else if (member.static_constructor_declaration() != null) { finalConstructorOrDestructor = member.static_constructor_declaration().Stop.TokenIndex; } else if (member.destructor_declaration() != null) { finalConstructorOrDestructor = member.destructor_declaration().Stop.TokenIndex; } } } int fieldStopIndex = finalField ?? finalConstantOrField ?? context.class_body().OPEN_BRACE().Symbol.TokenIndex; int?constructorStopIndex = null; int?propertyStopIndex = null; int?methodStopIndex = null; var fieldStringBuilder = new StringBuilder(); StringBuilder constructorStringBuilder = null; StringBuilder propertyStringBuilder = null; StringBuilder methodStringBuilder = null; if (_fieldDict.Keys.Count > 0) { // add fields foreach (var field in _fieldDict.Values) { fieldStringBuilder.Append(_cSharpParserService.GenerateFieldDeclaration( field, tabLevels, _tabString)); } } if (!_hasServiceConstructor) { // add ctor constructorStopIndex = finalProperty ?? finalConstantOrField ?? fieldStopIndex; constructorStringBuilder = constructorStopIndex == fieldStopIndex ? fieldStringBuilder : new StringBuilder(); constructorStringBuilder.Append(_cSharpParserService.GenerateConstructorDeclaration( _serviceFile.ServiceDeclaration.Body.ConstructorDeclaration, tabLevels, _tabString)); } if (_serviceFile.ServiceDeclaration.Body?.PropertyDeclarations != null && _serviceFile.ServiceDeclaration.Body.PropertyDeclarations.Count > 0) { // add properties propertyStopIndex = finalProperty ?? finalConstructorOrDestructor ?? constructorStopIndex ?? fieldStopIndex; propertyStringBuilder = propertyStopIndex == fieldStopIndex ? fieldStringBuilder : (propertyStopIndex == constructorStopIndex ? constructorStringBuilder : new StringBuilder()); propertyStringBuilder.Append( _cSharpParserService.GeneratePropertyDeclarations( _serviceFile.ServiceDeclaration.Body.PropertyDeclarations, tabLevels, _tabString)); } if (_serviceFile.ServiceDeclaration.Body?.MethodDeclarations != null && _serviceFile.ServiceDeclaration.Body.MethodDeclarations.Count > 0) { // add methods methodStopIndex = finalMethod ?? finalProperty ?? finalConstructorOrDestructor ?? constructorStopIndex ?? fieldStopIndex; methodStringBuilder = methodStopIndex == fieldStopIndex ? fieldStringBuilder : (methodStopIndex == constructorStopIndex ? constructorStringBuilder : (methodStopIndex == propertyStopIndex ? propertyStringBuilder : new StringBuilder())); methodStringBuilder.Append( _cSharpParserService.GenerateMethodDeclarations( _serviceFile.ServiceDeclaration.Body.MethodDeclarations, tabLevels, _tabString)); } var fieldString = fieldStringBuilder.ToString(); if (fieldString.Length > 0) { IsModified = true; Rewriter.InsertAfter(fieldStopIndex, fieldString); } if (constructorStringBuilder != null && constructorStopIndex != fieldStopIndex) { var constructorString = constructorStringBuilder.ToString(); if (constructorString.Length > 0) { IsModified = true; Rewriter.InsertAfter(constructorStopIndex ?? -1, constructorString); } } if (propertyStringBuilder != null && propertyStopIndex != constructorStopIndex && propertyStopIndex != fieldStopIndex) { var propertyString = propertyStringBuilder.ToString(); if (propertyString.Length > 0) { IsModified = true; Rewriter.InsertAfter(propertyStopIndex ?? -1, propertyString); } } if (methodStringBuilder != null && methodStopIndex != constructorStopIndex && methodStopIndex != fieldStopIndex && methodStopIndex != propertyStopIndex) { var methodString = methodStringBuilder.ToString(); if (methodString.Length > 0) { IsModified = true; Rewriter.InsertAfter(methodStopIndex ?? -1, methodString); } } } _ = _isCorrectClass.Pop(); _ = _currentClass.Pop(); return(null); }