///<summary>Creates a new document linked to an existing text buffer.</summary> public DocumentId CreateDocument(ProjectId projectId, ITextBuffer buffer) { // Our GetFileName() extension (which should probably be deleted) doesn't work on projection buffers var debugName = TextBufferExtensions.GetFileName(buffer) ?? "Markdown Embedded Code"; var id = DocumentId.CreateNewId(projectId, debugName); TryApplyChanges(CurrentSolution.AddDocument( id, debugName, TextLoader.From(buffer.AsTextContainer(), VersionStamp.Create()) )); OpenDocument(id, buffer); return(id); }
///<summary>Creates a new document linked to an existing text buffer.</summary> public Document CreateDocument(ProjectId projectId, ITextBuffer buffer) { var id = DocumentId.CreateNewId(projectId); documentBuffers.Add(id, buffer); // Our GetFileName() extension (which should probably be deleted) doesn't work on projection buffers var docInfo = DocumentInfo.Create(id, TextBufferExtensions.GetFileName(buffer) ?? "Markdown Embedded Code", loader: TextLoader.From(buffer.AsTextContainer(), VersionStamp.Create()), sourceCodeKind: SourceCodeKind.Script ); OnDocumentAdded(docInfo); OnDocumentOpened(id, buffer.AsTextContainer()); buffer.Changed += delegate { OnDocumentContextUpdated(id); }; return(CurrentSolution.GetDocument(id)); }
public static string GetFileName(this ITextBuffer buffer) { // TextBufferExtensions.GetFileName uses ITextDocument; I don't know if // it's possible for a buffer (eg, from a native editor) to not have it var firstTry = TextBufferExtensions.GetFileName(buffer); if (firstTry != null) { return(firstTry); } IVsTextBuffer bufferAdapter; if (!buffer.Properties.TryGetProperty(typeof(IVsTextBuffer), out bufferAdapter)) { return(null); } var persistFileFormat = bufferAdapter as IPersistFileFormat; string ppzsFilename = null; uint pnFormatIndex; int returnCode = -1; if (persistFileFormat != null) { try { returnCode = persistFileFormat.GetCurFile(out ppzsFilename, out pnFormatIndex); } catch (NotImplementedException) { return(null); } } if (returnCode != VSConstants.S_OK) { return(null); } return(ppzsFilename); }