public bool TryGetTextLinesAndInvisibleEditor( string fullPathFileName, out VsShell.IVsInvisibleEditor spEditor, out VsTextMgr.IVsTextLines textLines) { return TryGetTextLinesAndInvisibleEditor(fullPathFileName, null, out spEditor, out textLines); }
/// <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; }
/// <summary> /// This routine returns the document from the RDT. We wrote this routine because /// sometimes 8.3 filenames are used and other times long filenames, so this routine /// tries and, if it fails, canonicalizes the path and tries again. /// </summary> private static int FindAndLockDocument( uint rdtLockType, string fullPathFileName, out VsShell.IVsHierarchy ppHier, out uint pitemid, out IntPtr ppunkDocData, out uint pdwCookie) { var hr = VSConstants.S_FALSE; var rdt = Instance.GetRunningDocumentTable(); ppHier = null; pitemid = 0; ppunkDocData = IntPtr.Zero; pdwCookie = 0; if (rdt != null) { hr = rdt.FindAndLockDocument( rdtLockType, fullPathFileName, out ppHier, out pitemid, out ppunkDocData, out pdwCookie); if (NativeMethods.Failed(hr)) { try { if (Path.IsPathRooted(fullPathFileName) == false) { fullPathFileName = Path.GetFullPath(fullPathFileName); // Canonicalize the file path // Use to get rid of 8.3 filenames, otherwise this'll fail hr = rdt.FindAndLockDocument( rdtLockType, fullPathFileName, out ppHier, out pitemid, out ppunkDocData, out pdwCookie); } } catch (ArgumentException) { } catch (IOException) { } } } return hr; }