Esempio n. 1
0
        public static string GetContent(this EnvDTE.ProjectItem projectItem)
        {
            try
            {
                if (!projectItem.IsOpen)
                {
                    projectItem.Open();
                }

                var document = projectItem.Document;

                if (document != null)
                {
                    return(GetContent((EnvDTE.TextDocument)document.Object("TextDocument")));
                }

                var fileName = projectItem.TryGetFileName();

                if (string.IsNullOrEmpty(fileName))
                {
                    return(string.Empty);
                }

                return(File.ReadAllText(fileName));
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Opens the specified filename from the specified project name.
        /// </summary>
        public EditorWindow OpenItem(string project, params string[] path)
        {
            foreach (EnvDTE.Project proj in VSTestContext.DTE.Solution.Projects)
            {
                if (proj.Name == project)
                {
                    var items = proj.ProjectItems;
                    EnvDTE.ProjectItem item = null;
                    foreach (var itemName in path)
                    {
                        item  = items.Item(itemName);
                        items = item.ProjectItems;
                    }
                    Assert.IsNotNull(item);
                    var window = item.Open();
                    window.Activate();
                    return(App.GetDocument(item.Document.FullName));
                }
            }

            throw new InvalidOperationException(
                      String.Format(
                          "Failed to find {0} item in project {1}",
                          String.Join("\\", path),
                          project
                          )
                      );
        }
Esempio n. 3
0
        public static void SetContent(this EnvDTE.ProjectItem projectItem, string text)
        {
            if (!projectItem.IsOpen)
            {
                projectItem.Open(EnvDTE.Constants.vsViewKindCode);
            }

            var document = projectItem.Document;

            if (document != null)
            {
                SetContent(document, text);
            }
            else
            {
                var fileName = projectItem.TryGetFileName();

                if (string.IsNullOrEmpty(fileName))
                {
                    return;
                }

                File.WriteAllText(fileName, text);
            }
        }
Esempio n. 4
0
        void JSBeautifierProjectItem(EnvDTE.ProjectItem item)
        {
            if (!_package.OptionsPage.AllowDenyFilter.IsAllowed(item.Name))
            {
                return;
            }

            EnvDTE.Window documentWindow = null;
            try
            {
                if (!item.IsOpen[EnvDTE.Constants.vsViewKindTextView])
                {
                    documentWindow = item.Open(EnvDTE.Constants.vsViewKindTextView);
                    if (documentWindow == null)
                    {
                        return;
                    }
                }

                if (_package.JSBeautifier(item.Document))
                {
                    item.Document.Save();
                }
            }
            catch (COMException)
            {
                _package.OutputString($"Failed to process {item.Name}.");
            }
            finally
            {
                documentWindow?.Close();
            }
        }
Esempio n. 5
0
        public static string GetContent([NotNull] this EnvDTE.ProjectItem projectItem)
        {
            Contract.Requires(projectItem != null);
            Contract.Ensures(Contract.Result <string>() != null);

            try
            {
                if (!projectItem.IsOpen)
                {
                    projectItem.Open();
                }

                var document = projectItem.Document;

                if (document != null)
                {
                    // ReSharper disable once AssignNullToNotNullAttribute
                    return(GetContent((EnvDTE.TextDocument)document.Object("TextDocument")));
                }

                var fileName = projectItem.TryGetFileName();

                if (string.IsNullOrEmpty(fileName))
                {
                    return(string.Empty);
                }

                return(File.ReadAllText(fileName));
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }
        public static EnvDTE.Document TryGetDocument([CanBeNull] this EnvDTE.ProjectItem projectItem)
        {
            try
            {
                if (projectItem?.IsOpen != true)
                {
                    return(null);
                }

                projectItem.Open();
                return(projectItem.Document);
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 7
0
        //##########################################################

        private void OnListOnCellDoubleClick(object sender, DataGridViewCellEventArgs dataGridViewCellEventArgs)
        {
            if (dataGridViewCellEventArgs.RowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = list.Rows[dataGridViewCellEventArgs.RowIndex];
            Tuple <ILineChartPoints, IChartPoint> tagData = (Tuple <ILineChartPoints, IChartPoint>)row.Tag;

            EnvDTE.ProjectItem projItem = GetProjItem(tagData.Item1.data.fileData.projData.projName, tagData.Item1.data.fileData.fileName);
            if (projItem != null)
            {
                EnvDTE.Window wnd = projItem.Open();
                wnd.Activate();
                EnvDTE.TextSelection sel = (EnvDTE.TextSelection)projItem.Document.Selection;
                sel.MoveToLineAndOffset(tagData.Item1.data.pos.lineNum, tagData.Item1.data.pos.linePos);
            }
        }
        private EnvDTE.TextSelection OpenFile()
        {
            if (firstChildItem == null)
            {
                return(null);
            }

            if (!firstChildItem.get_IsOpen(null))
            {
                EnvDTE.Window window = firstChildItem.Open(null);
                window.Visible = true;
            }
            else
            {
                firstChildItem.Document.Activate();
            }

            EnvDTE.TextSelection selection = firstChildItem.Document.Selection as EnvDTE.TextSelection;
            selection.StartOfDocument(false);

            return(selection);
        }
        private void FormatDocument(EnvDTE.ProjectItem item)
        {
            Contract.Requires(item != null);

            try
            {
                var fileName = item.FileNames[0];

                Contract.Assume(Dte.ItemOperations != null);

                var shouldClose = fileName == null || !Dte.ItemOperations.IsFileOpen(fileName, VSConstants.LOGVIEWID.Code_string);

                var window = item.Open(VSConstants.LOGVIEWID.Code_string);

                if (window != null)
                {
                    window.Activate();

                    Contract.Assume(Dte.Commands != null);

                    Dte.Commands.Raise(Guids.vsStd2kCmdIdString, (int)VSConstants.VSStd2KCmdID.FORMATDOCUMENT, null, null);

                    if (shouldClose)
                    {
                        window.Close(EnvDTE.vsSaveChanges.vsSaveChangesYes);
                    }
                    else
                    {
                        item.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to format item.  Error: " + ex, packageTitle);
            }
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                //important to show the notification on the main thread of BIDS
                this.BeginInvoke(new MethodInvoker(delegate() { buttonOK_Click(sender, e); }));
            }
            else
            {
                try
                {
                    EnvDTE.Window           w         = projItem.Open(BIDSViewKinds.Designer); //opens the designer
                    IDesignerHost           designer  = (IDesignerHost)w.Object;
                    IComponentChangeService changesvc = (IComponentChangeService)designer.GetService(typeof(IComponentChangeService));
                    changesvc.OnComponentChanging(this.cloneCube, null);

                    dirtiedDimensions = new List <Microsoft.AnalysisServices.Dimension>();

                    //enable attributes
                    foreach (TreeNode nodeDimension in treeViewAggregation.Nodes)
                    {
                        foreach (TreeNode nodeAttribute in nodeDimension.Nodes)
                        {
                            if (!nodeAttribute.Checked)
                            {
                                CubeAttribute ca = (CubeAttribute)nodeAttribute.Tag;
                                ca.AttributeHierarchyOptimizedState = OptimizationType.FullyOptimized;

                                //if you're trying to enable indexes, make sure the dimension hierarchy is optimized, since you can't make the cube attribute optimized if the dimension attribute isn't
                                if (ca.Attribute.AttributeHierarchyOptimizedState == OptimizationType.NotOptimized)
                                {
                                    if (!dirtiedDimensions.Contains(ca.Attribute.Parent))
                                    {
                                        foreach (EnvDTE.ProjectItem pi in projItem.ContainingProject.ProjectItems)
                                        {
                                            if (!(pi.Object is Microsoft.AnalysisServices.Dimension))
                                            {
                                                continue;
                                            }
                                            if ((Microsoft.AnalysisServices.Dimension)pi.Object != ca.Attribute.Parent)
                                            {
                                                continue;
                                            }
                                            bool          bIsOpen = pi.get_IsOpen(EnvDTE.Constants.vsViewKindDesigner);
                                            EnvDTE.Window win     = null;
                                            if (bIsOpen)
                                            {
                                                foreach (EnvDTE.Window w2 in projItem.DTE.Windows)
                                                {
                                                    if (w2.ProjectItem != null && w2.ProjectItem.Document != null && w2.ProjectItem.Document.FullName == pi.Document.FullName)
                                                    {
                                                        win = w2;
                                                        break;
                                                    }
                                                }
                                            }
                                            if (win == null)
                                            {
                                                win = pi.Open(EnvDTE.Constants.vsViewKindDesigner);
                                                if (!bIsOpen)
                                                {
                                                    win.Visible = false;
                                                }
                                            }

                                            IDesignerHost           dimdesigner  = (IDesignerHost)win.Object;
                                            IComponentChangeService dimchangesvc = (IComponentChangeService)dimdesigner.GetService(typeof(IComponentChangeService));
                                            dimchangesvc.OnComponentChanging(ca.Attribute.Parent, null);

                                            //perform the update
                                            ca.Attribute.AttributeHierarchyOptimizedState = OptimizationType.FullyOptimized;

                                            dimchangesvc.OnComponentChanged(ca.Attribute.Parent, null, null, null); //marks the dimension designer as dirty
                                        }

                                        dirtiedDimensions.Add(ca.Attribute.Parent);
                                    }
                                    else
                                    {
                                        ca.Attribute.AttributeHierarchyOptimizedState = OptimizationType.FullyOptimized;
                                    }
                                }
                            }
                            else
                            {
                                CubeAttribute ca = (CubeAttribute)nodeAttribute.Tag;
                                ca.AttributeHierarchyOptimizedState = OptimizationType.NotOptimized;

                                //if you're trying to disable indexes, make sure the hierarchy isn't set to optimized
                                foreach (CubeHierarchy ch in ca.Parent.Hierarchies)
                                {
                                    foreach (Microsoft.AnalysisServices.Level l in ch.Hierarchy.Levels)
                                    {
                                        if (l.SourceAttributeID == ca.AttributeID && ch.Enabled)
                                        {
                                            ch.OptimizedState = OptimizationType.NotOptimized;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    changesvc.OnComponentChanged(this.cloneCube, null, null, null); //marks the cube designer as dirty

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrEmpty(ex.Message))
                    {
                        MessageBox.Show("Error saving: " + ex.Message);
                    }
                }
            }
        }
        /// <summary>
        /// Loops through each of the items in the project, attempting to extract any sections of code
        /// marked.
        /// </summary>
        /// <param name="dte"> Pointer to Object Model in which all actions should be performed </param>
        private bool ExtractItems(EnvDTE.ProjectItems projSourceItems, EnvDTE._DTE dte, EnvDTE.ProjectItems projDestItems)
        {
            EnvDTE.ProjectItem  projItem            = null;
            EnvDTE.TextDocument textDoc             = null;
            EnvDTE.Properties   extractorProperties = null;
            Extensions          extensions          = null;
            CommentPair         comments            = null;

            EnvDTE.Window w = null;

            bool   fSuccess = true;
            int    i, nItems, nLastIndex;
            string strExtension;

            extractorProperties = m_application.get_Properties("Assignment Manager", "Code Extractor");
            if (extractorProperties == null)
            {
                throw new Exception("The Academic Code Extractor is not properly installed and configured.");
            }
            extensions = extractorProperties.Item("Extensions").Object as Extensions;
            if (extensions == null)
            {
                throw new Exception("The Academic Code Extractor is not properly installed and configured.");
            }

            nItems = projDestItems.Count;
            for (i = 1; i <= nItems; i++)
            {
                projItem = projDestItems.Item(i);
                try
                {
                    if (projItem.ProjectItems.Count > 0)
                    {
                        ExtractItems(projSourceItems.Item(i).ProjectItems, dte, projItem.ProjectItems);
                    }
                    // Note that this will *actually* be happening in an invisible
                    // out-of-process instance of VS, so the user will not be
                    // subjected to appearing / disappearing windows.
                    w       = projItem.Open(EnvDTE.Constants.vsViewKindTextView);
                    textDoc = w.Document.Object("TextDocument") as EnvDTE.TextDocument;

                    strExtension = projItem.get_FileNames(1);

                    nLastIndex = strExtension.LastIndexOf('.');
                    if (nLastIndex == -1)
                    {
                        w.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                        continue;                                          // We have no capacity for files with no extension.
                    }
                    strExtension = strExtension.Remove(0, nLastIndex + 1); // Trim off the 'name.' part of 'name.ext'

                    comments = extensions[strExtension];
                    if (comments == null)
                    {
                        w.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                        continue;                         // This file has no associated extension type. Ignore it.
                    }

                    fSuccess &= ExtractText(textDoc, comments.BeginComment, comments.EndComment, projSourceItems);

                    w.Close(EnvDTE.vsSaveChanges.vsSaveChangesYes);
                }
                catch (Exception /*e*/)
                {
                    // If we end up here, that simply means that the file
                    // has no text. Since we obviously don't want to remove the
                    // textual tags from a file with no comments...
                    if (w != null)
                    {
                        w.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                    }
                }
            }

            return(fSuccess);
        }
Esempio n. 12
0
        /// <summary>
        /// Ensure that the current project item has the required extensions loaded.
        /// This is only called after verifying that the current project item does
        /// not satisfy the requirements.
        /// </summary>
        /// <param name="projectItem">The <see cref="EnvDTE.ProjectItem"/> to modify</param>
        /// <param name="serviceProvider">The <see cref="IServiceProvider"/> for document tracking.</param>
        /// <param name="extensions">An <see cref="T:ICollection{System.String}"/> of additional required extensions</param>
        public static bool EnsureExtensions(EnvDTE.ProjectItem projectItem, IServiceProvider serviceProvider, ICollection <string> extensions)
        {
            ServiceProvider provider = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)projectItem.DTE);

            // UNDONE: Localize message strings in here
            if ((int)DialogResult.No == VsShellUtilities.ShowMessageBox(
                    provider,
                    "Additional extensions are required to support the chosen generators. Would you like to load the required extensions now?",
                    "ORM Generator Selection",
                    OLEMSGICON.OLEMSGICON_QUERY,
                    OLEMSGBUTTON.OLEMSGBUTTON_YESNO,
                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST))
            {
                return(false);
            }
            EnvDTE.Document document    = projectItem.Document;
            bool            secondPass  = false;
            bool            tryDocument = true;
            string          itemPath    = null;

            while (tryDocument)
            {
                object     documentExtensionManager;
                MethodInfo methodInfo;
                if (null != document &&
                    null != (documentExtensionManager = ORMCustomToolUtility.GetDocumentExtension <object>(document, "ORMExtensionManager", itemPath ?? (itemPath = projectItem.get_FileNames(0)), serviceProvider)) &&
                    null != (methodInfo = documentExtensionManager.GetType().GetMethod("EnsureExtensions", new Type[] { typeof(string[]) })))
                {
                    string[] extensionsArray = new string[extensions.Count];
                    extensions.CopyTo(extensionsArray, 0);
                    methodInfo.Invoke(documentExtensionManager, new object[] { extensionsArray });
                    return(true);
                }

                if (secondPass)
                {
                    return(false);
                }
                tryDocument = false;
                secondPass  = true;

                // UNDONE: Localize message strings in here
                if ((int)DialogResult.No == VsShellUtilities.ShowMessageBox(
                        provider,
                        "The .ORM file must be open in the default designer to add extensions. Would you like to open or reopen the document now?",
                        "ORM Generator Selection",
                        OLEMSGICON.OLEMSGICON_QUERY,
                        OLEMSGBUTTON.OLEMSGBUTTON_YESNO,
                        OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST))
                {
                    return(false);
                }

                if (document != null)
                {
                    document.Close(EnvDTE.vsSaveChanges.vsSaveChangesPrompt);
                    document = projectItem.Document;
                }
                if (document == null)
                {
                    projectItem.Open(Guid.Empty.ToString("B")).Visible = true;
                    document    = projectItem.Document;
                    tryDocument = true;
                }
            }
            return(false);
        }