Example #1
0
        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);
        }