Exemplo n.º 1
0
        private static bool ProcessClassGuidAttribute(CodeEditPoint editorEditPoint, string guid)
        {
            CodeFunction currentFunction =
                editorEditPoint.GetCurrentCodeElement <CodeFunction>(vsCMElement.vsCMElementFunction);

            // if inside the function, then applying of an attribute to class/struct is prohibited:
            if (currentFunction != null)
            {
                return(false);
            }

            // if the current place is inside string characters: ""
            // then don't jump to the class definition:
            EditPoint startString = editorEditPoint.EditPoint.CreateEditPoint();

            startString.CharLeft(1);
            string s = startString.GetText(2);

            if (s == "\"\"" || editorEditPoint.IsSelected)
            {
                return(false);
            }

            CodeClass          currentClass  = editorEditPoint.GetCurrentCodeElement <CodeClass>(vsCMElement.vsCMElementClass);
            CodeStruct         currentStruct = editorEditPoint.GetCurrentCodeElement <CodeStruct>(vsCMElement.vsCMElementStruct);
            EditPoint          start         = null;
            CodeModelLanguages language      = CodeModelLanguages.Unknown;

            // find the start location of current class:
            if (currentClass != null)
            {
                start    = currentClass.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint();
                language = CodeHelper.GetCodeLanguage(currentClass.Language);
            }

            // find the start location of current structure:
            if (currentStruct != null)
            {
                start    = currentStruct.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint();
                language = CodeHelper.GetCodeLanguage(currentStruct.Language);
            }

            // append attributes at the 'start' location:
            if (start != null)
            {
                string sourceCodeSnippet = CodeHelper.GenerateFromAttribute(language,
                                                                            VariableHelper.GetGuidAttribute(guid));

                if (language == CodeModelLanguages.VisualBasic)
                {
                    sourceCodeSnippet += Environment.NewLine;
                }

                start.ReplaceText(start, sourceCodeSnippet, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
                return(true);
            }

            // nothing special found... just insert the Guid string...
            return(false);
        }
Exemplo n.º 2
0
        private void InsertGuid(Guid guid)
        {
            CodeEditPoint editorEditPoint = parent.CurrentEditPoint;
            string        guidText        = guid.ToString().ToUpper();

            try
            {
                // open the undo-context to combine all the modifications of the source code into one:
                parent.DTE.UndoContext.Open(SharedStrings.UndoContext_InsertGuid, true);

                // paste as Guid attribute of class or structure:
                if (!ProcessClassGuidAttribute(editorEditPoint, guid.ToString().ToUpper()))
                {
                    // otherwise type as normal text:
                    if (editorEditPoint.IsSelected)
                    {
                        editorEditPoint.InsertAsSelectionWithStringChars(guidText);
                    }
                    else
                    {
                        editorEditPoint.EditPoint.Insert(guidText);
                    }
                }
            }
            finally
            {
                // close the undo-context, so all the changes will be threated as one:
                parent.DTE.UndoContext.Close();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Invokes proper processing assigned to current action.
        /// </summary>
        public void Execute(object sender, EventArgs e)
        {
            CodeEditPoint editorEditPoint = parent.CurrentEditPoint;
            bool          isSelection     = editorEditPoint.IsSelected;
            string        newName         = nameGenerator.NextName();

            try
            {
                // open the undo-context to combine all the modifications of the source code into one:
                parent.DTE.UndoContext.Open(SharedStrings.UndoContext_ExtractPropertyRefactor, true);

                if (isSelection)
                {
                    // paste into selected text:
                    editorEditPoint.Selection.Insert(newName, (int)vsInsertFlags.vsInsertFlagsContainNewText);
                }
                else
                {
                    // just insert text:
                    editorEditPoint.EditPoint.Insert(newName);
                }
            }
            finally
            {
                // close the undo-context, so all the changes will be threated as one:
                parent.DTE.UndoContext.Close();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Invokes proper processing assigned to current action.
        /// </summary>
        public void Execute(object sender, EventArgs e)
        {
            CodeEditPoint editorEditPoint = parent.CurrentEditPoint;

            if (!editorEditPoint.IsRefactorValid)
            {
                return;
            }

            CodeEditSelection editorSelection = EditorHelper.GetSelectedVariables(editorEditPoint);

            if (editorSelection != null)
            {
                int  gotoLine;
                bool isRefactored;
                try
                {
                    // open the undo-context to combine all the modifications of the source code into one:
                    parent.DTE.UndoContext.Open(SharedStrings.UndoContext_ExtractPropertyRefactor, true);

                    isRefactored = Refactor(editorSelection.AllVariables, editorSelection.DisabledVariables, editorSelection.ParentName, editorSelection.ParentLanguage, editorSelection.CodeMembers, editorSelection.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint(), out gotoLine);
                }
                finally
                {
                    // close the undo-context, so all the changes will be threated as one:
                    parent.DTE.UndoContext.Close();
                }

                // jump without selection to the insertion place:
                if (isRefactored && gotoLine >= 0)
                {
                    editorEditPoint.GotoLine(gotoLine, 1);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Invokes proper processing assigned to current action.
        /// </summary>
        public void Execute(object sender, EventArgs e)
        {
            CodeEditPoint editorEditPoint = parent.CurrentEditPoint;
            bool          isRefactored;

            if (!editorEditPoint.IsRefactorValid)
            {
                return;
            }

            // get selected variables:
            CodeEditSelection editorSelection = EditorHelper.GetSelectedVariables(editorEditPoint);

            if (editorSelection != null && (editorSelection.Variables != null || editorSelection.Properties != null))
            {
                IList <CodeVariable> vars = editorSelection.AllVariables;
                EditPoint            ep;

                if (vars != null)
                {
                    ep = vars[vars.Count - 1].GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint();
                }
                else
                if (editorSelection.Variables != null)
                {
                    ep = editorSelection.Variables[editorSelection.Variables.Count - 1].GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint();
                }
                else
                {
                    ep = editorSelection.Properties[editorSelection.Properties.Count - 1].GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint();
                }

                int gotoLine;
                try
                {
                    // open the undo-context to combine all the modifications of the source code into one:
                    parent.DTE.UndoContext.Open(SharedStrings.UndoContext_InitConstructorRefactor, true);

                    // move cursor to the end of the line, not to interrupt any comments:
                    ep.EndOfLine();

                    // update the source code:
                    isRefactored = Refactor(CreateCodeNamedElements(editorSelection.AllVariables, editorSelection.DisabledVariables, editorSelection.AllProperties, editorSelection.DisabledProperties),
                                            editorSelection.ParentName, editorSelection.ParentLanguage, editorSelection.CodeMembers, ep, out gotoLine);
                }
                finally
                {
                    // close the undo-context, so all the changes will be threated as one:
                    parent.DTE.UndoContext.Close();
                }

                // jump without selection to the insertion place:
                if (isRefactored && gotoLine >= 0)
                {
                    editorEditPoint.GotoLine(gotoLine, 1);
                }
            }
        }
Exemplo n.º 6
0
        private static void InsertDatabaseConnection(CodeEditPoint editorEditPoint, bool isSelection, string connectionString)
        {
            // if selected text is equal to the one accepted by user - there is no point in editor update:
            if (isSelection && string.Compare(editorEditPoint.Selection.Text, connectionString, true) == 0)
            {
                return;
            }

            editorEditPoint.InsertTextOrReplaceSelection(SharedStrings.UndoContext_InsertConnectionString,
                                                         connectionString, true);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the currently selected text.
        /// </summary>
        private string GetSelection()
        {
            CodeEditPoint editPoint = parent.CurrentEditPoint;

            if (editPoint != null && editPoint.IsSelected)
            {
                return(editPoint.Selection.Text);
            }

            return(null);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Invokes proper processing assigned to current action.
        /// </summary>
        public void Execute(object sender, EventArgs e)
        {
            CodeEditPoint editorEditPoint = parent.CurrentEditPoint;

            string[]      selectedLines;
            string[]      resultLines;
            StringBuilder result;
            EditPoint     top, bottom;

            if (!editorEditPoint.IsRefactorValid)
            {
                return;
            }

            // get selected lines or current one:
            editorEditPoint.SelectFullLines(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, out top, out bottom);

            selectedLines = editorEditPoint.Selection.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            // get text and process it:
            if (ProcessText(selectedLines, out resultLines, editorEditPoint.CodeLanguage))
            {
                result = new StringBuilder();

                // convert to text
                if (resultLines != null && resultLines.Length > 0)
                {
                    foreach (string r in resultLines)
                    {
                        result.AppendLine(r);
                    }
                }

                try
                {
                    // open the undo-context to combine all the modifications of the source code into one:
                    parent.DTE.UndoContext.Open(SharedStrings.UndoContext_AssignReorderRefactor, true);

                    // paste processed text:
                    top.ReplaceText(bottom, result.ToString().Trim(), (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);

                    // remove the selection:
                    editorEditPoint.Selection.MoveToPoint(bottom, false);
                }
                finally
                {
                    // close the undo-context, so all the changes will be threated as one:
                    parent.DTE.UndoContext.Close();
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Invokes proper processing assigned to current action.
        /// </summary>
        public void Execute(object sender, EventArgs e)
        {
            CodeEditPoint editorEditPoint = parent.CurrentEditPoint;
            string        cs         = null;
            bool          isSelected = editorEditPoint.IsSelected;

            if (isSelected)
            {
                cs = editorEditPoint.Selection.Text;
            }

            // ask user for connection string:
            if (ConnectionHelper.PromptConnectionString(cs, out cs))
            {
                InsertDatabaseConnection(editorEditPoint, isSelected, cs);
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Init constructor.
 /// </summary>
 public CodeExtractorCSharp(CodeEditPoint editPoint)
     : base(editPoint)
 {
 }
Exemplo n.º 11
0
 /// <summary>
 /// Init constructor.
 /// </summary>
 public CodeExtractorDummy(CodeEditPoint editPoint)
     : base(editPoint)
 {
 }
Exemplo n.º 12
0
        /// <summary>
        /// Invokes proper processing assigned to current action.
        /// </summary>
        public void Execute(object sender, EventArgs e)
        {
            CodeEditPoint        editorEditPoint = parent.CurrentEditPoint;
            CodeEditSelection    selectionData;
            IList <CodeFunction> methods;
            IList <string>       names;
            int i = 0;

            if (!editorEditPoint.IsRefactorValid)
            {
                return;
            }

            // get selected methods inside the code editor:
            selectionData = EditorHelper.GetSelectedMethods(editorEditPoint, true);
            if (selectionData != null)
            {
                if (dlgRename == null)
                {
                    dlgRename = new MultiRenameForm();
                    PopulateConfig(dlgRename);
                }

                // show confirmation dialog:
                dlgRename.InitInterface(EditorHelper.FilterMethods(selectionData.AllMethods, vsCMFunction.vsCMFunctionConstructor, vsCMFunction.vsCMFunctionDestructor),
                                        EditorHelper.FilterMethods(selectionData.Methods, vsCMFunction.vsCMFunctionConstructor, vsCMFunction.vsCMFunctionDestructor));
                if (dlgRename.ShowDialog() == DialogResult.OK && dlgRename.ReadInterface(out methods, out names))
                {
                    // remember the latest settings:
                    StoreConfig(dlgRename);

                    // open the undo-context to combine all the modifications of the source code into one:
                    parent.DTE.UndoContext.Open(SharedStrings.UndoContext_MultiRenameRefactor, true);

                    // update the source code:
                    if (methods != null && names != null)
                    {
                        foreach (CodeFunction m in methods)
                        {
                            try
                            {
                                // update the names:
                                if (m.Name != names[i])
                                {
                                    m.Name = names[i];
                                }
                            }
                            catch (Exception ex)
                            {
                                Trace.WriteLine(ex.Message);
                            }

                            i++;
                        }
                    }

                    // close the undo-context, so all the changes will be threated as one:
                    parent.DTE.UndoContext.Close();
                }
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Init constructor.
 /// </summary>
 public CodeExtractorVBasic(CodeEditPoint editPoint)
     : base(editPoint)
 {
 }
Exemplo n.º 14
0
        /// <summary>
        /// Invokes proper processing assigned to current action.
        /// </summary>
        public void Execute(object sender, EventArgs e)
        {
            CodeEditPoint  point = parent.CurrentEditPoint;
            CodeTypeRef    type  = null;
            IList <string> names = null;

            if (point != null)
            {
                // check if there is variable defined:
                CodeVariable var = point.GetCurrentCodeElement <CodeVariable>(vsCMElement.vsCMElementVariable);
                type = (var != null ? var.Type : null);

                // or maybe clicked on parameter:
                if (type == null)
                {
                    CodeParameter param = point.GetCurrentCodeElement <CodeParameter>(vsCMElement.vsCMElementParameter);
                    type = (param != null ? param.Type : null);
                }
            }

            // extract enum values:
            if (type != null && type.CodeType as CodeEnum != null)
            {
                names = new List <string>();
                foreach (CodeElement f in type.CodeType.Members)
                {
                    names.Add(f.Name);
                }
            }

            if (type == null)
            {
                CodeElement elem = point.GetCurrentCodeElement <CodeElement>(vsCMElement.vsCMElementFunctionInvokeStmt);
                if (elem != null)
                {
                    names = new List <string>();
                    names.Add(elem.Name);
                }
            }

            if (type == null && names == null)
            {
                string identifier = point.CodeExtractor.CurrentIdentifier;

                System.Windows.Forms.MessageBox.Show(identifier);
                CodeType t = point.CodeExtractor.GetTypeInfo(identifier);
                names = new List <string>();
                if (point.CodeExtractor.Namespaces != null)
                {
                    foreach (string n in point.CodeExtractor.Namespaces)
                    {
                        names.Add(n);
                    }
                }
                names.Add("-----------");
                names.Add(identifier);
                if (t != null)
                {
                    foreach (CodeElement f in t.Members)
                    {
                        names.Add(f.Name);
                    }
                }

                //CodeFunction elem = point.GetCurrentCodeElement<CodeFunction>(vsCMElement.vsCMElementFunction);
                //if (elem != null)
                //{
                //    EditPoint body = elem.StartPoint.CreateEditPoint();
                //    string content = body.GetText(elem.EndPoint);
                //    names = new List<string>();

                //    names.Add(content);

                //    string[] xNames = new string[] { "System.GCCollectionMode", "GCCollectionMode", point.Selection.Text};
                //    foreach (string n in xNames)
                //    {
                //        CodeType t = point.GetTypeInfo(n);
                //        names.Add("-----------");
                //        names.Add(n);
                //        if (t != null)
                //            foreach (CodeElement f in t.Members)
                //                names.Add(f.Name);
                //    }
                //}
            }

            // and populate them into dialog
            if (names != null)
            {
                if (dlgExpand == null)
                {
                    dlgExpand = new ExpandEnumForm();
                }

                dlgExpand.SetUI(names);
                dlgExpand.ShowDialog();
            }
        }