Ejemplo n.º 1
0
        // The REPL window might change content type to host command content type (when a host command is typed at the beginning of the buffer).
        private void LanguageBufferContentTypeChanged(object sender, ContentTypeChangedEventArgs e)
        {
            // It's not clear whether this situation will ever happen, but just in case.
            if (e.BeforeContentType == e.AfterContentType)
            {
                return;
            }

            var buffer          = e.Before.TextBuffer;
            var contentTypeName = this.ContentType.TypeName;

            var afterIsLanguage            = e.AfterContentType.IsOfType(contentTypeName);
            var afterIsInteractiveCommand  = e.AfterContentType.IsOfType(PredefinedInteractiveCommandsContentTypes.InteractiveCommandContentTypeName);
            var beforeIsLanguage           = e.BeforeContentType.IsOfType(contentTypeName);
            var beforeIsInteractiveCommand = e.BeforeContentType.IsOfType(PredefinedInteractiveCommandsContentTypes.InteractiveCommandContentTypeName);

            Debug.Assert((afterIsLanguage && beforeIsInteractiveCommand) ||
                         (beforeIsLanguage && afterIsInteractiveCommand));

            // We're switching between the target language and the Interactive Command "language".
            // First, remove the current submission from the solution.

            var oldSolution = _workspace.CurrentSolution;
            var newSolution = oldSolution;

            foreach (var documentId in _workspace.GetRelatedDocumentIds(buffer.AsTextContainer()))
            {
                Debug.Assert(documentId != null);

                newSolution = newSolution.RemoveDocument(documentId);

                // TODO (tomat): Is there a better way to remove mapping between buffer and document in REPL?
                // Perhaps TrackingWorkspace should implement RemoveDocumentAsync?
                _workspace.ClearOpenDocument(documentId);
            }

            // Next, remove the previous submission project and update the workspace.
            newSolution = newSolution.RemoveProject(_currentSubmissionProjectId);
            _workspace.SetCurrentSolution(newSolution);

            // Add a new submission with the correct language for the current buffer.
            var languageName = afterIsLanguage
                ? this.LanguageName
                : InteractiveLanguageNames.InteractiveCommand;

            AddSubmission(buffer, languageName);
        }
Ejemplo n.º 2
0
        // The REPL window might change content type to host command content type (when a host command is typed at the beginning of the buffer).
        private void LanguageBufferContentTypeChanged(object sender, ContentTypeChangedEventArgs e)
        {
            var buffer         = e.Before.TextBuffer;
            var afterIsCSharp  = e.AfterContentType.IsOfType(ContentTypeNames.CSharpContentType);
            var beforeIsCSharp = e.BeforeContentType.IsOfType(ContentTypeNames.CSharpContentType);

            if (afterIsCSharp == beforeIsCSharp)
            {
                return;
            }

            if (afterIsCSharp)
            {
                // add a new document back to the project
                var project = _workspace.CurrentSolution.GetProject(_currentSubmissionProjectId);
                Debug.Assert(project != null);
                SetSubmissionDocument(buffer, project);
            }
            else
            {
                Debug.Assert(beforeIsCSharp);

                // remove document from the project
                foreach (var documentId in _workspace.GetRelatedDocumentIds(buffer.AsTextContainer()))
                {
                    Debug.Assert(documentId != null);
                    _workspace.SetCurrentSolution(_workspace.CurrentSolution.RemoveDocument(documentId));

                    // TODO (tomat): Is there a better way to remove mapping between buffer and document in REPL?
                    // Perhaps TrackingWorkspace should implement RemoveDocumentAsync?
                    _workspace.ClearOpenDocument(documentId);

                    // Ensure sure our buffer is still registered with the workspace. This allows consumers
                    // to get back to the interactive workspace from the Interactive Command buffer.
                    _workspace.RegisterText(buffer.AsTextContainer());
                }
            }
        }