~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ConvertToPythonMenuCommand.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using ICSharpCode.Core;
 
6
using ICSharpCode.Scripting;
 
7
using ICSharpCode.SharpDevelop;
 
8
using ICSharpCode.SharpDevelop.Gui;
 
9
 
 
10
namespace ICSharpCode.PythonBinding
 
11
{
 
12
        /// <summary>
 
13
        /// Converts VB.NET or C# code to Python.
 
14
        /// </summary>
 
15
        public class ConvertToPythonMenuCommand : AbstractMenuCommand
 
16
        {
 
17
                ScriptingTextEditorViewContent view;
 
18
                
 
19
                public override void Run()
 
20
                {
 
21
                        Run(new PythonWorkbench());
 
22
                }
 
23
                
 
24
                protected void Run(IScriptingWorkbench workbench)
 
25
                {
 
26
                        view = new ScriptingTextEditorViewContent(workbench);
 
27
                        string code = GeneratePythonCode();
 
28
                        ShowPythonCodeInNewWindow(code);
 
29
                }
 
30
                        
 
31
                string GeneratePythonCode()
 
32
                {
 
33
                        NRefactoryToPythonConverter converter = NRefactoryToPythonConverter.Create(view.PrimaryFileName);
 
34
                        converter.IndentString = view.TextEditorOptions.IndentationString;
 
35
                        return converter.Convert(view.EditableView.Text);
 
36
                }
 
37
                
 
38
                void ShowPythonCodeInNewWindow(string code)
 
39
                {
 
40
                        NewFile("Generated.py", "Python", code);
 
41
                }
 
42
                
 
43
                /// <summary>
 
44
                /// Creates a new file using the FileService by default.
 
45
                /// </summary>
 
46
                protected virtual void NewFile(string defaultName, string language, string content)
 
47
                {
 
48
                        FileService.NewFile(defaultName, content);
 
49
                }               
 
50
        }
 
51
}