protected override async Task <bool> TryHandleCommandAsync(IProjectTree node, bool focused, long commandExecuteOptions, IntPtr variantArgIn, IntPtr variantArgOut) { if (!_projectTree.NodeCanHaveAdditions(node)) { return(false); } __VSADDITEMFLAGS uiFlags = __VSADDITEMFLAGS.VSADDITEM_AddNewItems | __VSADDITEMFLAGS.VSADDITEM_SuggestTemplateName | __VSADDITEMFLAGS.VSADDITEM_AllowHiddenTreeView; string strBrowseLocations = _projectTree.TreeProvider.GetAddNewItemDirectory(node); string strFilter = string.Empty; await _projectVsServices.ThreadingService.SwitchToUIThread(); IVsAddProjectItemDlg addItemDialog = _serviceProvider.GetService <IVsAddProjectItemDlg, SVsAddProjectItemDlg>(); Assumes.Present(addItemDialog); Guid addItemTemplateGuid = Guid.Empty; // Let the dialog ask the hierarchy itself HResult res = addItemDialog.AddProjectItemDlg(node.GetHierarchyId(), ref addItemTemplateGuid, _projectVsServices.VsProject, (uint)uiFlags, DirName, VSResources.ClassTemplateName, ref strBrowseLocations, ref strFilter, out int iDontShowAgain); // Return true here regardless of whether or not the user clicked OK or they clicked Cancel. This ensures that some other // handler isn't called after we run. return(res == VSConstants.S_OK || res == VSConstants.OLE_E_PROMPTSAVECANCELLED); }
/// <summary> /// Create a file with the given template file and add it to the parent node. /// </summary> /// <param name="templateFile">The name of the template zip file.</param> /// <param name="parentNode">The node to which the new file will be added.</param> /// <param name="fileName">The name of the file to be created.</param> /// <returns>true if file is added successfully.</returns> public async Task <bool> CreateFileAsync(string templateFile, IProjectTree parentNode, string fileName) { Requires.NotNull(templateFile, nameof(templateFile)); Requires.NotNull(parentNode, nameof(parentNode)); Requires.NotNull(fileName, nameof(fileName)); await _projectVsServices.ThreadingService.SwitchToUIThread(); Project project = _projectVsServices.VsHierarchy.GetProperty <Project>(Shell.VsHierarchyPropID.ExtObject, null); var solution = project.DTE.Solution as Solution2; string templateFilePath = solution.GetProjectItemTemplate(templateFile, GetTemplateLanguage(project)); if (templateFilePath != null) { var parentId = parentNode.GetHierarchyId(); var result = new VSADDRESULT[1]; _projectVsServices.VsProject.AddItemWithSpecific(parentId, VSADDITEMOPERATION.VSADDITEMOP_RUNWIZARD, fileName, 0, new string[] { templateFilePath }, IntPtr.Zero, 0, Guid.Empty, null, Guid.Empty, result); if (result[0] == VSADDRESULT.ADDRESULT_Success) { return(true); } } return(false); }
/// <summary> /// Create a file with the given template file and add it to the parent node. /// </summary> /// <param name="templateFile">The name of the template zip file.</param> /// <param name="parentNode">The node to which the new file will be added.</param> /// <param name="fileName">The name of the file to be created.</param> /// <returns>true if file is added successfully.</returns> public async Task <bool> CreateFileAsync(string templateFile, IProjectTree parentNode, string fileName) { Requires.NotNull(templateFile, nameof(templateFile)); Requires.NotNull(parentNode, nameof(parentNode)); Requires.NotNull(fileName, nameof(fileName)); string templateLanguage = await GetTemplateLanguageAsync().ConfigureAwait(false); if (string.IsNullOrEmpty(templateLanguage)) { return(false); } await _projectVsServices.ThreadingService.SwitchToUIThread(); string templateFilePath = _dteServices.Solution.GetProjectItemTemplate(templateFile, templateLanguage); if (templateFilePath != null) { var parentId = parentNode.GetHierarchyId(); var result = new VSADDRESULT[1]; _projectVsServices.VsProject.AddItemWithSpecific(parentId, VSADDITEMOPERATION.VSADDITEMOP_RUNWIZARD, fileName, 0, new string[] { templateFilePath }, IntPtr.Zero, 0, Guid.Empty, null, Guid.Empty, result); if (result[0] == VSADDRESULT.ADDRESULT_Success) { return(true); } } return(false); }
public async Task <bool> TryHandleCommandAsync(IImmutableSet <IProjectTree> nodes, long commandId, bool focused, long commandExecuteOptions, IntPtr variantArgIn, IntPtr variantArgOut) { Requires.NotNull(nodes, nameof(nodes)); // We only support single node selections if (nodes.Count != 1) { return(false); } IProjectTree node = nodes.First(); // We only support nodes that can actually have things added to it if (!_projectTree.NodeCanHaveAdditions(node)) { return(false); } if (TryGetTemplateDetails(commandId, out TemplateDetails result)) { __VSADDITEMFLAGS uiFlags = __VSADDITEMFLAGS.VSADDITEM_AddNewItems | __VSADDITEMFLAGS.VSADDITEM_SuggestTemplateName | __VSADDITEMFLAGS.VSADDITEM_AllowHiddenTreeView; string strBrowseLocations = _projectTree.TreeProvider.GetAddNewItemDirectory(node); await _projectVsServices.ThreadingService.SwitchToUIThread(); // Look up the resources from each package to get the strings to pass to the Add Item dialog. // These strings must match what is used in the template exactly, including localized versions. Rather than relying on // our localizations being the same as the VS repository localizations we just load the right strings using the same // resource IDs as the templates themselves use. string dirName = _vsShell.Value.LoadPackageString(result.DirNamePackageGuid, result.DirNameResourceId); string templateName = _vsShell.Value.LoadPackageString(result.TemplateNamePackageGuid, result.TemplateNameResourceId); string strFilter = string.Empty; Guid addItemTemplateGuid = Guid.Empty; // Let the dialog ask the hierarchy itself HResult res = _addItemDialog.Value.AddProjectItemDlg(node.GetHierarchyId(), ref addItemTemplateGuid, _projectVsServices.VsProject, (uint)uiFlags, dirName, templateName, ref strBrowseLocations, ref strFilter, out _); // Return true here regardless of whether or not the user clicked OK or they clicked Cancel. This ensures that some other // handler isn't called after we run. return(res == HResult.OK || res == HResult.Ole.PromptSaveCancelled); } return(false); }
/// <summary> /// Obtains the absolute path and item id for a given project-relative source file path and project tree. /// </summary> /// <param name="sourceFile">The project-relative path to a source file.</param> /// <param name="tree">The tree to use to obtain the item id.</param> /// <returns>A tuple, where the first item is the absolute path to the source file and the second item is the item id.</returns> private Tuple <string, uint> SourceFileToLanguageServiceUnit(string sourceFile, IProjectTree tree) { string absolutePath = UnconfiguredProject.MakeRooted(sourceFile); IProjectTree sourceFileNode = PhysicalProjectTreeProvider.Value.FindByPath(tree, absolutePath); if (sourceFileNode != null) { uint itemid = sourceFileNode.GetHierarchyId(); return(new Tuple <string, uint>(absolutePath, itemid)); } return(null); }
private async Task <bool> ShowDialogAsync(IProjectTree node, __VSADDITEMFLAGS flags, string?localizedDirectoryName = null, string?localizedTemplateName = null) { string?path = _projectTree.TreeProvider.GetAddNewItemDirectory(node); if (path == null) { throw new ArgumentException("Node is marked with DisableAddItemFolder or DisableAddItemRecursiveFolder, call CanAddNewOrExistingItemTo before calling this method.", nameof(node)); } await _projectVsServices.ThreadingService.SwitchToUIThread(); string filter = string.Empty; Guid addItemTemplateGuid = Guid.Empty; // Let the dialog ask the hierarchy itself IVsAddProjectItemDlg?addProjectItemDialog = _addProjectItemDialog.Value; if (addProjectItemDialog == null) { return(false); } HResult result = addProjectItemDialog.AddProjectItemDlg( node.GetHierarchyId(), ref addItemTemplateGuid, _projectVsServices.VsProject, (uint)flags, localizedDirectoryName, localizedTemplateName, ref path, ref filter, out _); if (result == HResult.Ole.PromptSaveCancelled) { return(false); } if (result.Failed) { throw result.Exception !; } return(true); }
private static int ShowAddItemDialog(SVsServiceProvider serviceProvider, IProjectTree target, IVsProject vsProject, string strBrowseLocations, AddItemAction addItemAction) { #pragma warning disable RS0030 // Do not used banned APIs IVsAddProjectItemDlg addItemDialog = serviceProvider.GetService <IVsAddProjectItemDlg, SVsAddProjectItemDlg>(); #pragma warning restore RS0030 // Do not used banned APIs Assumes.Present(addItemDialog); __VSADDITEMFLAGS uiFlags = __VSADDITEMFLAGS.VSADDITEM_AddNewItems | __VSADDITEMFLAGS.VSADDITEM_SuggestTemplateName | __VSADDITEMFLAGS.VSADDITEM_AllowHiddenTreeView; if (addItemAction == AddItemAction.ExistingItem) { uiFlags = __VSADDITEMFLAGS.VSADDITEM_AddExistingItems | __VSADDITEMFLAGS.VSADDITEM_AllowMultiSelect | __VSADDITEMFLAGS.VSADDITEM_AllowStickyFilter | __VSADDITEMFLAGS.VSADDITEM_ProjectHandlesLinks; } string strFilter = string.Empty; Guid addItemTemplateGuid = Guid.Empty; // Let the dialog ask the hierarchy itself return(addItemDialog.AddProjectItemDlg(target.GetHierarchyId(), ref addItemTemplateGuid, vsProject, (uint)uiFlags, null, null, ref strBrowseLocations, ref strFilter, out _)); }