~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/CSharpBinding/Project/Src/Project/VBNetToCSharpConverter.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 System.Collections.Generic;
 
6
using System.IO;
 
7
using ICSharpCode.NRefactory;
 
8
using ICSharpCode.NRefactory.Ast;
 
9
using ICSharpCode.NRefactory.PrettyPrinter;
 
10
using ICSharpCode.SharpDevelop;
 
11
using ICSharpCode.SharpDevelop.Dom;
 
12
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
 
13
using ICSharpCode.SharpDevelop.Project;
 
14
using ICSharpCode.SharpDevelop.Project.Converter;
 
15
 
 
16
namespace CSharpBinding
 
17
{
 
18
        public class VBNetToCSharpConverter : NRefactoryLanguageConverter
 
19
        {
 
20
                public override string TargetLanguageName {
 
21
                        get {
 
22
                                return CSharpProjectBinding.LanguageName;
 
23
                        }
 
24
                }
 
25
                
 
26
                protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject)
 
27
                {
 
28
                        CSharpProject project = (CSharpProject)base.CreateProject(targetProjectDirectory, sourceProject);
 
29
                        IProjectItemListProvider provider = (IProjectItemListProvider)project;
 
30
                        provider.AddProjectItem(new ReferenceProjectItem(project, "Microsoft.VisualBasic"));
 
31
                        
 
32
                        FileProjectItem fileItem = new FileProjectItem(project, ItemType.Compile, "MyNamespaceSupportForCSharp.cs");
 
33
                        provider.AddProjectItem(fileItem);
 
34
                        try {
 
35
                                File.WriteAllText(fileItem.FileName, CSharpMyNamespaceBuilder.BuildMyNamespaceCode((CompilableProject)sourceProject));
 
36
                        } catch (Exception ex) {
 
37
                                conversionLog.AppendLine(ex.ToString());
 
38
                        }
 
39
                        
 
40
                        return project;
 
41
                }
 
42
                
 
43
                protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem)
 
44
                {
 
45
                        ConvertFile(sourceItem, targetItem, ".vb", ".cs", SupportedLanguage.VBNet, new CSharpOutputVisitor());
 
46
                }
 
47
                
 
48
                protected override void CopyProperties(IProject sourceProject, IProject targetProject)
 
49
                {
 
50
                        base.CopyProperties(sourceProject, targetProject);
 
51
                        
 
52
                        CSharpProject project = (CSharpProject)targetProject;
 
53
                        
 
54
                        // 1591 = missing XML comment - the VB compiler does not have this warning
 
55
                        // we disable it by default because many VB projects have XML documentation turned on
 
56
                        // even though only few members are commented
 
57
                        // (we replace existing NoWarn entries because VB and C# error codes don't match)
 
58
                        project.SetProperty("NoWarn", "1591");
 
59
                        
 
60
                        project.ChangeProperty("DefineConstants",
 
61
                                               v => v.Replace(',', ';'));
 
62
                        
 
63
                        project.ChangeProperty("ProjectTypeGuids",
 
64
                                               v => v.Replace(ProjectTypeGuids.VBNet, ProjectTypeGuids.CSharp, StringComparison.OrdinalIgnoreCase));
 
65
                }
 
66
                
 
67
                protected override void ConvertAst(CompilationUnit compilationUnit, List<ISpecial> specials, FileProjectItem sourceItem)
 
68
                {
 
69
                        PreprocessingDirective.VBToCSharp(specials);
 
70
                        CompilableProject project = (CompilableProject)sourceItem.Project;
 
71
                        RemoveWindowsFormsSpecificCode(compilationUnit, specials, project.OutputType == OutputType.WinExe);
 
72
                        
 
73
                        IProjectContent pc = ParserService.GetProjectContent(sourceItem.Project) ?? ParserService.CurrentProjectContent;
 
74
                        VBNetToCSharpConvertVisitor visitor = new VBNetToCSharpConvertVisitorWithMyFormsSupport(pc, ParserService.GetParseInformation(sourceItem.FileName), sourceItem.Project.RootNamespace);
 
75
                                                
 
76
                        // set project options
 
77
                        visitor.OptionInfer = (project.GetEvaluatedProperty("OptionInfer") ?? "Off")
 
78
                                .Equals("On", StringComparison.OrdinalIgnoreCase);
 
79
                        visitor.OptionStrict = (project.GetEvaluatedProperty("OptionStrict") ?? "Off")
 
80
                                .Equals("On", StringComparison.OrdinalIgnoreCase);
 
81
                        
 
82
                        compilationUnit.AcceptVisitor(visitor, null);
 
83
                }
 
84
                
 
85
                void RemoveWindowsFormsSpecificCode(CompilationUnit compilationUnit, List<ISpecial> specials, bool keepCode)
 
86
                {
 
87
                        for (int i = 0; i < specials.Count; i++) {
 
88
                                PreprocessingDirective ppd = specials[i] as PreprocessingDirective;
 
89
                                if (ppd != null && ppd.Cmd == "#if") {
 
90
                                        if (ppd.Arg == "_MyType = \"WindowsForms\"") {
 
91
                                                int depth = 1;
 
92
                                                for (int j = i + 1; j < specials.Count; j++) {
 
93
                                                        ppd = specials[j] as PreprocessingDirective;
 
94
                                                        if (ppd != null) {
 
95
                                                                if (ppd.Cmd == "#if") {
 
96
                                                                        depth++;
 
97
                                                                } else if (ppd.Cmd == "#endif") {
 
98
                                                                        depth--;
 
99
                                                                        if (depth == 0) {
 
100
                                                                                if (keepCode) {
 
101
                                                                                        // keep code, remove only the ifdef
 
102
                                                                                        specials.RemoveAt(j);
 
103
                                                                                        specials.RemoveAt(i);
 
104
                                                                                } else {
 
105
                                                                                        // remove ifdef including the code
 
106
                                                                                        compilationUnit.AcceptVisitor(new RemoveMembersInRangeVisitor(
 
107
                                                                                                DomRegion.FromLocation(specials[i].StartPosition, specials[j].EndPosition)), null);
 
108
                                                                                        specials.RemoveRange(i, j - i + 1);
 
109
                                                                                }
 
110
                                                                                i--;
 
111
                                                                                break;
 
112
                                                                        }
 
113
                                                                }
 
114
                                                        }
 
115
                                                }
 
116
                                        }
 
117
                                }
 
118
                        }
 
119
                }
 
120
        }
 
121
}