Exemple #1
0
 public string Indent(
     [NotNull] ITreeNode node,
     CustomIndentType indentType,
     [NotNull] FmtSettingsClassic <CSharpFormatSettingsKey> settings
     )
 {
     if (node == null)
     {
         throw new ArgumentNullException(nameof(node));
     }
     if (settings == null)
     {
         throw new ArgumentNullException(nameof(settings));
     }
     if (!node.IsPhysical())
     {
         return(null);
     }
     if (!IsInT4File(node))
     {
         return(null);
     }
     if (indentType == CustomIndentType.RelativeNodeCalculation)
     {
         return(node.GetIndentViaDocument());
     }
     if (IsEndComment(node))
     {
         return(IndentBlockEnd());
     }
     if (IsTransformTextMember(node))
     {
         return(IndentTransformTextMember(node, indentType, settings));
     }
     if (IsFeatureBlockMember(node))
     {
         return(IndentFeatureBlockMember(node, settings));
     }
     return(null);
 }
Exemple #2
0
        private string IndentTransformTextMember(
            [NotNull] ITreeNode node,
            CustomIndentType indentType,
            FmtSettingsClassic <CSharpFormatSettingsKey> settings
            )
        {
            var rangeTranslator = GetRangeTranslator(node);

            if (indentType == CustomIndentType.RelativeLineCalculation)
            {
                return(CalculateRelativeIndentInTransformText(node, rangeTranslator));
            }

            var statement = node as ICSharpStatement;

            if (node.GetTokenType() == CSharpTokenType.LBRACE)
            {
                statement = node.Parent as IBlock;
            }
            var block = BlockNavigator.GetByStatement(statement);

            if (node is IComment || node is IStartRegion || node is IEndRegion)
            {
                block = node.GetContainingNode <ITreeNode>() as IBlock;
            }
            if (block == null)
            {
                return(null);
            }

            var generatedTreeRange      = new TreeTextRange(node.GetTreeStartOffset());
            var blockGeneratedTreeRange = new TreeTextRange(block.GetTreeStartOffset());
            var originalRange           = rangeTranslator.GeneratedToOriginal(generatedTreeRange);
            var blockOriginalRange      = rangeTranslator.GeneratedToOriginal(blockGeneratedTreeRange);

            if (!originalRange.IsValid())
            {
                return(null);
            }

            var t4Element = rangeTranslator.OriginalFile.FindNodeAt(originalRange);
            var codeBlock = t4Element?.GetParentOfType <IT4CodeBlock>();

            if (codeBlock == null)
            {
                return(null);
            }

            string indentFromPreviousStatement = GetIndentFromPreviousStatement(node, rangeTranslator);

            if (indentFromPreviousStatement != null)
            {
                return(indentFromPreviousStatement);
            }

            if (blockOriginalRange.IsValid() && codeBlock.GetTreeTextRange().Contains(blockOriginalRange))
            {
                return(null);
            }
            var blockStart = codeBlock.GetTreeStartOffset();
            int nodeStart  = originalRange.StartOffset.Offset;

            if (!HasLineBreak(codeBlock, nodeStart, blockStart))
            {
                return(null);
            }
            if (HasStatementsBefore(rangeTranslator, node))
            {
                return(null);
            }
            return(settings.Settings.GetIndentStr());
        }