Example #1
0
 void OnFormattingPolicyUpdated(object sender, AlFormattingOptionsContainer container)
 {
     if (FormattingPolicyUpdated != null)
     {
         FormattingPolicyUpdated(sender, new AlFormattingPolicyUpdateEventArgs(container));
     }
 }
Example #2
0
        /// <summary>
        /// Creates a clone of current options container.
        /// </summary>
        /// <returns>Clone of options container.</returns>
        public AlFormattingOptionsContainer Clone()
        {
            var clone = new AlFormattingOptionsContainer(parent);

            clone.CloneFrom(this);
            return(clone);
        }
Example #3
0
        /// <summary>
        /// Retrieves the value of a formatting option by looking at current and (if nothing set here) parent
        /// containers.
        /// </summary>
        /// <param name="option">Name of option</param>
        /// <returns>True, if option with given type could be found in hierarchy. False otherwise.</returns>
        public object GetEffectiveOption(string option)
        {
            // Run up the hierarchy until we find a defined value for property
            AlFormattingOptionsContainer container = this;

            do
            {
                object val = null;
                if (container.activeOptions.Contains(option))
                {
                    PropertyInfo propertyInfo = typeof(AlFormattingOptions).GetProperty(option);
                    if (propertyInfo != null)
                    {
                        val = propertyInfo.GetValue(container.cachedOptions);
                    }
                }
                if (val != null)
                {
                    return(val);
                }
                container = container.parent;
            } while (container != null);

            return(null);
        }
Example #4
0
        /// <summary>
        /// Creates a new instance of formatting options policy, using given options to predefine the options container.
        /// </summary>
        /// <param name="propertiesContainer">Properties container to load from and save to.</param>
        /// <param name="initialContainer">Initial (empty) instance of formatting options container.</param>
        public AlFormattingPolicy(Properties propertiesContainer, AlFormattingOptionsContainer initialContainer)
        {
            if (initialContainer == null)
            {
                throw new ArgumentNullException("initialContainer");
            }

            this.propertiesContainer = propertiesContainer ?? new Properties();
            optionsContainer         = initialContainer;
        }
Example #5
0
 private AlFormattingOptionsContainer(AlFormattingOptionsContainer parent, HashSet <string> activeOptions)
 {
     this.parent = parent;
     if (parent != null)
     {
         parent.PropertyChanged += HandlePropertyChanged;
     }
     this.activeOptions = activeOptions;
     Reset();
     cachedOptions = CreateCachedOptions();
 }
Example #6
0
 /// <summary>
 /// Clones all properties from another options container.
 /// </summary>
 /// <returns>Clone of options container.</returns>
 public void CloneFrom(AlFormattingOptionsContainer options)
 {
     activeOptions.Clear();
     foreach (var activeOption in options.activeOptions)
     {
         activeOptions.Add(activeOption);
     }
     cachedOptions       = options.cachedOptions.Clone();
     indentationSize     = options.indentationSize;
     convertTabsToSpaces = options.convertTabsToSpaces;
     OnPropertyChanged(null);
 }
Example #7
0
        /// <summary>
        /// Saves formatting settings to properties container.
        /// </summary>
        /// <returns><c>True</c> if successful, <c>false</c> otherwise</returns>
        public bool Save()
        {
            // Apply all changes on working copy to main options container
            if (optionsContainerWorkingCopy != null)
            {
                optionsContainer.CloneFrom(optionsContainerWorkingCopy);
                optionsContainerWorkingCopy = null;
            }

            // Convert to SD properties
            optionsContainer.Save(propertiesContainer);
            OnFormattingPolicyUpdated(this, optionsContainer);
            return(true);
        }
Example #8
0
        /// <summary>
        /// Retrieves the value of v option by looking at current and (if nothing set here) parent
        /// containers.
        /// </summary>
        public bool?GetEffectiveConvertTabsToSpaces()
        {
            // Run up the hierarchy until we find a defined value for property
            AlFormattingOptionsContainer container = this;

            do
            {
                bool?val = container.convertTabsToSpaces;
                if (val.HasValue)
                {
                    return(val.Value);
                }
                container = container.parent;
            } while (container != null);

            return(null);
        }
Example #9
0
        /// <summary>
        /// Retrieves the value of "IndentationSize" option by looking at current and (if nothing set here) parent
        /// containers.
        /// </summary>
        public int?GetEffectiveIndentationSize()
        {
            // Run up the hierarchy until we find a defined value for property
            AlFormattingOptionsContainer container = this;

            do
            {
                int?val = container.indentationSize;
                if (val.HasValue)
                {
                    return(val.Value);
                }
                container = container.parent;
            } while (container != null);

            return(null);
        }
Example #10
0
        static void UpdateOptionBinding(DependencyObject o)
        {
            ComboBox comboBox = o as ComboBox;
            AlFormattingOptionsContainer container = GetContainer(comboBox);
            FormattingOption             option    = GetFormattingOption(comboBox);

            if ((option != null) && (comboBox != null) && (container != null))
            {
                if (container != null)
                {
                    if (container.Parent != null)
                    {
                        // Add "default" entry in ComboBox
                        comboBox.Items.Add(new ComboBoxItem {
                            Content = (container.Parent ?? container).DefaultText,
                            Tag     = null
                        });
                        comboBox.SelectedIndex = 0;
                    }
                    else if (option.AlwaysAllowDefault)
                    {
                        // Also add "default" entry, but without changeable text by container
                        comboBox.Items.Add(new ComboBoxItem {
                            Content = "(default)",
                            Tag     = null
                        });
                        comboBox.SelectedIndex = 0;
                    }

                    Type optionType = container.GetOptionType(option.Option);
                    FillComboValues(comboBox, optionType);
                    UpdateComboBoxValue(container, option.Option, comboBox);

                    comboBox.SelectionChanged += ComboBox_SelectionChanged;
                    container.PropertyChanged += (sender, eventArgs) =>
                    {
                        if ((eventArgs.PropertyName == null) || (eventArgs.PropertyName == option.Option))
                        {
                            UpdateComboBoxValue(container, option.Option, comboBox);
                        }
                    };
                }
            }
        }
Example #11
0
        static void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;

            if (comboBox != null)
            {
                FormattingOption             option    = GetFormattingOption(comboBox);
                AlFormattingOptionsContainer container = GetContainer(comboBox);
                if ((container != null) && (option != null))
                {
                    ComboBoxItem selectedItem = comboBox.SelectedItem as ComboBoxItem;
                    if (selectedItem != null)
                    {
                        // Set option to appropriate value
                        container.SetOption(option.Option, selectedItem.Tag);
                    }
                }
            }
        }
Example #12
0
        /// <summary>
        /// Formats the specified part of the document.
        /// </summary>
        public static void Format(ITextEditor editor, int offset, int length, AlFormattingOptionsContainer optionsContainer)
        {
            SyntaxTree syntaxTree = SyntaxTree.Parse(editor.Document);

            if (syntaxTree.Errors.Count > 0)
            {
                // Don't format files containing syntax errors!
                return;
            }

            TextEditorOptions editorOptions = editor.ToEditorOptions();

            optionsContainer.CustomizeEditorOptions(editorOptions);
            var formatter = new AlFormatter(optionsContainer.GetEffectiveOptions(), editorOptions);

            formatter.AddFormattingRegion(new DomRegion(editor.Document.GetLocation(offset), editor.Document.GetLocation(offset + length)));
            var changes = formatter.AnalyzeFormatting(editor.Document, syntaxTree);

            changes.ApplyChanges(offset, length);
        }
Example #13
0
        static void UpdateComboBoxValue(AlFormattingOptionsContainer container, string option, ComboBox comboBox)
        {
            object currentValue = container.GetOption(option);

            comboBox.SelectedItem = comboBox.Items.OfType <ComboBoxItem>().FirstOrDefault(item => object.Equals(currentValue, item.Tag));
        }
Example #14
0
 public AlFormattingOptionsContainer(AlFormattingOptionsContainer parent = null)
     : this(parent, new HashSet <string>())
 {
 }
Example #15
0
 /// <summary>
 /// Starts editing operation by creating a working copy of current formatter settings.
 /// </summary>
 /// <returns>
 /// New working copy of managed options container.
 /// </returns>
 public AlFormattingOptionsContainer StartEditing()
 {
     optionsContainerWorkingCopy = optionsContainer.Clone();
     return(optionsContainerWorkingCopy);
 }
Example #16
0
 public AlFormattingPolicyUpdateEventArgs(AlFormattingOptionsContainer container)
 {
     OptionsContainer = container;
 }
Example #17
0
 public static void SetContainer(Selector element, AlFormattingOptionsContainer container)
 {
     element.SetValue(ContainerProperty, container);
 }