Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit fa88338

Browse files
committed
Added "Reformat" menu item in Edit and editor context menues. Showing "(solution)" or "(global)" instead of "(default)" on settings referring to parent settings container.
1 parent 5da7b92 commit fa88338

8 files changed

Lines changed: 85 additions & 8 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingOptionsContainer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ private CSharpFormattingOptionsContainer(CSharpFormattingOptionsContainer parent
5555
cachedOptions = CreateCachedOptions();
5656
}
5757

58+
public string DefaultText
59+
{
60+
get;
61+
set;
62+
}
63+
5864
public CSharpFormattingOptionsContainer Parent
5965
{
6066
get {
@@ -69,7 +75,7 @@ public CSharpFormattingOptionsContainer Parent
6975
public void Reset(CSharpFormattingOptions options = null)
7076
{
7177
activeOptions.Clear();
72-
cachedOptions = options ?? FormattingOptionsFactory.CreateEmpty();
78+
cachedOptions = options ?? CreateCachedOptions();
7379
if ((options != null) || (parent == null)) {
7480
// Activate all options
7581
foreach (var property in typeof(CSharpFormattingOptions).GetProperties()) {

src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingOptionsPersistence.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public static void Initialize()
4747

4848
// Load global settings
4949
GlobalOptions = new CSharpFormattingOptionsPersistence(
50-
SD.PropertyService.MainPropertiesContainer, new CSharpFormattingOptionsContainer());
50+
SD.PropertyService.MainPropertiesContainer, new CSharpFormattingOptionsContainer()
51+
{
52+
DefaultText = "(global)" // TODO Localize!
53+
});
5154
GlobalOptions.Load();
5255

5356
// Handlers for solution loading/unloading
@@ -76,21 +79,27 @@ public static CSharpFormattingOptionsPersistence GetProjectOptions(IProject proj
7679
// Lazily create options container for project
7780
projectOptions[key] = new CSharpFormattingOptionsPersistence(
7881
csproject.ExtensionProperties,
79-
new CSharpFormattingOptionsContainer((SolutionOptions ?? GlobalOptions).OptionsContainer));
82+
new CSharpFormattingOptionsContainer((SolutionOptions ?? GlobalOptions).OptionsContainer)
83+
{
84+
DefaultText = "(project)" // TODO Localize!
85+
});
8086
}
8187

8288
return projectOptions[key];
8389
}
8490

8591
return SolutionOptions ?? GlobalOptions;
8692
}
87-
93+
8894
static void SolutionOpened(object sender, SolutionEventArgs e)
8995
{
9096
// Load solution settings
9197
SolutionOptions = new CSharpFormattingOptionsPersistence(
9298
e.Solution.GlobalPreferences,
93-
new CSharpFormattingOptionsContainer(GlobalOptions.OptionsContainer));
99+
new CSharpFormattingOptionsContainer(GlobalOptions.OptionsContainer)
100+
{
101+
DefaultText = "(solution)" // TODO Localize!
102+
});
94103
}
95104

96105
static void SolutionClosed(object sender, SolutionEventArgs e)

src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,18 @@ void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char
425425
}
426426
}
427427
return;
428+
429+
case (char) 0:
430+
// In any other case: Simply format selection or whole document
431+
var formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(SD.ProjectService.CurrentProject);
432+
int formattedTextOffset = 0;
433+
int formattedTextLength = textArea.Document.TextLength;
434+
if (textArea.SelectionLength != 0) {
435+
formattedTextOffset = textArea.SelectionStart;
436+
formattedTextLength = textArea.SelectionLength;
437+
}
438+
CSharpFormatterHelper.Format(textArea, formattedTextOffset, formattedTextLength, formattingOptions.OptionsContainer);
439+
break;
428440
}
429441
}
430442

src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/FormattingOptionBinding.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ static void OnOptionPropertyChanged(DependencyObject o, DependencyPropertyChange
6868
if (container.Parent != null) {
6969
// Add "default" entry in ComboBox
7070
// TODO Add located resource, maybe context-bound, like "(solution)" or "(global)"!
71-
comboBox.Items.Add(new ComboBoxItem { Content = "(default)", Tag = null });
71+
comboBox.Items.Add(new ComboBoxItem {
72+
Content = (container.Parent ?? container).DefaultText,
73+
Tag = null
74+
});
7275
comboBox.SelectedIndex = 0;
7376
}
7477

src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static void OnOptionsContainerPropertyChanged(DependencyObject o, DependencyProp
146146

147147
private void FillPresetList(CSharpFormattingOptionsContainer container)
148148
{
149-
presets["Default"] = () => null;
149+
presets["(default)"] = () => null;
150150
presets["Empty"] = FormattingOptionsFactory.CreateEmpty;
151151
presets["SharpDevelop"] = FormattingOptionsFactory.CreateSharpDevelop;
152152
presets["Mono"] = FormattingOptionsFactory.CreateMono;
@@ -158,7 +158,7 @@ private void FillPresetList(CSharpFormattingOptionsContainer container)
158158
// TODO Localize!
159159
if (container.Parent != null) {
160160
// Add a "default" preset
161-
presetItems.Add(new ComboBoxItem { Content = "Default", Tag = "Default" });
161+
presetItems.Add(new ComboBoxItem { Content = (container.Parent ?? container).DefaultText, Tag = "(default)" });
162162
}
163163
presetItems.Add(new ComboBoxItem { Content = "Empty", Tag = "Empty" });
164164
presetItems.Add(new ComboBoxItem { Content = "SharpDevelop", Tag = "SharpDevelop" });

src/Main/Base/Project/ICSharpCode.SharpDevelop.addin

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,10 @@
20782078
label = "${res:XML.TextAreaContextMenu.Indent}"
20792079
shortcut = "Control|I"
20802080
class = "ICSharpCode.SharpDevelop.Editor.Commands.IndentSelection" />
2081+
<MenuItem id = "Reformat"
2082+
label = "Reformat"
2083+
shortcut = "Control|Alt|F"
2084+
class = "ICSharpCode.SharpDevelop.Editor.Commands.ReformatSelection" />
20812085
</Path>
20822086

20832087
<Path name = "/SharpDevelop/ViewContent/TextEditor/ToolTips">
@@ -2122,6 +2126,7 @@
21222126
<MenuItem id = "Separator4" type = "Separator" />
21232127
<Include id = "Comment" item = "/SharpDevelop/ViewContent/TextEditor/ContextMenu/Comment" />
21242128
<Include id = "Indent" item = "/SharpDevelop/ViewContent/TextEditor/ContextMenu/Indent" />
2129+
<Include id = "Reformat" item = "/SharpDevelop/ViewContent/TextEditor/ContextMenu/Reformat" />
21252130
</MenuItem>
21262131
<MenuItem insertbefore = "SelectAll" id = "Separator3" type = "Separator" />
21272132
</Path>

src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
<Compile Include="Src\Editor\Commands\DeclaringTypeSubMenuBuilder.cs" />
230230
<Compile Include="Src\Editor\Commands\FindReferencesCommand.cs" />
231231
<Compile Include="Src\Editor\Commands\GotoLineNumber.cs" />
232+
<Compile Include="Src\Editor\Commands\ReformatSelection.cs" />
232233
<Compile Include="Src\Editor\Commands\SymbolUnderCaretMenuCommand.cs" />
233234
<Compile Include="Editor\CodeCompletion\CodeCompletionBinding.cs" />
234235
<Compile Include="Editor\CodeCompletion\CodeCompletionDataUsageCache.cs" />
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System;
20+
using ICSharpCode.Core;
21+
22+
namespace ICSharpCode.SharpDevelop.Editor.Commands
23+
{
24+
/// <summary>
25+
/// Menu command to reformat selected code or whole document according to formatter settings.
26+
/// </summary>
27+
public class ReformatSelection : AbstractMenuCommand
28+
{
29+
/// <summary>
30+
/// Starts the command.
31+
/// </summary>
32+
public override void Run()
33+
{
34+
ITextEditor editor = SD.GetActiveViewContentService<ITextEditor>();
35+
if (editor == null)
36+
return;
37+
38+
editor.Language.FormattingStrategy.FormatLine(editor, (char) 0);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)