Skip to content

Commit 445bb09

Browse files
Find active project from active document
Fixed #166
1 parent 7a5e5c3 commit 445bb09

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/WebCompilerVsix/Helpers/ProjectHelpers.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public static void AddFileToProject(this Project project, string file, string it
9898
if (project.IsKind(ProjectTypes.ASPNET_5))
9999
return;
100100

101-
102101
if (_dte.Solution.FindProjectItem(file) == null)
103102
{
104103
ProjectItem item = project.ProjectItems.AddFromFile(file);
@@ -218,13 +217,26 @@ public static Project GetActiveProject()
218217
{
219218
try
220219
{
220+
Window2 window = _dte.ActiveWindow as Window2;
221+
Document doc = _dte.ActiveDocument;
222+
223+
if (window != null && window.Type == vsWindowType.vsWindowTypeDocument)
224+
{
225+
// if a document is active, use the document's containing directory
226+
if (doc != null && !string.IsNullOrEmpty(doc.FullName))
227+
{
228+
ProjectItem docItem = _dte.Solution.FindProjectItem(doc.FullName);
229+
230+
if (docItem != null && docItem.ContainingProject != null)
231+
return docItem.ContainingProject;
232+
}
233+
}
234+
221235
Array activeSolutionProjects = _dte.ActiveSolutionProjects as Array;
222236

223237
if (activeSolutionProjects != null && activeSolutionProjects.Length > 0)
224238
return activeSolutionProjects.GetValue(0) as Project;
225239

226-
var doc = _dte.ActiveDocument;
227-
228240
if (doc != null && !string.IsNullOrEmpty(doc.FullName))
229241
{
230242
var item = _dte.Solution?.FindProjectItem(doc.FullName);

0 commit comments

Comments
 (0)