Exemple #1
0
        /// <summary>
        /// Formats a document.
        /// </summary>
        /// <param name="doc">The document to format.</param>
        public void FormatDocument(Document doc)
        {
            // If the user disabled the plugin, bail.
            if (!this.Options.Enabled)
            {
                return;
            }

            // If the document isn't text or an enabled language, bail.
            TextDocument textDoc = doc as TextDocument;

            if (textDoc == null || !this.LanguageSelectedForFormatting(textDoc.Language))
            {
                return;
            }

            // You can only format the active document, so we have to temporarily
            // activate each document that needs formatting.
            Document active = CodeRush.Documents.Active;

            if (textDoc != active)
            {
                textDoc.Activate();
            }
            CodeRush.Documents.Format();
            if (textDoc != active)
            {
                active.Activate();
            }
        }
        private void ConvertStringToAppSetting_Execute(Object sender, ApplyContentEventArgs ea)
        {
            TextDocument CodeDoc = CodeRush.Documents.ActiveTextDocument;

            using (CodeDoc.NewCompoundAction("Extract to App.config setting"))
            {
                ProjectElement      ActiveProject = CodeRush.Source.ActiveProject;
                PrimitiveExpression StringLiteral = ea.Element as PrimitiveExpression;

                // Ensure App.config
                string       BasePath  = System.IO.Path.GetDirectoryName(ActiveProject.FilePath);
                string       Filename  = BasePath + "\\App.Config";
                TextDocument configDoc = CodeRush.Documents.GetTextDocument(Filename);

                if (configDoc == null)
                {
                    // Open configDoc
                    configDoc = (TextDocument)CodeRush.File.Activate(Filename);
                }

                if (configDoc == null)
                {
                    // Create configDoc
                    string DefaultSettings = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><configuration></configuration>";
                    CodeRush.File.WriteTextFile(Filename, DefaultSettings);
                    CodeRush.Project.AddFile(ActiveProject, Filename);
                    configDoc = (TextDocument)CodeRush.File.Activate(Filename);
                }

                //Ensure RootNode
                SP.HtmlElement RootNode = (SP.HtmlElement)configDoc.FileNode.Nodes[1];

                // Get appSettings node
                SP.HtmlElement AppSettings = GetAppSettings(RootNode);
                if (AppSettings == null)
                {
                    AppSettings = CreateHTMLNode("appSettings");
                    RootNode.AddNode(AppSettings);
                    RewriteNodeInDoc(RootNode, configDoc);
                    RootNode    = (SP.HtmlElement)configDoc.FileNode.Nodes[1];
                    AppSettings = GetAppSettings(RootNode);
                }

                // Generate a new setting... Add it to correct location in App.config.
                string         SettingValue = (string)StringLiteral.PrimitiveValue;
                string         SettingName  = SettingValue.Replace(" ", "");
                SP.HtmlElement SettingNode  = CreateSettingNode(SettingName, SettingValue);
                AppSettings.AddNode(SettingNode);
                RewriteNodeInDoc(AppSettings, configDoc);

                // Add reference to System.Configuration dll.
                CodeRush.Project.AddReference(ActiveProject, "System.Configuration.dll");

                // Replace Literal with reference to setting through Configuration manager
                string Code         = String.Format("System.Configuration.ConfigurationManager.AppSettings[\"{0}\"]", SettingName);
                var    NewCodeRange = CodeDoc.SetText(StringLiteral.Range, Code);
                CodeDoc.ParseIfTextChanged();
                CodeDoc.ParseIfNeeded();
                var SourceNode = CodeDoc.GetNodeAt(NewCodeRange.Start);

                // Find Newly created Setting
                configDoc.ParseIfTextChanged();
                configDoc.ParseIfNeeded();
                RootNode    = (SP.HtmlElement)configDoc.FileNode.Nodes[1];
                AppSettings = GetAppSettings(RootNode);
                SettingNode = GetSettingWithKeyAndValue(AppSettings, SettingName, SettingValue);

                // Link Code and Setting.
                var         LinkSet         = CodeRush.LinkedIdentifiers.NewMultiDocumentContainer();
                SourceRange StringRange     = (SourceNode.Parent.Parent.Parent.Parent.DetailNodes[0] as LanguageElement).Range;
                SourceRange CodeSourceRange = new SourceRange(StringRange.Start.OffsetPoint(0, 1), StringRange.End.OffsetPoint(0, -1));
                LinkSet.Add(new FileSourceRange(CodeDoc.FileNode, CodeSourceRange));
                LinkSet.Add(new FileSourceRange(configDoc.FileNode, SettingNode.Attributes["key"].ValueRange));
                CodeRush.LinkedIdentifiers.Invalidate(configDoc);
                CodeDoc.Activate();
                CodeRush.LinkedIdentifiers.Invalidate(CodeDoc);
                CodeRush.Selection.SelectRange(CodeSourceRange);
                configDoc.ParseIfTextChanged();
                configDoc.ParseIfNeeded();
            }
        }
        private void StartTargetPicker(TextDocument textDocument)
        {
            TextView activeView = textDocument.ActiveView;
            if (activeView == null)
            {
                if (textDocument.FirstView != null)
                {
                    textDocument.Activate();
                    activeView = textDocument.FirstView;
                    activeView.Activate();
                }
            }

            // IElement -- Lightweight elements for representing source code, including referenced assemblies.
            // LanguageElement -- Heavier, bigger elements
            // We can convert from lightweight to heavy using LanguageElementRestorer.ConvertToLanguageElement();

            Class typeElement = _TypeElement.GetLanguageElement() as Class;
            if (typeElement == null)
            {
                // We just opened the file -- we need to get a new resolve on the _TypeElement.
                _TypeElement = _ObjectCreationExpression.ObjectType.GetTypeDeclaration();
                typeElement = _TypeElement.GetLanguageElement() as Class;
                if (typeElement == null)
                    return;
            }
            targetPicker1.Start(activeView, typeElement.FirstChild, InsertCode.UsePicker, null);
        }