private void TryWriteApply()
        {
            if (!_applyOpened)
            {
                _applyOpened = true;

                IDisposable blockDisposable;

                var delegateString = _delegateType.HasValue() ? "(" + _delegateType + ")" : "";

                if (_applyPrefix != null)
                {
                    blockDisposable = _source.BlockInvariant(".{0}_XamlApply({2}({1} => ", _applyPrefix, _closureName, delegateString);
                }
                else
                {
                    blockDisposable = _source.BlockInvariant(".Apply({1}({0} => ", _closureName, delegateString);
                }

                _applyDisposable = new DisposableAction(() =>
                {
                    blockDisposable.Dispose();
                    _source.AppendLineInvariant("))");
                });
            }
        }
示例#2
0
        public static Stack <IDisposable> AddToIndentedStringBuilder(this INamedTypeSymbol namedTypeSymbol, IIndentedStringBuilder builder, Action <IIndentedStringBuilder>?beforeClassHeaderAction = null)
        {
            var     stack  = new Stack <string>();
            ISymbol symbol = namedTypeSymbol;

            while (symbol != null)
            {
                if (symbol is INamespaceSymbol namespaceSymbol)
                {
                    if (!namespaceSymbol.IsGlobalNamespace)
                    {
                        stack.Push($"namespace {namespaceSymbol}");
                    }

                    break;
                }
                else if (symbol is INamedTypeSymbol namedSymbol)
                {
                    stack.Push(GetDeclarationHeaderFromNamedTypeSymbol(namedSymbol));
                }
                else
                {
                    throw new InvalidOperationException($"Unexpected symbol type {symbol}");
                }

                symbol = symbol.ContainingSymbol;
            }

            var outputDisposableStack = new Stack <IDisposable>();

            while (stack.Count > 0)
            {
                if (stack.Count == 1)
                {
                    // Only the original symbol is left (usually a class header). Execute the given action before adding the class (usually this adds attributes).
                    beforeClassHeaderAction?.Invoke(builder);
                }

                outputDisposableStack.Push(builder.BlockInvariant(stack.Pop()));
            }

            return(outputDisposableStack);
        }