Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Resolving Symbolic References in a CodeDOM (Part 7)

, 2 Dec 2012
Resolving symbolic references in a CodeDOM.
Nova.0.6.exe.zip
Nova.0.6.zip
Nova.CLI
Properties
Nova.CodeDOM
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Interfaces
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Assemblies
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Parsing
Base
Properties
Rendering
Resolving
Utilities
Mono.Cecil
Reflection
Nova.Examples
Properties
Nova.Studio
Images
About.png
Configuration.png
EditCopy.png
EditCut.png
EditDelete.png
EditPaste.png
EditRedo.png
EditUndo.png
Error.png
Exit.png
FileNew.png
FileOpen.png
FileSave.png
FileSaveAll.png
FileSaveAs.png
Find.png
Help.png
Info.png
Logo.png
Options.png
Print.png
PrintPreview.png
Properties.png
Todo.png
Warning.png
Objects.ico
Properties
Settings.settings
Nova.Test
Properties
Nova.UI
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Properties
Resolving
Utilties
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

using Nova.CodeDOM;
using Expression = Nova.CodeDOM.Expression;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Statement"/>.
    /// </summary>
    public abstract class StatementVM : CodeObjectVM
    {
        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Statement"/>.
        /// </summary>
        protected StatementVM(Statement statement, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(statement, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Statement"/> model.
        /// </summary>
        public Statement Statement
        {
            get { return (Statement)CodeObject; }
        }

        #endregion

        #region /* RENDERING */

        public override int BorderHPad
        {
            get { return 2; }
        }

        public override int BorderVPad
        {
            get { return 1; }
        }

        /// <summary>
        /// Determines if terminators should be hidden.
        /// </summary>
        public static bool HideTerminators;

        /// <summary>
        /// Determines if statement argument parens should be hidden.
        /// </summary>
        public static bool HideStatementParens;

        protected virtual void RenderPrefix(CodeRenderer renderer, RenderFlags flags)
        { }

        protected virtual void RenderStatement(CodeRenderer renderer, RenderFlags flags)
        {
            renderer.RenderText(Statement.Keyword, KEYWORD_BRUSH, this);
        }

        protected virtual void RenderArgumentPrefix(CodeRenderer renderer, RenderFlags flags)
        {
            renderer.RenderText(" ", PUNC_BRUSH, this);
        }

        protected virtual void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        { }

        protected virtual void RenderSuffix(CodeRenderer renderer, RenderFlags flags)
        {
            if (!flags.HasFlag(RenderFlags.Description))
                RenderTerminator(renderer, flags);
        }

        protected internal void RenderTerminator(CodeRenderer renderer, RenderFlags flags)
        {
            if (Statement.HasTerminator && !HideTerminators)
                renderer.RenderText(Statement.Terminator, PUNC_BRUSH, this);
        }

        protected override void RenderAfter(CodeRenderer renderer, RenderFlags flags)
        {
            if (!flags.HasFlag(RenderFlags.NoPostAnnotations))
                RenderAnnotations(renderer, AnnotationFlags.IsPostfix, flags);
        }

        protected virtual bool RenderParens()
        {
            return !HideStatementParens;
        }

        public override void Render(CodeRenderer renderer, RenderFlags flags)
        {
            int newLines = Statement.NewLines;
            bool isPrefix = flags.HasFlag(RenderFlags.IsPrefix);
            if (!isPrefix && newLines > 0)
            {
                if (!flags.HasFlag(RenderFlags.SuppressNewLine))
                    renderer.NewLines(newLines, ParentVM);
            }
            else if (flags.HasFlag(RenderFlags.PrefixSpace))
                renderer.RenderText(" ", PUNC_BRUSH, ParentVM);
            flags &= ~RenderFlags.SuppressNewLine;

            bool increaseIndent = flags.HasFlag(RenderFlags.IncreaseIndent);
            if (!flags.HasFlag(RenderFlags.NoBorder))
            {
                CreateBorder(renderer, flags);
                increaseIndent = true;  // Always increase the indent level for wrapped lines inside a border
            }

            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            RenderBefore(renderer, passFlags | RenderFlags.IsPrefix);

            // Increase the indent level for any newlines that occur within the statement if the flag is set
            if (increaseIndent)
                renderer.IndentOnNewLineBegin(this);

            RenderPrefix(renderer, flags);
            RenderStatement(renderer, flags);
            if (Statement.HasArgument)
            {
                RenderArgumentPrefix(renderer, passFlags);
                bool showParens = Statement.HasArgumentParens && RenderParens();
                if (showParens)
                    renderer.RenderText(Expression.ParseTokenStartGroup, PUNC_BRUSH, this);
                RenderArgument(renderer, passFlags | RenderFlags.ForceBorder);
                if (showParens)
                {
                    if (Statement.IsEndFirstOnLine)
                        renderer.NewLine();
                    renderer.RenderText(Expression.ParseTokenEndGroup, PUNC_BRUSH, this);
                }
            }
            RenderSuffix(renderer, flags);
            RenderEOLComments(renderer, flags);

            if (increaseIndent)
                renderer.IndentOnNewLineEnd(this);

            RenderAfter(renderer, passFlags | (flags & RenderFlags.NoPostAnnotations));

            // If we have a border, return to the previous one
            if (!flags.HasFlag(RenderFlags.NoBorder))
                renderer.ReturnToBorderParent(this);

            if (isPrefix)
            {
                // If this object is rendered as a child prefix object of another, then IsFirstOnLine actually
                // represents IsLastOnLine, and we render the whitespace here at the end instead of at the top.
                if (newLines > 0)
                    renderer.NewLines(newLines, ParentVM);
                else
                    renderer.RenderText(" ", PUNC_BRUSH, ParentVM);
            }
        }

        #endregion

        #region /* COMMANDS */

        public static readonly RoutedCommand HideTerminatorsCommand = new RoutedCommand("Hide Terminators", typeof(StatementVM));
        public static readonly RoutedCommand HideStatementParensCommand = new RoutedCommand("Hide Statement Parens", typeof(StatementVM));

        public static new void BindCommands(Window window)
        {
            WPFUtil.AddCommandBinding(window, HideTerminatorsCommand, hideTerminators_Executed);
            WPFUtil.AddCommandBinding(window, HideStatementParensCommand, hideParens_Executed);
        }

        private static void hideTerminators_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            HideTerminators = !HideTerminators;
            CodeRenderer.ReRenderAll();
        }

        private static void hideParens_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            HideStatementParens = !HideStatementParens;
            CodeRenderer.ReRenderAll();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

About the Author

KenBeckett
Software Developer (Senior)
United States United States
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 2 Dec 2012
Article Copyright 2012 by KenBeckett
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid