Skip to content

Commit

Permalink
VS2010: VS stops showing context menus if code window contains C++ co…
Browse files Browse the repository at this point in the history
…de (Issue 209)
  • Loading branch information
gasparnagy committed Oct 2, 2012
1 parent d080527 commit 21ef299
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using EnvDTE;
using TechTalk.SpecFlow.Vs2010Integration.LanguageService;

namespace TechTalk.SpecFlow.Vs2010Integration.Commands
{
Expand Down Expand Up @@ -35,7 +36,7 @@ protected override void BeforeQueryStatus(Microsoft.VisualStudio.Shell.OleMenuCo
command.Enabled = goToStepDefinitionCommand.IsEnabled(activeDocument);
command.Text = "Go To Step Definition";
}
else if (IsCodeFile(activeDocument.ProjectItem))
else if (goToStepsCommand.IsCodeFile(activeDocument.ProjectItem))
{
command.Enabled = goToStepsCommand.IsEnabled(activeDocument);
command.Text = "Go To SpecFlow Step Definition Usages";
Expand All @@ -54,7 +55,7 @@ protected override void Invoke(Microsoft.VisualStudio.Shell.OleMenuCommand comma
{
goToStepDefinitionCommand.Invoke(activeDocument);
}
else if (IsCodeFile(activeDocument.ProjectItem))
else if (goToStepsCommand.IsCodeFile(activeDocument.ProjectItem))
{
goToStepsCommand.Invoke(activeDocument);
}
Expand All @@ -64,10 +65,5 @@ internal static bool IsFeatureFile(ProjectItem projectItem)
{
return projectItem.Name.EndsWith(".feature", StringComparison.InvariantCultureIgnoreCase);
}

private bool IsCodeFile(ProjectItem projectItem)
{
return projectItem.FileCodeModel != null;
}
}
}
17 changes: 8 additions & 9 deletions IdeIntegration/Vs2010Integration/Commands/GoToStepsCommand.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using EnvDTE;
Expand Down Expand Up @@ -30,7 +31,7 @@ public GoToStepsCommand(IServiceProvider serviceProvider, DTE dte, IProjectScope
protected override void BeforeQueryStatus(OleMenuCommand command, SelectedItems selection)
{
var activeDocument = dte.ActiveDocument;
if (activeDocument == null || activeDocument.ProjectItem == null || activeDocument.ProjectItem.FileCodeModel == null)
if (activeDocument == null || activeDocument.ProjectItem == null || IsCodeFile(activeDocument.ProjectItem))
{
command.Visible = false;
return;
Expand All @@ -45,6 +46,11 @@ protected override void BeforeQueryStatus(OleMenuCommand command, SelectedItems
command.Enabled = IsEnabled(activeDocument);
}

public bool IsCodeFile(ProjectItem projectItem)
{
return VsProjectScope.IsCodeFileSupported(projectItem);
}

internal bool IsEnabled(Document activeDocument)
{
var bindingMethod = GetSelectedBindingMethod(activeDocument);
Expand Down Expand Up @@ -202,14 +208,7 @@ private CodeFunction GetSelectedFunction(Document activeDocument)
if (codeModel == null)
return null;

try
{
return codeModel.CodeElementFromPoint(textSelection.ActivePoint, vsCMElement.vsCMElementFunction) as CodeFunction;
}
catch(Exception)
{
return null;
}
return VsxHelper.TryGetCodeElementFromActivePoint(codeModel, textSelection) as CodeFunction;
}
}
}
20 changes: 20 additions & 0 deletions IdeIntegration/Vs2010Integration/LanguageService/VsProjectScope.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -168,6 +169,7 @@ public object Convert(object value, IBindingType typeToConvertTo, CultureInfo cu
throw new NotSupportedException();
}

[DebuggerStepThrough]
public bool CanConvert(object value, IBindingType typeToConvertTo, CultureInfo cultureInfo)
{
if (!(typeToConvertTo is RuntimeBindingType))
Expand Down Expand Up @@ -459,5 +461,23 @@ public static ProgrammingLanguage GetTargetLanguage(Project project)
return ProgrammingLanguage.FSharp;
return ProgrammingLanguage.Other;
}

public static bool IsCodeFileSupported(ProjectItem projectItem)
{
var codeFileLanguage = GetCodeFileLanguage(projectItem);
return codeFileLanguage != ProgrammingLanguage.Other && codeFileLanguage != ProgrammingLanguage.FSharp; //F# does not have code model
}

public static ProgrammingLanguage GetCodeFileLanguage(ProjectItem projectItem)
{
string name = projectItem.Name;
if (name.EndsWith(".cs"))
return ProgrammingLanguage.CSharp;
if (name.EndsWith(".vb"))
return ProgrammingLanguage.VB;
if (name.EndsWith(".fs"))
return ProgrammingLanguage.FSharp;
return ProgrammingLanguage.Other;
}
}
}
Expand Up @@ -92,7 +92,7 @@ private void ProjectItemsEventsOnItemRenamed(ProjectItem item, string oldName)

private void DocumentEventsOnDocumentSaved(Document document)
{
ProjectItem item = document.ProjectItem;
ProjectItem item = VsxHelper.TryGetProjectItem(document);
if (item == null || !IsItemRelevant(item))
return;

Expand Down
29 changes: 28 additions & 1 deletion IdeIntegration/Vs2010Integration/Utils/VsxHelper.cs
Expand Up @@ -15,6 +15,7 @@
using TechTalk.SpecFlow.Utils;
using VSLangProj;
using Constants = EnvDTE.Constants;
using DefGuidList = Microsoft.VisualStudio.Editor.DefGuidList;
using IServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace TechTalk.SpecFlow.Vs2010Integration.Utils
Expand Down Expand Up @@ -522,10 +523,36 @@ public static IWpfTextView GetWpfTextView(IVsTextView vTextView)
return null;

object holder;
Guid guidViewHost = Microsoft.VisualStudio.Editor.DefGuidList.guidIWpfTextViewHost;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out holder);
IWpfTextViewHost viewHost = (IWpfTextViewHost) holder;
return viewHost.TextView;
}

[DebuggerStepThrough]
public static ProjectItem TryGetProjectItem(Document document)
{
try
{
return document.ProjectItem;
}
catch (Exception)
{
return null;
}
}

[DebuggerStepThrough]
public static CodeElement TryGetCodeElementFromActivePoint(FileCodeModel codeModel, TextSelection textSelection)
{
try
{
return codeModel.CodeElementFromPoint(textSelection.ActivePoint, vsCMElement.vsCMElementFunction);
}
catch (Exception)
{
return null;
}
}
}
}
1 change: 1 addition & 0 deletions changelog.txt
Expand Up @@ -2,6 +2,7 @@

Fixed issues:
+ VS2012: Run specflow scenarios does not work: works as for RC, but still runs all scenarios from the file (Issue 210)
+ VS2010: VS stops showing context menus if code window contains C++ code (Issue 209)


1.9.0 - 2012/08/06
Expand Down

0 comments on commit 21ef299

Please sign in to comment.