Exemplo n.º 1
0
        private NemerleCodeBehindEventBinder GetBinder(EnvDTE.ProjectItem projectItem)
        {
            if (projectItem == null)
            {
                return(null);
            }

            if (projectItem.FileCodeModel == null)
            {
                return(null);
            }

            ITypeResolutionService typeResolutionService = null;

            if (this._serviceProvider != null)
            {
                DynamicTypeService typeService = this._serviceProvider.GetService(typeof(DynamicTypeService)) as DynamicTypeService;
                if (typeService != null)
                {
                    typeResolutionService = typeService.GetTypeResolutionService(this._hierarchy);
                }
            }

            return(new NemerleCodeBehindEventBinder(projectItem.FileCodeModel, typeResolutionService));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:CustomTypeProvider"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public CustomTypeProvider(IServiceProvider provider, List <Project> additionalProjects, bool useProjectItemWrapper)
        {
            this.useProjectItemWrapper = useProjectItemWrapper;
            DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService));

            Debug.Assert(typeService != null, "No dynamic type service registered.");

            availableTypes = new Dictionary <string, Type>();

            if (additionalProjects != null && additionalProjects.Count > 0)
            {
                foreach (Project project in additionalProjects)
                {
                    IVsHierarchy          additionalHierarchy = DteHelper.GetVsHierarchy(provider, project);
                    ITypeDiscoveryService additionalDiscovery = typeService.GetTypeDiscoveryService(additionalHierarchy);
                    AddTypes(additionalDiscovery, project);
                }
            }
            else
            {
                IVsHierarchy hier = DteHelper.GetCurrentSelection(provider);
                Debug.Assert(hier != null, "No active hierarchy is selected.");

                ITypeDiscoveryService discovery = typeService.GetTypeDiscoveryService(hier);
                Project dteProject = VSHelper.ToDteProject(hier);

                AddTypes(discovery, dteProject);
            }

            if (availableTypes.Count > 0 && TypesChanged != null)
            {
                TypesChanged(this, new EventArgs());
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Retrieves the <see cref="Type"/> corresponding to the <paramref name="codeClass"/>.
        /// </summary>
        /// <exception cref="InvalidOperationException">An <see cref="ITypeDiscoveryService"/> cannot be
        /// retrieved for the project the class lives in.</exception>
        /// <exception cref="InvalidOperationException">The <paramref name="codeClass"/> has not been
        /// compiled into the project still.</exception>
        public static Type ToType(CodeClass codeClass)
        {
            Guard.ArgumentNotNull(codeClass, "codeClass");

            string           typeFullName = codeClass.FullName;
            Type             returnType   = null;
            IServiceProvider provider     = ToServiceProvider(codeClass.DTE);

            DynamicTypeService typeService = VsHelper.GetService <DynamicTypeService>(provider, true);

            IVsHierarchy hier = ToHierarchy(codeClass.ProjectItem.ContainingProject);

            ITypeResolutionService typeResolution = typeService.GetTypeResolutionService(hier);

            if (typeResolution == null)
            {
                throw new InvalidOperationException(String.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Properties.Resources.ServiceMissing,
                                                        typeof(ITypeResolutionService)));
            }

            returnType = typeResolution.GetType(typeFullName);
            if (returnType == null)
            {
                throw new InvalidOperationException(String.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Properties.Resources.ClassNotCompiledYet,
                                                        typeFullName,
                                                        codeClass.ProjectItem.get_FileNames(1)));
            }

            return(returnType);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be null.</param>
        /// <returns>
        /// A <see cref="T:System.ComponentModel.TypeConverter.StandardValuesCollection"></see> that holds a standard set of valid values, or null if the data type does not support a standard set of values.
        /// </returns>
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            DynamicTypeService typeService = (DynamicTypeService)context.GetService(typeof(DynamicTypeService));

            Debug.Assert(typeService != null, "No dynamic type service registered.");

            IVsHierarchy hier = DteHelper.GetCurrentSelection(context);

            Debug.Assert(hier != null, "No active hierarchy is selected.");

            ITypeDiscoveryService typeDiscovery = typeService.GetTypeDiscoveryService(hier);

            if (typeDiscovery != null)
            {
                List <string> types = new List <string>();
                foreach (Type type in typeDiscovery.GetTypes(typeof(object), false))
                {
                    if (ShouldInclude(type))
                    {
                        types.Add(type.FullName);
                    }
                }

                types.Sort(StringComparer.CurrentCulture);

                return(new StandardValuesCollection(types));
            }
            else
            {
                return(new StandardValuesCollection(new List <string>()));
            }
        }
Exemplo n.º 5
0
            /// <summary>
            /// Initializes a new instance of the <see cref="T:CustomTypeProvider"/> class.
            /// </summary>
            /// <param name="provider">The provider.</param>
            public CustomTypeProvider(IServiceProvider provider)
            {
                DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService));

                System.Diagnostics.Debug.Assert(typeService != null, "No dynamic type service registered.");

                IVsHierarchy hier = DteHelper2.GetCurrentSelection(provider);

                System.Diagnostics.Debug.Assert(hier != null, "No active hierarchy is selected.");

                ITypeDiscoveryService discovery = typeService.GetTypeDiscoveryService(hier);
                Project dteProject = VsHelper.ToDteProject(hier);

                availableTypes = new Dictionary <string, Type>();
                LoadTypes(availableTypes, discovery.GetTypes(typeof(object), false), dteProject);

                // Try loading Project references
                LoadProjectReferenceTypesFromCurrentProject(availableTypes, provider, dteProject);

                // If we don't get any type loaded, try with the rest of the projects in the current sln
                if (availableTypes.Count == 0)
                {
                    LoadTypesFromSolution(availableTypes, provider, dteProject);
                }

                EnsureKnownTypes(availableTypes);

                if (availableTypes.Count > 0 && TypesChanged != null)
                {
                    TypesChanged(this, new EventArgs());
                }
            }
Exemplo n.º 6
0
        /// <summary>
        /// Returns the discovered types for autocomplete
        /// </summary>
        protected virtual IEnumerable <Type> DiscoverTypes()
        {
            DynamicTypeService    typeService          = (DynamicTypeService)DataGridView.Site.GetService(typeof(DynamicTypeService));
            IVsHierarchy          hier                 = DteHelper.GetCurrentSelection(DataGridView.Site);
            ITypeDiscoveryService typeDiscoveryService = typeService.GetTypeDiscoveryService(hier);

            return((IEnumerable <Type>)typeDiscoveryService.GetTypes(typeof(object), false));
        }
Exemplo n.º 7
0
        public NemerleContainedLanguage(IVsTextBufferCoordinator bufferCoordinator, NemerleIntellisenseProvider intellisenseProject, uint itemId, IVsHierarchy pHierarchy)
        {
            if (null == bufferCoordinator)
            {
                throw new ArgumentNullException("bufferCoordinator");
            }
            if (null == intellisenseProject)
            {
                throw new ArgumentNullException("intellisenseProject");
            }
            _hierarchy = pHierarchy;

            object projectItem = null;

            pHierarchy.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_ExtObject, out projectItem);

            _projectItem = projectItem as EnvDTE.ProjectItem;

            EnvDTE.Property prop = _projectItem.Properties.Item("FullPath");
            if (prop != null)
            {
                _filePath = prop.Value as string;
            }

            var project = _projectItem.ContainingProject as NemerleOAProject;

            if (project != null)
            {
                _projectInfo = ((NemerleProjectNode)project.Project).ProjectInfo;
            }

            _typeResolutionService = null;
            DynamicTypeService typeService = LanguageService.GetService(typeof(DynamicTypeService)) as DynamicTypeService;

            if (typeService != null)
            {
                _typeResolutionService = typeService.GetTypeResolutionService(this._hierarchy);
            }

            this.bufferCoordinator   = bufferCoordinator;
            this.intellisenseProject = intellisenseProject;
            this.itemId = itemId;

            // Make sure that the secondary buffer uses the IronPython language service.
            IVsTextLines buffer;

            ErrorHandler.ThrowOnFailure(bufferCoordinator.GetSecondaryBuffer(out buffer));

            Guid languageGuid;

            this.GetLanguageServiceID(out languageGuid);
            ErrorHandler.ThrowOnFailure(buffer.SetLanguageServiceID(ref languageGuid));

            _documentClosingEventHandler = new _dispDocumentEvents_DocumentClosingEventHandler(OnDocumentClosing);
        }
Exemplo n.º 8
0
        private void InitTypeDiscoveryService()
        {
            if (_discovery != null)
            {
                return;
            }
            DynamicTypeService typeService = _services.TypeService;

            if (typeService != null)
            {
                _discovery = typeService.GetTypeDiscoveryService(GetHierarchy());
            }
        }
Exemplo n.º 9
0
        string GetModuleType(Project project, CodeClass codeClass)
        {
            IVsSolution        solution     = (IVsSolution)GetService(typeof(IVsSolution));
            DynamicTypeService typeResolver = (DynamicTypeService)GetService(typeof(DynamicTypeService));

            IVsHierarchy hierarchy = null;

            solution.GetProjectOfUniqueName(project.UniqueName, out hierarchy);

            var typeResolutionService = typeResolver.GetTypeResolutionService(hierarchy);

            return(typeResolutionService.GetType(codeClass.FullName).AssemblyQualifiedName);
        }
        public Type GetTypeFromString(string type)
        {
            IServiceProvider serviceProvider = new ServiceProvider(_dte2 as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);

            DynamicTypeService typeService = serviceProvider.GetService(typeof(DynamicTypeService)) as DynamicTypeService;

            IVsSolution sln = serviceProvider.GetService(typeof(IVsSolution)) as IVsSolution;

            IVsHierarchy hier;

            sln.GetProjectOfUniqueName(_dte2.ActiveDocument.ProjectItem.ContainingProject.UniqueName, out hier);

            return(typeService.GetTypeResolutionService(hier).GetType(type, true));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Returns the type discovery service for the given project
        /// </summary>
        /// <param name="project">The project to use for the vsHierarchy</param>
        /// <returns>The type discovery service or null if it cannot be found</returns>
        /// <exception cref="WizardCancelledException"> is thrown if no hierarchy is available.</exception>
        private ITypeDiscoveryService GetTypeDiscoveryService(Project project)
        {
            DynamicTypeService dts = this.GetService(typeof(DynamicTypeService)) as DynamicTypeService;

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

            // Get the hierarchy.  Throws if not available.
            IVsHierarchy vsHierarchy = this.GetVsHierarchy(project);

            ITypeDiscoveryService tds = dts.GetTypeDiscoveryService(vsHierarchy);

            return(tds);
        }
Exemplo n.º 12
0
            private void LoadTypesFromProjects(List <EnvDTE.Project> projects,
                                               Dictionary <string, Type> availableTypes,
                                               IServiceProvider provider,
                                               Project currentProject)
            {
                DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService));

                foreach (Project project in projects)
                {
                    if (project.UniqueName != currentProject.UniqueName)
                    {
                        IVsHierarchy hier = DteHelper2.GetVsHierarchy(provider, project);
                        System.Diagnostics.Debug.Assert(hier != null, "No active hierarchy is selected.");
                        ITypeDiscoveryService discovery = typeService.GetTypeDiscoveryService(hier);
                        LoadTypes(availableTypes, discovery.GetTypes(typeof(object), false), project);
                    }
                }
            }
Exemplo n.º 13
0
        private void Initialize()
        {
            if (!initialized)
            {
                this.dynamicTypeService = serviceProvider.GetService(typeof(DynamicTypeService)) as DynamicTypeService;

                //this.dynamicTypeService.AssemblyRefreshed += new AssemblyRefreshedEventHandler(dynamicTypeService_AssemblyRefreshed);
                //this.dynamicTypeService.AssemblyDeleted += new AssemblyDeletedEventHandler(dynamicTypeService_AssemblyDeleted);

                var vs = this.serviceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
                this.buildEvents = vs.Events.BuildEvents;
                //this.buildEvents.OnBuildDone += new _dispBuildEvents_OnBuildDoneEventHandler(BuildEvents_OnBuildDone);

                initialized = true;

                //LoadTypes();
            }
        }
Exemplo n.º 14
0
        private void ValidateTypeName(string typeName)
        {
            DynamicTypeService typeService = (DynamicTypeService)site.GetService(typeof(DynamicTypeService));

            Debug.Assert(typeService != null, "No dynamic type service registered.");

            IVsHierarchy hier = DteHelper.GetCurrentSelection(site);

            Debug.Assert(hier != null, "No active hierarchy is selected.");

            ITypeResolutionService resolution = typeService.GetTypeResolutionService(hier);
            Type type = resolution.GetType(TypeHelper.ParseGenericType(typeName));

            if (type == null)
            {
                throw new TypeLoadException(string.Format(CultureInfo.CurrentUICulture, Resources.TypeNameValidatorTypeNotFound, typeName));
            }
        }
        private Type ResolveType(Project prj, CodeType codeType)
        {
            try
            {
                DynamicTypeService typeService = this.serviceProvider.GetService(typeof(DynamicTypeService)) as DynamicTypeService;
                IVsSolution        solution    = this.serviceProvider.GetService(typeof(IVsSolution)) as IVsSolution;

                IVsHierarchy hierarchy;
                solution.GetProjectOfUniqueName(prj.UniqueName, out hierarchy);

                ITypeResolutionService resolutionService = typeService.GetTypeResolutionService(hierarchy);
                return(resolutionService.GetType(codeType.FullName));
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 16
0
        private ITypeResolutionService GetResolutionService(Project currentProject)
        {
            DynamicTypeService typeService = ServiceProvider.GetService(typeof(DynamicTypeService)) as DynamicTypeService;

            if (typeService == null)
            {
                throw new InvalidOperationException("No dynamic type service registered.");
            }

            IVsSolution vsSolution = (IVsSolution)ServiceProvider.GetService(typeof(IVsSolution));

            vsSolution.GetProjectOfUniqueName(currentProject.UniqueName, out IVsHierarchy vsHierarchy);
            if (vsHierarchy == null)
            {
                throw new InvalidOperationException("No active hierarchy is selected.");
            }

            return(typeService.GetTypeResolutionService(vsHierarchy));
        }
Exemplo n.º 17
0
        public Type GetTypeFromString(string type)
        {
            IServiceProvider   serviceProvider = new ServiceProvider(_dte2 as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
            DynamicTypeService typeService     = serviceProvider.GetService(typeof(DynamicTypeService)) as DynamicTypeService;
            IVsSolution        solution        = serviceProvider.GetService(typeof(IVsSolution)) as IVsSolution;

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

            IVsHierarchy hierarchy;

            solution.GetProjectOfUniqueName(_dte2.ActiveDocument.ProjectItem.ContainingProject.UniqueName, out hierarchy);

            Type returnType = typeService?.GetTypeResolutionService(hierarchy)?.GetType(type, false);

            return(returnType);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Converts the specified value object to a <see cref="T:System.String"></see> object.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
        /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"></see> to use.</param>
        /// <param name="value">The <see cref="T:System.Object"></see> to convert.</param>
        /// <returns>
        /// An <see cref="T:System.Object"></see> that represents the converted value.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The conversion could not be performed. </exception>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                DynamicTypeService typeService = (DynamicTypeService)context.GetService(typeof(DynamicTypeService));
                Debug.Assert(typeService != null, "No dynamic type service registered.");

                IVsHierarchy hier = DteHelper.GetCurrentSelection(context);
                Debug.Assert(hier != null, "No active hierarchy is selected.");

                ITypeResolutionService resolution = typeService.GetTypeResolutionService(hier);
                Type type = resolution.GetType(value.ToString());
                return(type);
            }
            else
            {
                return(base.ConvertFrom(context, culture, value));
            }
        }
Exemplo n.º 19
0
        public static List <Type> GetProjectTypes(Type baseType, IServiceProvider serviceProvider, Projects projects)
        {
            IVsSolution        solution     = (IVsSolution)serviceProvider.GetService(typeof(IVsSolution));
            DynamicTypeService typeResolver = (DynamicTypeService)serviceProvider.GetService(typeof(DynamicTypeService));

            List <Type> result = new List <Type>();

            for (int i = 1; i <= projects.Count; i++)
            {
                Project      project   = projects.Item(i);
                IVsHierarchy hierarchy = null;
                solution.GetProjectOfUniqueName(project.UniqueName, out hierarchy);

                //_typeResolutionService = typeResolver.GetTypeResolutionService(hierarchy);
                var typeDiscoveryService             = typeResolver.GetTypeDiscoveryService(hierarchy);
                System.Collections.ICollection fined = typeDiscoveryService.GetTypes(baseType, true);
                foreach (Type t in fined)
                {
                    result.Add(t);
                }
            }
            return(result);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService));

            IVsHierarchy hier = DteHelper.GetCurrentSelection(provider);

            ITypeDiscoveryService typeDiscovery = typeService.GetTypeDiscoveryService(hier);

            Project project = ToDteProject(hier);

            if (DteHelper.IsWebProject(project))
            {
                VSWebSite     vsProject  = (VSWebSite)project.Object;
                List <string> assemblies = new List <string>();
                foreach (AssemblyReference reference in vsProject.References)
                {
                    if (!string.IsNullOrEmpty(reference.FullPath))
                    {
                        assemblies.Add(reference.FullPath);
                    }
                }

                MethodInfo setAsssembliesMethod = typeDiscovery.GetType().GetMethod(SetAssembliesMethodName,
                                                                                    BindingFlags.NonPublic | BindingFlags.Instance);
                setAsssembliesMethod.Invoke(typeDiscovery, new object[] { assemblies.ToArray() });
            }

            if (typeDiscovery != null)
            {
                List <string>   assembliesAdded = new List <string>();
                List <Assembly> assemblies      = new List <Assembly>();
                List <Type>     types           = new List <Type>();

                foreach (Type type in typeDiscovery.GetTypes(typeof(object), false))
                {
                    if (ShouldInclude(type))
                    {
                        if (!assembliesAdded.Contains(type.Assembly.FullName))
                        {
                            assembliesAdded.Add(type.Assembly.FullName);
                            assemblies.Add(type.Assembly);
                        }
                        types.Add(type);
                    }
                }

                IWindowsFormsEditorService svc  = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                ClassBrowserEditorForm     form = new ClassBrowserEditorForm(assemblies, types);
                DialogResult result;

                if (svc != null)
                {
                    result = svc.ShowDialog(form);
                }
                else
                {
                    result = form.ShowDialog();
                }

                if (result == DialogResult.OK)
                {
                    return(form.TypeFullName);
                }
                else
                {
                    return(value);
                }
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            DynamicTypeService typeResolverService = (DynamicTypeService)GetService(typeof(DynamicTypeService));


            if (GetService(typeof(IToolboxService)) is IVsToolboxService2 toolBoxService)
            {
                try
                {
                    var items = new List <Tuple <string, ToolboxItem, IEnumerable <FrameworkName>, Guid> >();
                    HashSet <string> processedAssemblies = new HashSet <string>();
                    foreach (var directory in GetReferenceDirectories())
                    {
                        foreach (string fileName in Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories))
                        {
                            var assembly = typeResolverService.CreateDynamicAssembly(fileName);
                            if (assembly != null && !processedAssemblies.Contains(assembly.FullName))
                            {
                                processedAssemblies.Add(assembly.FullName);
                                try
                                {
                                    foreach (Type type in assembly.GetTypes())
                                    {
                                        if (!type.IsAbstract && !type.IsGenericTypeDefinition && IsToolboxItem(type))
                                        {
                                            if (typeof(IComponent).IsAssignableFrom(type))
                                            {
                                                items.Add(new Tuple <string, ToolboxItem, IEnumerable <FrameworkName>, Guid>(
                                                              "VSAssemblyResolver", new ResolverToolboxItem(type), null, Guid.Empty));
                                            }
                                        }
                                    }
                                }
                                catch (ReflectionTypeLoadException) { }
                                catch (TypeLoadException) { }
                                catch (FileNotFoundException) { }
                            }
                        }
                    }
                    toolBoxService.AddToolboxItems(items, null);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
            }
            // Show a Message Box to prove we were here
            IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
            Guid       clsid   = Guid.Empty;

            ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(
                                            0,
                                            ref clsid,
                                            "VSAssemblyResolver",
                                            string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this),
                                            string.Empty,
                                            0,
                                            OLEMSGBUTTON.OLEMSGBUTTON_OK,
                                            OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                                            OLEMSGICON.OLEMSGICON_INFO,
                                            0, // false
                                            out int result));
        }