예제 #1
0
        /// <summary>
        /// Determines if the Roslyn extensions for Visual Studio are installed.
        /// </summary>
        /// <remarks>
        /// This method caches the result after it is first checked with the IDE.
        /// </remarks>
        /// <param name="serviceProvider">A service provider for accessing global IDE services.</param>
        /// <returns>
        /// <see langword="true"/> if the Roslyn extensions are installed.
        /// <para>-or-</para>
        /// <para><see langword="false"/> if the Roslyn extensions are not installed.</para>
        /// <para>-or-</para>
        /// <para>null if the result of this method has not been cached from a previous call, and <paramref name="serviceProvider"/> is <see langword="null"/> or could not be used to obtain an instance of <see cref="IVsShell"/>.</para>
        /// </returns>
        public static bool?IsRoslynInstalled(IServiceProvider serviceProvider)
        {
            if (roslynInstalled.HasValue)
            {
                return(roslynInstalled);
            }

            if (IsFinalRoslyn)
            {
                roslynInstalled = true;
                return(true);
            }

            if (serviceProvider == null)
            {
                return(null);
            }

            IVsShell vsShell = serviceProvider.GetService(typeof(SVsShell)) as IVsShell;

            if (vsShell == null)
            {
                return(null);
            }

            Guid guid = new Guid("6cf2e545-6109-4730-8883-cf43d7aec3e1");
            int  isInstalled;

            if (ErrorHandler.Succeeded(vsShell.IsPackageInstalled(ref guid, out isInstalled)))
            {
                roslynInstalled = isInstalled != 0;
            }

            return(roslynInstalled);
        }
예제 #2
0
        /// <summary>
        /// Returns a RegistryKey object for the root of a given storage type.
        /// It is up to the caller to dispose the returned object.
        /// </summary>
        /// <param name="provider">The service provider to use to access the Visual Studio's services.</param>
        /// <param name="registryType">The type of registry storage to open.</param>
        /// <param name="writable">Flag to indicate is the key should be writable.</param>
        public static RegistryKey RegistryRoot(IServiceProvider provider, __VsLocalRegistryType registryType, bool writable)
        {
            if (null == provider)
            {
                throw new ArgumentNullException("provider");
            }

            // The current implementation of the shell supports only RegType_UserSettings and
            // RegType_Configuration, so for any other values we have to return not implemented.
            if ((__VsLocalRegistryType.RegType_UserSettings != registryType) &&
                (__VsLocalRegistryType.RegType_Configuration != registryType))
            {
                throw new NotSupportedException();
            }

            // Try to get the new ILocalRegistry4 interface that is able to handle the new
            // registry paths.
            ILocalRegistry4 localRegistry = provider.GetService(typeof(SLocalRegistry)) as ILocalRegistry4;

            if (null != localRegistry)
            {
                uint   rootHandle;
                string rootPath;
                if (ErrorHandler.Succeeded(localRegistry.GetLocalRegistryRootEx((uint)registryType, out rootHandle, out rootPath)))
                {
                    // Check if we have valid data.
                    __VsLocalRegistryRootHandle handle = (__VsLocalRegistryRootHandle)rootHandle;
                    if (!string.IsNullOrEmpty(rootPath) && (__VsLocalRegistryRootHandle.RegHandle_Invalid != handle))
                    {
                        // Check if the root is inside HKLM or HKCU. Note that this does not depends only from
                        // the registry type, but also from instance-specific data like the RANU flag.
                        RegistryKey root = (__VsLocalRegistryRootHandle.RegHandle_LocalMachine == handle) ? Registry.LocalMachine : Registry.CurrentUser;
                        return(root.OpenSubKey(rootPath, writable));
                    }
                }
            }

            // We are here if the usage of the new interface failed for same reason, so we have to fall back to
            // the ond way to access the registry.
            ILocalRegistry2 oldRegistry = provider.GetService(typeof(SLocalRegistry)) as ILocalRegistry2;

            if (null == oldRegistry)
            {
                // There is something wrong with this installation or this service provider.
                return(null);
            }
            string registryPath;

            NativeMethods.ThrowOnFailure(oldRegistry.GetLocalRegistryRoot(out registryPath));
            if (string.IsNullOrEmpty(registryPath))
            {
                return(null);
            }

            RegistryKey regRoot = (__VsLocalRegistryType.RegType_Configuration == registryType) ? Registry.LocalMachine : Registry.CurrentUser;

            return(regRoot.OpenSubKey(registryPath, writable));
        }
        /// <summary>
        /// Called by the shell when a node has been renamed from the GUI
        /// </summary>
        /// <param name="label">The name of the new label.</param>
        /// <returns>A success or failure value.</returns>
        public override int SetEditLabel(string label)
        {
            int result = this.DelegateSetPropertyToNested((int)__VSHPROPID.VSHPROPID_EditLabel, label);

            if (ErrorHandler.Succeeded(result))
            {
                this.RenameNestedProjectInParentProject(label);
            }

            return(result);
        }
            private void Unadvise()
            {
                Debug.Assert(this.cookie.HasValue, "Already unadvised");

                if (ErrorHandler.Succeeded(this.InfoBarUIElement.Unadvise(this.cookie.Value)))
                {
                    this.cookie = null;
                }
                else
                {
                    Debug.Fail("Failed in IVsInfoBarUIElement.Unadvise");
                }
            }
            private void Advise()
            {
                uint syncCookie;

                Debug.Assert(!this.cookie.HasValue, "Already advised");

                if (ErrorHandler.Succeeded(this.InfoBarUIElement.Advise(this, out syncCookie)))
                {
                    this.cookie = syncCookie;
                }
                else
                {
                    Debug.Fail("Failed in IVsInfoBarUIElement.Advise");
                }
            }
예제 #6
0
        private IVsWindowFrame GetOrCreateWindowFrame(Guid toolWindowId)
        {
            // We want VS to ask the package to create the tool window if it doesn't already exist
            const uint flags = (uint)__VSFINDTOOLWIN.FTW_fForceCreate;

            var hr = shell.FindToolWindow(flags, toolWindowId, out var windowFrame);

            Debug.Assert(ErrorHandler.Succeeded(hr), $"Failed to find tool window. Guid: {toolWindowId}, hr: {hr} ");

            if (ErrorHandler.Succeeded(hr))
            {
                return(windowFrame);
            }
            return(null);
        }
예제 #7
0
        /// <summary>
        /// This function asks to the QueryEditQuerySave service if it is possible to
        /// edit the file.
        /// </summary>
        /// <returns>True if the editing of the file are enabled, otherwise returns false.</returns>
        private bool CanEditFile()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            // Check the status of the recursion guard
            if (gettingCheckoutStatus)
            {
                return(false);
            }

            try
            {
                // Set the recursion guard
                gettingCheckoutStatus = true;

                // Get the QueryEditQuerySave service
                IVsQueryEditQuerySave2 queryEditQuerySave = (IVsQueryEditQuerySave2)GetService(typeof(SVsQueryEditQuerySave));

                // Now call the QueryEdit method to find the edit status of this file
                string[] documents = { fileName };
                uint     result;
                uint     outFlags;

                // Note that this function can pop up a dialog to ask the user to checkout the file.
                // When this dialog is visible, it is possible to receive other request to change
                // the file and this is the reason for the recursion guard.
                int hr = queryEditQuerySave.QueryEditFiles(
                    0,              // Flags
                    1,              // Number of elements in the array
                    documents,      // Files to edit
                    null,           // Input flags
                    null,           // Input array of VSQEQS_FILE_ATTRIBUTE_DATA
                    out result,     // result of the checkout
                    out outFlags    // Additional flags
                    );
                if (ErrorHandler.Succeeded(hr) && (result == (uint)tagVSQueryEditResult.QER_EditOK))
                {
                    // In this case (and only in this case) we can return true from this function.
                    return(true);
                }
            }
            finally
            {
                gettingCheckoutStatus = false;
            }
            return(false);
        }
예제 #8
0
        /// <summary>
        /// Launch the Project Properties Editor (properties pages)
        /// </summary>
        /// <returns>If we succeeded or not</returns>
        public override bool EditComponent(ITypeDescriptorContext context, object component)
        {
            if (component is ProjectNodeProperties)
            {
                IVsPropertyPageFrame propertyPageFrame = (IVsPropertyPageFrame)serviceProvider.GetService((typeof(SVsPropertyPageFrame)));

                int hr = propertyPageFrame.ShowFrame(Guid.Empty);
                if (ErrorHandler.Succeeded(hr))
                {
                    return(true);
                }
                else
                {
                    propertyPageFrame.ReportError(hr);
                }
            }

            return(false);
        }
예제 #9
0
        protected override async Task InitializeAsync(
            CancellationToken cancellationToken, IProgress <VSShell.ServiceProgressData> progress)
        {
            await JoinableTaskFactory.SwitchToMainThreadAsync();

            var vsShell = (IVsShell) await GetServiceAsync(typeof(SVsShell));

            Debug.Assert(vsShell != null, "unexpected null value for vsShell");
            var packageGuid     = guidEscherPkg;
            var hrAlreadyLoaded = vsShell.IsPackageLoaded(ref packageGuid, out IVsPackage package);

            if (!VSErrorHandler.Succeeded(hrAlreadyLoaded) || package == null)
            {
                var hrLoad = vsShell.LoadPackage(ref packageGuid, out package);
                if (VSErrorHandler.Failed(hrLoad))
                {
                    var msg = string.Format(CultureInfo.CurrentCulture, Resources.PackageLoadFailureExceptionMessage, hrLoad);
                    throw new InvalidOperationException(msg);
                }
            }
        }
예제 #10
0
파일: VSFileFinder.cs 프로젝트: phekmat/ef6
        private void FindInProjectFast(IVsProject4 vsp4, IVsHierarchy projectHierarchy)
        {
            uint celt = 0;

            uint[] rgItemIds = null;
            uint   pcActual  = 0;

            //
            // call this method twice, first time is to get the count, second time is to get the data.
            //
            VSErrorHandler.ThrowOnFailure(vsp4.GetFilesEndingWith(_input, celt, rgItemIds, out pcActual));
            if (pcActual > 0)
            {
                // now we know the actual size of the array to allocate, so invoke again
                celt      = pcActual;
                rgItemIds = new uint[celt];
                VSErrorHandler.ThrowOnFailure(vsp4.GetFilesEndingWith(_input, celt, rgItemIds, out pcActual));
                Debug.Assert(celt == pcActual, "unexpected number of entries returned from GetFilesEndingWith()");

                for (var i = 0; i < celt; i++)
                {
                    object pvar;
                    // NOTE:  in cpp, this property is not the full path.  It is the full path in c# & vb projects.
                    var hr   = projectHierarchy.GetProperty(rgItemIds[i], (int)__VSHPROPID.VSHPROPID_SaveName, out pvar);
                    var path = pvar as string;
                    if (VSErrorHandler.Succeeded(hr) &&
                        path != null)
                    {
                        // Dev10 Bug 653879: Retrieving project item absolute URL is expensive so retrieve when we actually need it.
                        VSFileInfo fileInfo;
                        fileInfo.ItemId    = rgItemIds[i];
                        fileInfo.Path      = path;
                        fileInfo.Hierarchy = projectHierarchy;
                        _paths.Add(fileInfo);
                    }
                }
            }
        }
예제 #11
0
        public void HookCommand(CommandID command, EventHandler handler)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }
            else if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            Dictionary <int, EventHandler> map;

            if (!_commandMap.TryGetValue(command.Guid, out map))
            {
                map = new Dictionary <int, EventHandler>();
                _commandMap[command.Guid] = map;
            }

            EventHandler handlers;

            if (!map.TryGetValue(command.ID, out handlers))
            {
                handlers = null;
            }

            map[command.ID] = (handlers + handler);

            if (!_hooked)
            {
                IVsRegisterPriorityCommandTarget svc = (IVsRegisterPriorityCommandTarget)_provider.GetService(typeof(SVsRegisterPriorityCommandTarget));
                if (svc != null && ErrorHandler.Succeeded(svc.RegisterPriorityCommandTarget(0, this, out _cookie)))
                {
                    _hooked = true;
                }
            }
        }