/// <summary> /// This should be called when a new document is opened in the IDE. /// </summary> public void OnDocumentOpened(string filePath, ITextBuffer buffer) { var textContainer = buffer?.AsTextContainer(); if (!IsSupportedDocument(filePath) || textContainer == null) { return; } var workspaceRegistration = GetWorkspaceRegistration(textContainer); // Check if the document is already registered as open if (openDocuments.ContainsKey(workspaceRegistration)) { return; } workspaceRegistration.WorkspaceChanged += Registration_WorkspaceChanged; var openDocumentInfo = new OpenDocumentInfo { SourceTextContainer = textContainer, FilePath = filePath }; openDocuments[workspaceRegistration] = openDocumentInfo; if (workspaceRegistration.Workspace != null) { return; } AddDocument(workspaceRegistration, openDocumentInfo); }
/// <summary> /// If the DocumentId is currently part of our workspace, remove this document /// from our workspace. /// </summary> void RemoveDocument(OpenDocumentInfo openDocumentInfo) { var documentId = openDocumentInfo.DocumentId; if (documentId != null) { OnDocumentClosed(documentId, EmptyTextLoader.Instance); OnDocumentRemoved(documentId); openDocumentInfo.DocumentId = null; } }
/// <summary> /// Create a new DocumentId and a new Document, add that to the workspace and open in the text container /// </summary> /// <param name="registration"></param> /// <param name="openDocumentInfo"></param> void AddDocument(WorkspaceRegistration registration, OpenDocumentInfo openDocumentInfo) { var filePath = openDocumentInfo.FilePath; var sourceTextContainer = openDocumentInfo.SourceTextContainer; var sourceCodeKind = GetSourceCodeKind(filePath); openDocumentInfo.ProjectId = sourceCodeKind == SourceCodeKind.Script ? CreateScriptProject() : defaultProjectId; var documentId = DocumentId.CreateNewId(openDocumentInfo.ProjectId, filePath); openDocumentInfo.DocumentId = documentId; var documentInfo = DocumentInfo.Create( documentId, Path.GetFileName(filePath), sourceCodeKind: sourceCodeKind, filePath: filePath, loader: TextLoader.From(sourceTextContainer, VersionStamp.Create())); OnDocumentAdded(documentInfo); OnDocumentOpened(documentId, sourceTextContainer); ProjectId CreateScriptProject() { var projectId = ProjectId.CreateNewId(filePath); var compilationOptions = new CSharpCompilationOptions( outputKind: OutputKind.ConsoleApplication, sourceReferenceResolver: ScriptSourceResolver.Default, metadataReferenceResolver: ScriptMetadataResolver.Default); var projectInfo = ProjectInfo.Create( id: projectId, version: VersionStamp.Create(), name: filePath, assemblyName: Path.GetFileNameWithoutExtension(filePath), language: LanguageNames.CSharp, metadataReferences: defaultReferences, compilationOptions: compilationOptions, parseOptions: CSharpParseOptions .Default .WithLanguageVersion(LanguageVersion.Latest)); OnProjectAdded(projectInfo); return(projectInfo.Id); } }
/// <summary> /// If the DocumentId is currently part of our workspace, remove this document /// from our workspace. If the document is a script, remove its project as well. /// </summary> void RemoveDocument(OpenDocumentInfo openDocumentInfo) { var documentId = openDocumentInfo.DocumentId; if (documentId != null) { OnDocumentClosed(documentId, EmptyTextLoader.Instance); OnDocumentRemoved(documentId); openDocumentInfo.DocumentId = null; if (openDocumentInfo.ProjectId != null && openDocumentInfo.ProjectId != defaultProjectId) { OnProjectRemoved(openDocumentInfo.ProjectId); openDocumentInfo.ProjectId = null; } } }
/// <summary> /// Create a new DocumentId and a new Document, add that to the workspace and open in the text container /// </summary> /// <param name="registration"></param> /// <param name="openDocumentInfo"></param> void AddDocument(WorkspaceRegistration registration, OpenDocumentInfo openDocumentInfo) { var filePath = openDocumentInfo.FilePath; var sourceTextContainer = openDocumentInfo.SourceTextContainer; var documentId = DocumentId.CreateNewId(defaultProjectId, filePath); openDocumentInfo.DocumentId = documentId; var documentInfo = DocumentInfo.Create( documentId, Path.GetFileName(filePath), sourceCodeKind: GetSourceCodeKind(filePath), filePath: filePath, loader: TextLoader.From(sourceTextContainer, VersionStamp.Create())); OnDocumentAdded(documentInfo); OnDocumentOpened(documentId, sourceTextContainer); }