void OnFormattingPolicyUpdated(object sender, AlFormattingOptionsContainer container) { if (FormattingPolicyUpdated != null) { FormattingPolicyUpdated(sender, new AlFormattingPolicyUpdateEventArgs(container)); } }
/// <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); }
/// <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); }
/// <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; }
private AlFormattingOptionsContainer(AlFormattingOptionsContainer 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(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); }
/// <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); }
/// <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); }
/// <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); }
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); } }; } } }
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); } } } }
/// <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); }
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)); }
public AlFormattingOptionsContainer(AlFormattingOptionsContainer parent = null) : this(parent, new HashSet <string>()) { }
/// <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); }
public AlFormattingPolicyUpdateEventArgs(AlFormattingOptionsContainer container) { OptionsContainer = container; }
public static void SetContainer(Selector element, AlFormattingOptionsContainer container) { element.SetValue(ContainerProperty, container); }