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 ;
57using System . Diagnostics ;
68using System . IO ;
7- using System . Linq ;
8- using System . Threading . Tasks ;
99using System . Windows ;
10- using System . Windows . Controls ;
1110using Community . VisualStudio . Toolkit ;
12- using EnvDTE ;
1311using JPSoftworks . EditorBar . Options ;
1412using Microsoft . VisualStudio . Shell ;
1513using Microsoft . VisualStudio . Text ;
1614using Microsoft . VisualStudio . Text . Editor ;
17- using Project = EnvDTE . Project ;
1815
1916namespace 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 }
0 commit comments