Skip to content

Commit becc8a2

Browse files
committed
Update code style, refactor code, enable nullability by default.
1 parent 16c81cf commit becc8a2

14 files changed

+430
-185
lines changed

.editorconfig

Lines changed: 230 additions & 73 deletions
Large diffs are not rendered by default.

EditorBar.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.9.34310.174
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EditorBar", "src\EditorBar\EditorBar.csproj", "{097E6F5A-F198-49F5-B4CC-28128E82FE43}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JPSoftworks.EditorBar", "src\EditorBar\JPSoftworks.EditorBar.csproj", "{097E6F5A-F198-49F5-B4CC-28128E82FE43}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7FAD5B22-8341-4C9B-ACE6-4BE9AAD2D3EF}"
99
ProjectSection(SolutionItems) = preProject

src/EditorBar/BottomEditorBarFactory.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
using System.ComponentModel.Composition;
1+
// ------------------------------------------------------------
2+
//
3+
// Copyright (c) Jiří Polášek. All rights reserved.
4+
//
5+
// ------------------------------------------------------------
6+
7+
using System.ComponentModel.Composition;
28
using JPSoftworks.EditorBar.Options;
39
using Microsoft.VisualStudio.Text;
410
using Microsoft.VisualStudio.Text.Editor;
@@ -8,15 +14,15 @@ namespace JPSoftworks.EditorBar;
814

915
[Export(typeof(IWpfTextViewMarginProvider))]
1016
[Name("EditorBar-Bottom")]
11-
[Order(After = "Wpf Horizontal Scrollbar")]
17+
[Order(After = PredefinedMarginNames.HorizontalScrollBar)]
1218
[MarginContainer(PredefinedMarginNames.Bottom)]
13-
[ContentType("text")]
19+
[ContentType(StandardContentTypeNames.Text)]
1420
[TextViewRole(PredefinedTextViewRoles.Editable)]
1521
public class BottomEditorBarFactory : IWpfTextViewMarginProvider
1622
{
1723
private bool _isBottom;
1824
private IWpfTextView? _textView;
19-
private IWpfTextViewMargin _currentMargin;
25+
private IWpfTextViewMargin? _currentMargin;
2026

2127
public BottomEditorBarFactory()
2228
{
@@ -50,7 +56,7 @@ private static void RefreshTextView(IWpfTextView textView)
5056
}
5157

5258

53-
public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer)
59+
public IWpfTextViewMargin? CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer)
5460
{
5561
_isBottom = GeneralPage.Instance.BarPosition == BarPosition.Bottom;
5662
_textView = wpfTextViewHost.TextView;

src/EditorBar/EditorBarControl.xaml.cs

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
#nullable enable
1+
// ------------------------------------------------------------
2+
//
3+
// Copyright (c) Jiří Polášek. All rights reserved.
4+
//
5+
// ------------------------------------------------------------
26

3-
using System;
4-
using System.Collections.Generic;
57
using System.Diagnostics;
68
using System.IO;
7-
using System.Linq;
8-
using System.Threading.Tasks;
99
using System.Windows;
10-
using System.Windows.Controls;
1110
using Community.VisualStudio.Toolkit;
12-
using EnvDTE;
1311
using JPSoftworks.EditorBar.Options;
1412
using Microsoft.VisualStudio.Shell;
1513
using Microsoft.VisualStudio.Text;
1614
using Microsoft.VisualStudio.Text.Editor;
17-
using Project = EnvDTE.Project;
1815

1916
namespace JPSoftworks.EditorBar;
2017

2118
/// <summary>
2219
/// Interaction logic for EditorBarControl.xaml
2320
/// </summary>
24-
public partial class EditorBarControl : UserControl, IWpfTextViewMargin
21+
public partial class EditorBarControl : IWpfTextViewMargin
2522
{
2623
private readonly IWpfTextView? _textView;
2724

@@ -35,14 +32,14 @@ public EditorBarControl(IWpfTextView textView)
3532

3633
InitializeComponent();
3734

38-
Loaded += async (_, _) => await UpdateAsync();
39-
GeneralPage.Saved += async _ => await UpdateAsync();
40-
VS.Events.ProjectItemsEvents.AfterRenameProjectItems += async _ => await UpdateAsync();
41-
VS.Events.ProjectItemsEvents.AfterAddProjectItems += async _ => await UpdateAsync();
42-
VS.Events.ProjectItemsEvents.AfterRemoveProjectItems += async _ => await UpdateAsync();
43-
VS.Events.DocumentEvents.Saved += async _ => await UpdateAsync();
44-
VS.Events.SolutionEvents.OnAfterRenameProject += async _ => await UpdateAsync();
45-
VS.Events.SolutionEvents.OnAfterOpenSolution += async _ => await UpdateAsync();
35+
Loaded += (_, _) => UpdateAsync().FireAndForget();
36+
GeneralPage.Saved += _ => UpdateAsync().FireAndForget();
37+
VS.Events.ProjectItemsEvents.AfterRenameProjectItems += _ => UpdateAsync().FireAndForget();
38+
VS.Events.ProjectItemsEvents.AfterAddProjectItems += _ => UpdateAsync().FireAndForget();
39+
VS.Events.ProjectItemsEvents.AfterRemoveProjectItems += _ => UpdateAsync().FireAndForget();
40+
VS.Events.DocumentEvents.Saved += _ => UpdateAsync().FireAndForget();
41+
VS.Events.SolutionEvents.OnAfterRenameProject += _ => UpdateAsync().FireAndForget();
42+
VS.Events.SolutionEvents.OnAfterOpenSolution += _ => UpdateAsync().FireAndForget();
4643
}
4744

4845
public void Dispose()
@@ -81,11 +78,11 @@ private async Task UpdateAsync()
8178
document.FileActionOccurred -= DocumentOnFileActionOccurred;
8279
document.FileActionOccurred += DocumentOnFileActionOccurred;
8380
UpdateFilePathLabel(document);
84-
var project = await GetProjectFromDocumentAsync(document);
81+
var project = await VisualStudioHelper.GetProjectFromDocumentAsync(document);
8582
ProjectNameLabel.Content = project == null ? "(no project)" : project.Name;
8683
if (project != null)
8784
{
88-
var parents = await GetSolutionFolderPathAsync(await ConvertToSolutionItemAsync(project));
85+
var parents = await VisualStudioHelper.GetSolutionFolderPathAsync(await VisualStudioHelper.ConvertToSolutionItemAsync(project));
8986
SolutionFoldersList.ItemsSource = parents;
9087
}
9188
else
@@ -95,7 +92,7 @@ private async Task UpdateAsync()
9592
if (project2 != null)
9693
{
9794
ProjectNameLabel.Content = project2.Name;
98-
var parents = await GetSolutionFolderPathAsync(project2);
95+
var parents = await VisualStudioHelper.GetSolutionFolderPathAsync(project2);
9996
SolutionFoldersList.ItemsSource = parents;
10097
}
10198
}
@@ -110,7 +107,7 @@ private void UpdateFilePathLabel(ITextDocument document)
110107
{
111108
var slnPath = currentSolution.FullPath;
112109
var slnDir = Path.GetDirectoryName(slnPath);
113-
RelativePath = GetRelativePath(FilePath, slnDir);
110+
RelativePath = slnDir == null ? FilePath : GetRelativePath(FilePath, slnDir);
114111
}
115112
else
116113
{
@@ -132,57 +129,11 @@ private string GetRelativePath(string documentFilePath, string slnDir)
132129
return documentFilePath.Substring(slnDir.Length);
133130
}
134131

135-
private async Task<SolutionItem> ConvertToSolutionItemAsync(Project dteProject)
136-
{
137-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
138-
var all = await VS.Solutions.GetAllProjectsAsync(ProjectStateFilter.All);
139-
return all.FirstOrDefault(t => t.FullPath == dteProject.FullName);
140-
}
141-
142-
143-
private async Task<Project?> GetProjectFromDocumentAsync(ITextDocument document)
144-
{
145-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
146-
147-
var dte = (DTE)Package.GetGlobalService(typeof(DTE));
148-
var projects = dte.Solution.Projects.OfType<Project>().ToList();
149-
var projectFile = projects.FirstOrDefault(t => string.Equals(t.FullName, document.FilePath, StringComparison.OrdinalIgnoreCase));
150-
if (projectFile != null)
151-
{
152-
return projectFile;
153-
}
154-
155-
var projectItem = dte?.Solution.FindProjectItem(document.FilePath);
156-
return projectItem?.ContainingProject;
157-
}
158-
159-
private async Task<List<string>> GetSolutionFolderPathAsync(SolutionItem? project)
160-
{
161-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
162-
163-
var folderPath = new List<string>();
164-
if (project == null)
165-
{
166-
return folderPath;
167-
}
168-
169-
var parent = project.FindParent(SolutionItemType.SolutionFolder);
170-
while (parent != null)
171-
{
172-
folderPath.Add(parent.Name);
173-
parent = parent.FindParent(SolutionItemType.SolutionFolder);
174-
}
175-
176-
// Reverse to get the order from solution to project
177-
folderPath.Reverse();
178-
return folderPath;
179-
}
180-
181132

182133
private async void DocumentOnFileActionOccurred(object sender, TextDocumentFileActionEventArgs e)
183134
{
184135
FilePath = e.FilePath;
185-
await UpdateAsync();
136+
UpdateAsync().FireAndForget();
186137
}
187138

188139
private ITextDocument? GetCurrentDocument()
@@ -204,7 +155,7 @@ private void OpenFolderClicked(object sender, RoutedEventArgs e)
204155
var directoryName = Path.GetDirectoryName(fileName);
205156
if (!string.IsNullOrWhiteSpace(directoryName) && Directory.Exists(directoryName))
206157
{
207-
System.Diagnostics.Process.Start(new ProcessStartInfo("explorer.exe", "/select, " + fileName) { UseShellExecute = true });
158+
Process.Start(new ProcessStartInfo("explorer.exe", "/select, " + fileName) { UseShellExecute = true });
208159
}
209160
}
210161
}

src/EditorBar/EditorBarPackage.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using System;
1+
// ------------------------------------------------------------
2+
//
3+
// Copyright (c) Jiří Polášek. All rights reserved.
4+
//
5+
// ------------------------------------------------------------
6+
27
using System.Runtime.InteropServices;
38
using System.Threading;
49
using JPSoftworks.EditorBar.Options;
510
using Microsoft.VisualStudio.Shell;
6-
using Task = System.Threading.Tasks.Task;
711

812
namespace JPSoftworks.EditorBar;
913

@@ -35,8 +39,6 @@ public sealed class EditorBarPackage : AsyncPackage
3539
/// </summary>
3640
public const string PackageGuidString = "ef5d9a25-5e0d-4428-8762-56d4dc816eeb";
3741

38-
#region Package Members
39-
4042
/// <summary>
4143
/// Initialization of the package; this method is called right after the package is sited, so this is the place
4244
/// where you can put all the initialization code that rely on services provided by VisualStudio.
@@ -50,6 +52,4 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
5052
// Do any initialization that requires the UI thread after switching to the UI thread.
5153
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
5254
}
53-
54-
#endregion
5555
}

src/EditorBar/GlobalUsings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ------------------------------------------------------------
2+
//
3+
// Copyright (c) Jiøí Polášek. All rights reserved.
4+
//
5+
// ------------------------------------------------------------
6+
7+
global using System;
8+
global using System.Collections.Generic;
9+
global using System.Linq;
10+
global using System.Threading.Tasks;

src/EditorBar/EditorBar.csproj renamed to src/EditorBar/JPSoftworks.EditorBar.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
</PropertyGroup>
88
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
99
<LangVersion>default</LangVersion>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|arm64'">
1213
<LangVersion>default</LangVersion>
14+
<Nullable>enable</Nullable>
1315
</PropertyGroup>
1416
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
1517
<LangVersion>default</LangVersion>
18+
<Nullable>enable</Nullable>
1619
</PropertyGroup>
1720
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|arm64'">
1821
<LangVersion>default</LangVersion>
22+
<Nullable>enable</Nullable>
1923
</PropertyGroup>
2024
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
2125
<PropertyGroup>
@@ -51,6 +55,7 @@
5155
<LangVersion>default</LangVersion>
5256
<CreateVsixContainer>True</CreateVsixContainer>
5357
<CopyVsixExtensionFiles>False</CopyVsixExtensionFiles>
58+
<Nullable>enable</Nullable>
5459
</PropertyGroup>
5560
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
5661
<DebugType>pdbonly</DebugType>
@@ -60,16 +65,21 @@
6065
<ErrorReport>prompt</ErrorReport>
6166
<WarningLevel>4</WarningLevel>
6267
<LangVersion>default</LangVersion>
68+
<Nullable>enable</Nullable>
6369
</PropertyGroup>
6470
<ItemGroup>
6571
<Compile Include="BottomEditorBarFactory.cs" />
6672
<Compile Include="EditorBarControl.xaml.cs">
6773
<DependentUpon>EditorBarControl.xaml</DependentUpon>
6874
</Compile>
75+
<Compile Include="GlobalUsings.cs" />
76+
<Compile Include="Options\BarPosition.cs" />
77+
<Compile Include="Options\OptionsProvider.cs" />
6978
<Compile Include="TopEditorBarFactory.cs" />
7079
<Compile Include="Options\GeneralPage.cs" />
7180
<Compile Include="Properties\AssemblyInfo.cs" />
7281
<Compile Include="EditorBarPackage.cs" />
82+
<Compile Include="VisualStudioHelper.cs" />
7383
</ItemGroup>
7484
<ItemGroup>
7585
<None Include="source.extension.vsixmanifest">
@@ -105,6 +115,10 @@
105115
</ItemGroup>
106116
<ItemGroup />
107117
<ItemGroup>
118+
<Content Include="..\..\LICENSE.txt">
119+
<Link>Resources\LICENSE.txt</Link>
120+
<IncludeInVSIX>true</IncludeInVSIX>
121+
</Content>
108122
<Content Include="Resources\Icon.png">
109123
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
110124
<IncludeInVSIX>true</IncludeInVSIX>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ------------------------------------------------------------
2+
//
3+
// Copyright (c) Jiří Polášek. All rights reserved.
4+
//
5+
// ------------------------------------------------------------
6+
7+
namespace JPSoftworks.EditorBar.Options;
8+
9+
public enum BarPosition
10+
{
11+
Top = 0,
12+
Bottom = 1
13+
}
Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
using System;
1+
// ------------------------------------------------------------
2+
//
3+
// Copyright (c) Jiří Polášek. All rights reserved.
4+
//
5+
// ------------------------------------------------------------
6+
27
using System.ComponentModel;
3-
using System.Runtime.InteropServices;
4-
using System.Threading.Tasks;
58
using Community.VisualStudio.Toolkit;
69

710
namespace JPSoftworks.EditorBar.Options;
811

9-
internal partial class OptionsProvider
10-
{
11-
// Register the options with this attribute on your package class:
12-
// [ProvideOptionPage(typeof(OptionsProvider.GeneralPageOptions), "EditorBar.Options", "GeneralPage", 0, 0, true, SupportsProfiles = true)]
13-
[ComVisible(true)]
14-
public class GeneralPageOptions : BaseOptionPage<GeneralPage> { }
15-
}
16-
1712
public class GeneralPage : BaseOptionModel<GeneralPage>
1813
{
1914
[Category("Apperance")]
@@ -22,7 +17,7 @@ public class GeneralPage : BaseOptionModel<GeneralPage>
2217
[DefaultValue(Options.BarPosition.Top)]
2318
[TypeConverter(typeof(EnumConverter))]
2419
public BarPosition BarPosition { get; set; } = BarPosition.Top;
25-
20+
2621
[Category("Apperance")]
2722
[DisplayName("Show relative path")]
2823
[Description("Show path relative to the solution root")]
@@ -34,10 +29,4 @@ public override Task SaveAsync()
3429
Console.WriteLine("Saving...");
3530
return base.SaveAsync();
3631
}
37-
}
38-
39-
public enum BarPosition
40-
{
41-
Top = 0,
42-
Bottom = 1
4332
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ------------------------------------------------------------
2+
//
3+
// Copyright (c) Jiří Polášek. All rights reserved.
4+
//
5+
// ------------------------------------------------------------
6+
7+
using System.Runtime.InteropServices;
8+
using Community.VisualStudio.Toolkit;
9+
10+
namespace JPSoftworks.EditorBar.Options;
11+
12+
internal partial class OptionsProvider
13+
{
14+
// Register the options with this attribute on your package class:
15+
// [ProvideOptionPage(typeof(OptionsProvider.GeneralPageOptions), "EditorBar.Options", "GeneralPage", 0, 0, true, SupportsProfiles = true)]
16+
[ComVisible(true)]
17+
public class GeneralPageOptions : BaseOptionPage<GeneralPage> { }
18+
}

0 commit comments

Comments
 (0)