Example #1
0
        public override void Execute(TextArea textArea)
        {
            WorkbenchServiceFactory.Workbench.ErrorsListWindow.ClearErrorList();
            CodeFormatters.CodeFormatter          cf     = new CodeFormatters.CodeFormatter(VisualPABCSingleton.MainForm.UserOptions.TabIndent);
            List <PascalABCCompiler.Errors.Error> Errors = new List <PascalABCCompiler.Errors.Error>();
            //PascalABCCompiler.SyntaxTree.syntax_tree_node sn =
            //    MainForm.VisualEnvironmentCompiler.Compiler.ParsersController.Compile(
            //    FileName, TextEditor.Text, null, Errors, PascalABCCompiler.Parsers.ParseMode.Normal);
            string text = WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.SourceFilesProvider(VisualPABCSingleton.MainForm.CurrentCodeFileDocument.FileName, PascalABCCompiler.SourceFileOperation.GetText) as string;

            PascalABCCompiler.SyntaxTree.compilation_unit cu =
                CodeCompletion.CodeCompletionController.ParsersController.GetCompilationUnitForFormatter(
                    VisualPABCSingleton.MainForm.CurrentCodeFileDocument.FileName,
                    text, //VisualPascalABC.Form1.Form1_object._currentCodeFileDocument.TextEditor.Text,
                    Errors,
                    new List <PascalABCCompiler.Errors.CompilerWarning>());
            if (Errors.Count == 0)
            {
                string formattedText = cf.FormatTree(text, cu, textArea.Caret.Line + 1, textArea.Caret.Column + 1);
                bool   success       = true;
                if (success)
                {
                    //WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteAction(VisualPascalABCPlugins.VisualEnvironmentCompilerAction.SetCurrentSourceFileTextFormatting, "");
                    WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteAction(VisualPascalABCPlugins.VisualEnvironmentCompilerAction.SetCurrentSourceFileTextFormatting, formattedText);
                    WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteSourceLocationAction(
                        new PascalABCCompiler.SourceLocation(VisualPABCSingleton.MainForm.CurrentCodeFileDocument.FileName, cf.Line, cf.Column, cf.Line, cf.Column), VisualPascalABCPlugins.SourceLocationAction.GotoBeg);
                }
            }
            else
            {
                WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteAction(VisualPascalABCPlugins.VisualEnvironmentCompilerAction.AddMessageToErrorListWindow, new List <PascalABCCompiler.Errors.Error>(new PascalABCCompiler.Errors.Error[] { Errors[0] }));
            }
        }
        private List <SymbolsViewerSymbol> InternalFindReferences(string fname, PascalABCCompiler.SyntaxTree.expression expr, int line, int col, bool for_refact)
        {
            List <PascalABCCompiler.Parsers.Position> lst = new List <PascalABCCompiler.Parsers.Position>();
            List <SymbolsViewerSymbol> svs_lst            = new List <SymbolsViewerSymbol>();

            try
            {
                CodeCompletion.DomConverter dc = CodeCompletion.CodeCompletionController.comp_modules[fname] as CodeCompletion.DomConverter;
                IBaseScope fnd_scope           = null;
                IBaseScope cur_sc = null;
                if (dc != null)
                {
                    fnd_scope = dc.GetSymDefinition(expr, line, col, keyword);
                    cur_sc    = dc.FindScopeByLocation(line, col);
                }
                if (fnd_scope != null)
                {
                    foreach (string FileName in CodeCompletionParserController.open_files.Keys)
                    {
                        CodeCompletion.CodeCompletionController controller = new CodeCompletion.CodeCompletionController();
                        string text = VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.SourceFilesProvider(FileName, PascalABCCompiler.SourceFileOperation.GetText) as string;
                        PascalABCCompiler.SyntaxTree.compilation_unit cu = controller.ParseOnlySyntaxTree(FileName, text);
                        if (cu != null)
                        {
                            dc = CodeCompletion.CodeCompletionController.comp_modules[FileName] as CodeCompletion.DomConverter;
                            CodeCompletion.ReferenceFinder rf = null;
                            if (dc != null && dc.visitor.entry_scope != null)
                            {
                                rf = new CodeCompletion.ReferenceFinder(fnd_scope, dc.visitor.entry_scope, cu, FileName, for_refact);
                                lst.AddRange(rf.FindPositions());
                            }
                        }
                    }
                    PascalABCCompiler.Parsers.Position p = fnd_scope.GetPosition();
                    bool need_add_def = !for_refact; // true
                    if (p.file_name != null && need_add_def)
                    {
                        svs_lst.Add(new SymbolsViewerSymbol(new PascalABCCompiler.SourceLocation(p.file_name, p.line, p.column, p.end_line, p.end_column), ImagesProvider.GetPictureNum(fnd_scope.SymbolInfo)));
                    }
                    foreach (PascalABCCompiler.Parsers.Position pos in lst)
                    {
                        if (pos.file_name != null)
                        {
                            svs_lst.Add(new SymbolsViewerSymbol(new PascalABCCompiler.SourceLocation(pos.file_name, pos.line, pos.column, pos.end_line, pos.end_column), ImagesProvider.IconNumberGotoText));
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
            GC.Collect();
            return(svs_lst);
            //return svs_lst.ToArray();
        }
Example #3
0
        public static void RenameUnit(string FileName, string new_val)
        {
            if (CodeCompletion.CodeCompletionController.CurrentParser == null)
            {
                return;
            }
            ccp = new CodeCompletionProvider();
            IDocument doc = null;

            CodeCompletion.CodeCompletionController controller = new CodeCompletion.CodeCompletionController();
            string text = WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.SourceFilesProvider(FileName, PascalABCCompiler.SourceFileOperation.GetText) as string;

            PascalABCCompiler.SyntaxTree.compilation_unit cu       = controller.ParseOnlySyntaxTree(FileName, text);
            PascalABCCompiler.SyntaxTree.ident            unitName = null;
            if (cu is PascalABCCompiler.SyntaxTree.unit_module)
            {
                unitName = (cu as PascalABCCompiler.SyntaxTree.unit_module).unit_name.idunit_name;
            }
            else if (cu is PascalABCCompiler.SyntaxTree.program_module)
            {
                if ((cu as PascalABCCompiler.SyntaxTree.program_module).program_name == null)
                {
                    return;
                }
                unitName = (cu as PascalABCCompiler.SyntaxTree.program_module).program_name.prog_name;
            }

            List <SymbolsViewerSymbol> refers = ccp.Rename(unitName.name, unitName.name, FileName, unitName.source_context.begin_position.line_num, unitName.source_context.begin_position.column_num);

            if (refers == null || new_val == null)
            {
                return;
            }
            int addit = 0;

            PascalABCCompiler.SourceLocation tmp = new PascalABCCompiler.SourceLocation(null, 0, 0, 0, 0);
            string file_name = null;

            VisualPABCSingleton.MainForm.StopTimer();
            WorkbenchServiceFactory.CodeCompletionParserController.StopParseThread();
            foreach (IFileInfo fi in ProjectFactory.Instance.CurrentProject.SourceFiles)
            {
                WorkbenchServiceFactory.CodeCompletionParserController.RegisterFileForParsing(fi.Path);
            }
            WorkbenchServiceFactory.CodeCompletionParserController.ParseInThread();
            foreach (SymbolsViewerSymbol svs in refers)
            {
                if (svs.SourceLocation.BeginPosition.Line != tmp.BeginPosition.Line)
                {
                    addit = 0;
                }
                else if (svs.SourceLocation.BeginPosition.Column < tmp.BeginPosition.Column)
                {
                    addit = 0;
                }
                if (svs.SourceLocation.FileName != file_name)
                {
                    CodeFileDocumentControl cfdoc = VisualPABCSingleton.MainForm.FindTab(svs.SourceLocation.FileName);
                    if (cfdoc == null)
                    {
                        continue;
                    }
                    doc       = cfdoc.TextEditor.ActiveTextAreaControl.TextArea.Document;
                    file_name = svs.SourceLocation.FileName;
                }
                tmp = svs.SourceLocation;
                TextLocation tl_beg = new TextLocation(svs.SourceLocation.BeginPosition.Column - 1, svs.SourceLocation.BeginPosition.Line - 1);
                //TextLocation tl_end = new TextLocation(svs.SourceLocation.EndPosition.Line,svs.SourceLocation.EndPosition.Column);
                int offset = doc.PositionToOffset(tl_beg);
                //addit += new_val.Length - unitName.name.Length;
                doc.Replace(offset, unitName.name.Length, new_val);
                doc.CommitUpdate();
            }
            WorkbenchServiceFactory.CodeCompletionParserController.RunParseThread();
            VisualPABCSingleton.MainForm.StartTimer();
        }
        public string BuildCode(string existing_text, string generated_text, EventDescription event_description)
        {
            if (FirstCodeGeneration)
            {
                FirstCodeGeneration = false;
                string form_name = (Designer.Host.RootComponent as Control).Name;
                if (form_name == "Form1")
                {
                }
                string beg = string.Format(string_consts.begin_unit,
                                           Path.GetFileNameWithoutExtension(_file_name), (Designer.Host.RootComponent as Control).Name);
                existing_text   = beg + generated_text + string_consts.end_unit;
                TextEditor.Text = existing_text;
                if (event_description == null)
                {
                    return(existing_text); //beg + generated_text + SampleDesignerHost.string_consts.end_unit;
                }
            }
            //StringBuilder sb = new StringBuilder(existing_text); //TextEditor.Text);
            string[] sep = new string[1] {
                string_consts.nr
            };
            string[] lines = existing_text.Split(sep, StringSplitOptions.None);
            int      count = lines.Length;
            int      s_num = 0;
            string   trimed;

            //int end_region_num;
            while (s_num < count)
            {
                trimed = lines[s_num].TrimStart(' ', '\t');
                if (trimed.StartsWith(string_consts.begin_designer_region, StringComparison.InvariantCultureIgnoreCase))
                {
                    break;
                }
                s_num++;
            }
            if (s_num == count)
            {
                MessageDesignerCodeGenerationFailed();
                return(null);
            }
            int e_num = s_num + 1;

            while (e_num < count)
            {
                trimed = lines[e_num].TrimStart(' ', '\t');
                if (trimed.StartsWith(string_consts.end_designer_region, StringComparison.InvariantCultureIgnoreCase))
                {
                    break;
                }
                e_num++;
            }
            if (e_num == count)
            {
                MessageDesignerCodeGenerationFailed();
                return(null);
            }
            List <PascalABCCompiler.Errors.Error>           Errors   = new List <PascalABCCompiler.Errors.Error>();
            List <PascalABCCompiler.Errors.CompilerWarning> Warnings = new List <PascalABCCompiler.Errors.CompilerWarning>();

            //PascalABCCompiler.SyntaxTree.syntax_tree_node sn =
            //    MainForm.VisualEnvironmentCompiler.Compiler.ParsersController.Compile(
            //    FileName, TextEditor.Text, null, Errors, PascalABCCompiler.Parsers.ParseMode.Normal);
            PascalABCCompiler.SyntaxTree.compilation_unit sn =
                CodeCompletion.CodeCompletionController.ParsersController.GetCompilationUnit(
                    VisualPABCSingleton.MainForm._currentCodeFileDocument.FileName,
                    existing_text, //VisualPascalABC.Form1.Form1_object._currentCodeFileDocument.TextEditor.Text,
                    Errors,
                    Warnings);
            PascalABCCompiler.SyntaxTree.unit_module um = sn as PascalABCCompiler.SyntaxTree.unit_module;
            bool good_syntax = um != null;

            PascalABCCompiler.SyntaxTree.type_declaration form_decl = null;
            if (good_syntax)
            {
                good_syntax = um.implementation_part != null &&
                              um.interface_part != null &&
                              um.interface_part.interface_definitions != null &&
                              um.interface_part.interface_definitions.defs != null &&
                              um.interface_part.interface_definitions.defs.Count > 0;
            }
            if (good_syntax)
            {
                foreach (PascalABCCompiler.SyntaxTree.declaration decl in um.interface_part.interface_definitions.defs)
                {
                    PascalABCCompiler.SyntaxTree.type_declarations tdecls = decl as PascalABCCompiler.SyntaxTree.type_declarations;
                    if (tdecls != null)
                    {
                        foreach (PascalABCCompiler.SyntaxTree.type_declaration tdecl in tdecls.types_decl)
                        {
                            if (tdecl.source_context.begin_position.line_num - 1 < s_num &&
                                tdecl.source_context.end_position.line_num - 1 > e_num)
                            {
                                form_decl = tdecl;
                            }
                        }
                    }
                }
            }
            PascalABCCompiler.SyntaxTree.class_definition form_def = null;
            if (form_decl != null)
            {
                form_def = form_decl.type_def as PascalABCCompiler.SyntaxTree.class_definition;
            }
            if (form_decl == null || form_def == null || form_def.body == null)
            {
                MessageBox.Show(PascalABCCompiler.StringResources.Get("VP_MF_CODE_GENERATION_UNSUCCEFULL"),
                                PascalABCCompiler.StringResources.Get("VP_MF_FORM_DESIGNER"),
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(null);
            }
            else
            {
                string old_form_name           = form_decl.type_name.name;
                string new_form_name           = (Designer.Host.RootComponent as Control).Name;
                bool   implementation_not_null =
                    um.implementation_part.implementation_definitions != null &&
                    um.implementation_part.implementation_definitions.defs != null &&
                    um.implementation_part.implementation_definitions.defs.Count > 0;
                if (new_form_name != old_form_name)
                {
                    ReplaceName(form_decl.type_name, new_form_name, lines);
                    if (implementation_not_null)
                    {
                        foreach (PascalABCCompiler.SyntaxTree.declaration decl in
                                 um.implementation_part.implementation_definitions.defs)
                        {
                            PascalABCCompiler.SyntaxTree.procedure_definition pd = decl as PascalABCCompiler.SyntaxTree.procedure_definition;
                            if (pd != null)
                            {
                                if (pd.proc_header.name.class_name != null &&
                                    string.Compare(pd.proc_header.name.class_name.name, old_form_name, true) == 0)
                                {
                                    ReplaceName(pd.proc_header.name.class_name, new_form_name, lines);
                                }
                            }
                        }
                    }
                }
                if (event_description != null)
                {
                    MethodInfo mi = event_description.e.EventType.GetMethod(
                        PascalABCCompiler.TreeConverter.compiler_string_consts.invoke_method_name);
                    ParameterInfo[] pinfos        = mi.GetParameters();
                    bool            handler_found = false;

                    event_description.editor = TextEditor;
                    System.Text.RegularExpressions.MatchCollection matches =
                        System.Text.RegularExpressions.Regex.Matches(generated_text, string_consts.nr);
                    //строка, на которой последнее описание из секции реализаций
                    PascalABCCompiler.SyntaxTree.file_position last_defs_pos = null;
                    if (implementation_not_null)
                    {
                        last_defs_pos = um.implementation_part.implementation_definitions.defs[
                            um.implementation_part.implementation_definitions.defs.Count - 1
                                        ].source_context.end_position;

                        //Ищем описание обработчика
                        foreach (PascalABCCompiler.SyntaxTree.declaration decl in
                                 um.implementation_part.implementation_definitions.defs)
                        {
                            PascalABCCompiler.SyntaxTree.procedure_definition pd = decl as PascalABCCompiler.SyntaxTree.procedure_definition;
                            if (pd == null)
                            {
                                continue;
                            }
                            if (pd.proc_header.name == null || pd.proc_header.name.class_name == null ||              //roman//
                                String.Compare(pd.proc_header.name.class_name.name, new_form_name, true) != 0 ||
                                String.Compare(pd.proc_header.name.meth_name.name, event_description.EventName, true) != 0)
                            {
                                continue;
                            }
                            List <PascalABCCompiler.SyntaxTree.typed_parameters> syn_pars =
                                pd.proc_header.parameters.params_list;
                            bool should_continue = false;
                            int  par_count       = syn_pars.Count;
                            if (par_count != pinfos.Length)
                            {
                                continue;
                            }
                            for (int i = 0; i < par_count; ++i)
                            {
                                if (syn_pars[i].idents.idents.Count != 1)
                                {
                                    should_continue = true;
                                    break;
                                }
                                PascalABCCompiler.SyntaxTree.named_type_reference ntr =
                                    syn_pars[i].vars_type as PascalABCCompiler.SyntaxTree.named_type_reference;
                                if (ntr == null || syn_pars[i].param_kind != PascalABCCompiler.SyntaxTree.parametr_kind.none)
                                {
                                    should_continue = true;
                                    break;
                                }
                                string syn_name = BuildName(ntr.names);
                                if (String.Compare(syn_name, pinfos[i].ParameterType.Name) != 0 &&
                                    String.Compare(syn_name, pinfos[i].ParameterType.FullName) != 0)
                                {
                                    should_continue = true;
                                    break;
                                }
                            }
                            if (should_continue)
                            {
                                continue;
                            }
                            handler_found = true;
                            event_description.line_num = pd.proc_body.source_context.begin_position.line_num +
                                                         matches.Count + s_num - e_num + 2;
                            //last_defs_pos.line_num + s_num - e_num + matches.Count + 7;
                            event_description.column_num = pd.proc_body.source_context.begin_position.column_num;
                        }
                    }
                    else
                    {
                        last_defs_pos = um.implementation_part.source_context.end_position;
                    }
                    if (!handler_found)
                    {
                        string new_event = event_description.EventName;
                        if (pinfos.Length != 0)
                        {
                            new_event += "(";
                            new_event += pinfos[0].Name + ": " + pinfos[0].ParameterType.FullName.Replace("System.Windows.Forms.", "").Replace("System.", "");
                            for (int i = 1; i < pinfos.Length; ++i)
                            {
                                new_event += "; ";
                                new_event += pinfos[i].Name + ": " + pinfos[i].ParameterType.FullName.Replace("System.Windows.Forms.", "").Replace("System.", "");
                            }
                            new_event += ")";
                        }
                        new_event += ";";
                        string new_event_header = new_event;
                        new_event  = string_consts.nr + string_consts.nr + "procedure " + new_form_name + "." + new_event;
                        new_event += string_consts.nr + "begin" +
                                     string_consts.nr + "  " + string_consts.nr +
                                     "end;";
                        event_description.column_num      = 3;
                        event_description.line_num        = last_defs_pos.line_num + s_num - e_num + matches.Count + 7;
                        lines[last_defs_pos.line_num - 1] = lines[last_defs_pos.line_num - 1].Insert(last_defs_pos.column_num, new_event);
                        //Добавляем заголовок события
                        //int last_form_member_line = form_def.body.class_def_blocks[form_def.body.class_def_blocks.Count - 1].source_context.end_position.line_num - 1;
                        lines[s_num] = string_consts.event_handler_header_trim +
                                       "procedure " + new_event_header +
                                       string_consts.nr + lines[s_num];
                    }
                }
            }
            //generated_text = SampleDesignerHost.string_consts.tab + "private" +
            //    SampleDesignerHost.string_consts.nr + generated_text;
            string s1 = string.Join(string_consts.nr, lines, 0, s_num + 1);
            string s2 = string.Join(string_consts.nr, lines, e_num, lines.Length - e_num);

            return(s1 + string_consts.nr + string_consts.tab +
                   "internal" + string_consts.nr +
                   string_consts.tab2 + generated_text + s2);
        }