/// <summary>Should be overridden to reset settings to their default values.</summary> public override void ResetSettings() { Groups.Clear(); Groups.Add(new AlignGroup("Assignments", 1, new AlignPattern(EPattern.RegularExpression, @"(?<![+\-*/%^~&|!=<>])=(?![=<>])", 0, 1, "Assignment not preceded by: +,-,*,/,%,^,~,&,|,!,=,<,> and not followed by: =,<,>"), new AlignPattern(EPattern.RegularExpression, @"(?<![&|])[+\-*/%^~&|]={1}", -1, 2, "Assignment preceded by: +,-,*,/,%,^,~,&,| but not preceded by &&,||"), new AlignPattern(EPattern.RegularExpression, @"&&=|\|\|=", -2, 3, "Assignments &&= and ||="))); Groups.Add(new AlignGroup("Lambda", 1, new AlignPattern(EPattern.Substring, @"=>"))); Groups.Add(new AlignGroup("Comparisons", 1, new AlignPattern(EPattern.Substring, @"==", 0, 2), new AlignPattern(EPattern.Substring, @"!=", 0, 2), new AlignPattern(EPattern.Substring, @"<=", 0, 2), new AlignPattern(EPattern.Substring, @">=", 0, 2), new AlignPattern(EPattern.RegularExpression, @"(?<!=)>(?!=)", 0, 1, "> not preceded or followed by ="), new AlignPattern(EPattern.RegularExpression, @"(?<!=)<(?!=)", 0, 1, "< not preceded or followed by ="))); Groups.Add(new AlignGroup("Boolean operators", 1, new AlignPattern(EPattern.Substring, @"&&"), new AlignPattern(EPattern.Substring, @"||"))); Groups.Add(new AlignGroup("Line comments", 1, new AlignPattern(EPattern.RegularExpression, @"/{2,}", 0, 0, "Two or more '/' characters"))); Groups.Add(new AlignGroup("Open brackets", 0, new AlignPattern(EPattern.Substring, @"("))); Groups.Add(new AlignGroup("Close brackets", 0, new AlignPattern(EPattern.Substring, @")"))); Groups.Add(new AlignGroup("Scope start", 0, new AlignPattern(EPattern.Substring, @"{"))); Groups.Add(new AlignGroup("Scope end", 1, new AlignPattern(EPattern.Substring, @"}"))); Groups.Add(new AlignGroup("Increment / Decrement", 1, new AlignPattern(EPattern.Substring, @"++"), new AlignPattern(EPattern.Substring, @"--"))); Groups.Add(new AlignGroup("Plus / Minus", 1, new AlignPattern(EPattern.RegularExpression, @"(?<!\+)\+(?!\+)", 0, 1, "Matches '+' but not '++'"), new AlignPattern(EPattern.RegularExpression, @"(?<!\-)\-(?!\-)", 0, 1, "Matches '-' but not '--'"))); Groups.Add(new AlignGroup("Comma delimiter", 0, new AlignPattern(EPattern.Substring, @","))); Groups.Add(new AlignGroup("Members", 1, new AlignPattern(EPattern.RegularExpression, @"(?<![~^])(?<=\s)m_[0-9a-zA-Z_]*", 0, 1, "Matches class members that begin with 'm_'"), new AlignPattern(EPattern.RegularExpression, @"(?<![~^])(?<=\s)_[0-9a-zA-Z_]*", 0, 1, "Matches class members that begin with '_'"), new AlignPattern(EPattern.RegularExpression, @"(?<=\s)[_a-zA-z][_a-zA-Z0-9]*_", 0, 1, "Matches class members that end with '_'"))); AlignStyle = EAlignCharacters.Spaces; }
public Aligner(IEnumerable <AlignGroup> groups, EAlignCharacters style, IWpfTextView view, EAction action) { m_view = view; m_groups = groups.ToList(); m_snapshot = m_view.TextSnapshot; m_tab_size = m_view.Options.GetOptionValue(DefaultOptions.TabSizeOptionId); m_action = action; m_style = style; // Get the current line number, line, line position, and selection var selection = new Selection(m_view); // Prioritise the pattern groups to align on var grps = PrioritisePatterns(selection); // Find the edits to make var edits = FindAlignments(selection, grps, out var fallback_line_span); // Make the alignment edits if (edits.Count != 0) { switch (action) { case EAction.Align: DoAligning(edits); break; case EAction.Unalign: DoUnaligning(edits); break; } } else if (m_action == EAction.Unalign) { // If there is no selection then use the 'fallback_line_span' if available. This provides behaviour that the user // would otherwise interpret as a bug. Unalignment, when an alignment pattern is used, removes trailing whitespace // from all rows affected by the aligning. When there is nothing left to unalign, the user would still expected // trailing whitespace to be removed from a block that would otherwise be (un)aligned. var line_range = (selection.IsEmpty && fallback_line_span != null) ? fallback_line_span.Value : selection.Lines; UnalignSelection(line_range); } }
/// <summary>Load settings from AppData</summary> public override void LoadSettingsFromStorage() { // Note: the 'LoadSettingsFromXml' and 'SaveSettingsToXml' methods are // only used when the user Exports or Imports their settings. // To/From storage are used for normal saving. try { var filepath = SettingsFilepath; if (!Path_.FileExists(filepath)) { return; } // Load the settings XML file var root = XDocument.Load(filepath).Root; var grps = root.Elements(nameof(Groups), nameof(AlignGroup)).Select(x => x.As <AlignGroup>()); Groups.Assign(grps); // Load other settings AlignStyle = root.Element(nameof(AlignStyle)).As <EAlignCharacters>(EAlignCharacters.Spaces); } catch { } // Don't allow anything to throw from here, otherwise VS locks up... :-/ }