public override object VisitCompilation_unit([NotNull] CSharpParser.Compilation_unitContext context) { var missingUsingDirectives = _cSharpParserService.GetUsingDirectivesNotInContext( context, new List <string>() { _serviceNamespace }); if (missingUsingDirectives.Count > 0) { var usingStopIndex = _cSharpParserService.GetUsingStopIndex(context); var usingDirectiveStr = _cSharpParserService.GenerateUsingDirectives( missingUsingDirectives.ToList(), usingStopIndex.Equals(context.Start)); _isRewritten = true; Rewriter.InsertAfter(usingStopIndex, usingDirectiveStr); } VisitChildren(context); IsModified = _isRewritten && _isConstructorClassFound; if (!_isConstructorClassFound) { _logger.LogWarning($"No class found called {_constructorClassName} in namespace " + $"{_constructorClassNamespace}. Failed to inject {_serviceInterfaceType} into class."); } return(null); }
public override object VisitCompilation_unit([NotNull] CSharpParser.Compilation_unitContext context) { _isFoundConfigureServices = false; var usingNamespaceList = new List <string>(); foreach (var regInfo in _startupRegInfoList) { usingNamespaceList.Add(regInfo.ServiceNamespace); } var missingUsingDirectives = _cSharpParserService.GetUsingDirectivesNotInContext( context, usingNamespaceList); if (missingUsingDirectives.Count > 0) { var usingStopIndex = _cSharpParserService.GetUsingStopIndex(context); var usingDirectiveStr = _cSharpParserService.GenerateUsingDirectives( missingUsingDirectives.ToList(), usingStopIndex.Equals(context.Start)); IsModified = true; Rewriter.InsertAfter(usingStopIndex, usingDirectiveStr); } VisitChildren(context); return(null); }
public override object VisitNamespace_body([NotNull] CSharpParser.Namespace_bodyContext context) { var isServiceNamespace = GetCurrentNamespace() == _serviceFile.ServiceNamespace; if (isServiceNamespace) { _hasServiceNamespace = true; } VisitChildren(context); if (!_hasServiceInterface && isServiceNamespace) { var classInterfaceStopIndex = _cSharpParserService.GetClassInterfaceStopIndex(context); 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); var interfaceDeclaration = _cSharpParserService.GenerateClassInterfaceDeclaration( _serviceFile.ServiceDeclaration, tabLevels, _tabString); IsModified = true; Rewriter.InsertAfter(classInterfaceStopIndex, interfaceDeclaration); } return(null); }
public override object VisitCompilation_unit([NotNull] CSharpParser.Compilation_unitContext context) { VisitChildren(context); if (IsModified) { var missingUsingDirectives = _cSharpParserService.GetUsingDirectivesNotInContext( context, new List <string>() { _breadcrumbServiceNamespace }); if (missingUsingDirectives.Count > 0) { var usingStopIndex = _cSharpParserService.GetUsingStopIndex(context); var usingDirectiveStr = _cSharpParserService.GenerateUsingDirectives( missingUsingDirectives.ToList(), usingStopIndex.Equals(context.Start)); Rewriter.InsertAfter(usingStopIndex, usingDirectiveStr); } } return(null); }
// add parentheses to calls that do not have them public override void EnterICS_B_MemberProcedureCall([NotNull] VBAParser.ICS_B_MemberProcedureCallContext context) { if (!context.argsCall().IsEmpty) { Rewriter.InsertBefore(context.argsCall().Start, "("); Rewriter.InsertAfter(context.argsCall().Stop, ")"); } }
public override object VisitCompilation_unit([NotNull] CSharpParser.Compilation_unitContext context) { var usingDirs = context?.using_directive(); if (usingDirs != null) { foreach (var usingDir in usingDirs) { var usingInner = usingDir.using_directive_inner().GetText(); if (_usingSet.Contains(usingInner)) { _usingSet.Remove(usingInner); } } } VisitChildren(context); if (_usingSet.Count > 0) { var usingStopIndex = _cSharpParserService.GetUsingStopIndex(context); var usingDirectivesStr = _cSharpParserService.GenerateUsingDirectives( _usingSet.ToList(), usingStopIndex.Equals(context.Start)); IsModified = true; Rewriter.InsertAfter(usingStopIndex, usingDirectivesStr); } if (!_hasBreadcrumbNamespace) { var namespaceStopIndex = _cSharpParserService.GetNamespaceStopIndex(context); var breadcrumbNamespaceDeclaration = _breadcrumbCommandParserService.GenerateBreadcrumbNamespaceDeclaration( _breadcrumbNamespace, _breadcrumbDeclaration); IsModified = true; Rewriter.InsertAfter(namespaceStopIndex, breadcrumbNamespaceDeclaration); } return(null); }
// wrap the VBA code with a Module public override void ExitStartRule(VBAParser.StartRuleContext context) { // if an option is present it must before everything else // therefore we have to check where to put the module start var baseText = $"Imports System {Environment.NewLine}{Environment.NewLine}Imports Microsoft.VisualBasic{Environment.NewLine}Imports System.Math{Environment.NewLine}Imports System.Linq{Environment.NewLine}Imports System.Collections.Generic{Environment.NewLine}{Environment.NewLine}Module {FileName}{Environment.NewLine}"; if (TokenImport != null) { // add imports and module Rewriter.InsertAfter(TokenImport, $"{Environment.NewLine}{baseText}"); } else { // add imports and module Rewriter.InsertBefore(context.Start, baseText); } Rewriter.InsertAfter(context.Stop.StopIndex, $"{Environment.NewLine}End Module"); }
public override object VisitNamespace_declaration([NotNull] CSharpParser.Namespace_declarationContext context) { _currentNamespace.Push(context.qualified_identifier().GetText()); var isBreadcrumbNamespace = false; if (GetCurrentNamespace() == _breadcrumbNamespace) { _usingSet.Remove(GetCurrentNamespace()); _hasBreadcrumbNamespace = true; isBreadcrumbNamespace = true; } VisitChildren(context); if (isBreadcrumbNamespace) { if (!_hasBreadcrumbClass) { var classStopIndex = _cSharpParserService.GetClassInterfaceStopIndex(context.namespace_body()); var prenamespaceWhitespace = Tokens.GetHiddenTokensToLeft(context.Start.TokenIndex, Lexer.Hidden); int tabLevels = 1 + ((prenamespaceWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels(prenamespaceWhitespace[0]?.Text ?? string.Empty, _tabString) : 0); var breadcrumbClassString = _breadcrumbCommandParserService.GenerateBreadcrumbClassInterfaceDeclaration( _breadcrumbDeclaration, tabLevels, _tabString); IsModified = true; Rewriter.InsertAfter(classStopIndex, breadcrumbClassString); } } _ = _currentNamespace.Pop(); return(null); }
public override object VisitConstructor_declaration( [NotNull] CSharpParser.Constructor_declarationContext context) { if (GetCurrentNamespace() == _breadcrumbNamespace && GetCurrentClass() == _breadcrumbDeclaration.Identifier) { _hasBreadcrumbConstructor = true; var preCtorWhitespace = Tokens.GetHiddenTokensToLeft(context.Start.TokenIndex, Lexer.Hidden); int tabLevels = 1 + ((preCtorWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels(preCtorWhitespace[0]?.Text ?? string.Empty, _tabString) : 0); var fixedParams = context?.constructor_declarator()?.formal_parameter_list()?.fixed_parameters() ?.fixed_parameter(); if (fixedParams != null) { foreach (var fixedParam in fixedParams) { var paramName = $"{fixedParam.type_().GetText()} {fixedParam.identifier().GetText()}"; if (_ctorParamDict.ContainsKey(paramName)) { _ctorParamDict.Remove(paramName); } } } var ctorStatements = context?.constructor_body()?.block()?.statement_list(); if (ctorStatements != null) { foreach (var statement in ctorStatements.statement()) { var minStatement = _cSharpParserService.GetTextWithWhitespaceMinified(Tokens, statement); if (_ctorAssignmentDict.ContainsKey(minStatement)) { _ctorAssignmentDict.Remove(minStatement); } } } if (_ctorParamDict.Keys.Count > 0) { // add params var ctorParams = _ctorParamDict.Values.ToList(); var formalParamList = context?.constructor_declarator()?.formal_parameter_list(); var finalFixedParam = context?.constructor_declarator() ?.formal_parameter_list() ?.fixed_parameters() ?.fixed_parameter() ?.Last(); int fixedParamStopIndex = finalFixedParam?.Stop?.TokenIndex ?? context.constructor_declarator().OPEN_PARENS().Symbol.TokenIndex; var paramStringBuilder = new StringBuilder(); if (finalFixedParam != null) { var preFinalParamWhitespace = Tokens.GetHiddenTokensToLeft( finalFixedParam?.Start?.TokenIndex ?? -1, Lexer.Hidden); int finalParamtabs = (preFinalParamWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels(preFinalParamWhitespace[0]?.Text ?? string.Empty, _tabString) : 0; if (finalParamtabs > 0) { paramStringBuilder.Append(_cSharpParserService.GenerateFixedParameters( ctorParams, tabLevels, _tabString)); if (formalParamList?.parameter_array() != null) { var preParamArrayWhitespaceArray = Tokens.GetHiddenTokensToLeft( formalParamList.parameter_array().Start.TokenIndex, Lexer.Hidden); string preParamArrayWhitespace = preParamArrayWhitespaceArray.Count > 0 ? preParamArrayWhitespaceArray[0].Text : string.Empty; if (!Regex.IsMatch(preParamArrayWhitespace, @"\r?\n")) { IsModified = true; Rewriter.InsertBefore( formalParamList.parameter_array().Start.TokenIndex, "\r\n" + _stringUtilService.TabString(string.Empty, finalParamtabs, _tabString)); } } } else { paramStringBuilder.Append(_cSharpParserService.GenerateFixedParameters( ctorParams, tabLevels, _tabString, false, true)); } } else { paramStringBuilder.Append(_cSharpParserService.GenerateFixedParameters( ctorParams, tabLevels, _tabString, true)); if (formalParamList?.parameter_array() != null) { var preParamArrayWhitespaceArray = Tokens.GetHiddenTokensToLeft( formalParamList.parameter_array().Start.TokenIndex, Lexer.Hidden); string preParamArrayWhitespace = preParamArrayWhitespaceArray.Count > 0 ? preParamArrayWhitespaceArray[0].Text : string.Empty; if (!Regex.IsMatch(preParamArrayWhitespace, $"\r?\n")) { IsModified = true; Rewriter.InsertBefore( formalParamList.parameter_array().Start.TokenIndex, "\r\n" + _stringUtilService.TabString(string.Empty, tabLevels, _tabString)); } } } var paramString = paramStringBuilder.ToString(); if (paramString.Length > 0) { IsModified = true; Rewriter.InsertAfter(fixedParamStopIndex, paramString); } } if (_ctorAssignmentDict.Keys.Count > 0) { var ctorAssignments = _ctorAssignmentDict.Values.ToList(); var assignmentsString = _cSharpParserService.GenerateSimpleAssignments( ctorAssignments, tabLevels, _tabString); var constructorBody = context?.constructor_body(); if (constructorBody.SEMICOLON() != null) { var conBodyBuilder = new StringBuilder(); conBodyBuilder.Append("\r\n"); conBodyBuilder.Append(_stringUtilService.TabString("{", tabLevels - 1, _tabString)); conBodyBuilder.Append(assignmentsString); conBodyBuilder.Append("\r\n"); conBodyBuilder.Append(_stringUtilService.TabString("}", tabLevels - 1, _tabString)); IsModified = true; Rewriter.Replace(constructorBody.SEMICOLON().Symbol.TokenIndex, conBodyBuilder.ToString()); } else { var block = constructorBody.block(); int?finalAssignment = block.statement_list()?.statement()?.Last()?.Stop?.TokenIndex; int assignmentStopIndex = finalAssignment ?? block.OPEN_BRACE().Symbol.TokenIndex; var assignmentStringBuilder = new StringBuilder(); assignmentStringBuilder.Append(assignmentsString); var postAssignmentStopWhitespaceArray = Tokens.GetHiddenTokensToRight( assignmentStopIndex, Lexer.Hidden); string postAssignmentStopWhitespace = postAssignmentStopWhitespaceArray.Count > 0 ? postAssignmentStopWhitespaceArray[0].Text : string.Empty; if (!Regex.IsMatch(postAssignmentStopWhitespace, @"\r?\n")) { assignmentStringBuilder.Append("\r\n"); } var assignmentString = assignmentStringBuilder.ToString(); if (assignmentString.Length > 0) { IsModified = true; Rewriter.InsertAfter(assignmentStopIndex, assignmentString); } } } } VisitChildren(context); return(null); }
public override object VisitClass_declaration([NotNull] CSharpParser.Class_declarationContext context) { _currentClass.Push(context.identifier().GetText()); var isBreadcrumbClass = false; if (GetCurrentNamespace() == _breadcrumbNamespace && GetCurrentClass() == _breadcrumbDeclaration.Identifier) { _hasBreadcrumbClass = true; isBreadcrumbClass = true; } VisitChildren(context); if (isBreadcrumbClass) { 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?methodStopIndex = null; var fieldStringBuilder = new StringBuilder(); StringBuilder constructorStringBuilder = 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 (!_hasBreadcrumbConstructor) { // add ctor constructorStopIndex = finalProperty ?? finalConstantOrField ?? fieldStopIndex; constructorStringBuilder = constructorStopIndex == fieldStopIndex ? fieldStringBuilder : new StringBuilder(); constructorStringBuilder.Append(_cSharpParserService.GenerateConstructorDeclaration( _breadcrumbDeclaration.Body.ConstructorDeclaration, tabLevels, _tabString)); } if (_methodDictionary.Keys.Count > 0) { // add methods methodStopIndex = finalMethod ?? finalConstructorOrDestructor ?? constructorStopIndex ?? fieldStopIndex; methodStringBuilder = methodStopIndex == fieldStopIndex ? fieldStringBuilder : (methodStopIndex == constructorStopIndex ? constructorStringBuilder : new StringBuilder()); methodStringBuilder.Append(_breadcrumbCommandParserService.GenerateBreadcrumbMethodDeclarations( _methodDictionary.Values.ToList(), 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 (methodStringBuilder != null && methodStopIndex != constructorStopIndex && methodStopIndex != fieldStopIndex) { var methodString = methodStringBuilder.ToString(); if (methodString.Length > 0) { IsModified = true; Rewriter.InsertAfter(methodStopIndex ?? -1, methodString); } } } _ = _currentClass.Pop(); return(null); }
public override object VisitClass_declaration([NotNull] CSharpParser.Class_declarationContext context) { var currentNamespace = string.Join(".", _currentNamespace.ToArray().Reverse()); if (context.identifier().GetText() == _constructorClassName && currentNamespace == _constructorClassNamespace) { _isConstructorClassFound = true; var preclassWhitespace = Tokens.GetHiddenTokensToLeft(context.Start.TokenIndex, Lexer.Hidden); int classBodyTabLevels = 1 + ((preclassWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels(preclassWhitespace[0]?.Text ?? string.Empty, _tabString) : 0); int ctorBodyTabLevels = classBodyTabLevels + 1; int?finalConstantOrField = null; int?finalField = null; int?finalProperty = null; CSharpParser.Constructor_declarationContext constructorContext = null; bool hasServiceField = false; bool hasCtorParam = false; bool hasCtorAssignment = false; var members = context?.class_body()?.class_member_declarations()?.class_member_declaration(); if (members != null) { foreach (var member in members) { if (member.constant_declaration() != null) { finalConstantOrField = member.constant_declaration().Stop.TokenIndex; } else if (member.field_declaration() != null) { var fieldDec = member.field_declaration(); finalField = fieldDec.Stop.TokenIndex; finalConstantOrField = fieldDec.Stop.TokenIndex; if (fieldDec.type_().GetText() == _serviceInterfaceType) { foreach (var varDec in fieldDec.variable_declarators().variable_declarator()) { if (varDec.identifier().GetText() == $"_{_serviceIdentifier}") { hasServiceField = true; break; } } } } else if (member.property_declaration() != null) { finalProperty = member.property_declaration().Stop.TokenIndex; } else if (member.constructor_declaration() != null) { constructorContext ??= member.constructor_declaration(); } } } int fieldStopIndex = finalField ?? finalConstantOrField ?? context.class_body().OPEN_BRACE().Symbol.TokenIndex; int?constructorStopIndex = null; var fieldStringBuilder = new StringBuilder(); StringBuilder constructorStringBuilder = null; if (!hasServiceField) { fieldStringBuilder.Append(_cSharpParserService.GenerateFieldDeclaration( _fieldDeclaration, classBodyTabLevels, _tabString)); } if (constructorContext is null) { constructorStopIndex = finalProperty ?? finalConstantOrField ?? fieldStopIndex; constructorStringBuilder = constructorStopIndex == fieldStopIndex ? fieldStringBuilder : new StringBuilder(); constructorStringBuilder.Append( _cSharpParserService.GenerateConstructorDeclaration( _constructorDeclaration, classBodyTabLevels, _tabString)); } else { CSharpParser.Fixed_parameterContext finalFixedParam = null; var formalParamList = constructorContext?.constructor_declarator()?.formal_parameter_list(); if (formalParamList != null) { var fixedParams = formalParamList.fixed_parameters(); if (fixedParams != null) { foreach (var fixedParam in fixedParams.fixed_parameter()) { if (fixedParam.type_().GetText() == _serviceInterfaceType && fixedParam.identifier().GetText() == _serviceIdentifier) { hasCtorParam = true; break; } } finalFixedParam = fixedParams.fixed_parameter().Last(); } } if (!hasCtorParam) { var ctorParam = _cSharpCommonStgService.RenderFixedParameter(_constructorParameter); int fixedParamStopIndex = finalFixedParam?.Stop?.TokenIndex ?? constructorContext.constructor_declarator().OPEN_PARENS().Symbol.TokenIndex; var paramStringBuilder = new StringBuilder(); if (finalFixedParam != null) { var preFinalParamWhitespace = Tokens.GetHiddenTokensToLeft( finalFixedParam?.Start?.TokenIndex ?? -1, Lexer.Hidden); int finalParamtabs = (preFinalParamWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels( preFinalParamWhitespace?[0]?.Text ?? string.Empty, _tabString) : 0; if (finalParamtabs > 0) { paramStringBuilder.Append(",\r\n"); paramStringBuilder.Append(_stringUtilService.TabString( ctorParam, finalParamtabs, _tabString)); if (formalParamList?.parameter_array() != null) { var preParamArrayWhitespaceArray = Tokens.GetHiddenTokensToLeft( formalParamList.parameter_array().Start.TokenIndex, Lexer.Hidden); string preParamArrayWhitespace = preParamArrayWhitespaceArray.Count > 0 ? preParamArrayWhitespaceArray[0].Text : string.Empty; if (!Regex.IsMatch(preParamArrayWhitespace, @"\r?\n")) { _isRewritten = true; Rewriter.InsertBefore( formalParamList.parameter_array().Start.TokenIndex, "\r\n" + _stringUtilService.TabString(string.Empty, finalParamtabs, _tabString)); } } } else { paramStringBuilder.Append(", "); paramStringBuilder.Append(ctorParam); } } else { paramStringBuilder.Append("\r\n"); paramStringBuilder.Append( _stringUtilService.TabString(ctorParam, ctorBodyTabLevels, _tabString)); if (formalParamList?.parameter_array() != null) { var preParamArrayWhitespaceArray = Tokens.GetHiddenTokensToLeft( formalParamList.parameter_array().Start.TokenIndex, Lexer.Hidden); string preParamArrayWhitespace = preParamArrayWhitespaceArray.Count > 0 ? preParamArrayWhitespaceArray[0].Text : string.Empty; if (!Regex.IsMatch(preParamArrayWhitespace, $"\r?\n")) { _isRewritten = true; Rewriter.InsertBefore( formalParamList.parameter_array().Start.TokenIndex, "\r\n" + _stringUtilService.TabString(string.Empty, ctorBodyTabLevels, _tabString)); } } } var paramString = paramStringBuilder.ToString(); if (paramString.Length > 0) { _isRewritten = true; Rewriter.InsertAfter(fixedParamStopIndex, paramString); } } string ctorAssignString = _cSharpCommonStgService.RenderSimpleAssignment(_constructorAssignment); var constructorBody = constructorContext?.constructor_body(); if (constructorBody.SEMICOLON() != null) { _isRewritten = true; Rewriter.Replace(constructorBody.SEMICOLON().Symbol.TokenIndex, _stringUtilService.TabString( $@"\r\n{{\r\n{_tabString}{ctorAssignString}\r\n}}", classBodyTabLevels, _tabString)); } else { var block = constructorBody.block(); var statementList = block?.statement_list()?.GetText(); if (statementList != null) { var assignmentMatchString = $@"[\s;{{]_{_serviceIdentifier}\s*=\s*{_serviceIdentifier}\s*;"; hasCtorAssignment = Regex.Match(statementList, assignmentMatchString).Success; } if (!hasCtorAssignment) { int?finalAssignment = block.statement_list().statement().Last().Stop.TokenIndex; int assignmentStopIndex = finalAssignment ?? block.OPEN_BRACE().Symbol.TokenIndex; var assignmentStringBuilder = new StringBuilder(); assignmentStringBuilder.Append("\r\n"); assignmentStringBuilder.Append(_stringUtilService.TabString( ctorAssignString, ctorBodyTabLevels, _tabString)); var postAssignmentStopWhitespaceArray = Tokens.GetHiddenTokensToRight( assignmentStopIndex, Lexer.Hidden); string postAssignmentStopWhitespace = postAssignmentStopWhitespaceArray.Count > 0 ? postAssignmentStopWhitespaceArray[0].Text : string.Empty; if (!Regex.IsMatch(postAssignmentStopWhitespace, @"\r?\n")) { assignmentStringBuilder.Append("\r\n"); } var assignmentString = assignmentStringBuilder.ToString(); if (assignmentString.Length > 0) { _isRewritten = true; Rewriter.InsertAfter(assignmentStopIndex, assignmentString); } } } } var fieldString = fieldStringBuilder.ToString(); if (fieldString.Length > 0) { _isRewritten = true; Rewriter.InsertAfter(fieldStopIndex, fieldString); } if (constructorStringBuilder != null && constructorStopIndex != fieldStopIndex) { var constructorString = constructorStringBuilder.ToString(); if (constructorString.Length > 0) { _isRewritten = true; Rewriter.InsertAfter(Tokens.Get(constructorStopIndex ?? -1), constructorString); } } } VisitChildren(context); return(null); }
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 VisitMethod_declaration([NotNull] CSharpParser.Method_declarationContext context) { if (_isControllerClass.Peek()) { bool isNonAction = false; bool isPublic = false; var attributes = context.method_header()?.attributes()?.attribute_section(); if (attributes != null) { foreach (var attributeSection in attributes) { foreach (var attribute in attributeSection.attribute_list().attribute()) { if (attribute.GetText() == "NonAction") { isNonAction = true; } } } } var modifiers = context.method_header()?.method_modifier(); if (modifiers != null) { foreach (var modifier in modifiers) { if (modifier.GetText() == Keywords.Public) { isPublic = true; } } } if (!isNonAction && isPublic) { // Add endpoint info to results var currentNamespace = GetCurrentNamespace(); var currentClass = GetCurrentClass(); var controllerRootName = GetControllerRootName(currentClass); var actionName = context.method_header().member_name().identifier().GetText(); bool?hasId = false; var fixedParams = context.method_header()?.formal_parameter_list() ?.fixed_parameters()?.fixed_parameter(); if (fixedParams != null) { foreach (var fixedParam in fixedParams) { if (Regex.Match(fixedParam?.type_()?.GetText(), @"^int\??$").Success && fixedParam?.identifier()?.GetText() == "id") { hasId = true; } } } if (ControllerDict.NamespaceDict.ContainsKey(currentNamespace) && ControllerDict.NamespaceDict[currentNamespace] .ClassDict.ContainsKey(currentClass) && ControllerDict.NamespaceDict[currentNamespace] .ClassDict[currentClass].ActionDict.ContainsKey(actionName)) { if (!Regex.Match( context.method_body().GetText(), @"ViewData\[""BreadcrumbNode""\]\s*=").Success) { var preMethodWhitespace = Tokens.GetHiddenTokensToLeft( context.Start.TokenIndex, Lexer.Hidden); int tabLevels = 1 + ((preMethodWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels( preMethodWhitespace[0]?.Text ?? string.Empty, _tabString) : 0); int openBraceIndex = context.method_body()?.block()?.OPEN_BRACE().Symbol.TokenIndex ?? -1; if (openBraceIndex > -1) { var assignmentString = _breadcrumbCommandParserService.GenerateBreadcrumbAssignment( controllerRootName, actionName, hasId, tabLevels, _tabString); SetIsClassModifiedToTrue(); IsModified = true; Rewriter.InsertAfter(openBraceIndex, assignmentString); } } } } } 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); }