Exemplo n.º 1
0
        protected override async Task OnSave()
        {
            if (warnOverwrite)
            {
                string question = GettextCatalog.GetString(
                    "This file {0} has been changed outside of {1}. Are you sure you want to overwrite the file?",
                    FilePath, BrandingService.ApplicationName
                    );
                if (MessageService.AskQuestion(question, AlertButton.Cancel, AlertButton.OverwriteFile) != AlertButton.OverwriteFile)
                {
                    return;
                }

                warnOverwrite = false;
                DismissInfoBar();
                ShowNotification = false;
            }

            if (!string.IsNullOrEmpty(FilePath))
            {
                AutoSave.RemoveAutoSaveFile(FilePath);
            }

            FormatOnSave();

            await base.OnSave();
        }
Exemplo n.º 2
0
        protected override void OnDispose()
        {
            if (IsDisposed)
            {
                return;
            }

            textBufferRegistration?.Dispose();
            textBufferRegistration = null;

            // Parity behavior with the old editor
            if (autoSaveTask != null)
            {
                autoSaveTask.Wait(TimeSpan.FromSeconds(5));
            }
            RemoveAutoSaveTimer();
            if (!string.IsNullOrEmpty(FilePath))
            {
                AutoSave.RemoveAutoSaveFile(FilePath);
            }

            UnsubscribeFromEvents();

            if (policyContainer != null)
            {
                policyContainer.PolicyChanged -= PolicyChanged;
            }
            if (editorConfigContext != null)
            {
                editorConfigContext.CodingConventionsChangedAsync -= UpdateOptionsFromEditorConfigAsync;
                EditorConfigService.RemoveEditConfigContext(FilePath).Ignore();
            }

            base.OnDispose();
        }
Exemplo n.º 3
0
        void InformAutoSave()
        {
            if (IsDisposed)
            {
                return;
            }
            RemoveAutoSaveTimer();
            autoSaveTimer = GLib.Timeout.Add(500, delegate {
                autoSaveTimer = 0;
                if (autoSaveTask != null && !autoSaveTask.IsCompleted)
                {
                    return(false);
                }

                autoSaveTask = AutoSave.InformAutoSaveThread(
                    new AutoSaveTextSourceFacade(TextBuffer, TextDocument), FilePath, HasUnsavedChanges);
                return(false);
            });
        }
Exemplo n.º 4
0
        protected override void OnFileNameChanged()
        {
            base.OnFileNameChanged();

            if (TextDocument == null)
            {
                return;
            }

            UpdateTextBufferRegistration();

            warnOverwrite = false;

            if (editorConfigContext != null)
            {
                editorConfigContext.CodingConventionsChangedAsync -= UpdateOptionsFromEditorConfigAsync;
                EditorConfigService.RemoveEditConfigContext(TextDocument.FilePath).Ignore();
                editorConfigContext = null;
            }

            if (FilePath != TextDocument.FilePath && !string.IsNullOrEmpty(TextDocument.FilePath))
            {
                AutoSave.RemoveAutoSaveFile(TextDocument.FilePath);
            }

            if (FilePath != null)             // Happens when a file is converted to an untitled file, but even in that case the text editor should be associated with the old location, otherwise typing can be messed up due to change of .editconfig settings etc.
            {
                TextDocument.Rename(FilePath);
            }

            // update the file type condition with the new path
            fileTypeCondition.SetFileName(FilePath);

            // TODO: Actually implement file rename support. Below is from old editor.
            //       Need to remove or update mimeType field, too.

            //if (this.WorkbenchWindow?.Document != null)
            //	textEditor.InitializeExtensionChain (this.WorkbenchWindow.Document);

            UpdateTextEditorOptions(null, null);
        }
Exemplo n.º 5
0
        Task Load(bool reloading)
        {
            // We actually load initial content at construction time, so this
            // overload only needs to cover reload and autosave scenarios

            if (warnOverwrite)
            {
                warnOverwrite = false;
                DismissInfoBar();
                ShowNotification = false;
            }

            if (reloading)
            {
                TextDocument.Reload();
            }
            else if (AutoSave.AutoSaveExists(FilePath))
            {
                var autosaveContent = AutoSave.LoadAutoSave(FilePath);

                MarkDirty();
                warnOverwrite = true;

                // Set editor read-only until user picks one of the above options.
                var setWritable = !TextView.Options.DoesViewProhibitUserInput();
                if (setWritable)
                {
                    TextView.Options.SetOptionValue(DefaultTextViewOptions.ViewProhibitUserInputId, true);
                }

                var(primaryMessageText, secondaryMessageText) = SplitMessageString(
                    BrandingService.BrandApplicationName(GettextCatalog.GetString(
                                                             "<b>An autosave file has been found for this file.</b>\n" +
                                                             "This could mean that another instance of MonoDevelop is editing this " +
                                                             "file, or that MonoDevelop crashed with unsaved changes.\n\n" +
                                                             "Do you want to use the original file, or load from the autosave file?")));

                PresentInfobar(
                    primaryMessageText,
                    secondaryMessageText,
                    new InfoBarAction(
                        GetButtonString(GettextCatalog.GetString("_Use original file")),
                        UseOriginalFile),
                    new InfoBarAction(
                        GetButtonString(GettextCatalog.GetString("_Load from autosave")),
                        LoadFromAutosave,
                        isDefault: true));

                void OnActionSelected()
                {
                    DismissInfoBar();
                    if (setWritable)
                    {
                        TextView.Options.SetOptionValue(DefaultTextViewOptions.ViewProhibitUserInputId, false);
                    }
                }

                void LoadFromAutosave()
                {
                    try {
                        AutoSave.RemoveAutoSaveFile(FilePath);
                        ReplaceContent(autosaveContent.Text, autosaveContent.Encoding);
                    } catch (Exception e) {
                        LoggingService.LogError("Could not load the autosave file", e);
                    } finally {
                        OnActionSelected();
                    }
                }

                void UseOriginalFile()
                {
                    try {
                        AutoSave.RemoveAutoSaveFile(FilePath);
                    } catch (Exception e) {
                        LoggingService.LogError("Could not remove the autosave file", e);
                    } finally {
                        OnActionSelected();
                    }
                }
            }

            return(Task.CompletedTask);
        }