Beispiel #1
0
        /// Adds a field to the class
        public async Task BindToField(Stetic.Component obj)
        {
            if (targetObject == null)
            {
                return;
            }

            string name = GetMemberName(obj);
            var    cls  = GetClass();

            if (FindField(cls, name) != null)
            {
                return;
            }

            var location = GetSourceLocation(cls);
            var doc      = await IdeApp.Workbench.OpenDocument(location.SourceTree.FilePath, project, true);

            var editor = doc.Editor;

            if (editor != null)
            {
                await CodeGenerationService.AddNewMember(project, cls, cls.Locations.First(), GetFieldCode (cls, obj, name));
            }
        }
Beispiel #2
0
        /// Adds a field to the class
        public void BindToField(Stetic.Component obj)
        {
            if (targetObject == null)
            {
                return;
            }

            string name = GetMemberName(obj);
            var    cls  = GetClass();

            if (FindField(cls.Resolve(project), name) != null)
            {
                return;
            }

            Document doc = IdeApp.Workbench.OpenDocument(cls.Region.FileName, true);

            IEditableTextFile editor = doc.GetContent <IEditableTextFile> ();

            if (editor != null)
            {
                var resolvedCls = cls.Resolve(cls.ParsedFile.GetTypeResolveContext(TypeSystemService.GetCompilation(project), cls.Region.Begin)).GetDefinition();
                CodeGenerationService.AddNewMember(resolvedCls, cls, GetFieldCode(cls, obj, name));
            }
        }
Beispiel #3
0
        public async Task UpdateField(Stetic.Component obj, string oldName)
        {
            if (targetObject == null)
            {
                return;
            }

            if (obj == targetObject)
            {
                return;                 // The root widget name can only be changed internally.
            }
            var cls = GetClass(false);

            string newName = GetObjectName(obj);

            if (newName.Length == 0)
            {
                return;
            }

            if (cls != null)
            {
                var f = ClassUtils.FindWidgetField(cls, oldName);
                if (f != null)
                {
                    await MonoDevelop.Refactoring.Rename.RenameRefactoring.Rename(f, newName);
                }
            }
        }
Beispiel #4
0
        /// Adds a field to the class
        public void BindToField(Stetic.Component obj)
        {
            if (targetObject == null)
            {
                return;
            }

            string name = GetMemberName(obj);
            IType  cls  = GetClass();

            if (FindField(cls, name) != null)
            {
                return;
            }

            Document doc = IdeApp.Workbench.OpenDocument(cls.CompilationUnit.FileName, true);

            IEditableTextFile editor = doc.GetContent <IEditableTextFile> ();

            if (editor != null)
            {
                CodeRefactorer gen = GetCodeGenerator();
                gen.AddMember(cls, GetFieldCode(obj, name));
            }
        }
Beispiel #5
0
        void UpdateBindings(Stetic.Component obj, ITypeSymbol cls)
        {
            if (targetObject == null || cls == null)
            {
                return;
            }

            // Remove signals for which there isn't a handler in the class

            Stetic.SignalCollection objectSignals = obj.GetSignals();
            if (objectSignals != null)
            {
                Stetic.Signal[] signals = new Stetic.Signal [objectSignals.Count];
                objectSignals.CopyTo(signals, 0);
                foreach (Stetic.Signal signal in signals)
                {
                    if (FindSignalHandler(cls, signal) == null)
                    {
                        obj.RemoveSignal(signal);
                    }
                }
            }

            // Update children

            foreach (Stetic.Component ob in obj.GetChildren())
            {
                UpdateBindings(ob, cls);
            }
        }
Beispiel #6
0
 IUnresolvedField GetFieldCode(IUnresolvedTypeDefinition cls, Stetic.Component obj, string name)
 {
     return(new DefaultUnresolvedField(cls, name)
     {
         ReturnType = ReflectionHelper.ParseReflectionName(obj.Type.ClassName),
         Accessibility = Accessibility.Protected
     });
 }
Beispiel #7
0
        CodeMemberField GetFieldCode(Stetic.Component obj, string name)
        {
            string          type  = obj.Type.ClassName;
            CodeMemberField field = new CodeMemberField(type, name);

            field.Attributes = MemberAttributes.Family;
            return(field);
        }
Beispiel #8
0
        public CodeBinder(MonoDevelop.Projects.Project project, ITextFileProvider textFileProvider, Stetic.Component targetObject)
        {
            this.project          = project;
            this.textFileProvider = textFileProvider;

            gproject = GtkDesignInfo.FromProject(project).GuiBuilderProject;

            TargetObject = targetObject;
        }
Beispiel #9
0
 IField GetFieldCode(Stetic.Component obj, string name)
 {
     return(new DomField()
     {
         Name = name,
         ReturnType = new DomReturnType(obj.Type.ClassName),
         Modifiers = Modifiers.Protected
     });
 }
Beispiel #10
0
 FieldDeclarationSyntax GetFieldCode(ITypeSymbol cls, Stetic.Component obj, string name)
 {
     return(SyntaxFactory.FieldDeclaration(
                SyntaxFactory.VariableDeclaration(
                    SyntaxFactory.ParseTypeName(obj.Type.ClassName),
                    new SeparatedSyntaxList <VariableDeclaratorSyntax> {
         SyntaxFactory.VariableDeclarator(name)
     }
                    )
                ).AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword)));
 }
Beispiel #11
0
        static bool IsValidClass(ITypeSymbol cls, Stetic.Component obj)
        {
            if (cls.BaseType.SpecialType == SpecialType.System_Object)
            {
                return(false);
            }
            string typeName = obj.Type.ClassName;

            if (cls.BaseType.GetFullName() == typeName)
            {
                return(true);
            }
            return(IsValidClass(cls.BaseType, obj));
        }
Beispiel #12
0
        static bool IsValidClass(IType cls, Stetic.Component obj)
        {
            string typeName = obj.Type.ClassName;

            foreach (var bt in cls.DirectBaseTypes)
            {
                if (bt.FullName == typeName)
                {
                    return(true);
                }

                if (IsValidClass(bt, obj))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #13
0
        void AddSignalsRec(CodeTypeDeclaration type, Stetic.Component comp)
        {
            foreach (Stetic.Signal signal in comp.GetSignals())
            {
                CodeMemberMethod met = new CodeMemberMethod();
                met.Name       = signal.Handler;
                met.Attributes = MemberAttributes.Family;
                met.ReturnType = new CodeTypeReference(signal.SignalDescriptor.HandlerReturnTypeName);

                foreach (Stetic.ParameterDescriptor pinfo in signal.SignalDescriptor.HandlerParameters)
                {
                    met.Parameters.Add(new CodeParameterDeclarationExpression(pinfo.TypeName, pinfo.Name));
                }

                type.Members.Add(met);
            }
            foreach (Stetic.Component cc in comp.GetChildren())
            {
                AddSignalsRec(type, cc);
            }
        }
Beispiel #14
0
        static bool IsValidClass(ProjectDom ctx, IType cls, Stetic.Component obj)
        {
            if (cls.BaseTypes != null)
            {
                string typeName = obj.Type.ClassName;

                foreach (IReturnType bt in cls.BaseTypes)
                {
                    System.Console.WriteLine("tn:" + typeName + " bt:" + bt.FullName);
                    if (bt.FullName == typeName)
                    {
                        return(true);
                    }

                    IType baseCls = ctx.GetType(bt);
                    if (baseCls != null && IsValidClass(ctx, baseCls, obj))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #15
0
        public void UpdateField(Stetic.Component obj, string oldName)
        {
            if (targetObject == null)
            {
                return;
            }

            CodeRefactorer cr = GetCodeGenerator();

            IType cls;

            if (obj == targetObject)
            {
                return;                 // The root widget name can only be changed internally.
            }
            else
            {
                cls = GetClass(false);
            }

            string newName = GetObjectName(obj);

            if (newName.Length == 0)
            {
                return;
            }

            if (cls != null)
            {
                IField f = ClassUtils.FindWidgetField(cls, oldName);
                if (f != null)
                {
                    cr.RenameMember(new NullProgressMonitor(), cls, f, newName, RefactoryScope.File);
                }
            }
        }
        static string GenerateSteticCodeStructure(DotNetProject project, Stetic.ProjectItemInfo item, Stetic.Component component, Stetic.ComponentNameEventArgs args, bool saveToFile, bool overwrite)
        {
            // Generate a class which contains fields for all bound widgets of the component

            string name     = item != null ? item.Name : component.Name;
            string fileName = GetBuildCodeFileName(project, name);

            string ns = "";
            int    i  = name.LastIndexOf('.');

            if (i != -1)
            {
                ns   = name.Substring(0, i);
                name = name.Substring(i + 1);
            }

            if (saveToFile && !overwrite && File.Exists(fileName))
            {
                return(fileName);
            }

            if (item != null)
            {
                component = item.Component;
            }

            CodeCompileUnit cu = new CodeCompileUnit();

            if (project.UsePartialTypes)
            {
                CodeNamespace cns = new CodeNamespace(ns);
                cu.Namespaces.Add(cns);

                CodeTypeDeclaration type = new CodeTypeDeclaration(name);
                type.IsPartial      = true;
                type.Attributes     = MemberAttributes.Public;
                type.TypeAttributes = System.Reflection.TypeAttributes.Public;
                cns.Types.Add(type);
                type.Members.Add(
                    new CodeMemberMethod()
                {
                    Name = "Build"
                }
                    );

                foreach (Stetic.ObjectBindInfo binfo in component.GetObjectBindInfo())
                {
                    // When a component is being renamed, we have to generate the
                    // corresponding field using the old name, since it will be renamed
                    // later using refactory
                    string nname = args != null && args.NewName == binfo.Name ? args.OldName : binfo.Name;
                    type.Members.Add(
                        new CodeMemberField(
                            binfo.TypeName,
                            nname
                            )
                        );
                }
            }
            else
            {
                if (!saveToFile)
                {
                    return(fileName);
                }
                CodeNamespace cns = new CodeNamespace();
                cns.Comments.Add(new CodeCommentStatement("Generated code for component " + component.Name));
                cu.Namespaces.Add(cns);
            }

            CodeDomProvider provider = project.LanguageBinding.GetCodeDomProvider();

            if (provider == null)
            {
                throw new UserException("Code generation not supported for language: " + project.LanguageName);
            }

            string text;
            var    pol = project.Policies.Get <TextStylePolicy> ();

            using (var fileStream = new StringWriter()) {
                var options = new CodeGeneratorOptions()
                {
                    IndentString             = pol.TabsToSpaces? new string (' ', pol.TabWidth) : "\t",
                    BlankLinesBetweenMembers = true,
                };
                provider.GenerateCodeFromCompileUnit(cu, fileStream, options);
                text = fileStream.ToString();
                text = FormatGeneratedFile(fileName, text, project, provider);
            }
            if (saveToFile)
            {
                File.WriteAllText(fileName, text);
            }
            TypeSystemService.NotifyFileChange(fileName, text);
//
//			if (ProjectDomService.HasDom (project)) {
//				// Only update the parser database if the project is actually loaded in the IDE.
//				ProjectDomService.Parse (project, fileName, text);
//				if (saveToFile)
//					FileService.NotifyFileChanged (fileName);
//			}

            return(fileName);
        }
 public static string GenerateSteticCodeStructure(DotNetProject project, Stetic.Component component, Stetic.ComponentNameEventArgs args, bool saveToFile, bool overwrite)
 {
     return(GenerateSteticCodeStructure(project, null, component, args, saveToFile, overwrite));
 }
Beispiel #18
0
        static void GenerateSteticCodeStructure(DotNetProject project, Stetic.ProjectItemInfo item, Stetic.Component component, Stetic.ComponentNameEventArgs args, bool saveToFile, bool overwrite)
        {
            // Generate a class which contains fields for all bound widgets of the component

            string name     = item != null ? item.Name : component.Name;
            string fileName = GetBuildCodeFileName(project, name);

            if (fileName == null)
            {
                return;
            }

            string ns = "";
            int    i  = name.LastIndexOf('.');

            if (i != -1)
            {
                ns   = name.Substring(0, i);
                name = name.Substring(i + 1);
            }

            if (saveToFile && !overwrite && File.Exists(fileName))
            {
                return;
            }

            if (item != null)
            {
                component = item.Component;
            }

            CodeCompileUnit cu = new CodeCompileUnit();

            if (project.UsePartialTypes)
            {
                CodeNamespace cns = new CodeNamespace(ns);
                cu.Namespaces.Add(cns);

                CodeTypeDeclaration type = new CodeTypeDeclaration(name);
                type.IsPartial      = true;
                type.Attributes     = MemberAttributes.Public;
                type.TypeAttributes = System.Reflection.TypeAttributes.Public;
                cns.Types.Add(type);

                foreach (Stetic.ObjectBindInfo binfo in component.GetObjectBindInfo())
                {
                    // When a component is being renamed, we have to generate the
                    // corresponding field using the old name, since it will be renamed
                    // later using refactory
                    string nname = args != null && args.NewName == binfo.Name ? args.OldName : binfo.Name;
                    type.Members.Add(
                        new CodeMemberField(
                            binfo.TypeName,
                            nname
                            )
                        );
                }
            }
            else
            {
                if (!saveToFile)
                {
                    return;
                }
                CodeNamespace cns = new CodeNamespace();
                cns.Comments.Add(new CodeCommentStatement("Generated code for component " + component.Name));
                cu.Namespaces.Add(cns);
            }

            CodeDomProvider provider = project.LanguageBinding.GetCodeDomProvider();

            if (provider == null)
            {
                throw new UserException("Code generation not supported for language: " + project.LanguageName);
            }

            TextWriter fileStream;

            if (saveToFile)
            {
                fileStream = new StreamWriter(fileName);
            }
            else
            {
                fileStream = new StringWriter();
            }

            try {
                provider.GenerateCodeFromCompileUnit(cu, fileStream, new CodeGeneratorOptions());
            } finally {
                fileStream.Close();
            }

            if (ProjectDomService.HasDom(project))
            {
                // Only update the parser database if the project is actually loaded in the IDE.
                if (saveToFile)
                {
                    ProjectDomService.Parse(project, fileName, "");
                    FileService.NotifyFileChanged(fileName);
                }
                else
                {
                    ProjectDomService.Parse(project, fileName, ((StringWriter)fileStream).ToString());
                }
            }
        }
Beispiel #19
0
 internal static string GetObjectName(Stetic.Component obj)
 {
     return(obj.Name);
 }
Beispiel #20
0
 internal static string GetMemberName(Stetic.Component obj)
 {
     return(obj.Name);
 }
Beispiel #21
0
 internal static string GetClassName(Stetic.Component obj)
 {
     return(GetObjectName(obj));
 }