3

Is there a method to detecting the current programming language and disabling an extension based on that language?

I'm creating a VSIX package that is only compatible in a C++ build. I can programmatically detect the current programming language and respond to unsupported languages and not open my dialog box, but i feel like there should be a property or something that would enable/disable an extension in a certain language.

Omar Martinez
  • 708
  • 6
  • 19

1 Answers1

4

You can create a custom UI context rule for C++ and use it to load your package. It looks like this for C# and VB:

[ProvideAutoLoad(UIContexts.LoadContext)]
[ProvideUIContextRule(UIContexts.LoadContext,
    "RightFileTypeOpen",
    "(CSharpFileOpen | VBFileOpen)",
    new[] { "CSharpFileOpen", "VBFileOpen" },
    new[] { "ActiveEditorContentType:CSharp", "ActiveEditorContentType:Basic" })]

See How to: Use Rule-based UI Context for Visual Studio Extensions on MSDN and Mads Kristensen's sample for more details.

Simple Sandman
  • 900
  • 3
  • 11
  • 34
Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • For my VSIX package, the above method works when the user opens an existing file with the ".cs" extension. When, instead, the user creates a new text file (File / New / TextFile) and saves it with Save As changing the file extension from ".txt" to ".cs", Visual Studio does not instantiate my Package class and doesn't call my InitializeAsync method. If the user then opens another editor window and then switches back to the one containing the newly renamed .cs file, at that point Visual Studio instantiates my Package class. Are you aware of this issue and do you know of other solutions? – Alessandro Nov 09 '19 at 17:58
  • @Alessandro Looks like a VS bug. – Sergey Vlasov Nov 11 '19 at 17:44