Ejemplo n.º 1
0
        public static async Task <IWpfTextView> TypeText(string extension, string keystrokes)
        {
            DTE.ItemOperations.NewFile(Name: Guid.NewGuid() + "." + extension.TrimStart('.'));
            await TypeString(keystrokes);

            return(ProjectHelpers.GetCurentTextView());
        }
Ejemplo n.º 2
0
        async void IService.HelloWorld()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); //воротаемся в UI поток.


            vproject = ProjectHelpers.GetActiveProject();
            string rootfolder  = vproject.GetRootFolder(); //корневая папка проекта с csproj открытым в студии
            string vsixdllpath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "");
            string vsixfolder  = Path.GetDirectoryName(vsixdllpath);

            string filename     = "tmp1.cs";
            string path2addfile = Path.Combine(vsixfolder, "Templates", filename);
            var    filetoadd    = new FileInfo(path2addfile);

            ProjectItem projectItem  = null;
            string      fileprojpath = Path.Combine(rootfolder, filename);

            var filedest = new FileInfo(Path.Combine(fileprojpath));

            if (1 == 1)
            {
                if (1 == 1) //добавить файл в проект из VSIX папки
                {
                    ProjectHelpers.CopyTmpToProjectFile(vproject, filetoadd.FullName, filedest.FullName);
                }
                projectItem = vproject.AddFileToProject(filedest);

                vproject.DTE.ItemOperations.OpenFile(fileprojpath); //открыть редактор с новым файлом

                if (true)                                           //добавить текст
                {
                    Microsoft.VisualStudio.Text.Editor.IWpfTextView view = ProjectHelpers.GetCurentTextView();

                    if (view != null)
                    {
                        try
                        {
                            ITextEdit edit = view.TextBuffer.CreateEdit();
                            edit.Insert(0, Environment.NewLine);
                            edit.Apply();
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }
                if (true) //добавление абсолютной ссылки в проект
                {
                    VSProject refproject;
                    refproject = (VSProject)vproject.Object;
                    try
                    {
                        refproject.References.Add(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\Microsoft.Build.dll");
                    }
                    catch (Exception ddd)
                    {
                    }
                }
            }
        }
        private async System.Threading.Tasks.Task CreateDtoFile(string inputCamel, string folder,
                                                                object selectedItem,
                                                                ProjectItem dtoProject,
                                                                Project project,
                                                                TemplateType templateType)
        {
            var file = CreateFileInfo(inputCamel, folder, templateType);

            PackageUtilities.EnsureOutputPath(folder);

            if (!file.Exists)
            {
                int position = await WriteFileAsync(project, file.FullName, inputCamel, templateType);

                try
                {
                    ProjectItem projectItem = null;
                    if (selectedItem is ProjectItem projItem)
                    {
                        if ("{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}" == projItem.Kind) // Constants.vsProjectItemKindVirtualFolder
                        {
                            projectItem = projItem.ProjectItems.AddFromFile(file.FullName);
                        }
                    }

                    if (projectItem == null)
                    {
                        projectItem = dtoProject.ProjectItems.AddFromFile(file.FullName);
                    }

                    if (file.FullName.EndsWith("__dummy__"))
                    {
                        projectItem?.Delete();
                        return;
                    }

                    VsShellUtilities.OpenDocument(this, file.FullName);

                    // Move cursor into position
                    if (position > 0)
                    {
                        Microsoft.VisualStudio.Text.Editor.IWpfTextView view = ProjectHelpers.GetCurentTextView();

                        view?.Caret.MoveTo(new SnapshotPoint(view.TextBuffer.CurrentSnapshot, position));
                    }

                    _dte.ExecuteCommand("SolutionExplorer.SyncWithActiveDocument");
                    _dte.ActiveDocument.Activate();
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("The file '" + file + "' already exist.");
            }
        }
Ejemplo n.º 4
0
        private async void MenuItemCallback(object sender, EventArgs e)
        {
            string currentFilePath = GetCurrentFilePath();

            if (string.IsNullOrEmpty(currentFilePath) || !File.Exists(currentFilePath))
            {
                return;
            }

            Project project = ProjectHelpers.GetActiveProject();

            if (project == null)
            {
                return;
            }

            var currentFileRelativePathFromCurrentProject = GetPathFromProjectFolder(project, currentFilePath);
            var testProjectData = FindMatchingTestProject(project);
            var testProject     = GetTestProject(testProjectData);

            string file = Path.Combine(Path.GetDirectoryName(testProject.FullName) + testProjectData.Path) + currentFileRelativePathFromCurrentProject;
            string dir  = Path.GetDirectoryName(file);

            PackageUtilities.EnsureOutputPath(dir);

            if (!File.Exists(file))
            {
                int position = await WriteFile(testProject, file);

                try
                {
                    testProject.AddFileToProject(file);
                    var window = (Window2)_dte.ItemOperations.OpenFile(file);

                    // Move cursor into position
                    if (position > 0)
                    {
                        var view = ProjectHelpers.GetCurentTextView();

                        if (view != null)
                        {
                            view.Caret.MoveTo(new SnapshotPoint(view.TextBuffer.CurrentSnapshot, position));
                        }
                    }

                    _dte.ExecuteCommand("SolutionExplorer.SyncWithActiveDocument");
                    _dte.ActiveDocument.Activate();
                    _dte.ExecuteCommand("File.SaveAll");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show($"Don't worry KAPARA, already got tests for this file ({0})");
            }
        }
Ejemplo n.º 5
0
        public void NavigateTo()
        {
            WebEssentialsPackage.DTE.ItemOperations.OpenFile(_file);

            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                var view       = ProjectHelpers.GetCurentTextView();
                var textBuffer = ProjectHelpers.GetCurentTextBuffer();
                var span       = new SnapshotSpan(textBuffer.CurrentSnapshot, _selector.Start, _selector.Length);
                var point      = new SnapshotPoint(textBuffer.CurrentSnapshot, _selector.Start + _selector.Length);

                view.ViewScroller.EnsureSpanVisible(span);
                view.Caret.MoveTo(point);
                view.Selection.Select(span, false);
            }), DispatcherPriority.ApplicationIdle, null);
        }
 private void Dispatch(string file, int positionInFile)
 {
     Dispatcher.BeginInvoke(new Action(() =>
     {
         try
         {
             IWpfTextView view;
             view = (file != Document.FilePath) ? ProjectHelpers.GetCurentTextView() : SourceTextView;
             ITextSnapshot snapshot = view.TextBuffer.CurrentSnapshot;
             view.Caret.MoveTo(new SnapshotPoint(snapshot, positionInFile));
             view.ViewScroller.EnsureSpanVisible(new SnapshotSpan(snapshot, positionInFile, 1), EnsureSpanVisibleOptions.AlwaysCenter);
         }
         catch
         { }
     }), DispatcherPriority.ApplicationIdle, null);
 }
Ejemplo n.º 7
0
        protected override bool Execute(VSConstants.VSStd97CmdID commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (!string.IsNullOrEmpty(_path))
            {
                _path = _path.TrimStart('~').Trim();
                string absolute = ProjectHelpers.ToAbsoluteFilePathFromActiveFile(_path);

                if (File.Exists(absolute))
                {
                    FileHelpers.OpenFileInPreviewTab(absolute);
                    return(true);
                }

                EditorExtensionsPackage.DTE.StatusBar.Text = "Couldn't find " + _path;
            }
            else if (!string.IsNullOrEmpty(_className))
            {
                int    position;
                string file = FindFile(new[] { ".less", ".scss", ".css" }, out position);

                if (!string.IsNullOrEmpty(file))
                {
                    FileHelpers.OpenFileInPreviewTab(file);

                    Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                    {
                        try
                        {
                            IWpfTextView view      = ProjectHelpers.GetCurentTextView();
                            ITextSnapshot snapshot = view.TextBuffer.CurrentSnapshot;
                            view.Caret.MoveTo(new SnapshotPoint(snapshot, position));
                            view.ViewScroller.EnsureSpanVisible(new SnapshotSpan(snapshot, position, 1), EnsureSpanVisibleOptions.AlwaysCenter);
                        }
                        catch
                        { }
                    }), DispatcherPriority.ApplicationIdle, null);

                    return(true);
                }

                EditorExtensionsPackage.DTE.StatusBar.Text = "Couldn't find " + _className;
            }

            return(false);
        }