Пример #1
0
 public ImportTreeNode(ClassModel importModel, int imageIndex, bool showQualifiedClassNames)
     : base(importModel, imageIndex, false)
 {
     Text = importModel.Name;
     Tag = "class";
     Label = showQualifiedClassNames ? importModel.QualifiedName : importModel.Name;
 }
 public InheritedClassTreeNode(ClassModel classModel, int imageIndex, bool showQualifiedClassNames)
     : base(classModel, classModel, imageIndex, false)
 {
     Text = classModel.Name;
     Tag = "class";
     Label = showQualifiedClassNames ? classModel.QualifiedName : classModel.Name;
 }
Пример #3
0
 static void RenameMember(ClassModel inClass, string name, string newName, bool outputResults)
 {
     MemberModel m = inClass.Members.Items.FirstOrDefault(it => it.Name == name);
     if (m == null) return;
     ASResult result = new ASResult();
     ASComplete.FindMember(name, inClass, result, FlagType.Dynamic | FlagType.Function, 0);
     if (result.Member == null) return;
     queue.Add(new Rename(result, outputResults, newName));
 }
Пример #4
0
 private void ReadInterfaces(JsonReader reader, ClassModel cClass)
 {
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ArrayEnd) break;
         if (reader.Token == JsonToken.String)
         {
             string type = reader.Value.ToString();
             if (cClass.Implements == null) cClass.Implements = new List<string>();
             cClass.Implements.Add(type);
         }
     }
 }
Пример #5
0
        static public void GenerateJob(GeneratorJobType job, MemberModel member, ClassModel inClass, string itemLabel, Object data)
        {
            ScintillaNet.ScintillaControl Sci = ASContext.CurSciControl;
            lookupPosition = Sci.CurrentPos;

            int position;
            MemberModel latest = null;
            bool detach = true;
            switch (job)
            {
                case GeneratorJobType.Getter:
                case GeneratorJobType.Setter:
                case GeneratorJobType.GetterSetter:
                    GenerateProperty(job, member, inClass, Sci);
                    break;

                case GeneratorJobType.BasicEvent:
                case GeneratorJobType.ComplexEvent:

                    latest = TemplateUtils.GetTemplateBlockMember(Sci,
                        TemplateUtils.GetBoundary("EventHandlers"));
                    if (latest == null)
                    {
                        if (ASContext.CommonSettings.MethodsGenerationLocations == MethodsGenerationLocations.AfterSimilarAccessorMethod)
                            latest = GetLatestMemberForFunction(inClass, GetDefaultVisibility(), member);
                        if (latest == null)
                            latest = member;
                    }

                    position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                    Sci.SetSel(position, position);
                    string type = contextParam;
                    if (job == GeneratorJobType.BasicEvent)
                        if (itemLabel.IndexOf("DataEvent") >= 0) type = "DataEvent"; else type = "Event";
                    GenerateEventHandler(contextToken, type, member, position);
                    break;

                case GeneratorJobType.Delegate:
                    position = Sci.PositionFromLine(member.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                    Sci.SetSel(position, position);
                    GenerateDelegateMethod(contextToken, member, position);
                    break;

                case GeneratorJobType.Constant:
                case GeneratorJobType.Variable:
                case GeneratorJobType.VariablePublic:
                    Sci.BeginUndoAction();
                    try
                    {
                        GenerateVariableJob(job, Sci, member, detach, inClass);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.Function:
                case GeneratorJobType.FunctionPublic:
                    Sci.BeginUndoAction();
                    try
                    {
                        GenerateFunctionJob(job, Sci, member, detach, inClass);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.ImplementInterface:
                    ClassModel aType = ASContext.Context.ResolveType(contextParam, ASContext.Context.CurrentModel);
                    if (aType.IsVoid()) return;

                    latest = GetLatestMemberForFunction(inClass, Visibility.Public, null);
                    if (latest == null)
                        latest = FindLatest(0, 0, inClass, false, false);

                    if (latest == null) return;

                    position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                    Sci.SetSel(position, position);
                    GenerateImplementation(aType, position);
                    break;

                case GeneratorJobType.MoveLocalUp:
                    Sci.BeginUndoAction();
                    try
                    {
                        if (!RemoveLocalDeclaration(Sci, contextMember)) return;

                        position = GetBodyStart(member.LineFrom, member.LineTo, Sci);
                        Sci.SetSel(position, position);

                        string varType = contextMember.Type;
                        if (varType == "") varType = null;

                        string template = TemplateUtils.GetTemplate("Variable");
                        template = TemplateUtils.ReplaceTemplateVariable(template, "Name", contextMember.Name);
                        template = TemplateUtils.ReplaceTemplateVariable(template, "Type", varType);
                        template = TemplateUtils.ReplaceTemplateVariable(template, "Modifiers", null);
                        template = TemplateUtils.ReplaceTemplateVariable(template, "Value", null);
                        template += "\n$(Boundary)";

                        lookupPosition += SnippetHelper.InsertSnippetText(Sci, position, template);

                        Sci.SetSel(lookupPosition, lookupPosition);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.PromoteLocal:
                    Sci.BeginUndoAction();
                    try
                    {
                        if (!RemoveLocalDeclaration(Sci, contextMember)) return;

                        latest = GetLatestMemberForVariable(GeneratorJobType.Variable, inClass, GetDefaultVisibility(), member);
                        if (latest == null) return;

                        position = FindNewVarPosition(Sci, inClass, latest);
                        if (position <= 0) return;
                        Sci.SetSel(position, position);

                        contextMember.Flags -= FlagType.LocalVar;
                        if ((member.Flags & FlagType.Static) > 0)
                            contextMember.Flags |= FlagType.Static;
                        contextMember.Access = GetDefaultVisibility();
                        GenerateVariable(contextMember, position, detach);

                        Sci.SetSel(lookupPosition, lookupPosition);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.AddAsParameter:
                    Sci.BeginUndoAction();
                    try
                    {
                        AddAsParameter(inClass, Sci, member, detach);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    
                    break;

                case GeneratorJobType.AddImport:
                    position = Sci.CurrentPos;
                    if ((member.Flags & (FlagType.Class | FlagType.Enum | FlagType.Struct | FlagType.TypeDef)) == 0)
                    {
                        if (member.InFile == null) break;
                        member.Type = member.Name;
                    }
                    Sci.BeginUndoAction();
                    try
                    {
                        int offset = InsertImport(member, true);
                        position += offset;
                        Sci.SetSel(position, position);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.Class:
                    String clasName = Sci.GetWordFromPosition(Sci.CurrentPos);
                    GenerateClass(Sci, clasName, inClass);
                    break;

                case GeneratorJobType.Constructor:
                    member = new MemberModel(inClass.Name, inClass.QualifiedName, FlagType.Constructor | FlagType.Function, Visibility.Public);
                    GenerateFunction(
                        member,
                        Sci.CurrentPos, false, inClass);
                    break;

                case GeneratorJobType.ToString:
                    Sci.BeginUndoAction();
                    try
                    {
                        GenerateToString(inClass, Sci, member);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.FieldFromPatameter:
                    Sci.BeginUndoAction();
                    try
                    {
                        GenerateFieldFromParameter(Sci, member, inClass, (Visibility)(((Hashtable)data)["scope"]));
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.AddInterfaceDef:
                    Sci.BeginUndoAction();
                    try
                    {
                        AddInterfaceDefJob(inClass, Sci, member, (String)data);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.ConvertToConst:
                    Sci.BeginUndoAction();
                    try
                    {
                        ConvertToConst(inClass, Sci, member, detach);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.ChangeMethodDecl:
                    Sci.BeginUndoAction();
                    try
                    {
                        ChangeMethodDecl(Sci, member, inClass);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.ChangeConstructorDecl:
                    Sci.BeginUndoAction();
                    try
                    {
                        ChangeConstructorDecl(Sci, member, inClass);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.EventMetatag:
                    Sci.BeginUndoAction();
                    try
                    {
                        EventMetatag(inClass, Sci, member);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;

                case GeneratorJobType.AssignStatementToVar:
                    Sci.BeginUndoAction();
                    try
                    {
                        AssignStatementToVar(inClass, Sci, member);
                    }
                    finally
                    {
                        Sci.EndUndoAction();
                    }
                    break;
            }
        }
Пример #6
0
 public GeneratorItem(string label, GeneratorJobType job, MemberModel member, ClassModel inClass, Object data) : this(label, job, member, inClass)
 {
     
     this.data = data;
 }
Пример #7
0
        private static string GetSuperCall(MemberModel member, List<string> typesUsed, ClassModel aType)
        {
            string args = "";
            if (member.Parameters != null)
                foreach (MemberModel param in member.Parameters)
                {
                    if (param.Name.StartsWith(".")) break;
                    args += ", " + param.Name;
                    addTypeOnce(typesUsed, getQualifiedType(param.Type, aType));
                }

            bool noRet = member.Type == null || member.Type.Length == 0 || member.Type.Equals("void", StringComparison.OrdinalIgnoreCase);
            if (!noRet) addTypeOnce(typesUsed, getQualifiedType(member.Type, aType));

            string action = "";
            if ((member.Flags & FlagType.Function) > 0)
            {
                action =
                    (noRet ? "" : "return ")
                    + "super." + member.Name
                    + ((args.Length > 2) ? "(" + args.Substring(2) + ")" : "()") + ";";
            }
            else if ((member.Flags & FlagType.Setter) > 0 && args.Length > 0)
            {
                action = "super." + member.Name + " = " + member.Parameters[0].Name + ";";
            }
            else if ((member.Flags & FlagType.Getter) > 0)
            {
                action = "return super." + member.Name + ";";
            }
            return action;
        }
Пример #8
0
        static public void GenerateOverride(ScintillaNet.ScintillaControl Sci, ClassModel ofClass, MemberModel member, int position)
        {
            ContextFeatures features = ASContext.Context.Features;
            List<string> typesUsed = new List<string>();
            bool isProxy = (member.Namespace == "flash_proxy");
            if (isProxy) typesUsed.Add("flash.utils.flash_proxy");
            bool isAS2Event = ASContext.Context.Settings.LanguageId == "AS2" && member.Name.StartsWith("on");
            bool isObjectMethod = ofClass.QualifiedName == "Object";

            int line = Sci.LineFromPosition(position);
            string currentText = Sci.GetLine(line);
            int startPos = currentText.Length;
            GetStartPos(currentText, ref startPos, features.privateKey);
            GetStartPos(currentText, ref startPos, features.protectedKey);
            GetStartPos(currentText, ref startPos, features.internalKey);
            GetStartPos(currentText, ref startPos, features.publicKey);
            GetStartPos(currentText, ref startPos, features.staticKey);
            GetStartPos(currentText, ref startPos, features.overrideKey);
            startPos += Sci.PositionFromLine(line);

            FlagType flags = member.Flags;
            string acc = "";
            string decl = "";
            if (features.hasNamespaces && !string.IsNullOrEmpty(member.Namespace) && member.Namespace != "internal")
                acc = member.Namespace;
            else if ((member.Access & Visibility.Public) > 0) acc = features.publicKey;
            else if ((member.Access & Visibility.Internal) > 0) acc = features.internalKey;
            else if ((member.Access & Visibility.Protected) > 0) acc = features.protectedKey;
            else if ((member.Access & Visibility.Private) > 0 && features.methodModifierDefault != Visibility.Private) 
                acc = features.privateKey;

            bool isStatic = (flags & FlagType.Static) > 0;
            if (isStatic) acc = features.staticKey + " " + acc;

            if (!isAS2Event && !isObjectMethod)
                acc = features.overrideKey + " " + acc;

            acc = Regex.Replace(acc, "[ ]+", " ").Trim();

            if ((flags & (FlagType.Getter | FlagType.Setter)) > 0)
            {
                string type = member.Type;
                string name = member.Name;
                if (member.Parameters != null && member.Parameters.Count == 1)
                    type = member.Parameters[0].Type;
                type = FormatType(type);
                if (type == null && !features.hasInference) type = features.objectKey;

                bool genGetter = ofClass.Members.Search(name, FlagType.Getter, 0) != null;
                bool genSetter = ofClass.Members.Search(name, FlagType.Setter, 0) != null;

                if (isHaxe)
                {
                    // property is public but not the methods
                    acc = features.overrideKey;
                }

                if (genGetter)
                {
                    string tpl = TemplateUtils.GetTemplate("OverrideGetter", "Getter");
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Modifiers", acc);
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Name", name);
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Type", type);
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Member", "super." + name);
                    decl += tpl;
                }
                if (genSetter)
                {
                    string tpl = TemplateUtils.GetTemplate("OverrideSetter", "Setter");
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Modifiers", acc);
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Name", name);
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Type", type);
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Member", "super." + name);
                    tpl = TemplateUtils.ReplaceTemplateVariable(tpl, "Void", ASContext.Context.Features.voidKey ?? "void");
                    if (decl.Length > 0)
                    {
                        tpl = "\n\n" + tpl.Replace("$(EntryPoint)", "");
                    }
                    decl += tpl;
                }
                decl = TemplateUtils.ReplaceTemplateVariable(decl, "BlankLine", "");
            }
            else
            {
                string type = FormatType(member.Type);
                //if (type == null) type = features.objectKey;
                
                decl = acc + features.functionKey + " ";
                bool noRet = type == null || type.Equals("void", StringComparison.OrdinalIgnoreCase);
                type = (noRet && type != null) ? ASContext.Context.Features.voidKey : type;
                if (!noRet)
                {
                    string qType = getQualifiedType(type, ofClass);
                    typesUsed.Add(qType);
                    if (qType == type)
                    {
                        ClassModel rType = ASContext.Context.ResolveType(type, ofClass.InFile);
                        if (!rType.IsVoid()) type = rType.Name;
                    }
                }

                string action = (isProxy || isAS2Event) ? "" : GetSuperCall(member, typesUsed, ofClass);
                string template = TemplateUtils.GetTemplate("MethodOverride");
                
                // fix parameters if needed
                if (member.Parameters != null)
                    foreach (MemberModel para in member.Parameters)
                       if (para.Type == "any") para.Type = "*";

                template = TemplateUtils.ReplaceTemplateVariable(template, "Modifiers", acc);
                template = TemplateUtils.ReplaceTemplateVariable(template, "Name", member.Name);
                template = TemplateUtils.ReplaceTemplateVariable(template, "Arguments", TemplateUtils.ParametersString(member, true));
                template = TemplateUtils.ReplaceTemplateVariable(template, "Type", type);
                template = TemplateUtils.ReplaceTemplateVariable(template, "Method", action);
                decl = template;
            }

            Sci.BeginUndoAction();
            try
            {
                if (ASContext.Context.Settings.GenerateImports && typesUsed.Count > 0)
                {
                    int offset = AddImportsByName(typesUsed, line);
                    position += offset;
                    startPos += offset;
                }

                Sci.SetSel(startPos, position + member.Name.Length);
                InsertCode(startPos, decl);
            }
            finally { Sci.EndUndoAction(); }
        }
Пример #9
0
 static private MemberModel FindLatest(FlagType match, Visibility visi, ClassModel inClass)
 {
     return FindLatest(match, visi, inClass, true, true);
 }
Пример #10
0
        static private MemberModel FindMember(string name, ClassModel inClass)
        {
            MemberList list;
            if (inClass == ClassModel.VoidClass)
                list = ASContext.Context.CurrentModel.Members;
            else list = inClass.Members;

            MemberModel found = null;
            foreach (MemberModel member in list)
            {
                if (member.Name == name)
                {
                    found = member;
                    break;
                }
            }
            return found;
        }
Пример #11
0
        private static void GenerateVariableJob(GeneratorJobType job, ScintillaNet.ScintillaControl Sci, MemberModel member,
            bool detach, ClassModel inClass)
        {
            int position = 0;
            MemberModel latest = null;
            bool isOtherClass = false;

            Visibility varVisi = job.Equals(GeneratorJobType.Variable) ? GetDefaultVisibility() : Visibility.Public;
            FlagType ft = job.Equals(GeneratorJobType.Constant) ? FlagType.Constant : FlagType.Variable;

            // evaluate, if the variable (or constant) should be generated in other class
            ASResult varResult = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(Sci.CurrentPos, true));

            int contextOwnerPos = GetContextOwnerEndPos(Sci, Sci.WordStartPosition(Sci.CurrentPos, true));
            MemberModel isStatic = new MemberModel();
            if (contextOwnerPos != -1)
            {
                ASResult contextOwnerResult = ASComplete.GetExpressionType(Sci, contextOwnerPos);
                if (contextOwnerResult != null)
                {
                    if (contextOwnerResult.Member == null && contextOwnerResult.Type != null)
                    {
                        isStatic.Flags |= FlagType.Static;
                    }
                }
            }
            else if (member != null && (member.Flags & FlagType.Static) > 0)
            {
                isStatic.Flags |= FlagType.Static;
            }

            ASResult returnType = null;
            int lineNum = Sci.CurrentLine;
            string line = Sci.GetLine(lineNum);
            
            Match m = Regex.Match(line, "\\b" + Regex.Escape(contextToken) + "\\(");
            if (m.Success)
            {
                returnType = new ASResult();
                returnType.Type = ASContext.Context.ResolveType("Function", null);
            }
            else
            {
                m = Regex.Match(line, @"=\s*[^;\n\r}}]+");
                if (m.Success)
                {
                    int posLineStart = Sci.PositionFromLine(lineNum);
                    if (posLineStart + m.Index >= Sci.CurrentPos)
                    {
                        line = line.Substring(m.Index);
                        StatementReturnType rType = GetStatementReturnType(Sci, inClass, line, posLineStart + m.Index);
                        if (rType != null)
                        {
                            returnType = rType.resolve;
                        }
                    }
                }
            }

            if (varResult.RelClass != null && !varResult.RelClass.IsVoid() && !varResult.RelClass.Equals(inClass))
            {
                AddLookupPosition();
                lookupPosition = -1;

                ASContext.MainForm.OpenEditableDocument(varResult.RelClass.InFile.FileName, false);
                Sci = ASContext.CurSciControl;
                isOtherClass = true;

                FileModel fileModel = new FileModel();
                fileModel.Context = ASContext.Context;
                ASFileParser parser = new ASFileParser();
                parser.ParseSrc(fileModel, Sci.Text);

                foreach (ClassModel cm in fileModel.Classes)
                {
                    if (cm.QualifiedName.Equals(varResult.RelClass.QualifiedName))
                    {
                        varResult.RelClass = cm;
                        break;
                    }
                }
                inClass = varResult.RelClass;

                ASContext.Context.UpdateContext(inClass.LineFrom);
            }

            latest = GetLatestMemberForVariable(job, inClass, varVisi, isStatic);
            
            // if we generate variable in current class..
            if (!isOtherClass && member == null)
            {
                detach = false;
                lookupPosition = -1;
                position = Sci.WordStartPosition(Sci.CurrentPos, true);
                Sci.SetSel(position, Sci.WordEndPosition(position, true));
            }
            else // if we generate variable in another class
            {
                if (latest != null)
                {
                    position = FindNewVarPosition(Sci, inClass, latest);
                }
                else
                {
                    position = GetBodyStart(inClass.LineFrom, inClass.LineTo, Sci);
                    detach = false;
                }
                if (position <= 0) return;
                Sci.SetSel(position, position);
            }

            // if this is a constant, we assign a value to constant
            string returnTypeStr = null;
            string eventValue = null;
            if (job == GeneratorJobType.Constant && returnType == null)
            {
                isStatic.Flags |= FlagType.Static;
                eventValue = "String = \"" + Camelize(contextToken) + "\"";
            }
            else if (returnType != null)
            {
                ClassModel inClassForImport = null;
                if (returnType.InClass != null)
                {
                    inClassForImport = returnType.InClass;
                }
                else if (returnType.RelClass != null)
                {
                    inClassForImport = returnType.RelClass;
                }
                else
                {
                    inClassForImport = inClass;
                }
                List<String> imports = new List<string>();
                if (returnType.Member != null)
                {
                    if (returnType.Member.Type != ASContext.Context.Features.voidKey)
                    {
                        returnTypeStr = FormatType(GetShortType(returnType.Member.Type));
                        imports.Add(getQualifiedType(returnType.Member.Type, inClassForImport));
                    }
                }
                else if (returnType != null && returnType.Type != null)
                {
                    returnTypeStr = FormatType(GetShortType(returnType.Type.QualifiedName));
                    imports.Add(getQualifiedType(returnType.Type.QualifiedName, inClassForImport));
                }
                if (imports.Count > 0)
                {
                    position += AddImportsByName(imports, Sci.LineFromPosition(position));
                    Sci.SetSel(position, position);
                }
            }
            MemberModel newMember = NewMember(contextToken, isStatic, ft, varVisi);
            if (returnTypeStr != null)
            {
                newMember.Type = returnTypeStr;
            }
            else if (eventValue != null)
            {
                newMember.Type = eventValue;
            }
            GenerateVariable(newMember, position, detach);
        }
Пример #12
0
        private static void GenerateToString(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member)
        {
            MemberModel resultMember = new MemberModel("toString", "String", FlagType.Function, Visibility.Public);

            bool isOverride = false;
            inClass.ResolveExtends();
            if (inClass.Extends != null)
            {
                ClassModel aType = inClass.Extends;
                while (!aType.IsVoid() && aType.QualifiedName != "Object")
                {
                    foreach (MemberModel method in aType.Members)
                    {
                        if (method.Name == "toString")
                        {
                            isOverride = true;
                            break;
                        }
                    }
                    if (isOverride)
                    {
                        resultMember.Flags |= FlagType.Override;
                        break;
                    }
                    // interface inheritance
                    aType = aType.Extends;
                }
            }
            MemberList members = inClass.Members;
            StringBuilder membersString = new StringBuilder();
            StringBuilder oneMembersString;
            int len = 0;
            int indent = Sci.GetLineIndentation(Sci.CurrentLine);
            foreach (MemberModel m in members)
            {
                if (((m.Flags & FlagType.Variable) > 0 || (m.Flags & FlagType.Getter) > 0)
                    && (m.Access & Visibility.Public) > 0
                    && (m.Flags & FlagType.Constant) == 0)
                {
                    oneMembersString = new StringBuilder();
                    oneMembersString.Append(" ").Append(m.Name).Append("=\" + ").Append(m.Name).Append(" + ");
                    membersString.Append(oneMembersString);
                    len += oneMembersString.Length;
                    if (len > 80)
                    {
                        len = 0;
                        membersString.Append("\n\t\t\t\t");
                    }
                    membersString.Append("\"");
                }
            }


            string template = TemplateUtils.GetTemplate("ToString");
            string result = TemplateUtils.ToDeclarationWithModifiersString(resultMember, template);
            result = TemplateUtils.ReplaceTemplateVariable(result, "Body", "\"[" + inClass.Name + membersString.ToString() + "]\"");

            InsertCode(Sci.CurrentPos, result);
        }
Пример #13
0
        public string GenerateIntrinsic(bool caching)
        {
            if (this == Ignore)
            {
                return("");
            }

            StringBuilder sb   = new StringBuilder();
            string        nl   = (caching) ? "" : "\r\n";
            char          semi = ';';
            string        tab  = (caching) ? "" : "\t";

            // header
            if (Version > 2)
            {
                sb.Append("package");
                if (Package.Length > 0)
                {
                    sb.Append(" ").Append(Package);
                }
                if (haXe)
                {
                    sb.Append(semi).Append(nl).Append(nl);
                }
                else
                {
                    sb.Append(nl).Append("{").Append(nl);
                }
            }

            // imports
            if (Imports.Count > 0)
            {
                foreach (MemberModel import in Imports)
                {
                    sb.Append(tab).Append("import ").Append(import.Type).Append(semi).Append(nl);
                }
                sb.Append(nl);
            }

            // event/style metadatas
            ASMetaData.GenerateIntrinsic(MetaDatas, sb, nl, tab);

            // members
            string decl;

            foreach (MemberModel member in Members)
            {
                ASMetaData.GenerateIntrinsic(member.MetaDatas, sb, nl, tab);
                if ((member.Flags & FlagType.Variable) > 0)
                {
                    sb.Append(ClassModel.CommentDeclaration(member.Comments, tab));
                    sb.Append(tab).Append(ClassModel.MemberDeclaration(member)).Append(semi).Append(nl);
                }
                else if ((member.Flags & FlagType.Function) > 0)
                {
                    decl = ClassModel.MemberDeclaration(member);
                    sb.Append(ClassModel.CommentDeclaration(member.Comments, tab));
                    sb.Append(tab).Append(decl).Append(semi).Append(nl);
                }
            }

            foreach (ClassModel aClass in Classes)
            {
                sb.Append(aClass.GenerateIntrinsic(caching));
                sb.Append(nl);
            }

            if (Version == 3)
            {
                sb.Append('}').Append(nl);
            }
            return(sb.ToString());
        }
Пример #14
0
        static public string ClassDeclaration(ClassModel ofClass, bool qualified)
        {
            // package
            if (ofClass.Flags == FlagType.Package)
            {
                return("package " + ofClass.Name.Replace('\\', '.'));
            }
            else
            {
                // modifiers
                FlagType   ft        = ofClass.Flags;
                Visibility acc       = ofClass.Access;
                string     modifiers = "";
                if ((ofClass.Flags & FlagType.Intrinsic) > 0)
                {
                    if ((ofClass.Flags & FlagType.Extern) > 0)
                    {
                        modifiers += "extern ";
                    }
                    else
                    {
                        modifiers += "intrinsic ";
                    }
                }
                else if (ofClass.InFile.Version > 2)
                {
                    if (ofClass.Namespace != null && ofClass.Namespace.Length > 0 &&
                        ofClass.Namespace != "internal")
                    {
                        //  if ((ft & FlagType.Interface) == 0)
                        modifiers += ofClass.Namespace + " ";
                    }
                    else
                    {
                        //  if ((ft & FlagType.Interface) == 0)
                        //  {
                        if ((acc & Visibility.Public) > 0)
                        {
                            modifiers += "public ";
                        }
                        else if ((acc & Visibility.Internal) > 0)
                        {
                            modifiers += "internal ";
                        }
                        else if ((acc & Visibility.Protected) > 0)
                        {
                            modifiers += "protected ";
                        }
                        else if ((acc & Visibility.Private) > 0)
                        {
                            modifiers += "private ";
                        }
                        //  }
                    }
                }

                if ((ofClass.Flags & FlagType.Final) > 0)
                {
                    modifiers += "final ";
                }

                if ((ofClass.Flags & FlagType.Dynamic) > 0)
                {
                    modifiers += "dynamic ";
                }

                string classType = "class";
                if ((ofClass.Flags & FlagType.Interface) > 0)
                {
                    classType = "interface";
                }
                else if ((ofClass.Flags & FlagType.Enum) > 0)
                {
                    classType = "enum";
                }
                else if ((ofClass.Flags & FlagType.Abstract) > 0)
                {
                    classType = "abstract";
                }
                else if ((ofClass.Flags & FlagType.TypeDef) > 0)
                {
                    classType = "typedef";
                }
                else if ((ofClass.Flags & FlagType.Struct) > 0)
                {
                    classType = "struct";
                }
                else if ((ofClass.Flags & FlagType.Delegate) > 0)
                {
                    classType = "delegate";
                }

                // signature
                if (qualified)
                {
                    return(String.Format("{0}{1} {2}", modifiers, classType, ofClass.QualifiedName));
                }
                else
                {
                    return(String.Format("{0}{1} {2}", modifiers, classType, ofClass.FullName));
                }
            }
        }
Пример #15
0
        private MemberModel ReadMethod(JsonReader reader, ClassModel cClass)
        {
            MemberModel member = new MemberModel();
            member.Flags = FlagType.Function;

            string prop = null;
            while (reader.Read())
            {
                if (reader.Token == JsonToken.ObjectEnd) break;
                if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
                else if (reader.Token == JsonToken.String)
                {
                    string val = reader.Value.ToString();
                    switch (prop)
                    {
                        case "name": 
                            if (!val.StartsWith("__op_")) {
                                member.Name = val; 
                                cClass.Members.Add(member); 
                            }
                            break;
                        case "returntype": if (member.Type == null) member.Type = CleanType(val); break;
                        case "docstring": member.Comments = ExtractDoc(val); break;
                    }
                }
                else if (reader.Token == JsonToken.ArrayStart)
                {
                    switch (prop)
                    {
                        case "methodattributes": 
                            ReadAttributes(reader, member); 
                            if ((member.Flags & FlagType.Static) == 0) member.Flags |= FlagType.Dynamic;
                            break;
                        case "parameters": ReadParameters(reader, member); break;
                        case "metainfo": ReadMetas(reader, member); break;
                        default: reader.SkipArray(); break;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    switch (prop)
                    {
                        case "templatetypes": ReadTemplateTypes(reader, member); break;
                        default: reader.SkipObject(); break;
                    }
                }
            }
            return member;
        }
Пример #16
0
        private static void GenerateFunctionJob(GeneratorJobType job, ScintillaNet.ScintillaControl Sci, MemberModel member,
            bool detach, ClassModel inClass)
        {
            int position = 0;
            MemberModel latest = null;
            bool isOtherClass = false;

            Visibility funcVisi = job.Equals(GeneratorJobType.FunctionPublic) ? Visibility.Public : GetDefaultVisibility();
            int wordPos = Sci.WordEndPosition(Sci.CurrentPos, true);
            List<FunctionParameter> functionParameters = ParseFunctionParameters(Sci, wordPos);

            // evaluate, if the function should be generated in other class
            ASResult funcResult = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(Sci.CurrentPos, true));

            int contextOwnerPos = GetContextOwnerEndPos(Sci, Sci.WordStartPosition(Sci.CurrentPos, true));
            MemberModel isStatic = new MemberModel();
            if (contextOwnerPos != -1)
            {
                ASResult contextOwnerResult = ASComplete.GetExpressionType(Sci, contextOwnerPos);
                if (contextOwnerResult != null)
                {
                    if (contextOwnerResult.Member == null && contextOwnerResult.Type != null)
                    {
                        isStatic.Flags |= FlagType.Static;
                    }
                }
            }
            else if (member != null && (member.Flags & FlagType.Static) > 0)
            {
                isStatic.Flags |= FlagType.Static;
            }


            if (funcResult.RelClass != null && !funcResult.RelClass.IsVoid() && !funcResult.RelClass.Equals(inClass))
            {
                AddLookupPosition();
                lookupPosition = -1;

                DockContent dc = ASContext.MainForm.OpenEditableDocument(funcResult.RelClass.InFile.FileName, true);
                Sci = ASContext.CurSciControl;
                isOtherClass = true;

                FileModel fileModel = new FileModel();
                fileModel.Context = ASContext.Context;
                ASFileParser parser = new ASFileParser();
                parser.ParseSrc(fileModel, Sci.Text);

                foreach (ClassModel cm in fileModel.Classes)
                {
                    if (cm.QualifiedName.Equals(funcResult.RelClass.QualifiedName))
                    {
                        funcResult.RelClass = cm;
                        break;
                    }
                }
                inClass = funcResult.RelClass;

                ASContext.Context.UpdateContext(inClass.LineFrom);
            }

            string blockTmpl = null;
            if ((isStatic.Flags & FlagType.Static) > 0)
            {
                blockTmpl = TemplateUtils.GetBoundary("StaticMethods");
            }
            else if ((funcVisi & Visibility.Public) > 0)
            {
                blockTmpl = TemplateUtils.GetBoundary("PublicMethods");
            }
            else
            {
                blockTmpl = TemplateUtils.GetBoundary("PrivateMethods");
            }
            latest = TemplateUtils.GetTemplateBlockMember(Sci, blockTmpl);
            if (latest == null || (!isOtherClass && member == null))
            {
                latest = GetLatestMemberForFunction(inClass, funcVisi, isStatic);

                // if we generate function in current class..
                if (!isOtherClass)
                {
                    MethodsGenerationLocations location = ASContext.CommonSettings.MethodsGenerationLocations;
                    if (member == null)
                    {
                        detach = false;
                        lookupPosition = -1;
                        position = Sci.WordStartPosition(Sci.CurrentPos, true);
                        Sci.SetSel(position, Sci.WordEndPosition(position, true));
                    }
                    else if (latest != null && location == MethodsGenerationLocations.AfterSimilarAccessorMethod)
                    {
                        position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                        Sci.SetSel(position, position);
                    }
                    else
                    {
                        position = Sci.PositionFromLine(member.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                        Sci.SetSel(position, position);
                    }
                }
                else // if we generate function in another class..
                {
                    if (latest != null)
                    {
                        position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                    }
                    else
                    {
                        position = GetBodyStart(inClass.LineFrom, inClass.LineTo, Sci);
                        detach = false;
                    }
                    Sci.SetSel(position, position);
                }
            }
            else
            {
                position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                Sci.SetSel(position, position);
            }

            // add imports to function argument types
            if (functionParameters.Count > 0)
            {
                List<string> l = new List<string>();
                foreach (FunctionParameter fp in functionParameters)
                {
                    try
                    {
                        l.Add(fp.paramQualType);
                    }
                    catch (Exception) { }
                }
                int o = AddImportsByName(l, Sci.LineFromPosition(position));
                position += o;
                if (latest == null)
                    Sci.SetSel(position, Sci.WordEndPosition(position, true));
                else
                    Sci.SetSel(position, position);
            }
            
            List<MemberModel> parameters = new List<MemberModel>();
            for (int i = 0; i < functionParameters.Count; i++)
            {
                string name = functionParameters[i].paramName;
                string type = functionParameters[i].paramType;
                parameters.Add(new MemberModel(name, type, FlagType.ParameterVar, 0));
            }
            MemberModel newMember = NewMember(contextToken, isStatic, FlagType.Function, funcVisi);
            newMember.Parameters = parameters;
            GenerateFunction(newMember, position, detach, inClass);
        }
Пример #17
0
 static public string ClassDeclaration(ClassModel ofClass)
 {
     return(ClassDeclaration(ofClass, true));
 }
Пример #18
0
        private static void GenerateClass(ScintillaNet.ScintillaControl Sci, String className, ClassModel inClass)
        {
            AddLookupPosition(); // remember last cursor position for Shift+F4

            List<FunctionParameter> parameters = ParseFunctionParameters(Sci, Sci.WordEndPosition(Sci.CurrentPos, true));
            List<MemberModel> constructorArgs = new List<MemberModel>();
            List<String> constructorArgTypes = new List<String>();
            MemberModel paramMember = new MemberModel();
            for (int i = 0; i < parameters.Count; i++)
            {
                FunctionParameter p = parameters[i];
                constructorArgs.Add(new MemberModel(p.paramName, p.paramType, FlagType.ParameterVar, 0));
                constructorArgTypes.Add(CleanType(getQualifiedType(p.paramQualType, inClass)));
            }
            
            paramMember.Parameters = constructorArgs;

            IProject project = PluginBase.CurrentProject;
            if (String.IsNullOrEmpty(className)) className = "Class";
            string projFilesDir = Path.Combine(PathHelper.TemplateDir, "ProjectFiles");
            string projTemplateDir = Path.Combine(projFilesDir, project.GetType().Name);
            string paramsString = TemplateUtils.ParametersString(paramMember, true);
            Hashtable info = new Hashtable();
            info["className"] = className;
            if (project.Language.StartsWith("as")) info["templatePath"] = Path.Combine(projTemplateDir, "Class.as.fdt");
            else if (project.Language.StartsWith("haxe")) info["templatePath"] = Path.Combine(projTemplateDir, "Class.hx.fdt");
            else if (project.Language.StartsWith("loom")) info["templatePath"] = Path.Combine(projTemplateDir, "Class.ls.fdt");
            info["inDirectory"] = Path.GetDirectoryName(inClass.InFile.FileName);
            info["constructorArgs"] = paramsString.Length > 0 ? paramsString : null;
            info["constructorArgTypes"] = constructorArgTypes;
            DataEvent de = new DataEvent(EventType.Command, "ProjectManager.CreateNewFile", info);
            EventManager.DispatchEvent(null, de);
            if (de.Handled) return;
        }
Пример #19
0
 static private MemberModel FindLatest(FlagType match, ClassModel inClass)
 {
     return FindLatest(match, 0, inClass);
 }
Пример #20
0
        private static int FindNewVarPosition(ScintillaNet.ScintillaControl Sci, ClassModel inClass, MemberModel latest)
        {
            firstVar = false;
            // found a var?
            if ((latest.Flags & FlagType.Variable) > 0)
                return Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);

            // add as first member
            int line = 0;
            int maxLine = Sci.LineCount;
            if (inClass != null)
            {
                line = inClass.LineFrom;
                maxLine = inClass.LineTo;
            }
            else if (ASContext.Context.InPrivateSection) line = ASContext.Context.CurrentModel.PrivateSectionIndex;
            else maxLine = ASContext.Context.CurrentModel.PrivateSectionIndex;
            while (line < maxLine)
            {
                string text = Sci.GetLine(line++);
                if (text.IndexOf('{') >= 0)
                {
                    firstVar = true;
                    return Sci.PositionFromLine(line) - ((Sci.EOLMode == 0) ? 2 : 1);
                }
            }
            return -1;
        }
Пример #21
0
        static private MemberModel FindLatest(FlagType match, Visibility visi, ClassModel inClass,
                bool isFlagMatchStrict, bool isVisibilityMatchStrict)
        {
            MemberList list;
            if (inClass == ClassModel.VoidClass)
                list = ASContext.Context.CurrentModel.Members;
            else
                list = inClass.Members;

            MemberModel latest = null;
            MemberModel fallback = null;
            foreach (MemberModel member in list)
            {
                fallback = member;
                if (isFlagMatchStrict && isVisibilityMatchStrict)
                {
                    if ((member.Flags & match) == match && (visi == 0 || (member.Access & visi) == visi))
                    {
                        latest = member;
                    }
                }
                else if (isFlagMatchStrict && !isVisibilityMatchStrict)
                {
                    if ((member.Flags & match) == match && (visi == 0 || (member.Access & visi) > 0))
                    {
                        latest = member;
                    }
                }
                else if (!isFlagMatchStrict && isVisibilityMatchStrict)
                {
                    if ((member.Flags & match) > 0 && (visi == 0 || (member.Access & visi) == visi))
                    {
                        latest = member;
                    }
                }
                else
                {
                    if ((member.Flags & match) > 0 && (visi == 0 || (member.Access & visi) > 0))
                    {
                        latest = member;
                    }
                }

            }
            if (isFlagMatchStrict || isVisibilityMatchStrict)
                fallback = null;
            return latest ?? fallback;
        }
Пример #22
0
        private static StatementReturnType GetStatementReturnType(ScintillaNet.ScintillaControl Sci, ClassModel inClass, string line, int startPos)
        {
            Regex target = new Regex(@"[;\s\n\r]*", RegexOptions.RightToLeft);
            Match m = target.Match(line);
            if (!m.Success)
            {
                return null;
            }
            line = line.Substring(0, m.Index);

            if (line.Length == 0)
            {
                return null;
            }

            line = ReplaceAllStringContents(line);

            ASResult resolve = null;
            int pos = -1; 
            string word = null;
            int stylemask = (1 << Sci.StyleBits) - 1;
            ClassModel type = null;

            if (line[line.Length - 1] == ')')
            {
                pos = -1;
                int lastIndex = 0;
                int bracesBalance = 0;
                while (true)
                {
                    int pos1 = line.IndexOf("(", lastIndex);
                    int pos2 = line.IndexOf(")", lastIndex);
                    if (pos1 != -1 && pos2 != -1)
                    {
                        lastIndex = Math.Min(pos1, pos2);
                    }
                    else if (pos1 != -1 || pos2 != -1)
                    {
                        lastIndex = Math.Max(pos1, pos2);
                    }
                    else
                    {
                        break;
                    }
                    if (lastIndex == pos1)
                    {
                        bracesBalance++;
                        if (bracesBalance == 1)
                        {
                            pos = lastIndex;
                        }
                    }
                    else if (lastIndex == pos2)
                    {
                        bracesBalance--;
                    }
                    lastIndex++;
                }
            }
            else
            {
                pos = line.Length;
            }
            if (pos != -1)
            {
                line = line.Substring(0, pos);
                pos += startPos;
                pos -= line.Length - line.TrimEnd().Length + 1;
                pos = Sci.WordEndPosition(pos, true);
                resolve = ASComplete.GetExpressionType(Sci, pos);
                if (resolve.IsNull()) resolve = null;
                word = Sci.GetWordFromPosition(pos);
            }

            IASContext ctx = inClass.InFile.Context;
            m = Regex.Match(line, "new\\s+([\\w\\d.<>,_$-]+)+(<[^]]+>)|(<[^]]+>)", RegexOptions.IgnoreCase);

            if (m.Success)
            {
                string m1 = m.Groups[1].Value;
                string m2 = m.Groups[2].Value;

                string cname;
                if (string.IsNullOrEmpty(m1) && string.IsNullOrEmpty(m2))
                    cname = m.Groups[0].Value;
                else
                    cname = String.Concat(m1, m2);

                if (cname.StartsWith("<"))
                    cname = "Vector." + cname; // literal vector

                type = ctx.ResolveType(cname, inClass.InFile);
                if (!type.IsVoid()) resolve = null;
            }
            else
            {
                char c = (char)Sci.CharAt(pos);
                if (c == '"' || c == '\'')
                {
                    type = ctx.ResolveType("String", inClass.InFile);
                }
                else if (c == '}')
                {
                    type = ctx.ResolveType(ctx.Features.objectKey, inClass.InFile);
                }
                else if (c == '>')
                {
                    type = ctx.ResolveType("XML", inClass.InFile);
                }
                else if (c == ']')
                {
                    resolve = ASComplete.GetExpressionType(Sci, pos + 1);
                    if (resolve.Type != null) type = resolve.Type;
                    else type = ctx.ResolveType(ctx.Features.arrayKey, inClass.InFile);
                    resolve = null;
                }
                else if (word != null && Char.IsDigit(word[0]))
                {
                    type = ctx.ResolveType(ctx.Features.numberKey, inClass.InFile);
                }
                else if (word == "true" || word == "false")
                {
                    type = ctx.ResolveType(ctx.Features.booleanKey, inClass.InFile);
                }
                if (type != null && type.IsVoid()) type = null;
            }
            if (resolve == null) resolve = new ASResult();
            if (resolve.Type == null) resolve.Type = type;
            return new StatementReturnType(resolve, pos, word);
        }
Пример #23
0
        public static void GenerateDelegateMethods(ScintillaNet.ScintillaControl Sci, MemberModel member,
            Dictionary<MemberModel, ClassModel> selectedMembers, ClassModel classModel, ClassModel inClass)
        {
            Sci.BeginUndoAction();
            try
            {
                string result = TemplateUtils.ReplaceTemplateVariable(
                    TemplateUtils.GetTemplate("DelegateMethodsHeader"), 
                    "Class", 
                    classModel.Type);

                int position = -1;
                ClassModel type;
                List<string> importsList = new List<string>();
                bool isStaticMember = false;

                if ((member.Flags & FlagType.Static) > 0)
                    isStaticMember = true;

                inClass.ResolveExtends();
                
                Dictionary<MemberModel, ClassModel>.KeyCollection selectedMemberKeys = selectedMembers.Keys;
                foreach (MemberModel m in selectedMemberKeys)
                {
                    MemberModel mCopy = (MemberModel) m.Clone();

                    string methodTemplate = NewLine;

                    bool overrideFound = false;
                    ClassModel baseClassType = inClass;
                    while (baseClassType != null && !baseClassType.IsVoid())
                    {
                        MemberList inClassMembers = baseClassType.Members;
                        foreach (MemberModel inClassMember in inClassMembers)
                        {
                            if ((inClassMember.Flags & FlagType.Function) > 0
                               && m.Name.Equals(inClassMember.Name))
                            {
                                mCopy.Flags |= FlagType.Override;
                                overrideFound = true;
                                break;
                            }
                        }

                        if (overrideFound)
                            break;

                        baseClassType = baseClassType.Extends;
                    }

                    if (isStaticMember && (m.Flags & FlagType.Static) == 0)
                        mCopy.Flags |= FlagType.Static;

                    if ((m.Flags & FlagType.Setter) > 0)
                    {
                        methodTemplate += TemplateUtils.GetTemplate("Setter");
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Modifiers", 
                            (TemplateUtils.GetStaticExternOverride(m) + TemplateUtils.GetModifiers(m)).Trim());
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Name", m.Name);
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "EntryPoint", "");
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Type", m.Parameters[0].Type);
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Member", member.Name + "." + m.Name);
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Void", ASContext.Context.Features.voidKey ?? "void");
                    }
                    else if ((m.Flags & FlagType.Getter) > 0)
                    {
                        methodTemplate += TemplateUtils.GetTemplate("Getter");
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Modifiers",
                            (TemplateUtils.GetStaticExternOverride(m) + TemplateUtils.GetModifiers(m)).Trim());
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Name", m.Name);
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "EntryPoint", "");
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Type", FormatType(m.Type));
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Member", member.Name + "." + m.Name);
                    }
                    else
                    {
                        methodTemplate += TemplateUtils.GetTemplate("Function");
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Body", "<<$(Return) >>$(Body)");
                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "EntryPoint", null);
                        methodTemplate = TemplateUtils.ToDeclarationWithModifiersString(mCopy, methodTemplate);
                        if (m.Type != null && m.Type.ToLower() != "void")
                            methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Return", "return");
                        else
                            methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Return", null);

                        // check for varargs
                        bool isVararg = false;
                        if (m.Parameters != null && m.Parameters.Count > 0)
                        {
                            MemberModel mm = m.Parameters[m.Parameters.Count - 1];
                            if (mm.Name.StartsWith("..."))
                                isVararg = true;
                        }

                        string callMethodTemplate = TemplateUtils.GetTemplate("CallFunction");
                        if (!isVararg)
                        {
                            callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Name", member.Name + "." + m.Name);
                            callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Arguments", 
                                TemplateUtils.CallParametersString(m));
                            callMethodTemplate += ";";
                        }
                        else 
                        {
                            List<MemberModel> pseudoParamsList = new List<MemberModel>();
                            pseudoParamsList.Add(new MemberModel("null", null, FlagType.ParameterVar, 0));
                            pseudoParamsList.Add(new MemberModel("[$(Subarguments)].concat($(Lastsubargument))", null, FlagType.ParameterVar, 0));
                            MemberModel pseudoParamsOwner = new MemberModel();
                            pseudoParamsOwner.Parameters = pseudoParamsList;

                            callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Name",
                                member.Name + "." + m.Name + ".apply");
                            callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Arguments",
                                TemplateUtils.CallParametersString(pseudoParamsOwner));
                            callMethodTemplate += ";";

                            List<MemberModel> arrayParamsList = new List<MemberModel>();
                            for (int i = 0; i < m.Parameters.Count - 1; i++)
                            {
                                MemberModel param = m.Parameters[i];
                                arrayParamsList.Add(param);
                            }

                            pseudoParamsOwner.Parameters = arrayParamsList;

                            callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Subarguments",
                                TemplateUtils.CallParametersString(pseudoParamsOwner));

                            callMethodTemplate = TemplateUtils.ReplaceTemplateVariable(callMethodTemplate, "Lastsubargument", 
                                m.Parameters[m.Parameters.Count - 1].Name.TrimStart(new char[] { '.', ' '}));
                        }

                        methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "Body", callMethodTemplate);
                    }
                    methodTemplate = TemplateUtils.ReplaceTemplateVariable(methodTemplate, "BlankLine", NewLine);
                    result += methodTemplate;

                    if (m.Parameters != null)
                    {
                        for (int i = 0; i < m.Parameters.Count; i++)
                        {
                            MemberModel param = m.Parameters[i];
                            if (param.Type != null)
                            {
                                type = ASContext.Context.ResolveType(param.Type, selectedMembers[m].InFile);
                                importsList.Add(type.QualifiedName);
                            }
                        }
                    }

                    if (position < 0)
                    {
                        MemberModel latest = GetLatestMemberForFunction(inClass, mCopy.Access, mCopy);
                        if (latest == null)
                        {
                            position = Sci.WordStartPosition(Sci.CurrentPos, true);
                            Sci.SetSel(position, Sci.WordEndPosition(position, true));
                        }
                        else
                        {
                            position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                            Sci.SetSel(position, position);
                        }
                    }
                    else
                    {
                        position = Sci.CurrentPos;
                    }

                    if (m.Type != null)
                    {
                        type = ASContext.Context.ResolveType(m.Type, selectedMembers[m].InFile);
                        importsList.Add(type.QualifiedName);
                    }
                }

                if (importsList.Count > 0 && position > -1)
                {
                    int o = AddImportsByName(importsList, Sci.LineFromPosition(position));
                    position += o;
                    Sci.SetSel(position, position);
                }

                InsertCode(position, result);
            }
            finally { Sci.EndUndoAction(); }
        }
Пример #24
0
        private static void GenerateImplementation(ClassModel aType, int position)
        {
            List<string> typesUsed = new List<string>();

            StringBuilder sb = new StringBuilder(TemplateUtils.ReplaceTemplateVariable(TemplateUtils.GetTemplate("ImplementHeader"), "Class", aType.Type));
            sb.Append(NewLine);
            bool entry = true;
            ASResult result = new ASResult();
            IASContext context = ASContext.Context;
            ClassModel cClass = context.CurrentClass;
            ContextFeatures features = context.Features;
            bool canGenerate = false;

            aType.ResolveExtends(); // resolve inheritance chain
            while (!aType.IsVoid() && aType.QualifiedName != "Object")
            {
                foreach (MemberModel method in aType.Members)
                {
                    if ((method.Flags & (FlagType.Function | FlagType.Getter | FlagType.Setter)) == 0
                        || method.Name == aType.Name)
                        continue;

                    // check if method exists
                    ASComplete.FindMember(method.Name, cClass, result, method.Flags, 0);
                    if (!result.IsNull()) continue;

                    string decl = entry ? NewLine : "";
                    if ((method.Flags & FlagType.Getter) > 0)
                        decl = TemplateUtils.ToDeclarationWithModifiersString(method, TemplateUtils.GetTemplate("Getter"));
                    else if ((method.Flags & FlagType.Setter) > 0)
                        decl = TemplateUtils.ToDeclarationWithModifiersString(method, TemplateUtils.GetTemplate("Setter"));
                    else
                        decl = TemplateUtils.ToDeclarationWithModifiersString(method, TemplateUtils.GetTemplate("Function"));
                    decl = TemplateUtils.ReplaceTemplateVariable(decl, "Member", "_" + method.Name);
                    decl = TemplateUtils.ReplaceTemplateVariable(decl, "Void", features.voidKey);
                    decl = TemplateUtils.ReplaceTemplateVariable(decl, "Body", null);
                    decl = TemplateUtils.ReplaceTemplateVariable(decl, "BlankLine", NewLine);

                    if (!entry)
                    {
                        decl = TemplateUtils.ReplaceTemplateVariable(decl, "EntryPoint", null);
                    }

                    decl += NewLine;

                    entry = false;

                    sb.Append(decl);
                    canGenerate = true;

                    addTypeOnce(typesUsed, getQualifiedType(method.Type, aType));

                    if (method.Parameters != null && method.Parameters.Count > 0)
                        foreach (MemberModel param in method.Parameters)
                            addTypeOnce(typesUsed, getQualifiedType(param.Type, aType));
                }
                // interface inheritance
                aType = aType.Extends;
            }
            if (!canGenerate)
                return;

            ScintillaNet.ScintillaControl Sci = ASContext.CurSciControl;
            Sci.BeginUndoAction();
            try
            {
                position = Sci.CurrentPos;
                if (ASContext.Context.Settings.GenerateImports && typesUsed.Count > 0)
                {
                    int offset = AddImportsByName(typesUsed, Sci.LineFromPosition(position));
                    position += offset;
                    Sci.SetSel(position, position);
                }
                InsertCode(position, sb.ToString());
            }
            finally { Sci.EndUndoAction(); }
        }
Пример #25
0
 public GeneratorItem(string label, GeneratorJobType job, MemberModel member, ClassModel inClass)
 {
     this.label = label;
     this.job = job;
     this.member = member;
     this.inClass = inClass;
 }
Пример #26
0
        private static string getQualifiedType(string type, ClassModel aType)
        {
            if (type == null || type == "") return "*";
            if (type.IndexOf('<') > 0) // Vector.<Point>
            {
                Match mGeneric = Regex.Match(type, "<([^>]+)>");
                if (mGeneric.Success)
                {
                    return getQualifiedType(mGeneric.Groups[1].Value, aType);
                }
            }

            if (type.IndexOf('.') > 0) return type;

            ClassModel aClass = ASContext.Context.ResolveType(type, aType.InFile);
            if (!aClass.IsVoid())
            {
                if (aClass.InFile.Package.Length != 0)
                    return aClass.QualifiedName;
            }
            return "*";
        }
Пример #27
0
 public FoundDeclaration()
 {
     member = null;
     inClass = ClassModel.VoidClass;
 }
Пример #28
0
 private static void GenerateFunction(MemberModel member, int position, bool detach, ClassModel inClass)
 {
     string template = "";
     string decl = "";
     if ((inClass.Flags & FlagType.Interface) > 0)
     {
         template = TemplateUtils.GetTemplate("IFunction");
         decl = TemplateUtils.ToDeclarationString(member, template);
     }
     else if ((member.Flags & FlagType.Constructor) > 0)
     {
         template = TemplateUtils.GetTemplate("Constructor");
         decl = TemplateUtils.ToDeclarationWithModifiersString(member, template);
     }
     else
     {
         template = TemplateUtils.GetTemplate("Function");
         decl = TemplateUtils.ToDeclarationWithModifiersString(member, template);
         decl = TemplateUtils.ReplaceTemplateVariable(decl, "Body", null);
     }
     if (detach) decl = NewLine + TemplateUtils.ReplaceTemplateVariable(decl, "BlankLine", NewLine);
     else decl = TemplateUtils.ReplaceTemplateVariable(decl, "BlankLine", null);
     InsertCode(position, decl);
 }
Пример #29
0
        private void ReadType(JsonReader reader)
        {
            // root/modules/types
            FileModel cFile;
            ClassModel cClass;
            cFile = new FileModel();
            cFile.HasPackage = true;
            cFile.Version = 3;
            cFile.Context = context;
            cClass = new ClassModel();
            cClass.Flags = FlagType.Class;
            MemberModel cDelegate = new MemberModel();
            List<string> names;

            string prop = null;
            while (reader.Read())
            {
                if (reader.Token == JsonToken.ObjectEnd) break;
                if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
                else if (reader.Token == JsonToken.String)
                {
                    string val = reader.Value.ToString();
                    switch (prop)
                    {
                        case "name": cClass.Name = val; break;
                        case "source":
                            if (val.IndexOf('/') > 0) val = val.Substring(val.IndexOf('/') + 1);
                            cFile.FileName = path.Path + "::" + val;
                            break;
                        case "package":
                            if (val.ToLower() == "cocos2d") val = "cocos2d"; // random casing through source
                            cFile.Package = val; 
                            break;
                        case "baseType": cClass.ExtendsType = val; break;
                        case "delegateReturnType": cDelegate.Type = CleanType(val); break;
                        case "type":
                            switch (val)
                            {
                                case "CLASS": break;
                                case "INTERFACE": cClass.Flags |= FlagType.Interface; break;
                                case "STRUCT": cClass.Flags |= FlagType.Struct; break;
                                case "DELEGATE": cDelegate.Name = cClass.Name; break;
                            }
                            break;
                        case "docstring": cClass.Comments = ExtractDoc(val); break;
                    }
                }
                else if (reader.Token == JsonToken.ArrayStart)
                {
                    switch (prop)
                    {
                        case "classattributes": ReadAttributes(reader, cClass); break;
                        case "fields": ReadFields(reader, cClass); break;
                        case "methods": ReadMethods(reader, cClass); break;
                        case "properties": ReadProperties(reader, cClass); break;
                        case "metainfo": ReadMetas(reader, cClass); break;
                        case "imports": 
                            names = ReadNames(reader);
                            foreach (string name in names)
                                cFile.Imports.Add(new MemberModel(name, name, FlagType.Import, Visibility.Public)); 
                            break;
                        case "interfaces": ReadInterfaces(reader, cClass); break;
                        case "delegateTypes":
                            names = ReadNames(reader);
                            if (names.Count > 0)
                            {
                                cDelegate.Parameters = new List<MemberModel>();
                                foreach (string argType in names)
                                {
                                    cDelegate.Parameters.Add(
                                        new MemberModel("p" + cDelegate.Parameters.Count, argType, FlagType.ParameterVar, Visibility.Public));
                                }
                            }
                            break;
                        default: reader.SkipArray(); break;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    switch (prop)
                    {
                        case "constructor": 
                            MemberModel ctor = ReadMethod(reader, cClass);
                            cClass.Constructor = ctor.Name;
                            break;
                        default: reader.SkipObject(); break;
                    }
                }
            }

            if (cFile.FileName == null) return;
            string key = cFile.FileName.ToUpper();
            if (files.ContainsKey(key)) cFile = files[key];
            else files.Add(key, cFile);

            if (cFile.Package.ToLower() == "system") // System classes tweaks
            {
                cFile.Package = "";
                if (cClass.Name == "Vector") cClass.Name = "Vector.<T>";
                if (cClass.Name == "Object") cClass.ExtendsType = "void";
            }
            if (cClass.Access == Visibility.Private) cClass.Access = Visibility.Public;

            if (cDelegate.Name != null)
            {
                if (cDelegate.Parameters == null) cDelegate.Parameters = new List<MemberModel>();
                cDelegate.Access = Visibility.Public;
                cDelegate.Flags = FlagType.Function | FlagType.Delegate;
                cDelegate.InFile = cFile;
                cDelegate.IsPackageLevel = true;
                cFile.Members.Add(cDelegate);
            }
            else
            {
                cClass.Type = CleanType(String.IsNullOrEmpty(cFile.Package) ? cClass.Name : cFile.Package + "." + cClass.Name);
                cClass.InFile = cFile;
                cFile.Classes.Add(cClass);
            }
        }
Пример #30
0
 private static MemberModel GetLatestMemberForFunction(ClassModel inClass, Visibility funcVisi, MemberModel isStatic)
 {
     MemberModel latest = null;
     if (isStatic != null && (isStatic.Flags & FlagType.Static) > 0)
     {
         latest = FindLatest(FlagType.Function | FlagType.Static, funcVisi, inClass);
         if (latest == null)
         {
             latest = FindLatest(FlagType.Function | FlagType.Static, 0, inClass, true, false);
         }
     }
     else
     {
         latest = FindLatest(FlagType.Function, funcVisi, inClass);
     }
     if (latest == null)
     {
         latest = FindLatest(FlagType.Function, 0, inClass, true, false);
     }
     if (latest == null)
     {
         latest = FindLatest(FlagType.Function, 0, inClass, false, false);
     }
     return latest;
 }
Пример #31
0
 private void ReadProperties(JsonReader reader, ClassModel cClass)
 {
     while (reader.Read())
     {
         if (reader.Token == JsonToken.ArrayEnd) break;
         if (reader.Token == JsonToken.ObjectStart) ReadProperty(reader, cClass);
     }
 }
Пример #32
0
 private static MemberModel GetLatestMemberForVariable(GeneratorJobType job, ClassModel inClass, Visibility varVisi, MemberModel isStatic)
 {
     MemberModel latest = null;
     if (job.Equals(GeneratorJobType.Constant))
     {
         if ((isStatic.Flags & FlagType.Static) > 0)
         {
             latest = FindLatest(FlagType.Constant | FlagType.Static, varVisi, inClass);
         }
         else
         {
             latest = FindLatest(FlagType.Constant, varVisi, inClass);
         }
         if (latest == null)
         {
             latest = FindLatest(FlagType.Constant, 0, inClass, true, false);
         }
     }
     else
     {
         if ((isStatic.Flags & FlagType.Static) > 0)
         {
             latest = FindLatest(FlagType.Variable | FlagType.Static, varVisi, inClass);
             if (latest == null)
             {
                 latest = FindLatest(FlagType.Variable | FlagType.Static, 0, inClass, true, false);
             }
         }
         else
         {
             latest = FindLatest(FlagType.Variable, varVisi, inClass);
         }
     }
     if (latest == null)
     {
         latest = FindLatest(FlagType.Variable, varVisi, inClass, false, false);
     }
     return latest;
 }
Пример #33
0
        private void ReadProperty(JsonReader reader, ClassModel cClass)
        {
            MemberModel member = new MemberModel();
            member.Flags = FlagType.Variable;

            MemberModel getter = null;
            MemberModel setter = null;
            string prop = null;
            string name = null;
            string doc = null;
            while (reader.Read())
            {
                if (reader.Token == JsonToken.ObjectEnd) break;
                if (reader.Token == JsonToken.PropertyName) prop = reader.Value.ToString();
                else if (reader.Token == JsonToken.String)
                {
                    string val = reader.Value.ToString();
                    switch (prop)
                    {
                        case "name": name = val; break;
                        case "docstring": doc = ExtractDoc(val); break;
                    }
                }
                else if (reader.Token == JsonToken.ArrayStart)
                {
                    switch (prop)
                    {
                        //case "propertyattributes": ReadAttributes(reader, member); break;
                        //case "metainfo": reader.SkipArray(); break;
                        default: reader.SkipArray(); break;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    switch (prop)
                    {
                        case "getter": 
                            getter = ReadMethod(reader, cClass);
                            getter.Flags |= FlagType.Getter;
                            break;
                        case "setter":
                            setter = ReadMethod(reader, cClass);
                            setter.Flags |= FlagType.Setter;
                            break;
                        default: reader.SkipObject(); break;
                    }
                }
            }
            if (getter != null)
            {
                getter.Name = name;
                getter.Comments = doc;
                getter.Flags &= ~FlagType.Function;
            }
            if (setter != null)
            {
                setter.Name = name;
                if (getter == null) setter.Comments = doc;
                setter.Flags &= ~FlagType.Function;
            }
        }
Пример #34
0
 static ClassModel()
 {
     VoidClass        = new ClassModel();
     VoidClass.Name   = "void";
     VoidClass.InFile = new FileModel("");
 }