예제 #1
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void Execute(object sender, EventArgs _)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // open the file in a VS code window and activate the pane
            DTE      dte = Package.GetGlobalService(typeof(DTE)) as DTE;
            Document doc = dte?.ActiveDocument;

            TextDocument txt = doc?.Object() as TextDocument;

            if (dte == null || doc == null || txt == null)
            {
                SystemSounds.Beep.Play();
                return;
            }

            TextSelection textSelection = doc.ActiveWindow.Document.Selection as TextSelection;
            var           cursor        = textSelection.ActivePoint as VirtualPoint;
            var           index         = cursor.AbsoluteCharOffset;

            try {
                var text = txt.GetText().Replace("\r", "");
                foreach (var frame in RegenEngine.CompileFrame(text).Reverse())
                {
                    frame.ApplyChanges(ref text);
                }

                var ed = txt.CreateEditPoint(txt.StartPoint);
                ed.Delete(txt.EndPoint);
                ed.Insert(text);
                textSelection.MoveToAbsoluteOffset(index);
            } catch (Exception e) {
                Logger.Log(e);
#if DEBUG
                Message($"Failed parsing file...\n" + e);
#else
                Message($"Failed parsing file...\n" + e.Message + "\n" + e.InnerException?.Message);
#endif
            }

            // now set the cursor to the beginning of the function
            //textSelection.MoveToPoint(function.StartPoint);
            void Message(string msg)
            {
                // Show a message box to prove we were here
                VsShellUtilities.ShowMessageBox(
                    this.package,
                    msg, //$"index: {pt.AbsoluteCharOffset}, path: {doc.FullName}\n+{textSelection.Text.Length}",
                    "Regen",
                    OLEMSGICON.OLEMSGICON_INFO,
                    OLEMSGBUTTON.OLEMSGBUTTON_OK,
                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
            }
        }
예제 #2
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void Execute(object sender, EventArgs _)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // open the file in a VS code window and activate the pane
            DTE      dte = Package.GetGlobalService(typeof(DTE)) as DTE;
            Document doc = dte?.ActiveDocument;

            TextDocument templateDoc = doc?.Object() as TextDocument;

            if (dte == null || doc == null || templateDoc == null)
            {
                SystemSounds.Beep.Play();
                return;
            }

            var templatePath    = doc.FullName;
            var templateContent = templateDoc.GetText().Replace("\r", "");

            //compile outputs
            var outputs = RegenFileTemplateEngine.Compile(templateContent, templatePath);

            //find out if these files exist
            var projitem = doc.ProjectItem.ContainingProject;

            Logger.Log(projitem.FullName);
            Logger.Log(projitem.FileName);
            var solution = Package.GetGlobalService(typeof(IVsSolution)) as IVsSolution;
            var projects = GetProjects(solution);

            Project            targetProject = null;
            List <ProjectItem> targetItems   = null;

            //get current project and its files
            foreach (Project project in projects)
            {
                if (!projitem.FullName.Equals(project.FullName))
                {
                    continue;
                }
                targetProject = project;
                Logger.Log(project.FullName);
                targetItems = new List <ProjectItem>(project.ProjectItems.Cast <ProjectItem>());
                foreach (ProjectItem projectItem in targetItems)
                {
                    Logger.Log("    " + projectItem.Name);
                }

                break;
            }

            if (targetProject == null || targetItems == null)
            {
                throw new Exception("ERR #0010");
            }

            //every output either write file or replace contents.
            foreach ((string path, string content) in outputs)
            {
                //we compile regen frames
                var output = RegenEngine.Compile(content);

                //we add the compiled file to solution
                Directory.CreateDirectory(Path.GetDirectoryName(path));
                File.WriteAllText(path, output, Encoding.UTF8);
                AddFile(targetProject, path);
            }

            //now after we successfully outputted these files
            //we need to compile the files



            // now set the cursor to the beginning of the function
            //textSelection.MoveToPoint(function.StartPoint);
            void Message(string msg)
            {
                // Show a message box to prove we were here
                VsShellUtilities.ShowMessageBox(
                    this.package,
                    msg, //$"index: {pt.AbsoluteCharOffset}, path: {doc.FullName}\n+{textSelection.Text.Length}",
                    "Regen",
                    OLEMSGICON.OLEMSGICON_INFO,
                    OLEMSGBUTTON.OLEMSGBUTTON_OK,
                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
            }
        }