Example #1
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);
        }
Example #2
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);
        }
Example #3
0
 public static FileModel ParseFile(FileModel fileModel)
 {
     string src = "";
     // parse file
     if (fileModel.FileName.Length > 0)
     {
         if (File.Exists(fileModel.FileName))
         {
             src = PluginCore.Helpers.FileHelper.ReadFile(fileModel.FileName);
             ASFileParser parser = new ASFileParser();
             fileModel.LastWriteTime = File.GetLastWriteTime(fileModel.FileName);
             if (cachedPath != null)
                 cacheLastWriteTime = fileModel.LastWriteTime;
             parser.ParseSrc(fileModel, src);
         }
         // the file is not available (for the moment?)
         else if (Path.GetExtension(fileModel.FileName).Length > 0)
         {
             fileModel.OutOfDate = true;
         }
     }
     // this is a package
     else
     {
         // ignore
     }
     return fileModel;
 }
Example #4
0
        private static void ChangeConstructorDecl(ScintillaNet.ScintillaControl Sci, MemberModel member, ClassModel inClass)
        {
            int wordPos = Sci.WordEndPosition(Sci.CurrentPos, true);
            List<FunctionParameter> functionParameters = ParseFunctionParameters(Sci, wordPos);
            ASResult funcResult = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(Sci.CurrentPos, true));

            if (funcResult == null || funcResult.Type == null) return;
            if (funcResult.Type != null && !funcResult.Type.Equals(inClass))
            {
                AddLookupPosition();
                lookupPosition = -1;

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

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

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

                inClass = funcResult.Type;
                ASContext.Context.UpdateContext(inClass.LineFrom);
            }

            foreach (MemberModel m in inClass.Members)
            {
                if ((m.Flags & FlagType.Constructor) > 0)
                {
                    funcResult.Member = m;
                    break;
                }
            }

            if (funcResult.Member == null) return;
            if (isHaxe) funcResult.Member.Name = "new";

            ChangeDecl(Sci, funcResult.Member, functionParameters);
        }
Example #5
0
        private static void ChangeMethodDecl(ScintillaControl sci, MemberModel member, ClassModel inClass)
        {
            int wordPos = sci.WordEndPosition(sci.CurrentPos, true);
            List<FunctionParameter> functionParameters = ParseFunctionParameters(sci, wordPos);

            ASResult funcResult = ASComplete.GetExpressionType(sci, sci.WordEndPosition(sci.CurrentPos, true));
            if (funcResult == null || funcResult.Member == null) return;
            if (funcResult.InClass != null && !funcResult.InClass.Equals(inClass))
            {
                AddLookupPosition();
                lookupPosition = -1;

                ASContext.MainForm.OpenEditableDocument(funcResult.InClass.InFile.FileName, true);
                sci = ASContext.CurSciControl;

                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.InClass.QualifiedName))
                    {
                        funcResult.InClass = cm;
                        break;
                    }
                }
                inClass = funcResult.InClass;

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

            MemberList members = inClass.Members;
            foreach (MemberModel m in members)
            {
                if (m.Equals(funcResult.Member))
                {
                    funcResult.Member = m;
                    break;
                }
            }

            ChangeDecl(sci, funcResult.Member, functionParameters);
        }
Example #6
0
        private void ExtractFilesFromArchive()
        {
            string[] masks = context.GetExplorerMask();
            for (int i = 0; i < masks.Length; i++)
                masks[i] = masks[i].Substring(masks[i].IndexOf('*') + 1);

            Stream fileStream = File.OpenRead(pathModel.Path);
            ZipFile zipFile = new ZipFile(fileStream);
            ASFileParser parser = new ASFileParser();
            Dictionary<string, FileModel> models = new Dictionary<string, FileModel>();

            foreach (ZipEntry entry in zipFile)
            {
                string ext = Path.GetExtension(entry.Name).ToLower();
                foreach (string mask in masks)
                    if (ext == mask)
                    {
                        string src = UnzipFile(zipFile, entry);
                        FileModel model = new FileModel(Path.Combine(pathModel.Path, entry.Name));
                        model.Context = pathModel.Owner;
                        parser.ParseSrc(model, src);
                        models.Add(model.FileName, model);
                    }
            }
            zipFile.Close();
            fileStream.Close();

            pathModel.SetFiles(models);
        }
Example #7
0
        /// <summary>
        /// Refresh the file model
        /// </summary>
        /// <param name="updateUI">Update outline view</param>
        public virtual void UpdateCurrentFile(bool updateUI)
        {
            if (cFile == null || CurSciControl == null)
                return;
            ASFileParser parser = new ASFileParser();
            parser.ParseSrc(cFile, CurSciControl.Text);
            cLine = CurSciControl.CurrentLine;
            UpdateContext(cLine);

            // update outline
            if (updateUI) Context = this;
        }
Example #8
0
        public static void GenerateExtractVariable(ScintillaControl sci, string newName)
        {
            string expression = sci.SelText.Trim(new char[] { '=', ' ', '\t', '\n', '\r', ';', '.' });
            expression = expression.TrimEnd(new char[] { '(', '[', '{', '<' });
            expression = expression.TrimStart(new char[] { ')', ']', '}', '>' });

            var cFile = ASContext.Context.CurrentModel;
            ASFileParser parser = new ASFileParser();
            parser.ParseSrc(cFile, sci.Text);

            MemberModel current = cFile.Context.CurrentMember;

            string characterClass = ScintillaControl.Configuration.GetLanguage(sci.ConfigurationLanguage).characterclass.Characters;

            int funcBodyStart = GetBodyStart(current.LineFrom, current.LineTo, sci);
            sci.SetSel(funcBodyStart, sci.LineEndPosition(current.LineTo));
            string currentMethodBody = sci.SelText;
            var insertPosition = funcBodyStart + currentMethodBody.IndexOfOrdinal(expression);
            var line = sci.LineFromPosition(insertPosition);
            insertPosition = sci.LineIndentPosition(line);

            int lastPos = -1;
            sci.Colourise(0, -1);
            while (true)
            {
                lastPos = currentMethodBody.IndexOfOrdinal(expression, lastPos + 1);
                if (lastPos > -1)
                {
                    char prevOrNextChar;
                    if (lastPos > 0)
                    {
                        prevOrNextChar = currentMethodBody[lastPos - 1];
                        if (characterClass.IndexOf(prevOrNextChar) > -1)
                        {
                            continue;
                        }
                    }
                    if (lastPos + expression.Length < currentMethodBody.Length)
                    {
                        prevOrNextChar = currentMethodBody[lastPos + expression.Length];
                        if (characterClass.IndexOf(prevOrNextChar) > -1)
                        {
                            continue;
                        }
                    }

                    var pos = funcBodyStart + lastPos;
                    int style = sci.BaseStyleAt(pos);
                    if (ASComplete.IsCommentStyle(style)) continue;
                    sci.SetSel(pos, pos + expression.Length);
                    sci.ReplaceSel(newName);
                    currentMethodBody = currentMethodBody.Substring(0, lastPos) + newName + currentMethodBody.Substring(lastPos + expression.Length);
                    lastPos += newName.Length;
                }
                else
                {
                    break;
                }
            }

            sci.CurrentPos = insertPosition;
            sci.SetSel(sci.CurrentPos, sci.CurrentPos);
            MemberModel m = new MemberModel(newName, "", FlagType.LocalVar, 0);
            m.Value = expression;

            string snippet = TemplateUtils.GetTemplate("Variable");
            snippet = TemplateUtils.ReplaceTemplateVariable(snippet, "Modifiers", null);
            snippet = TemplateUtils.ToDeclarationString(m, snippet);
            snippet += NewLine + "$(Boundary)";
            SnippetHelper.InsertSnippetText(sci, sci.CurrentPos, snippet);
        }
        public void Execute()
        {
            Sci = PluginBase.MainForm.CurrentDocument.SciControl;
            Sci.BeginUndoAction();
            try
            {
                IASContext context = ASContext.Context;
                Int32 pos = Sci.CurrentPos;

                string expression = Sci.SelText.Trim(new char[] { '=', ' ', '\t', '\n', '\r', ';', '.' });
                expression = expression.TrimEnd(new char[] { '(', '[', '{', '<' });
                expression = expression.TrimStart(new char[] { ')', ']', '}', '>' });

                cFile = ASContext.Context.CurrentModel;
                ASFileParser parser = new ASFileParser();
                parser.ParseSrc(cFile, Sci.Text);

                MemberModel current = cFile.Context.CurrentMember;
               
                string characterClass = ScintillaControl.Configuration.GetLanguage(Sci.ConfigurationLanguage).characterclass.Characters;

                int funcBodyStart = ASGenerator.GetBodyStart(current.LineFrom, current.LineTo, Sci);
                Sci.SetSel(funcBodyStart, Sci.LineEndPosition(current.LineTo));
                string currentMethodBody = Sci.SelText;

                bool isExprInSingleQuotes = (expression.StartsWith("'") && expression.EndsWith("'"));
                bool isExprInDoubleQuotes = (expression.StartsWith("\"") && expression.EndsWith("\""));
                int stylemask = (1 << Sci.StyleBits) - 1;
                int lastPos = -1;
                char prevOrNextChar;
                Sci.Colourise(0, -1);
                while (true)
                {
                    lastPos = currentMethodBody.IndexOf(expression, lastPos + 1);
                    if (lastPos > -1)
                    {
                        if (lastPos > 0)
                        {
                            prevOrNextChar = currentMethodBody[lastPos - 1];
                            if (characterClass.IndexOf(prevOrNextChar) > -1)
                            {
                                continue;
                            }
                        }
                        if (lastPos + expression.Length < currentMethodBody.Length)
                        {
                            prevOrNextChar = currentMethodBody[lastPos + expression.Length];
                            if (characterClass.IndexOf(prevOrNextChar) > -1)
                            {
                                continue;
                            }
                        }

                        int style = Sci.StyleAt(funcBodyStart + lastPos) & stylemask;
                        if (ASComplete.IsCommentStyle(style))
                        {
                            continue;
                        }
                        else if ((isExprInDoubleQuotes && currentMethodBody[lastPos] == '"' && currentMethodBody[lastPos + expression.Length - 1] == '"')
                            || (isExprInSingleQuotes && currentMethodBody[lastPos] == '\'' && currentMethodBody[lastPos + expression.Length - 1] == '\''))
                        {
                            
                        }
                        else if (!ASComplete.IsTextStyle(style))
                        {
                            continue;
                        }

                        Sci.SetSel(funcBodyStart + lastPos, funcBodyStart + lastPos + expression.Length);
                        Sci.ReplaceSel(NewName);
                        currentMethodBody = currentMethodBody.Substring(0, lastPos) + NewName + currentMethodBody.Substring(lastPos + expression.Length);
                        lastPos += NewName.Length;
                    }
                    else
                    {
                        break;
                    }
                }

                Sci.CurrentPos = funcBodyStart;
                Sci.SetSel(Sci.CurrentPos, Sci.CurrentPos);

                string snippet = "var " + NewName + ":$(EntryPoint) = " + expression + ";\n$(Boundary)";
                SnippetHelper.InsertSnippetText(Sci, Sci.CurrentPos, snippet);
            }
            finally
            {
                Sci.EndUndoAction();
            }
        }
Example #10
0
 /// <summary>
 /// Build the file DOM
 /// </summary>
 /// <param name="filename">File path</param>
 protected override void GetCurrentFileModel(string fileName)
 {
     string ext = Path.GetExtension(fileName);
     if (!re_PHPext.IsMatch(ext))
     {
         cFile = FileModel.Ignore;
         UpdateContext(cLine);
     }
     else
     {
         cFile = new FileModel(fileName);
         cFile.Context = this;
         cFile.HasFiltering = true;
         ASFileParser parser = new ASFileParser();
         parser.ParseSrc(cFile, CurSciControl.Text);
         cLine = CurSciControl.CurrentLine;
         UpdateContext(cLine);
     }
 }
Example #11
0
        /// <summary>
        /// Refresh the file model
        /// </summary>
        /// <param name="updateUI">Update outline view</param>
        public override void UpdateCurrentFile(bool updateUI)
        {
            if (cFile == null || CurSciControl == null)
                return;
            ASFileParser parser = new ASFileParser();
            parser.ParseSrc(cFile, CurSciControl.Text);
            ScriptToClass(cFile);
            cLine = CurSciControl.LineFromPosition(CurSciControl.CurrentPos);
            UpdateContext(cLine);

            // update outline
            if (updateUI) ASContext.Context = this;
        }
Example #12
0
        private static void ChangeMethodDecl(ScintillaNet.ScintillaControl Sci, MemberModel member, ClassModel inClass)
        {
            int wordPos = Sci.WordEndPosition(Sci.CurrentPos, true);
            List<FunctionParameter> functionParameters = ParseFunctionParameters(Sci, wordPos);

            ASResult funcResult = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(Sci.CurrentPos, true));
            if (funcResult.Member == null)
            {
                return;
            }
            if (funcResult != null && funcResult.inClass != null && !funcResult.inClass.Equals(inClass))
            {
                AddLookupPosition();
                lookupPosition = -1;

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

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

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

            MemberList members = inClass.Members;
            foreach (MemberModel m in members)
            {
                if (m.Name == funcResult.Member.Name)
                {
                    funcResult.Member = m;
                }
            }

            bool paramsDiffer = false;
            if (funcResult.Member.Parameters != null)
            {
                // check that parameters have one and the same type
                if (funcResult.Member.Parameters.Count == functionParameters.Count)
                {
                    if (functionParameters.Count > 0)
                    {
                        List<MemberModel> parameters = funcResult.Member.Parameters;
                        for (int i = 0; i < parameters.Count; i++)
                        {
                            MemberModel p = parameters[i];
                            if (p.Type != functionParameters[i].paramType)
                            {
                                paramsDiffer = true;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    paramsDiffer = true;
                }
            }
                // check that parameters count differs
            else if (functionParameters.Count != 0)
            {
                paramsDiffer = true;
            }

            if (paramsDiffer)
            {
                int app = 0;
                List<MemberModel> newParameters = new List<MemberModel>();
                List<MemberModel> existingParameters = funcResult.Member.Parameters;
                for (int i = 0; i < functionParameters.Count; i++)
                {
                    FunctionParameter p = functionParameters[i];
                    if (existingParameters != null
                        && existingParameters.Count > (i - app)
                        && existingParameters[i - app].Type == p.paramType)
                    {
                        newParameters.Add(existingParameters[i - app]);
                    }
                    else
                    {
                        if (existingParameters != null && existingParameters.Count < functionParameters.Count)
                        {
                            app++;
                        }
                        newParameters.Add(new MemberModel(p.param, p.paramType, FlagType.ParameterVar, 0));
                    }
                }
                funcResult.Member.Parameters = newParameters;

                int posStart = Sci.PositionFromLine(funcResult.Member.LineFrom);
                int posEnd = Sci.LineEndPosition(funcResult.Member.LineTo);
                Sci.SetSel(posStart, posEnd);
                string selectedText = Sci.SelText;
                Regex rStart = new Regex(String.Format(@"\s+{0}\s*\(([^\)]*)\)(\s*:\s*([^({{|\n|\r|\s|;)]+))?", funcResult.Member.Name));
                Match mStart = rStart.Match(selectedText);
                if (!mStart.Success)
                {
                    return;
                }

                int start = mStart.Index + posStart;
                int end = start + mStart.Length;

                Sci.SetSel(start, end);

                string decl = funcResult.Member.ToDeclarationString();
                Sci.ReplaceSel(" " + decl);

                // 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)
                        {
                        }
                    }
                    start += AddImportsByName(l, Sci.LineFromPosition(end));
                }

                Sci.SetSel(start, start);
            }
        }
        public void Execute()
        {
            Sci = PluginBase.MainForm.CurrentDocument.SciControl;
            Sci.BeginUndoAction();
            try
            {
                IASContext context = ASContext.Context;

                string selection = Sci.SelText;
                if (selection == null || selection.Length == 0)
                {
                    return;
                }

                if (selection.TrimStart().Length == 0)
                {
                    return;
                }

                Sci.SetSel(Sci.SelectionStart + selection.Length - selection.TrimStart().Length,
                    Sci.SelectionEnd);
                Sci.CurrentPos = Sci.SelectionEnd;

                Int32 pos = Sci.CurrentPos;

                int lineStart = Sci.LineFromPosition(Sci.SelectionStart);
                int lineEnd = Sci.LineFromPosition(Sci.SelectionEnd);
                int firstLineIndent = Sci.GetLineIndentation(lineStart);
                int entryPointIndent = Sci.Indent;

                for (int i = lineStart; i <= lineEnd; i++)
                {
                    int indent = Sci.GetLineIndentation(i);
                    if (i > lineStart)
                    {
                        Sci.SetLineIndentation(i, indent - firstLineIndent + entryPointIndent);
                    }
                }

                string selText = Sci.SelText;
                Sci.ReplaceSel(NewName + "();");

                cFile = ASContext.Context.CurrentModel;
                ASFileParser parser = new ASFileParser();
                parser.ParseSrc(cFile, Sci.Text);

                bool isAs3 = cFile.Context.Settings.LanguageId == "AS3";

                FoundDeclaration found = GetDeclarationAtLine(Sci, lineStart);
                if (found == null || found.member == null)
                {
                    return;
                }

                int position = Sci.PositionFromLine(found.member.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                Sci.SetSel(position, position);

                StringBuilder sb = new StringBuilder();
                sb.Append("$(Boundary)\n\n");
                if ((found.member.Flags & FlagType.Static) > 0)
                {
                    sb.Append("static ");
                }
                sb.Append(ASGenerator.GetPrivateKeyword());
                sb.Append(" function ");
                sb.Append(NewName);
                sb.Append("():");
                sb.Append(isAs3 ? "void " : "Void ");
                sb.Append("$(CSLB){\n\t");
                sb.Append(selText);
                sb.Append("$(EntryPoint)");
                sb.Append("\n}\n$(Boundary)");

                ASGenerator.InsertCode(position, sb.ToString());
            }
            finally
            {
                Sci.EndUndoAction();
            }
        }
Example #14
0
        public static void GenerateExtractVariable(ScintillaNet.ScintillaControl Sci, string NewName)
        {
            FileModel cFile;
            IASContext context = ASContext.Context;
            Int32 pos = Sci.CurrentPos;

            string expression = Sci.SelText.Trim(new char[] { '=', ' ', '\t', '\n', '\r', ';', '.' });
            expression = expression.TrimEnd(new char[] { '(', '[', '{', '<' });
            expression = expression.TrimStart(new char[] { ')', ']', '}', '>' });

            cFile = ASContext.Context.CurrentModel;
            ASFileParser parser = new ASFileParser();
            parser.ParseSrc(cFile, Sci.Text);

            MemberModel current = cFile.Context.CurrentMember;

            string characterClass = ScintillaNet.ScintillaControl.Configuration.GetLanguage(Sci.ConfigurationLanguage).characterclass.Characters;

            int funcBodyStart = ASGenerator.GetBodyStart(current.LineFrom, current.LineTo, Sci);
            Sci.SetSel(funcBodyStart, Sci.LineEndPosition(current.LineTo));
            string currentMethodBody = Sci.SelText;

            bool isExprInSingleQuotes = (expression.StartsWith("'") && expression.EndsWith("'"));
            bool isExprInDoubleQuotes = (expression.StartsWith("\"") && expression.EndsWith("\""));
            int stylemask = (1 << Sci.StyleBits) - 1;
            int lastPos = -1;
            char prevOrNextChar;
            Sci.Colourise(0, -1);
            while (true)
            {
                lastPos = currentMethodBody.IndexOf(expression, lastPos + 1);
                if (lastPos > -1)
                {
                    if (lastPos > 0)
                    {
                        prevOrNextChar = currentMethodBody[lastPos - 1];
                        if (characterClass.IndexOf(prevOrNextChar) > -1)
                        {
                            continue;
                        }
                    }
                    if (lastPos + expression.Length < currentMethodBody.Length)
                    {
                        prevOrNextChar = currentMethodBody[lastPos + expression.Length];
                        if (characterClass.IndexOf(prevOrNextChar) > -1)
                        {
                            continue;
                        }
                    }

                    int style = Sci.StyleAt(funcBodyStart + lastPos) & stylemask;
                    if (ASComplete.IsCommentStyle(style))
                    {
                        continue;
                    }
                    else if ((isExprInDoubleQuotes && currentMethodBody[lastPos] == '"' && currentMethodBody[lastPos + expression.Length - 1] == '"')
                        || (isExprInSingleQuotes && currentMethodBody[lastPos] == '\'' && currentMethodBody[lastPos + expression.Length - 1] == '\''))
                    {

                    }
                    else if (!ASComplete.IsTextStyle(style))
                    {
                        continue;
                    }

                    Sci.SetSel(funcBodyStart + lastPos, funcBodyStart + lastPos + expression.Length);
                    Sci.ReplaceSel(NewName);
                    currentMethodBody = currentMethodBody.Substring(0, lastPos) + NewName + currentMethodBody.Substring(lastPos + expression.Length);
                    lastPos += NewName.Length;
                }
                else
                {
                    break;
                }
            }

            Sci.CurrentPos = funcBodyStart;
            Sci.SetSel(Sci.CurrentPos, Sci.CurrentPos);

            MemberModel m = new MemberModel(NewName, "", FlagType.LocalVar, 0);
            m.Value = expression;

            string snippet = TemplateUtils.GetTemplate("Variable");
            snippet = TemplateUtils.ReplaceTemplateVariable(snippet, "Modifiers", null);
            snippet = TemplateUtils.ToDeclarationString(m, snippet);
            snippet += NewLine + "$(Boundary)";
            SnippetHelper.InsertSnippetText(Sci, Sci.CurrentPos, snippet);
        }
Example #15
0
 /// <summary>
 /// Set local code parser features
 /// </summary>
 /// <returns></returns>
 protected virtual ASFileParser GetCodeParser()
 {
     ASFileParser parser = new ASFileParser();
     parser.Features.varKey = Context.Features.varKey;
     parser.Features.constKey = Context.Features.constKey;
     parser.Features.functionKey = Context.Features.functionKey;
     parser.Features.hasEcmaTyping = Context.Features.hasEcmaTyping;
     parser.Features.hasConsts = Context.Features.hasConsts;
     parser.Features.hasVars = Context.Features.hasVars;
     parser.Features.hasMethods = Context.Features.hasMethods;
     parser.Features.hasGenerics = Context.Features.hasGenerics;
     parser.Features.hasCArrays = Context.Features.hasCArrays;
     parser.Features.CArrayTemplate = Context.Features.CArrayTemplate;
     return parser;
 }
Example #16
0
        public static void GenerateExtractMethod(ScintillaNet.ScintillaControl Sci, string NewName)
        {
            FileModel cFile;
            IASContext context = ASContext.Context;

            string selection = Sci.SelText;
            if (selection == null || selection.Length == 0)
            {
                return;
            }

            if (selection.TrimStart().Length == 0)
            {
                return;
            }

            Sci.SetSel(Sci.SelectionStart + selection.Length - selection.TrimStart().Length,
                Sci.SelectionEnd);
            Sci.CurrentPos = Sci.SelectionEnd;

            Int32 pos = Sci.CurrentPos;

            int lineStart = Sci.LineFromPosition(Sci.SelectionStart);
            int lineEnd = Sci.LineFromPosition(Sci.SelectionEnd);
            int firstLineIndent = Sci.GetLineIndentation(lineStart);
            int entryPointIndent = Sci.Indent;

            for (int i = lineStart; i <= lineEnd; i++)
            {
                int indent = Sci.GetLineIndentation(i);
                if (i > lineStart)
                {
                    Sci.SetLineIndentation(i, indent - firstLineIndent + entryPointIndent);
                }
            }

            string selText = Sci.SelText;
            string template = TemplateUtils.GetTemplate("CallFunction");
            template = TemplateUtils.ReplaceTemplateVariable(template, "Name", NewName);
            template = TemplateUtils.ReplaceTemplateVariable(template, "Arguments", "");

            ASGenerator.InsertCode(Sci.CurrentPos, template + ";");

            cFile = ASContext.Context.CurrentModel;
            ASFileParser parser = new ASFileParser();
            parser.ParseSrc(cFile, Sci.Text);

            bool isAs3 = cFile.Context.Settings.LanguageId == "AS3";

            FoundDeclaration found = GetDeclarationAtLine(Sci, lineStart);
            if (found == null || found.member == null)
            {
                return;
            }

            lookupPosition = Sci.CurrentPos;
            AddLookupPosition();

            MemberModel latest = TemplateUtils.GetTemplateBlockMember(Sci, TemplateUtils.GetBoundary("PrivateMethods"));

            if (latest == null)
                latest = GetLatestMemberForFunction(found.inClass, ASGenerator.GetDefaultVisibility(), found.member);

            if (latest == null)
                latest = found.member;

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

            FlagType flags = FlagType.Function;
            if ((found.member.Flags & FlagType.Static) > 0)
            {
                flags |= FlagType.Static;
            }

            MemberModel m = new MemberModel(NewName, context.Features.voidKey, flags, ASGenerator.GetDefaultVisibility());

            template = NewLine + TemplateUtils.GetTemplate("Function");
            template = TemplateUtils.ToDeclarationWithModifiersString(m, template);
            template = TemplateUtils.ReplaceTemplateVariable(template, "Body", selText);
            template = TemplateUtils.ReplaceTemplateVariable(template, "BlankLine", NewLine);
            InsertCode(position, template);
        }
Example #17
0
        private static void GenerateFunctionJob(GeneratorJobType job, ScintillaControl sci, MemberModel member, bool detach, ClassModel inClass)
        {
            var position = 0;
            bool isOtherClass = false;
            Visibility visibility = job.Equals(GeneratorJobType.FunctionPublic) ? Visibility.Public : GetDefaultVisibility(inClass);
            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));
            var memberIsStatic = member != null && (member.Flags & FlagType.Static) > 0;
            var dot = ASContext.Context.Features.dot;
            if (ASContext.CommonSettings.GenerateScope && !funcResult.Context.Value.Contains(dot))
            {
                position = sci.CurrentPos;
                var start = sci.WordStartPosition(position, true);
                var length = sci.MBSafeTextLength(contextToken);
                sci.SetSel(start, start + length);
                var scope = memberIsStatic ? inClass.QualifiedName : "this";
                var text = scope + dot + contextToken;
                sci.ReplaceSel(text);
                UpdateLookupPosition(position, text.Length - length);
            }
            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
                    && (contextOwnerResult.Member == null || (contextOwnerResult.Member.Flags & FlagType.Constructor) > 0)
                    && contextOwnerResult.Type != null)
                {
                    isStatic.Flags |= FlagType.Static;
                }
            }
            else if (memberIsStatic)
            {
                isStatic.Flags |= FlagType.Static;
            }
            if (funcResult.RelClass != null && !funcResult.RelClass.IsVoid() && !funcResult.RelClass.Equals(inClass))
            {
                AddLookupPosition();
                lookupPosition = -1;

                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;
            if ((isStatic.Flags & FlagType.Static) > 0)
            {
                blockTmpl = TemplateUtils.GetBoundary("StaticMethods");
            }
            else if ((visibility & Visibility.Public) > 0)
            {
                blockTmpl = TemplateUtils.GetBoundary("PublicMethods");
            }
            else
            {
                blockTmpl = TemplateUtils.GetBoundary("PrivateMethods");
            }
            var latest = TemplateUtils.GetTemplateBlockMember(sci, blockTmpl);
            if (latest == null || (!isOtherClass && member == null))
            {
                latest = GetLatestMemberForFunction(inClass, visibility, 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> typesUsed = new List<string>();
                foreach (FunctionParameter parameter in functionParameters)
                {
                    try
                    {
                        typesUsed.Add(parameter.paramQualType);
                    }
                    catch (Exception) { }
                }
                int o = AddImportsByName(typesUsed, 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>();
            foreach (FunctionParameter parameter in functionParameters)
            {
                parameters.Add(new MemberModel(parameter.paramName, parameter.paramType, FlagType.ParameterVar, 0));
            }
            var newMember = NewMember(contextToken, isStatic, FlagType.Function, visibility);
            newMember.Parameters = parameters;
            GenerateFunction(newMember, position, detach, inClass);
        }