VSIX extension for VS2012 not running when debugging

后端 未结 3 2178
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 10:50

I created a new VSIX extension project in Visual Studio 2012, and wrote a MEF classifier (as a test) that should simply highlight all text in a .mylang file. Here a

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 11:31

    280Z28 solved the problem! For completeness, this is the full tried and tested code that will create a super simple VSIX Visual Studio MEF extension that colors all text in a .mylang file blue (or whatever the current keyword color is).

    How to create a simple coloring MEF VSIX extension

    1. Make sure you have the Visual Studio SDK installed. (VS2010 SP1 SDK, VS2012 SDK)
    2. Create a new VSIX Project
      (From the template under InstalledTemplatesVisual C#Extensibility.)
    3. Enter something in the Author field of the VSIX manifest editor, then save and close it.
    4. Add references to the following libraries,
      version 10.0.0.0 for VS2010, or 11.0.0.0 for VS2012:

      • Microsoft.VisualStudio.CoreUtility.dll
      • Microsoft.VisualStudio.Language.StandardClassification.dll
      • Microsoft.VisualStudio.Text.Data.dll
      • Microsoft.VisualStudio.Text.Logic.dll
      • Microsoft.VisualStudio.Text.UI.dll
      • Microsoft.VisualStudio.Text.UI.Wpf.dll
    5. Add a reference to the following library:

      • System.ComponentModel.Composition.dll version 4.0.0.0
    6. Create and add a new code file MyLang.cs, and copy-and-paste the code below in it.

    7. Edit source.extension.vsixmanifest as XML.

      • For Visual Studio 2010, add the following XML just before the closing tag , and save:

        
            |%CurrentProject%|
        
        

        (If there is already an empty , remove it.)

      • For Visual Stuio 2012, add the following XML just before the closing tag , and save:

        
            
        
        

        (If there is already an empty , remove it.)

    8. Only for Visual Studio 2010:

      • Unload the VSIX project (right-click the project → Unload project).

      • Edit the .csproj project file (right-click the project → Edit MyProject.csproj).

      • Change the value at to true.

      • Save and close the file.

      • Reload the VSIX project (right-click the project → Reload project).

    9. Now build and run it. When you load a .mylang file, all text should be colored blue (or whatever the default keyword color is).


    MyLang.cs

    using Microsoft.VisualStudio.Language.StandardClassification;
    using Microsoft.VisualStudio.Text;
    using Microsoft.VisualStudio.Text.Classification;
    using Microsoft.VisualStudio.Utilities;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    
    namespace VSIXProject1
    {
        internal static class MyLangLanguage
        {
            public const string ContentType = "mylang";
    
            public const string FileExtension = ".mylang";
    
            [Export]
            [Name(ContentType)]
            [BaseDefinition("code")]
            internal static ContentTypeDefinition MyLangSyntaxContentTypeDefinition = null;
    
            [Export]
            [FileExtension(FileExtension)]
            [ContentType(ContentType)]
            internal static FileExtensionToContentTypeDefinition MyLangSyntaxFileExtensionDefinition = null;
        }
    
        [Export(typeof(IClassifierProvider))]
        [ContentType(MyLangLanguage.ContentType)]
        [Name("MyLangSyntaxProvider")]
        internal sealed class MyLangSyntaxProvider : IClassifierProvider
        {
            [Import]
            internal IClassificationTypeRegistryService ClassificationRegistry = null;
    
            public IClassifier GetClassifier(ITextBuffer buffer)
            {
                return buffer.Properties.GetOrCreateSingletonProperty(() => new MyLangSyntax(ClassificationRegistry, buffer));
            }
        }
    
        internal sealed class MyLangSyntax : IClassifier
        {
            private ITextBuffer buffer;
            private IClassificationType identifierType;
            private IClassificationType keywordType;
    
            public event EventHandler ClassificationChanged;
    
            internal MyLangSyntax(IClassificationTypeRegistryService registry, ITextBuffer buffer)
            {
                this.identifierType = registry.GetClassificationType(PredefinedClassificationTypeNames.Identifier);
                this.keywordType = registry.GetClassificationType(PredefinedClassificationTypeNames.Keyword);
                this.buffer = buffer;
                this.buffer.Changed += OnBufferChanged;
            }
    
            public IList GetClassificationSpans(SnapshotSpan snapshotSpan)
            {
                var classifications = new List();
    
                string text = snapshotSpan.GetText();
                var span = new SnapshotSpan(snapshotSpan.Snapshot, snapshotSpan.Start.Position, text.Length);
                classifications.Add(new ClassificationSpan(span, keywordType));
    
                return classifications;
            }
    
            private void OnBufferChanged(object sender, TextContentChangedEventArgs e)
            {
                foreach (var change in e.Changes)
                    ClassificationChanged(this, new ClassificationChangedEventArgs(new SnapshotSpan(e.After, change.NewSpan)));
            }
        }
    }
    

提交回复
热议问题