예제 #1
0
        /// <summary>
        ///     Get string content of a file.
        /// </summary>
        /// <remarks>
        ///     This method created for refactoring usage, refactoring will work on all kinds of
        ///     docdata, not only SqlEditorDocData.  If change this method, please get let LiangZ
        ///     know.
        /// </remarks>
        /// <param name="fullPathFileName">File name with full path.</param>
        /// <returns>Content of that file in string format.</returns>
        public string ReadFromFile(string fullPathFileName)
        {
            string content;

            VsShell.IVsInvisibleEditor invisibleEditor = null;
            VsTextMgr.IVsTextLines     textLines;
            try
            {
                if (IsFileInRdt(fullPathFileName))
                {
                    // File is in RDT
                    textLines = GetTextLines(fullPathFileName);
                }
                else
                {
                    // File is not in RDT, open it in invisible editor.
                    if (!TryGetTextLinesAndInvisibleEditor(fullPathFileName, out invisibleEditor, out textLines))
                    {
                        // Failed to get text lines or invisible editor.
                        textLines = null;
                    }
                }
                content = GetAllTextFromTextLines(textLines);
            }
            finally
            {
                // Close invisible editor from RDT
                if (invisibleEditor != null)
                {
                    Marshal.ReleaseComObject(invisibleEditor);
                }
            }
            return(content);
        }
예제 #2
0
        /// <summary>
        ///     Open the file in invisible editor in RDT, and get text buffer from it.
        /// </summary>
        /// <remarks>
        ///     This method created for refactoring usage, refactoring will work on all kinds of
        ///     docdata, not only SqlEditorDocData.  If change this method, please get let LiangZ
        ///     know.
        /// </remarks>
        /// <param name="fullPathFileName">File name with full path.</param>
        /// <param name="project">the hierarchy for the document</param>
        /// <param name="spEditor">The result invisible editor.</param>
        /// <param name="textLines">The result text buffer.</param>
        /// <returns>True, if the file is opened correctly in invisible editor.</returns>
        public bool TryGetTextLinesAndInvisibleEditor(
            string fullPathFileName, VsShell.IVsProject project, out VsShell.IVsInvisibleEditor spEditor,
            out VsTextMgr.IVsTextLines textLines)
        {
            spEditor  = null;
            textLines = null;

            // Need to open this file.  Use the invisible editor manager to do so.
            VsShell.IVsInvisibleEditorManager invisibleEditorMgr;
            var  ppDocData = IntPtr.Zero;
            bool result;

            var iidIVsTextLines = typeof(VsTextMgr.IVsTextLines).GUID;

            try
            {
                invisibleEditorMgr = _invisibleEditorManager;

                NativeMethods.ThrowOnFailure(
                    invisibleEditorMgr.RegisterInvisibleEditor(
                        fullPathFileName, project, (uint)VsShell._EDITORREGFLAGS.RIEF_ENABLECACHING, null, out spEditor));
                if (spEditor != null)
                {
                    var hr = spEditor.GetDocData(0, ref iidIVsTextLines, out ppDocData);
                    if (hr == VSConstants.S_OK &&
                        ppDocData != IntPtr.Zero)
                    {
                        textLines = Marshal.GetTypedObjectForIUnknown(ppDocData, typeof(VsTextMgr.IVsTextLines)) as VsTextMgr.IVsTextLines;
                        result    = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
                else
                {
                    result = false;
                }
            }
            finally
            {
                if (ppDocData != IntPtr.Zero)
                {
                    Marshal.Release(ppDocData);
                }
            }

            return(result);
        }
예제 #3
0
        public bool WriteToFile(string fullPathFileName, string content, bool saveFile, bool createIfNotExist)
        {
            var succeed = true;

            VsShell.IVsInvisibleEditor invisibleEditor = null;
            VsTextMgr.IVsTextLines     textLines       = null;
            var fileDidNotExistAndShouldBeCreated      = false;

            try
            {
                if (IsFileInRdt(fullPathFileName))
                {
                    // File is in RDT
                    textLines = GetTextLines(fullPathFileName);
                }
                else
                {
                    if (createIfNotExist &&
                        File.Exists(fullPathFileName) == false)
                    {
                        fileDidNotExistAndShouldBeCreated = true;

                        FileStream   fileStream = null;
                        StreamWriter writer     = null;
                        try
                        {
                            fileStream = File.Create(fullPathFileName);
                            // Make the newly created file unicode
                            writer = new StreamWriter(fileStream, Encoding.UTF8);

                            writer.Write(content);
                        }
                        catch (IOException e)
                        {
                            throw new InvalidOperationException(e.Message);
                        }
                        catch (ArgumentException e)
                        {
                            throw new InvalidOperationException(e.Message);
                        }
                        finally
                        {
                            if (writer != null)
                            {
                                writer.Close();
                            }
                            if (fileStream != null)
                            {
                                fileStream.Close();
                            }
                        }
                    }

                    // File is not in RDT, open it in invisible editor.
                    if (fileDidNotExistAndShouldBeCreated == false &&
                        !TryGetTextLinesAndInvisibleEditor(fullPathFileName, out invisibleEditor, out textLines))
                    {
                        // Failed to get text lines or invisible editor.
                        textLines = null;
                    }
                }

                if (fileDidNotExistAndShouldBeCreated == false)
                {
                    if (textLines != null)
                    {
                        int line, column;
                        var result = textLines.GetLastLineIndex(out line, out column);
                        if (result == VSConstants.S_OK)
                        {
                            var pContent = IntPtr.Zero;
                            try
                            {
                                // Copy the content to textLines.
                                pContent = Marshal.StringToHGlobalAuto(content);
                                result   = textLines.ReloadLines(
                                    0, 0, line, column, pContent, content.Length,
                                    new[] { new VsTextMgr.TextSpan() });
                                if (result != VSConstants.S_OK)
                                {
                                    succeed = false;
                                }
                            }
                            finally
                            {
                                if (pContent != IntPtr.Zero)
                                {
                                    Marshal.FreeHGlobal(pContent);
                                }
                            }
                            if (saveFile)
                            {
                                var list = new List <string> {
                                    fullPathFileName
                                };
                                Instance.SaveDirtyFiles(list);
                            }
                        }
                        else
                        {
                            succeed = false;
                        }
                    }
                    else
                    {
                        succeed = false;
                    }
                }
            }
            finally
            {
                // Close invisible editor from RDT
                if (invisibleEditor != null)
                {
                    Marshal.ReleaseComObject(invisibleEditor);
                }
            }
            return(succeed);
        }
예제 #4
0
 public bool TryGetTextLinesAndInvisibleEditor(
     string fullPathFileName, out VsShell.IVsInvisibleEditor spEditor, out VsTextMgr.IVsTextLines textLines)
 {
     return(TryGetTextLinesAndInvisibleEditor(fullPathFileName, null, out spEditor, out textLines));
 }
예제 #5
0
        /// <summary>
        ///     Get IVsTextLines from a file.  If that file is in RDT, get text buffer from it.
        ///     If the file is not in RDT, open that file in invisible editor and get text buffer
        ///     from it.
        ///     If failed to get text buffer, it will return null.
        /// </summary>
        /// <remarks>
        ///     This method created for refactoring usage, refactoring will work on all kinds of
        ///     docdata, not only SqlEditorDocData.  If change this method, please get let LiangZ
        ///     know.
        /// </remarks>
        /// <param name="fullPathFileName">File name with full path.</param>
        /// <returns>Text buffer for that file.</returns>
        public VsTextMgr.IVsTextLines GetTextLines(string fullPathFileName)
        {
            VsTextMgr.IVsTextLines textLines = null;

            var rdt = Instance.GetRunningDocumentTable();

            if (rdt != null)
            {
                VsShell.IVsHierarchy ppHier;
                uint pitemid, pdwCookie;
                var  ppunkDocData = IntPtr.Zero;
                try
                {
                    NativeMethods.ThrowOnFailure(
                        FindAndLockDocument(
                            (uint)(VsShell._VSRDTFLAGS.RDT_NoLock),
                            fullPathFileName,
                            out ppHier,
                            out pitemid,
                            out ppunkDocData,
                            out pdwCookie));
                    if (pdwCookie != 0)
                    {
                        if (ppunkDocData != IntPtr.Zero)
                        {
                            try
                            {
                                // Get text lines from the doc data
                                textLines = Marshal.GetObjectForIUnknown(ppunkDocData) as VsTextMgr.IVsTextLines;
                            }
                            catch (ArgumentException)
                            {
                                // Do nothing here, it will return null stream at the end.
                            }
                        }
                    }
                    else
                    {
                        // The file is not in RDT, open it in invisible editor and get the text lines from it.
                        VsShell.IVsInvisibleEditor invisibleEditor = null;
                        try
                        {
                            TryGetTextLinesAndInvisibleEditor(fullPathFileName, out invisibleEditor, out textLines);
                        }
                        finally
                        {
                            if (invisibleEditor != null)
                            {
                                Marshal.ReleaseComObject(invisibleEditor);
                            }
                        }
                    }
                }
                finally
                {
                    if (ppunkDocData != IntPtr.Zero)
                    {
                        Marshal.Release(ppunkDocData);
                    }
                }
            }
            return(textLines);
        }