<commit_before>﻿// Visual Studio Shared Project
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudioTools.Project.Automation;
using VsCommands2K = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID;
using VSConstants = Microsoft.VisualStudio.VSConstants;
#if DEV14_OR_LATER
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Imaging.Interop;
#endif

namespace Microsoft.VisualStudioTools.Project {
    internal class CommonFileNode : FileNode {
        private OAVSProjectItem _vsProjectItem;
        private CommonProjectNode _project;

        public CommonFileNode(CommonProjectNode root, ProjectElement e)
            : base(root, e) {
            _project = root;
        }

        #region properties
        /// <summary>
        /// Returns bool indicating whether this node is of subtype "Form"
        /// </summary>
        public bool IsFormSubType {
            get {
                string result = this.ItemNode.GetMetadata(ProjectFileConstants.SubType);
                if (!String.IsNullOrEmpty(result) && string.Compare(result, ProjectFileAttributeValue.Form, true, CultureInfo.InvariantCulture) == 0)
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// Returns the SubType of a dynamic FileNode. It is 
        /// </summary>
        public string SubType {
            get {
                return this.ItemNode.GetMetadata(ProjectFileConstants.SubType);
            }
            set {
                this.ItemNode.SetMetadata(ProjectFileConstants.SubType, value);
            }
        }

        protected internal VSLangProj.VSProjectItem VSProjectItem {
            get {
                if (null == _vsProjectItem) {
                    _vsProjectItem = new OAVSProjectItem(this);
                }
                return _vsProjectItem;
            }
        }

#if DEV11_OR_LATER
        public override __VSPROVISIONALVIEWINGSTATUS ProvisionalViewingStatus {
            get {
                return __VSPROVISIONALVIEWINGSTATUS.PVS_Enabled;
            }
        }
#endif

        #endregion

        #region overridden properties

        internal override object Object {
            get {
                return this.VSProjectItem;
            }
        }

        #endregion

        #region overridden methods

#if DEV14_OR_LATER
        protected override bool SupportsIconMonikers {
            get { return true; }
        }

        protected virtual ImageMoniker CodeFileIconMoniker {
            get { return KnownMonikers.Document; }
        }

        protected virtual ImageMoniker StartupCodeFileIconMoniker {
            get { return CodeFileIconMoniker; }
        }

        protected virtual ImageMoniker FormFileIconMoniker {
            get { return KnownMonikers.WindowsForm; }
        }

        protected override ImageMoniker GetIconMoniker(bool open) {
            if (ItemNode.IsExcluded) {
                return KnownMonikers.HiddenFile;
            } else if (!File.Exists(Url)) {
                return KnownMonikers.DocumentWarning;
            } else if (IsFormSubType) {
                return FormFileIconMoniker;
            } else if (this._project.IsCodeFile(FileName)) {
                if (CommonUtils.IsSamePath(this.Url, _project.GetStartupFile())) {
                    return StartupCodeFileIconMoniker;
                } else {
                    return CodeFileIconMoniker;
                }
            }
            return default(ImageMoniker);
        }
#else
        public override int ImageIndex {
            get {
                if (ItemNode.IsExcluded) {
                    return (int)ProjectNode.ImageName.ExcludedFile;
                } else if (!File.Exists(Url)) {
                    return (int)ProjectNode.ImageName.MissingFile;
                } else if (IsFormSubType) {
                    return (int)ProjectNode.ImageName.WindowsForm;
                } else if (this._project.IsCodeFile(FileName)) {
                    if (CommonUtils.IsSamePath(this.Url, _project.GetStartupFile())) {
                        return _project.ImageOffset + (int)CommonImageName.StartupFile;
                    } else {
                        return _project.ImageOffset + (int)CommonImageName.File;
                    }
                }
                return base.ImageIndex;
            }
        }
#endif


        /// <summary>
        /// Open a file depending on the SubType property associated with the file item in the project file
        /// </summary>
        protected override void DoDefaultAction() {
            FileDocumentManager manager = this.GetDocumentManager() as FileDocumentManager;
            Utilities.CheckNotNull(manager, "Could not get the FileDocumentManager");

			if (IsFormSubType)
				manager.Open(false, false, VSConstants.LOGVIEWID_Designer, out frame, WindowFrameShowAction.Show);
			else
				//manager.Open(false, false, VSConstants.LOGVIEWID_Code, out frame, WindowFrameShowAction.Show);    // Do not use, as in VS2010 css files are opened as plain text
				manager.Open(false, false, WindowFrameShowAction.Show);
		}

        private static Guid CLSID_VsTextBuffer = new Guid("{8E7B96A8-E33D-11d0-A6D5-00C04FB67F6A}");

        /// <summary>
        /// Gets the text buffer for the file opening the document if necessary.
        /// </summary>
        public ITextBuffer GetTextBuffer(bool create = true) {
            // http://pytools.codeplex.com/workitem/672
            // When we FindAndLockDocument we marshal on the main UI thread, and the docdata we get
            // back is marshalled back so that we'll marshal any calls on it back.  When we pass it
            // into IVsEditorAdaptersFactoryService we don't go through a COM boundary (it's a managed
            // call) and we therefore don't get the marshaled value, and it doesn't know what we're
            // talking about.  So run the whole operation on the UI thread.
            return ProjectMgr.Site.GetUIThread().Invoke(() => GetTextBufferOnUIThread(create));
        }

        private ITextBuffer GetTextBufferOnUIThread(bool create) {<commit_msg>Build fix<commit_after>﻿// Visual Studio Shared Project
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudioTools.Project.Automation;
using VsCommands2K = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID;
using VSConstants = Microsoft.VisualStudio.VSConstants;
#if DEV14_OR_LATER
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Imaging.Interop;
#endif

namespace Microsoft.VisualStudioTools.Project {
    internal class CommonFileNode : FileNode {
        private OAVSProjectItem _vsProjectItem;
        private CommonProjectNode _project;

        public CommonFileNode(CommonProjectNode root, ProjectElement e)
            : base(root, e) {
            _project = root;
        }

        #region properties
        /// <summary>
        /// Returns bool indicating whether this node is of subtype "Form"
        /// </summary>
        public bool IsFormSubType {
            get {
                string result = this.ItemNode.GetMetadata(ProjectFileConstants.SubType);
                if (!String.IsNullOrEmpty(result) && string.Compare(result, ProjectFileAttributeValue.Form, true, CultureInfo.InvariantCulture) == 0)
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// Returns the SubType of a dynamic FileNode. It is 
        /// </summary>
        public string SubType {
            get {
                return this.ItemNode.GetMetadata(ProjectFileConstants.SubType);
            }
            set {
                this.ItemNode.SetMetadata(ProjectFileConstants.SubType, value);
            }
        }

        protected internal VSLangProj.VSProjectItem VSProjectItem {
            get {
                if (null == _vsProjectItem) {
                    _vsProjectItem = new OAVSProjectItem(this);
                }
                return _vsProjectItem;
            }
        }

#if DEV11_OR_LATER
        public override __VSPROVISIONALVIEWINGSTATUS ProvisionalViewingStatus {
            get {
                return __VSPROVISIONALVIEWINGSTATUS.PVS_Enabled;
            }
        }
#endif

        #endregion

        #region overridden properties

        internal override object Object {
            get {
                return this.VSProjectItem;
            }
        }

        #endregion

        #region overridden methods

#if DEV14_OR_LATER
        protected override bool SupportsIconMonikers {
            get { return true; }
        }

        protected virtual ImageMoniker CodeFileIconMoniker {
            get { return KnownMonikers.Document; }
        }

        protected virtual ImageMoniker StartupCodeFileIconMoniker {
            get { return CodeFileIconMoniker; }
        }

        protected virtual ImageMoniker FormFileIconMoniker {
            get { return KnownMonikers.WindowsForm; }
        }

        protected override ImageMoniker GetIconMoniker(bool open) {
            if (ItemNode.IsExcluded) {
                return KnownMonikers.HiddenFile;
            } else if (!File.Exists(Url)) {
                return KnownMonikers.DocumentWarning;
            } else if (IsFormSubType) {
                return FormFileIconMoniker;
            } else if (this._project.IsCodeFile(FileName)) {
                if (CommonUtils.IsSamePath(this.Url, _project.GetStartupFile())) {
                    return StartupCodeFileIconMoniker;
                } else {
                    return CodeFileIconMoniker;
                }
            }
            return default(ImageMoniker);
        }
#else
        public override int ImageIndex {
            get {
                if (ItemNode.IsExcluded) {
                    return (int)ProjectNode.ImageName.ExcludedFile;
                } else if (!File.Exists(Url)) {
                    return (int)ProjectNode.ImageName.MissingFile;
                } else if (IsFormSubType) {
                    return (int)ProjectNode.ImageName.WindowsForm;
                } else if (this._project.IsCodeFile(FileName)) {
                    if (CommonUtils.IsSamePath(this.Url, _project.GetStartupFile())) {
                        return _project.ImageOffset + (int)CommonImageName.StartupFile;
                    } else {
                        return _project.ImageOffset + (int)CommonImageName.File;
                    }
                }
                return base.ImageIndex;
            }
        }
#endif


        /// <summary>
        /// Open a file depending on the SubType property associated with the file item in the project file
        /// </summary>
        protected override void DoDefaultAction() {
            FileDocumentManager manager = this.GetDocumentManager() as FileDocumentManager;
            Utilities.CheckNotNull(manager, "Could not get the FileDocumentManager");

			IVsWindowFrame frame;
			if (IsFormSubType)
				manager.Open(false, false, VSConstants.LOGVIEWID_Designer, out frame, WindowFrameShowAction.Show);
			else
				//manager.Open(false, false, VSConstants.LOGVIEWID_Code, out frame, WindowFrameShowAction.Show);    // Do not use, as in VS2010 css files are opened as plain text
				manager.Open(false, false, WindowFrameShowAction.Show);
		}

        private static Guid CLSID_VsTextBuffer = new Guid("{8E7B96A8-E33D-11d0-A6D5-00C04FB67F6A}");

        /// <summary>
        /// Gets the text buffer for the file opening the document if necessary.
        /// </summary>
        public ITextBuffer GetTextBuffer(bool create = true) {
            // http://pytools.codeplex.com/workitem/672
            // When we FindAndLockDocument we marshal on the main UI thread, and the docdata we get
            // back is marshalled back so that we'll marshal any calls on it back.  When we pass it
            // into IVsEditorAdaptersFactoryService we don't go through a COM boundary (it's a managed
            // call) and we therefore don't get the marshaled value, and it doesn't know what we're
            // talking about.  So run the whole operation on the UI thread.
            return ProjectMgr.Site.GetUIThread().Invoke(() => GetTextBufferOnUIThread(create));
        }

        private ITextBuffer GetTextBufferOnUIThread(bool create) {
{'fraction_non_alphanumeric': 0.07567383145684067, 'fraction_numerical': 0.005595359945411122, 'mean_word_length': 2.132293225048087, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1780749', 'n_tokens_mistral': 3922, 'n_tokens_neox': 3641, 'n_words': 1037}