예제 #1
0
        public static ImmutableArray <string> GetClassificationsForSpan(IWpfTextView view, Span span)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // If the cursor is after the last character return empty array.
            if (span.Start == view.TextBuffer.CurrentSnapshot.Length)
            {
                return(ImmutableArray <string> .Empty);
            }

            // The span to classify must have a length.
            var snapshotSpan = span.Length == 0
                ? new SnapshotSpan(view.TextSnapshot, span.Start, 1)
                : new SnapshotSpan(view.TextSnapshot, span);

            var classificationService = VSServiceHelpers.GetMefExport <IClassifierAggregatorService>();
            var classifier            = (IAccurateClassifier)classificationService.GetClassifier(view.TextBuffer);

            var classifiedSpans = classifier.GetAllClassificationSpans(snapshotSpan, CancellationToken.None);

            var classifications = ImmutableArray.CreateBuilder <string>();

            foreach (var classifiedSpan in classifiedSpans)
            {
                CollectClassifications(classifiedSpan.ClassificationType, classifications);
            }

            return(classifications.ToImmutable());
        public static ImmutableDictionary <string, string> GetClassificationNameMap()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // This results in a dictionary where the keys are font/color item
            // names and the values are VS editor format classification names.

            var builder = ImmutableDictionary.CreateBuilder <string, string>();
            var efds    = VSServiceHelpers.GetMefExports <EditorFormatDefinition>().ToArray();

            foreach (var efd in efds)
            {
                var type = efd.GetType();
                var uv   = type.GetCustomAttribute <UserVisibleAttribute>();
                if (uv?.UserVisible != true)
                {
                    continue;
                }
                var name = type.GetCustomAttribute <NameAttribute>()?.Name;
                var ctns = type.GetCustomAttribute <ClassificationTypeAttribute>()?.ClassificationTypeNames;
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }
                builder.Add(name, ctns ?? name);
            }

            return(builder.ToImmutable());
        }
        static ClassificationMap()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var builder = ImmutableDictionary.CreateBuilder <string, string>();
            var efds    = VSServiceHelpers.GetMefExports <EditorFormatDefinition>().ToArray();

            foreach (var efd in efds)
            {
                var type = efd.GetType();
                var uv   = type.GetCustomAttribute <UserVisibleAttribute>();
                if (uv?.UserVisible != true)
                {
                    continue;
                }
                var name = type.GetCustomAttribute <NameAttribute>()?.Name;
                var ctns = type.GetCustomAttribute <ClassificationTypeAttribute>()?.ClassificationTypeNames;
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }
                builder.Add(name, ctns ?? name);
            }

            itemNameToClassification = builder.ToImmutable();
        }
예제 #4
0
        public static ImmutableDictionary <string, string> GetClassificationNameMap()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            return(VSServiceHelpers.GetMefExports <EditorFormatDefinition>()
                   .Select(definition => definition.GetType())
                   .Where(type => type.GetCustomAttribute <UserVisibleAttribute>()?.UserVisible == true)
                   .Select(type => (type.GetCustomAttribute <NameAttribute>()?.Name, type.GetCustomAttribute <ClassificationTypeAttribute>()?.ClassificationTypeNames))
                   .Where(names => !string.IsNullOrEmpty(names.Name))
                   .ToImmutableDictionary(t => t.Name, t => t.ClassificationTypeNames ?? t.Name));
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowControl"/> class.
        /// </summary>
        public MainWindowControl()
        {
            DataContext = _viewModel = new MainWindowControlViewModel();
            InitializeComponent();

            _activeWindowTracker = new ActiveWindowTracker();
            _activeWindowTracker.PropertyChanged += ActiveWindowPropertyChanged;

            var editorFormatMapService = VSServiceHelpers.GetMefExport <IEditorFormatMapService>();
            var editorFormatMap        = editorFormatMapService.GetEditorFormatMap("text");

            editorFormatMap.FormatMappingChanged += (object s, FormatItemsEventArgs e) => UpdateClassifications(e.ChangedItems);

            void UpdateClassifications(ReadOnlyCollection <string> definitionNames)
            {
                _viewModel.OnThemeChanged(definitionNames.ToLookup(name => name));
            }
        }