<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Avalonia.Designer;
using AvaloniaVS.Controls;
using AvaloniaVS.Helpers;
using AvaloniaVS.IntelliSense;
using AvaloniaVS.Internals;
using AvaloniaVS.ViewModels;
using AvaloniaVS.Views;
using Debugger = System.Diagnostics.Debugger;
using HorizontalAlignment = System.Windows.HorizontalAlignment;

namespace AvaloniaVS.Infrastructure
{
    [ComVisible(true), Guid("75b0ba12-1e01-4f80-a035-32239896bcab")]
    public partial class AvaloniaDesignerPane : WindowPane
    {
        private const int WM_KEYFIRST = 0x0100;
        private const int WM_KEYLAST = 0x0109;

        private AvaloniaDesignerHostView _designerHostView;
        private AvaloniaDesignerHostViewModel _designerHost;
        private readonly string _fileName;
        private readonly IAvaloniaDesignerSettings _designerSettings;
        private AvaloniaDesigner _designer;
        private string _targetExe;
        private long _lastRestartToken;
        private IVsCodeWindow _vsCodeWindow;
        private readonly ITextBuffer _textBuffer;

        public AvaloniaDesignerPane(IVsCodeWindow vsCodeWindow, IVsTextLines textBuffer, string fileName, IAvaloniaDesignerSettings designerSettings)
        {
            _vsCodeWindow = vsCodeWindow;
            _textBuffer = textBuffer.GetTextBuffer();
            _fileName = fileName;
            _designerSettings = designerSettings;
        }

        protected override void Initialize()
        {
            base.Initialize();
            InitializePane();
            RegisterMenuCommands();
        }

        object GetContent(object adapter)
        {
            return adapter.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
                .FirstOrDefault(p => p.Name == "Content").GetMethod.Invoke(adapter, null);
        }

        private void InitializePane()
        {
            
            // initialize the designer host view.
            _designerHost = new AvaloniaDesignerHostViewModel(_fileName)
            {
                EditView = GetContent(_vsCodeWindow),
                Orientation = _designerSettings.SplitOrientation == SplitOrientation.Default ||
                              _designerSettings.SplitOrientation == SplitOrientation.Horizontal
                    ? Orientation.Horizontal
                    : Orientation.Vertical,
                IsReversed = _designerSettings.IsReversed
            };
            _designerHostView = new AvaloniaDesignerHostView {DataContext = _designerHost};
            _designerHostView.Init(_designerSettings);

            InitializeDesigner();
            _designerHost.TargetExeChanged += UpdateTargetExe;
        }

        void UpdateTargetExe(string exe)
        {
            _targetExe = exe;
            _designer.TargetExe = exe;
            _designerHost.DesignView = _targetExe == null
                ? (object) new TextBlock
                {
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    Text = $"{Path.GetFileName(_fileName)} cannot be edited in the Design view. Make sure that it's referenced from at least one desktop exe project"
                }
                : _designer;
            ReloadMetadata();
        }

        private void InitializeDesigner()
        {
            _designer = new AvaloniaDesigner()
            {
                SourceAssembly = Utils.GetContainerProject(_fileName)?.GetAssemblyPath()
            };
            _designerHost.SourceAssemblyChanged += sa =>
            {
                if (_designer != null)
                    _designer.SourceAssembly = sa;
            };
            UpdateTargetExe(_designerHost.TargetExe);
            _designerHost.RestartDesigner = new RelayCommand(() =>
            {
                Restart();
                ReloadMetadata();
            });
            _designer.Xaml = _textBuffer.CurrentSnapshot.GetText();<commit_msg>Modified help text<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Avalonia.Designer;
using AvaloniaVS.Controls;
using AvaloniaVS.Helpers;
using AvaloniaVS.IntelliSense;
using AvaloniaVS.Internals;
using AvaloniaVS.ViewModels;
using AvaloniaVS.Views;
using Debugger = System.Diagnostics.Debugger;
using HorizontalAlignment = System.Windows.HorizontalAlignment;

namespace AvaloniaVS.Infrastructure
{
    [ComVisible(true), Guid("75b0ba12-1e01-4f80-a035-32239896bcab")]
    public partial class AvaloniaDesignerPane : WindowPane
    {
        private const int WM_KEYFIRST = 0x0100;
        private const int WM_KEYLAST = 0x0109;

        private AvaloniaDesignerHostView _designerHostView;
        private AvaloniaDesignerHostViewModel _designerHost;
        private readonly string _fileName;
        private readonly IAvaloniaDesignerSettings _designerSettings;
        private AvaloniaDesigner _designer;
        private string _targetExe;
        private long _lastRestartToken;
        private IVsCodeWindow _vsCodeWindow;
        private readonly ITextBuffer _textBuffer;

        public AvaloniaDesignerPane(IVsCodeWindow vsCodeWindow, IVsTextLines textBuffer, string fileName, IAvaloniaDesignerSettings designerSettings)
        {
            _vsCodeWindow = vsCodeWindow;
            _textBuffer = textBuffer.GetTextBuffer();
            _fileName = fileName;
            _designerSettings = designerSettings;
        }

        protected override void Initialize()
        {
            base.Initialize();
            InitializePane();
            RegisterMenuCommands();
        }

        object GetContent(object adapter)
        {
            return adapter.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
                .FirstOrDefault(p => p.Name == "Content").GetMethod.Invoke(adapter, null);
        }

        private void InitializePane()
        {
            
            // initialize the designer host view.
            _designerHost = new AvaloniaDesignerHostViewModel(_fileName)
            {
                EditView = GetContent(_vsCodeWindow),
                Orientation = _designerSettings.SplitOrientation == SplitOrientation.Default ||
                              _designerSettings.SplitOrientation == SplitOrientation.Horizontal
                    ? Orientation.Horizontal
                    : Orientation.Vertical,
                IsReversed = _designerSettings.IsReversed
            };
            _designerHostView = new AvaloniaDesignerHostView {DataContext = _designerHost};
            _designerHostView.Init(_designerSettings);

            InitializeDesigner();
            _designerHost.TargetExeChanged += UpdateTargetExe;
        }

        void UpdateTargetExe(string exe)
        {
            _targetExe = exe;
            _designer.TargetExe = exe;
            _designerHost.DesignView = _targetExe == null
                ? (object) new TextBlock
                {
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    TextWrapping = TextWrapping.Wrap,
                    Text = $"{Path.GetFileName(_fileName)} cannot be edited in the Design view. Make sure that it's referenced from at least one desktop exe project and rebuild solution. FixMe button below might also help."
                }
                : _designer;
            ReloadMetadata();
        }

        private void InitializeDesigner()
        {
            _designer = new AvaloniaDesigner()
            {
                SourceAssembly = Utils.GetContainerProject(_fileName)?.GetAssemblyPath()
            };
            _designerHost.SourceAssemblyChanged += sa =>
            {
                if (_designer != null)
                    _designer.SourceAssembly = sa;
            };
            UpdateTargetExe(_designerHost.TargetExe);
            _designerHost.RestartDesigner = new RelayCommand(() =>
            {
                Restart();
                ReloadMetadata();
            });
            _designer.Xaml = _textBuffer.CurrentSnapshot.GetText();
{'fraction_non_alphanumeric': 0.06896935933147633, 'fraction_numerical': 0.007576601671309192, 'mean_word_length': 2.2975753122703892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2009199', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2219, 'n_words': 470}