static public CodeElement GetCodeElementAtCursor(DTE dte, vsCMElement elementType)
        {
            try {
                CodeElement objCodeElement = null;

                var objCursorTextPoint = GetCursorTextPoint(dte);

                if ((objCursorTextPoint != null))
                {
                    // Get the class at the cursor
                    objCodeElement = GetCodeElementAtTextPoint(elementType, dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElements, objCursorTextPoint);
                }

                //if (objCodeElement == null) {
                //    MessageBox.Show("No matching elementType found at the cursor!");
                //}
                //else {
                //    MessageBox.Show("Class at the cursor: " + objCodeElement.FullName);
                //}
                return(objCodeElement);
            }
            catch (Exception ex)
            {
                Debug.DebugHere(ex);
            }
            return(null);
        }
예제 #2
0
 /// <summary>
 /// Parse Attribute Argument into the actual string value
 /// </summary>
 /// <param name="propType"></param>
 /// <param name="value"></param>
 /// <remarks>
 /// Attribute argument is presented exactly as it was typed
 /// Ex: SomeArg:="Test" would result in the Argument.Value "Test" (with quote)
 /// Ex: SomeArg:=("Test") would result in the Argument.Value ("Test") (with parentheses and quote)
 /// </remarks>
 private static object ParseAttributeProperty(Type propType, string value)
 {
     try
     {
         if (value == null)
         {
             return(GetDefaultValue(propType));
         }
         object parsed;
         if (propType.IsEnum)
         {
             parsed = value.StripQualifier();
         }
         else if (propType == typeof(string))
         {
             parsed = value.Trim('\"');
         }
         else
         {
             parsed = Convert.ChangeType(value, propType);
         }
         return(parsed);
     }
     catch (Exception ex)
     {
         Debug.DebugHere(ex);
         throw;
     }
 }
예제 #3
0
 public static IEnumerable <CodeVariable> GetDependencyProperties(this CodeClass cls)
 {
     try {
         var sharedFields = cls.GetVariables().Where(x => x.IsShared && x.Type.CodeType != null);
         return(sharedFields.Where(x => x.Type.CodeType.FullName == "System.Windows.DependencyProperty"));
     }
     catch (Exception ex) {
         Debug.DebugHere(ex);
     }
     return(null);
 }
예제 #4
0
 private static T ParseAttributeProperty <T>(string value)
 {
     try
     {
         return((T)ParseAttributeProperty(typeof(T), value));
     }
     catch (Exception e)
     {
         Debug.DebugHere(e);
         throw;
     }
 }
예제 #5
0
 /// <summary>
 /// Selects project item in Solution Explorer
 /// </summary>
 /// <remarks></remarks>
 public static void SelectSolutionExplorerNode(this DTE2 dte2, string nodePath)
 {
     try
     {
         var item = dte2.ToolWindows.SolutionExplorer.GetItem(nodePath);
         item.Select(vsUISelectionType.vsUISelectionTypeSelect);
     }
     catch (Exception ex)
     {
         Debug.DebugHere(ex);
     }
 }
        /// <summary>
        /// Render within target file, instead of into a separate file
        /// </summary>
        /// <remarks></remarks>
        private void RenderWithinTarget()
        {
            var undoCtx = Dte.UndoContext;

            undoCtx.Open(OptionAttributeType.Name);
            try {
                //render shared library. It has to be created before the interface can be added to the classes. Otherwise EnvDte would throw exception
                RenderLibrary();

                var validClasses = GetValidClasses();

                var sw       = new Stopwatch();
                var hasError = false;
                //!for each class
                foreach (var cc in validClasses)
                {
                    sw.Start();

                    var classWriter = _manager.CreateWriter(cc);

                    //!generate
                    GenerateInClass(classWriter);

                    //!if also doing derivedClasses
                    var applyToSubclasses = classWriter.OptionTag.OptionAttribute.ApplyToDerivedClasses;
                    if (applyToSubclasses)
                    {
                        //!for each subclass
                        foreach (var derivedC in cc.GetSubclasses())
                        {
                            var childInfo = _manager.CreateWriter(cc, derivedC);
                            //generate
                            GenerateInClass(childInfo);
                            //combine status
                            if (childInfo.HasError)
                            {
                                classWriter.HasError = true;
                                classWriter.Status.AppendLine(childInfo.Status.ToString());
                            }
                        }
                    }

                    //if there's error
                    if (classWriter.HasError)
                    {
                        hasError = true;
                        MessageBox.Show(classWriter.Status.ToString());
                    }
                    //finish up
                    sw.Stop();
                    DebugWriteLine(string.Format("Finished {0} in {1}", cc.Name, sw.Elapsed));
                    sw.Reset();
                }


                //if there's error
                if (hasError)
                {
                    //undo everything
                    undoCtx.SetAborted();
                }
                else
                {
                    undoCtx.Close();
                    //automatically save, since we are changing the target file
                    var doc = ProjectItem.DteObject.Document;
                    //if anything is changed, save
                    if (doc != null && !doc.Saved)
                    {
                        doc.Save();
                    }
                }
            }
            catch (Exception ex) {
                Debug.DebugHere(ex);
                if (undoCtx.IsOpen)
                {
                    undoCtx.SetAborted();
                }
            }
        }