/// <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
            CSharpFormattingOptionsContainer container = this;

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

            return(null);
        }
        static void OnOptionPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            string   option   = e.NewValue as string;
            ComboBox comboBox = o as ComboBox;
            CSharpFormattingOptionsContainer container = GetContainer(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;
                    }

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

                    comboBox.SelectionChanged += ComboBox_SelectionChanged;
                    container.PropertyChanged += (sender, eventArgs) =>
                    {
                        if ((eventArgs.PropertyName == null) || (eventArgs.PropertyName == option))
                        {
                            UpdateComboBoxValue(container, option, comboBox);
                        }
                    };
                }
            }
        }
 void OnFormattingPolicyUpdated(object sender, CSharpFormattingOptionsContainer container)
 {
     if (FormattingPolicyUpdated != null)
     {
         FormattingPolicyUpdated(sender, new CSharpFormattingPolicyUpdateEventArgs(container));
     }
 }
        /// <summary>
        /// Creates a clone of current options container.
        /// </summary>
        /// <returns>Clone of options container.</returns>
        public CSharpFormattingOptionsContainer Clone()
        {
            var clone = new CSharpFormattingOptionsContainer(parent);

            clone.CloneFrom(this);
            return(clone);
        }
		/// <summary>
		/// Formats the specified part of the document.
		/// </summary>
		public static void Format(ITextEditor editor, int offset, int length, CSharpFormattingOptionsContainer optionsContainer)
		{
			var formatter = new CSharpFormatter(optionsContainer.GetEffectiveOptions(), editor.ToEditorOptions());
			formatter.AddFormattingRegion(new DomRegion(editor.Document.GetLocation(offset), editor.Document.GetLocation(offset + length)));
			var changes = formatter.AnalyzeFormatting(editor.Document, SyntaxTree.Parse(editor.Document));
			changes.ApplyChanges(offset, length);
		}
        /// <summary>
        /// Formats the specified part of the document.
        /// </summary>
        public static void Format(ITextEditor editor, int offset, int length, CSharpFormattingOptionsContainer optionsContainer)
        {
            var formatter = new CSharpFormatter(optionsContainer.GetEffectiveOptions(), editor.ToEditorOptions());

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

            changes.ApplyChanges(offset, length);
        }
Exemple #7
0
		/// <summary>
		/// Clones all properties from another options container.
		/// </summary>
		/// <returns>Clone of options container.</returns>
		public void CloneFrom(CSharpFormattingOptionsContainer options)
		{
			activeOptions.Clear();
			foreach (var activeOption in options.activeOptions)
				activeOptions.Add(activeOption);
			cachedOptions = options.cachedOptions.Clone();
			indentationSize = options.indentationSize;
			convertTabsToSpaces = options.convertTabsToSpaces;
			OnPropertyChanged(null);
		}
        /// <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 CSharpFormattingPolicy(Properties propertiesContainer, CSharpFormattingOptionsContainer initialContainer)
        {
            if (initialContainer == null)
            {
                throw new ArgumentNullException("initialContainer");
            }

            this.propertiesContainer = propertiesContainer ?? new Properties();
            optionsContainer         = initialContainer;
        }
Exemple #9
0
		private CSharpFormattingOptionsContainer(CSharpFormattingOptionsContainer parent, HashSet<string> activeOptions)
		{
			this.parent = parent;
			if (parent != null) {
				parent.PropertyChanged += HandlePropertyChanged;
			}
			this.activeOptions = activeOptions;
			Reset();
			cachedOptions = CreateCachedOptions();
		}
 /// <summary>
 /// Clones all properties from another options container.
 /// </summary>
 /// <returns>Clone of options container.</returns>
 public void CloneFrom(CSharpFormattingOptionsContainer options)
 {
     activeOptions.Clear();
     foreach (var activeOption in options.activeOptions)
     {
         activeOptions.Add(activeOption);
     }
     cachedOptions  = options.cachedOptions.Clone();
     autoFormatting = options.autoFormatting;
     OnPropertyChanged(null);
 }
Exemple #11
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);
            return(true);
        }
Exemple #12
0
        /// <summary>
        /// Creates a new instance of formatting options persistence helper, 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 CSharpFormattingOptionsPersistence(Properties propertiesContainer, CSharpFormattingOptionsContainer initialContainer)
        {
            if (propertiesContainer == null)
            {
                throw new ArgumentNullException("propertiesContainer");
            }
            if (initialContainer == null)
            {
                throw new ArgumentNullException("initialContainer");
            }

            this.propertiesContainer = propertiesContainer;
            optionsContainer         = initialContainer;
        }
		/// <summary>
		/// Formats the specified part of the document.
		/// </summary>
		public static void Format(ITextEditor editor, int offset, int length, CSharpFormattingOptionsContainer 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 CSharpFormatter(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);
		}
Exemple #14
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
			CSharpFormattingOptionsContainer container = this;
			do
			{
				int? val = container.indentationSize;
				if (val.HasValue) {
					return val.Value;
				}
				container = container.parent;
			} while (container != null);
			
			return null;
		}
Exemple #15
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
			CSharpFormattingOptionsContainer container = this;
			do
			{
				bool? val = container.convertTabsToSpaces;
				if (val.HasValue) {
					return val.Value;
				}
				container = container.parent;
			} while (container != null);
			
			return null;
		}
        static void UpdateOptionBinding(DependencyObject o)
        {
            ComboBox comboBox = o as ComboBox;
            CSharpFormattingOptionsContainer 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);
                        }
                    };
                }
            }
        }
        static void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;

            if (comboBox != null)
            {
                FormattingOption option = GetFormattingOption(comboBox);
                CSharpFormattingOptionsContainer 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);
                    }
                }
            }
        }
        /// <summary>
        /// Formats the specified part of the document.
        /// </summary>
        public static void Format(ITextEditor editor, int offset, int length, CSharpFormattingOptionsContainer 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 CSharpFormatter(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);
        }
        static void UpdateComboBoxValue(CSharpFormattingOptionsContainer container, string option, ComboBox comboBox)
        {
            object currentValue = container.GetOption(option);

            comboBox.SelectedItem = comboBox.Items.OfType <ComboBoxItem>().FirstOrDefault(item => object.Equals(currentValue, item.Tag));
        }
		/// <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 CSharpFormattingPolicy(Properties propertiesContainer, CSharpFormattingOptionsContainer initialContainer)
		{
			if (initialContainer == null)
				throw new ArgumentNullException("initialContainer");
			
			this.propertiesContainer = propertiesContainer ?? new Properties();
			optionsContainer = initialContainer;
		}
		static void UpdateComboBoxValue(CSharpFormattingOptionsContainer container, string option, ComboBox comboBox)
		{
			object currentValue = container.GetOption(option);
			comboBox.SelectedItem = comboBox.Items.OfType<ComboBoxItem>().FirstOrDefault(item => object.Equals(currentValue, item.Tag));
		}
		public CodeEditorFormattingOptionsAdapter(ITextEditorOptions globalOptions, CSharpFormattingOptionsContainer container)
		{
			if (globalOptions == null)
				throw new ArgumentNullException("globalOptions");
			if (container == null)
				throw new ArgumentNullException("container");
			
			this.globalOptions = globalOptions;
			this.globalCodeEditorOptions = globalOptions as ICodeEditorOptions;
			this.container = container;
			
			CSharpFormattingPolicies.Instance.FormattingPolicyUpdated += OnFormattingPolicyUpdated;
			globalOptions.PropertyChanged += OnGlobalOptionsPropertyChanged;
		}
		/// <summary>
		/// Starts editing operation by creating a working copy of current formatter settings.
		/// </summary>
		/// <returns>
		/// New working copy of managed options container.
		/// </returns>
		public CSharpFormattingOptionsContainer StartEditing()
		{
			optionsContainerWorkingCopy = optionsContainer.Clone();
			return optionsContainerWorkingCopy;
		}
Exemple #24
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 CSharpFormattingOptionsContainer StartEditing()
 {
     optionsContainerWorkingCopy = optionsContainer.Clone();
     return(optionsContainerWorkingCopy);
 }
		public CSharpFormattingPolicyUpdateEventArgs(CSharpFormattingOptionsContainer container)
		{
			OptionsContainer = container;
		}
		/// <summary>
		/// Creates a clone of current options container.
		/// </summary>
		/// <returns>Clone of options container.</returns>
		public CSharpFormattingOptionsContainer Clone()
		{
			var clone = new CSharpFormattingOptionsContainer(parent);
			clone.CloneFrom(this);
			return clone;
		}
Exemple #27
0
		public CSharpFormattingOptionsContainer(CSharpFormattingOptionsContainer parent = null)
			: this(parent, new HashSet<string>())
		{
		}
		public static void SetContainer(Selector element, CSharpFormattingOptionsContainer container)
		{
			element.SetValue(ContainerProperty, container);
		}
		/// <summary>
		/// Clones all properties from another options container.
		/// </summary>
		/// <returns>Clone of options container.</returns>
		public void CloneFrom(CSharpFormattingOptionsContainer options)
		{
			activeOptions.Clear();
			foreach (var activeOption in options.activeOptions)
				activeOptions.Add(activeOption);
			cachedOptions = options.cachedOptions.Clone();
			autoFormatting = options.autoFormatting;
			OnPropertyChanged(null);
		}
 public CSharpFormattingPolicyUpdateEventArgs(CSharpFormattingOptionsContainer container)
 {
     OptionsContainer = container;
 }
 public static void SetContainer(Selector element, CSharpFormattingOptionsContainer container)
 {
     element.SetValue(ContainerProperty, container);
 }
		/// <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;
		}
Exemple #33
0
		public CodeEditorFormattingOptionsAdapter(TextEditorOptions originalAvalonEditOptions, ITextEditorOptions originalSDOptions, CSharpFormattingOptionsContainer container)
		{
			if (originalAvalonEditOptions == null)
				throw new ArgumentNullException("originalAvalonEditOptions");
			if (originalSDOptions == null)
				throw new ArgumentNullException("originalSDOptions");
			if (container == null)
				throw new ArgumentNullException("container");
			
			this.originalAvalonEditOptions = originalAvalonEditOptions;
			this.avalonEditOptions = new TextEditorOptions(originalAvalonEditOptions);
			this.originalSDOptions = originalSDOptions;
			this.container = container;
			
			// Update overridden options once
			UpdateOverriddenProperties();
			
			CSharpFormattingPolicies.Instance.FormattingPolicyUpdated += OnFormattingPolicyUpdated;
			this.originalAvalonEditOptions.PropertyChanged += OnOrigAvalonOptionsPropertyChanged;
			this.originalSDOptions.PropertyChanged += OnSDOptionsPropertyChanged;
		}
		void OnFormattingPolicyUpdated(object sender, CSharpFormattingOptionsContainer container)
		{
			if (FormattingPolicyUpdated != null) {
				FormattingPolicyUpdated(sender, new CSharpFormattingPolicyUpdateEventArgs(container));
			}
		}
		private void FillPresetList(CSharpFormattingOptionsContainer container)
		{
			presets["(default)"] = () => null;
			presets["Empty"] = FormattingOptionsFactory.CreateEmpty;
			presets["SharpDevelop"] = FormattingOptionsFactory.CreateSharpDevelop;
			presets["Mono"] = FormattingOptionsFactory.CreateMono;
			presets["K&R"] = FormattingOptionsFactory.CreateKRStyle;
			presets["Allman"] = FormattingOptionsFactory.CreateAllman;
			presets["Whitesmiths"] = FormattingOptionsFactory.CreateWhitesmiths;
			presets["GNU"] = FormattingOptionsFactory.CreateGNU;
			
			// TODO Localize!
			if (container.Parent != null) {
				// Add a "default" preset
				presetItems.Add(new ComboBoxItem { Content = (container.Parent ?? container).DefaultText, Tag = "(default)" });
			}
			presetItems.Add(new ComboBoxItem { Content = "SharpDevelop", Tag = "SharpDevelop" });
			presetItems.Add(new ComboBoxItem { Content = "Mono", Tag = "Mono" });
			presetItems.Add(new ComboBoxItem { Content = "K&R", Tag = "K&R" });
			presetItems.Add(new ComboBoxItem { Content = "Allman", Tag = "Allman" });
			presetItems.Add(new ComboBoxItem { Content = "Whitesmiths", Tag = "Whitesmiths" });
			presetItems.Add(new ComboBoxItem { Content = "GNU", Tag = "GNU" });
			presetItems.Add(new ComboBoxItem { Content = "Empty", Tag = "Empty" });
			
			presetComboBox.SelectedIndex = 0;
		}