예제 #1
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;
        }