Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var handle = GetConsoleWindow();

            try
            {
                ShowWindow(handle, SW_HIDE);
                if (args.Length == 2)
                {
                    args[0] = args[0].Replace('*', ' ');
                    //MessageBox.Show(String.Format("Openinig {0}", args[0]), "VS", MessageBoxButtons.OK);
                    dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.16.0");
                    Window window = dte2.ItemOperations.OpenFile(args[0]);
                    if (window != null)
                    {
                        //dte2.ActiveDocument.Selection
                        //TextSelection.MoveToLineAndOffset(Line, 0, False)
                        int Line = 1;
                        Int32.TryParse(args[1], out Line);
                        if (Line == 0)
                        {
                            Line = 1;
                        }
                        TextSelection sel = window.Document.Selection;
                        try
                        {
                            sel.GotoLine(Line);
                        }
                        catch (Exception)
                        {
                            sel.GotoLine(1);
                        }
                        dte2.Application.MainWindow.Activate();
                    }
                    else
                    {
                        throw (new Exception(String.Format("Could not open {0}", args[0])));
                    }
                }
                else
                {
                    throw (new Exception(String.Format("Argument count is not 2 but {0}", args.Length)));
                }
            }
            catch (Exception e)
            {
                ShowWindow(handle, SW_SHOW);
                //Console.WriteLine(e.ToString());
                string msg = e.ToString();
                msg += "\nargs:";
                foreach (string s in args)
                {
                    msg += "\n";
                    msg += s;
                }
                MessageBox.Show(msg, "VS", MessageBoxButtons.OK);
            }
        }
Ejemplo n.º 2
0
        public void Execute(TextSelection textSelection, Func <string, string> seletionCallback)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            textSelection.GotoLine(1, true);
            textSelection.SelectAll();
            var contents   = textSelection.Text;
            var changedTxt = seletionCallback.Invoke(contents);

            textSelection.Insert(changedTxt);
            textSelection.SmartFormat();
            textSelection.GotoLine(1, false);
        }
        /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
        /// <param term='commandName'>The name of the command to execute.</param>
        /// <param term='executeOption'>Describes how the command should be run.</param>
        /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
        /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
        /// <param term='handled'>Informs the caller if the command was handled or not.</param>
        /// <seealso class='Exec' />
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName == "FormatVariableDefine.Connect.FormatVariableDefine" ||
                    commandName == "FormatVariableDefine.Connect.FormatVariableDefineRightClick")
                {
                    TextSelection select         = ((TextSelection)_applicationObject.ActiveDocument.Selection);
                    int           nTopLine       = select.TopLine;
                    int           nBottomLine    = select.BottomLine;
                    bool          bLastLineEmpty = select.BottomPoint.AtStartOfLine;
                    select.GotoLine(nTopLine, true);
                    select.LineDown(true, nBottomLine - nTopLine);
                    select.EndOfLine(true);

                    if (bLastLineEmpty)
                    {
                        select.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, true);
                    }
                    string selectedCode = select.Text;
                    string outCode      = CodeSmart.AlignText(selectedCode);       //对齐选中文本
                    select.Insert(outCode, (int)vsInsertFlags.vsInsertFlagsCollapseToEnd);
                    handled = true;
                    return;
                }
            }
        }
Ejemplo n.º 4
0
        private void OnPolicyFailureActivation(CodeDocumentationPolicyFailure policyFailure)
        {
            _DTE ide = (_DTE)PendingCheckin.GetService(typeof(_DTE));

            if (ide == null)
            {
                return;
            }

            Document document = ide.Documents.Open(policyFailure.Violation.Filepath);

            if (document == null)
            {
                return;
            }

            TextSelection selection = (TextSelection)document.Selection;

            if (selection == null)
            {
                return;
            }

            selection.GotoLine(policyFailure.Violation.Line, true);
        }
Ejemplo n.º 5
0
        public static void GoToLine(DTE dte, String file, int line)
        {
            Window        win       = dte.ItemOperations.OpenFile(file, EnvDTE.Constants.vsViewKindTextView);
            TextSelection selection = dte.ActiveDocument.Selection as TextSelection;

            selection.GotoLine(line, true);
        }
Ejemplo n.º 6
0
        public static void OpenWithVisualStudio(String file, int lineNumber)
        {
            DTE vsDTE = null;

            try
            {
                //check for running instance
                vsDTE = (DTE)Marshal.GetActiveObject("VisualStudio.DTE");
            }
            catch
            {
                //no running instance, create one
                Type DTEType = Type.GetTypeFromProgID("VisualStudio.DTE");
                vsDTE = (DTE)Activator.CreateInstance(DTEType);
            }

            //make VS visible and not close when the application closes
            vsDTE.MainWindow.Visible = true;
            vsDTE.UserControl        = true;

            //open file and select line
            Window        window    = vsDTE.ItemOperations.OpenFile(file, Constants.vsViewKindTextView);
            TextSelection selection = (TextSelection)vsDTE.ActiveDocument.Selection;

            selection.GotoLine(lineNumber, true);
        }
Ejemplo n.º 7
0
 private void NavigateToLine(TextSelection textSelector, int lineNumber)
 {
     if (textSelector != null && lineNumber > 0)
     {
         textSelector.GotoLine(lineNumber);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Highlights all characters in line.
        /// </summary>
        /// <param name="lineNumber">Line number</param>
        public void HighlightLineInActiveDocument(int lineNumber)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
            TextSelection sel = (TextSelection)_dte.ActiveDocument.Selection;

            sel.GotoLine(lineNumber, true);
        }
        /// <summary>
        /// Move cursor to the <paramref name="line"/> number of the document window.
        /// </summary>
        /// <param name="window"><seealso cref="Window"/></param>
        /// <param name="line">The line number of the source file.</param>
        private static void GotoLine(Window window, int line)
        {
            window.Visible = true;
            TextSelection selection = window.Document.Selection as TextSelection;
            TextPoint     tp        = selection.TopPoint;

            selection.GotoLine(line, Select: false);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Goes to specified line in the text editor and jumps to particular word in that line.
 /// </summary>
 public void GotoLine(int line, int word)
 {
     selection.GotoLine(line, false);
     if (word > 0)
     {
         selection.WordRight(false, word);
     }
 }
        private Boolean IsCommentEnd(ref TextSelection selection, int lineIndex)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
            selection.GotoLine(lineIndex, true);
            String text = selection.Text;

            return(text.Contains(DocumentTag.CommentBaseEnd.JavaDocTag));
        }
Ejemplo n.º 12
0
        public Window GotoLine(int line)
        {
            Open();
            TextSelection selection = _window.Document.Selection as TextSelection;
            TextPoint     tp        = selection.TopPoint;

            selection.GotoLine(line, Select: false);
            return(_window);
        }
        /// <summary>
        /// Move cursor to the <paramref name="line"/> number of the document window.
        /// </summary>
        /// <param name="window"><seealso cref="Window"/></param>
        /// <param name="line">The line number of the source file.</param>
        private static void GotoLine(Window window, int line)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            window.Visible = true;
            TextSelection selection = window.Document.Selection as TextSelection;
            TextPoint     tp        = selection.TopPoint;

            selection.GotoLine(line, Select: false);
        }
Ejemplo n.º 14
0
        private void btnGenerateScript_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (_bismNormalizerPackage.Dte != null)
                {
                    _bismNormalizerPackage.Dte.StatusBar.Text = "BISM Normalizer - creating script ...";
                }

                string   script = _comparison.ScriptDatabase(); //doing this here in case errors before opening file in VS
                Document file   = NewXmlaFile(_comparison.CompatibilityLevel >= 1200, (_comparisonInfo.ConnectionInfoTarget.UseProject ? _comparisonInfo.ConnectionInfoTarget.ProjectName : _comparisonInfo.ConnectionInfoTarget.DatabaseName));
                if (file != null)
                {
                    TextSelection selection = (TextSelection)file.Selection;
                    selection.SelectAll();
                    selection.Insert(script);
                    selection.GotoLine(1);

                    return;
                }

                //If we get here, there was a problem generating the xmla file (maybe file item templates not installed), so offer saving to a file instead
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                if (_comparison.CompatibilityLevel >= 1200)
                {
                    saveFile.Filter = "JSON Files|*.json|Text Files|*.txt|All files|*.*";
                }
                else
                {
                    saveFile.Filter = "XMLA Files|*.xmla|Text Files|*.txt|All files|*.*";
                }
                saveFile.CheckFileExists = false;
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    File.WriteAllText(saveFile.FileName, _comparison.ScriptDatabase());

                    if (_bismNormalizerPackage.Dte != null)
                    {
                        _bismNormalizerPackage.Dte.StatusBar.Text = "BISM Normalizer - finished generating script";
                    }
                    MessageBox.Show("Created script\n" + saveFile.FileName, _bismNormalizerCaption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, _bismNormalizerCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                SetNotComparedState();
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                _bismNormalizerPackage.Dte.StatusBar.Text = "";
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Handles the project item.
        /// </summary>
        /// <param name="projectItem">The project item.</param>
        /// <param name="review">The review.</param>
        private void HandleProjectItem(ProjectItem projectItem, Review review)
        {
            VisualStudioInstance.ItemOperations.OpenFile(projectItem.get_FileNames(0), Constants.vsViewKindPrimary);
            Document document = projectItem.Document;

            document.Activate();
            TextSelection textSelection = (TextSelection)VisualStudioInstance.ActiveDocument.Selection;

            textSelection.GotoLine(review.Line, false);
            textSelection.SetBookmark();
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Navigates to the specific line in the specific file
        /// </summary>
        public void NavigateToItem()
        {
            // First open the file
            applicationObject.ExecuteCommand("File.OpenFile", String.Format("\"{0}\"", FullFilename));

            // Then get a reference to the active document
            TextSelection objSel = (TextSelection)applicationObject.ActiveDocument.Selection;

            // Finally, jump to the line and highlight it
            objSel.GotoLine(Line, true);
        }
Ejemplo n.º 17
0
        public void GotoError(Violation violation)
        {
            _DTE dte = (_DTE)this.Provider.GetService(typeof(_DTE));

            Window window = dte.OpenFile(EnvDTE.Constants.vsViewKindCode, violation.name);

            window.Activate();

            TextSelection t = window.Document.Selection as TextSelection;

            t.GotoLine(violation.startPosition.line, false);
        }
Ejemplo n.º 18
0
        public IList Read(long readCount)
        {
            var lines = new ArrayList();

            while (0 <= --readCount && !_selection.AnchorPoint.AtEndOfDocument)
            {
                _selection.GotoLine(++_lineIndex, true);
                var text = _selection.Text;
                lines.Add(text);
            }
            return(lines);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Move to the violation in the ide.
        /// </summary>
        /// <param name="violation">The violation to move to.</param>
        internal static void MoveToViolation(DTE dte, Violation violation)
        {
            // Open the file of the violation
            Window window = dte.OpenFile(EnvDTE.Constants.vsViewKindCode, violation.SourceCode.Path);

            window.Activate();

            // Set the cursor to the right position.
            TextSelection t = window.Document.Selection as TextSelection;

            t.GotoLine(violation.Line, false);
        }
Ejemplo n.º 20
0
        /// <summary>
        ///     Raises the <see cref="Navigate" /> event.
        /// </summary>
        /// <param name="e">An <see cref="System.EventArgs" /> containing event data.</param>
        protected override void OnNavigate(EventArgs e)
        {
            _DTE dte = (_DTE)this.Provider.GetService(typeof(_DTE));

            Window window = dte.OpenFile(Constants.vsViewKindCode, this.Violation.name);

            window.Activate();

            TextSelection t = window.Document.Selection as TextSelection;

            t.GotoLine(this.Violation.startPosition.line, false);

            base.OnNavigate(e);
        }
Ejemplo n.º 21
0
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataRow dataRow = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;

            if (dataRow["File"] != DBNull.Value)
            {
                Close();
                DTE    dte      = (DTE)Package.GetGlobalService(typeof(DTE));
                String fileName = (String)dataRow["File"];
                dte.ItemOperations.OpenFile(fileName);
                TextSelection selection = (TextSelection)dte.ActiveDocument.Selection;
                selection.GotoLine(Convert.ToInt16(dataRow["Row"]), true);
            }
        }
Ejemplo n.º 22
0
        void goToPage(object[] param)
        {
            if (param == null || param.Length != 3 || param[0].GetType() != typeof(string) || param[1].GetType() != typeof(int) || param[2].GetType() != typeof(int))
            {
                return;
            }
            m_applicationObject.ItemOperations.OpenFile((string)param[0]);
            TextSelection ts = m_applicationObject.ActiveDocument.Selection as TextSelection;

            if (ts != null)
            {
                ts.GotoLine((int)param[1]);
            }
        }
Ejemplo n.º 23
0
        private void NavigateTo(object iSender, EventArgs iArgs)
        {
            Task task = (Task)iSender;

            if (task == null)
            {
                return;
            }

            Window        openedFile    = mApplication.ItemOperations.OpenFile(task.Document, EnvDTE.Constants.vsViewKindCode);
            TextSelection textSelection = (TextSelection)mApplication.ActiveDocument.Selection;

            textSelection.GotoLine(task.Line + 1, false);
        }
Ejemplo n.º 24
0
        internal void CreateFile(string fileName, string content)
        {
            DTE dte = (DTE)GetService(typeof(DTE));

            dte.ItemOperations.NewFile("General\\Text File", fileName);

            TextSelection textSel = (TextSelection)dte.ActiveDocument.Selection;
            TextDocument  textDoc = (TextDocument)dte.ActiveDocument.Object();

            textSel.SelectAll();
            textSel.Delete();
            textSel.Insert(content);

            textSel.GotoLine(1);
        }
        /// <summary>
        /// Opens the file specified in the ide specifed and navigates to the linenumber
        /// </summary>
        /// <param name="dte">The DTE.</param>
        /// <param name="fileToOpen">The file to open.</param>
        /// <param name="lineNumber">The line number.</param>
        private void OpenFileAndNavigateToLocation(_DTE dte, string fileToOpen, int lineNumber)
        {
            dte.MainWindow.Visible = true;
            dte.UserControl        = true;
            //Open file and select given line
            Window        win       = dte.ItemOperations.OpenFile(fileToOpen, "{7651A703-06E5-11D1-8EBD-00A0C90F26EA}");
            TextSelection selection = dte.ActiveDocument.Selection as TextSelection;

            selection.GotoLine(lineNumber, false);
            dte.MainWindow.Activate();
            if (GetForegroundWindow().ToInt32() != win.HWnd)
            {
                SwitchToThisWindow(new IntPtr(win.HWnd), false);
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Gets full text string from TextSelection.
        /// </summary>
        /// <param name="selection"></param>
        /// <returns></returns>
        public static string GetText(this TextSelection selection)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
            TextPoint pnt = selection.ActivePoint;
            int       oldLineCharBeginOffset = pnt.LineCharOffset;
            int       oldLineCharEndOffset   = selection.AnchorPoint.LineCharOffset;

            selection.GotoLine(pnt.Line, true);
            try
            {
                return(selection.Text);
            }
            finally
            {
                RedoMarker(pnt, selection, oldLineCharBeginOffset, oldLineCharEndOffset);
            }
        }
Ejemplo n.º 27
0
        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count > 0 && null != this._applicationObject)
            {
                ListViewItem item = this.listView.SelectedItems[0];
                string       file = item.SubItems[6].Text;

                int    lineNumber = (int)item.Tag;
                Window win        =
                    this._applicationObject.DTE.OpenFile(EnvDTE.Constants.vsViewKindCode, file);

                win.SetFocus();

                DTE           dte       = this._applicationObject.DTE;
                TextSelection selection = (TextSelection)dte.ActiveDocument.Selection;
                selection.GotoLine(lineNumber, true);
            }
        }
Ejemplo n.º 28
0
        public static void ExtendToFullLine(this TextSelection selection)
        {
            var line1 = selection.TopLine;
            var line2 = selection.BottomLine;

            if (selection.IsEmpty || !selection.BottomPoint.AtStartOfLine)
            {
                line2 += 1;
            }

            if (line2 > selection.Parent.EndPoint.Line)
            {
                selection.Parent.EndPoint.CreateEditPoint().Insert("\r\n");
            }

            selection.GotoLine(line1);
            selection.MoveToLineAndOffset(line2, 1, true);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Show the editor with specified file and select specified line.
        /// The file must be opened.
        /// </summary>
        /// <param name="applicationObject">The application objectname of the addin.</param>
        /// <param name="filename">The line to show.</param>
        /// <param name="lineNumber">The file name.</param>
        /// <param name="selectLine">Select the line.</param>
        public static void ShowLine(DTE2 applicationObject, string filename, int lineNumber, bool selectLine)
        {
            if (lineNumber > 0)
            {
                try
                {
                    // Retrieve the document
                    Document document = applicationObject.Documents.Item(filename);

                    // Select the node's line
                    TextSelection selection = (TextSelection)document.Selection;
                    selection.GotoLine(lineNumber, selectLine);
                }
                catch (ArgumentException)
                {
                    // Do nothing
                }
            }
        }
Ejemplo n.º 30
0
        bool TryToMoveTo(string fileName,int line,string searchToken)
        {
            if (!File.Exists(fileName))
            {
                return(false);
            }

            try
            {
                OpenFile(fileName);
                TextSelection ts      = m_dte.ActiveDocument.Selection as TextSelection;
                TextDocument  textDoc = ts.Parent;
                if (ts != null && textDoc != null && line >= textDoc.StartPoint.Line && line <= textDoc.EndPoint.Line)
                {
                    ts.GotoLine(line,true);
                    var lineStr   = ts.Text;
                    var formatStr = string.Format(@"\b{0}\b",searchToken);
                    var matches   = Regex.Matches(lineStr,formatStr,RegexOptions.ExplicitCapture);
                    int column    = -1;
                    foreach (Match nextMatch in matches)
                    {
                        column = nextMatch.Index + 1;
                        break;
                    }
                    if (column == -1)
                    {
                        return(false);
                    }
                    ts.MoveToLineAndOffset(line,column,false);
                }
            }
            catch (Exception e)
            {
                return(false);
            }
            return(true);
        }