コード例 #1
0
        /// <summary>
        /// Merges the generated code into the specified document.
        /// </summary>
        /// <param name="component">The designer host.</param>
        /// <param name="document">The document that the generated code will be merged into.</param>
        /// <param name="parseInfo">The current compilation unit for the <paramref name="document"/>.</param>
        public static void Merge(IDesignerHost host, IDocument document, ICompilationUnit compilationUnit, ITextEditorProperties textEditorProperties, IDesignerSerializationManager serializationManager)
        {
            // Get the document's initialize components method.
            IMethod method = GetInitializeComponents(compilationUnit);

            // Generate the python source code.
            PythonCodeDomSerializer serializer = new PythonCodeDomSerializer(NRefactoryToPythonConverter.GetIndentString(textEditorProperties));
            int indent = method.Region.BeginColumn;

            if (textEditorProperties.ConvertTabsToSpaces)
            {
                indent = (indent / textEditorProperties.IndentationSize);
                if (textEditorProperties.IndentationSize > 1)
                {
                    indent += 1;
                }
            }
            string rootNamespace = GetProjectRootNamespace(compilationUnit);
            string methodBody    = serializer.GenerateInitializeComponentMethodBody(host, serializationManager, rootNamespace, indent);

            // Merge the code.
            DomRegion methodRegion = GetBodyRegionInDocument(method);
            int       startOffset  = GetStartOffset(document, methodRegion);
            int       endOffset    = GetEndOffset(document, methodRegion);

            document.Replace(startOffset, endOffset - startOffset, methodBody);
        }
        public void TabIndent()
        {
            MockTextEditorProperties properties = new MockTextEditorProperties();

            properties.ConvertTabsToSpaces = false;
            Assert.AreEqual("\t", NRefactoryToPythonConverter.GetIndentString(properties));
        }
        public void TwoChaSpaceIndent()
        {
            MockTextEditorProperties properties = new MockTextEditorProperties();

            properties.ConvertTabsToSpaces = true;
            properties.IndentationSize     = 2;
            Assert.AreEqual("  ", NRefactoryToPythonConverter.GetIndentString(properties));
        }
コード例 #4
0
        protected void Run(IWorkbench workbench, ITextEditorProperties textEditorProperties)
        {
            // Get the code to convert.
            IViewContent viewContent = workbench.ActiveWorkbenchWindow.ActiveViewContent;
            IEditable    editable    = viewContent as IEditable;

            // Generate the python code.
            NRefactoryToPythonConverter converter = NRefactoryToPythonConverter.Create(viewContent.PrimaryFileName);

            converter.IndentString = NRefactoryToPythonConverter.GetIndentString(textEditorProperties);
            string pythonCode = converter.Convert(editable.Text);

            // Show the python code in a new window.
            NewFile("Generated.py", "Python", pythonCode);
        }
コード例 #5
0
        /// <summary>
        /// Returns the generated event handler.
        /// </summary>
        /// <param name="indentation">The indent string to use for the event handler.</param>
        protected string CreateEventHandler(string eventMethodName, string body, string indentation)
        {
            if (String.IsNullOrEmpty(body))
            {
                body = "pass";
            }

            StringBuilder eventHandler = new StringBuilder();

            eventHandler.Append(indentation);
            eventHandler.Append("def ");
            eventHandler.Append(eventMethodName);
            eventHandler.Append("(self, sender, e):");
            eventHandler.AppendLine();
            eventHandler.Append(indentation);
            eventHandler.Append(NRefactoryToPythonConverter.GetIndentString(textEditorProperties));
            eventHandler.Append(body);

            return(eventHandler.ToString());
        }
コード例 #6
0
        /// <summary>
        /// Inserts an event handler.
        /// </summary>
        public bool InsertComponentEvent(IComponent component, EventDescriptor edesc, string eventMethodName, string body, out string file, out int position)
        {
            position = GetExistingEventHandler(eventMethodName);
            if (position == -1)
            {
                // Ensure the text editor has the latest version
                // of the source code before we insert any new code.
                viewContent.MergeFormChanges();

                // Insert the event handler at the end of the class with an extra
                // new line before it.
                IDocument   doc             = viewContent.DesignerCodeFileDocument;
                string      eventHandler    = CreateEventHandler(eventMethodName, body, NRefactoryToPythonConverter.GetIndentString(textEditorProperties));
                int         line            = doc.LineSegmentCollection.Count;
                LineSegment lastLineSegment = doc.GetLineSegment(line - 1);
                int         offset          = lastLineSegment.Offset + lastLineSegment.Length;

                string newContent = "\r\n" + eventHandler;
                if (lastLineSegment.Length > 0)
                {
                    // Add an extra new line between the last line and the event handler.
                    newContent = "\r\n" + newContent;
                }
                doc.Insert(offset, newContent);

                // Set position so it points to the line
                // where the event handler was inserted.
                position = line + 1;
            }

            // Set the filename so it refers to the form being designed.
            file = viewContent.DesignerCodeFile.FileName;

            return(true);
        }