Ejemplo n.º 1
0
        // <returns>An instance to a <see cref="FileSave"/> class generated from the <see cref="ScintillaTabbedDocument"/> class instance.</returns>

        /// <summary>
        /// Creates a <see cref="FileSave"/> entity from a given <see cref="ScintillaTabbedDocument"/> document.
        /// </summary>
        /// <param name="document">The document to create a file save from.</param>
        /// <param name="encoding">The encoding of the file save.</param>
        /// <param name="fileSession">The file session.</param>
        /// <param name="isHistory">if set to <c>true</c> the resulting <see cref="FileSave"/> instance is marked as a history file.</param>
        /// <returns>An instance to a <see cref="FileSave"/> modified class.</returns>
        public static FileSave CreateFromTabbedDocument(ScintillaTabbedDocument document, Encoding encoding,
                                                        FileSession fileSession, bool isHistory = false)
        {
            var fileSave = new FileSave
            {
                ExistsInFileSystem = File.Exists(document.FileName),
                FileNameFull       = document.FileName,
                FileName           = Path.GetFileName(document.FileName),
                FilePath           = Path.GetDirectoryName(document.FileName),
                FileSystemModified = File.Exists(document.FileName)
                    ? new FileInfo(document.FileName).LastWriteTime
                    : DateTime.MinValue,
                LexerType       = document.LexerType,
                VisibilityOrder = (int)document.FileTabButton.Tag,
                Session         = ScriptNotepadDbContext.DbContext.FileSessions.FirstOrDefault(f =>
                                                                                               f.SessionName == fileSession.SessionName),
                IsActive                = document.FileTabButton.IsActive,
                IsHistory               = isHistory,
                CurrentCaretPosition    = document.Scintilla.CurrentPosition,
                UseSpellChecking        = true,
                EditorZoomPercentage    = document.ZoomPercentage,
                UseFileSystemOnContents = fileSession.UseFileSystemOnContents,
            };

            fileSave.SetDatabaseModified(DateTime.Now);

            fileSave.SetEncoding(encoding);

            fileSave.SetFileContents(encoding.GetBytes(document.Scintilla.Text), true, false, true);

            ScriptNotepadDbContext.DbContext.FileSaves.Add(fileSave);
            ScriptNotepadDbContext.DbContext.SaveChanges();
            return(fileSave);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reloads the contents of the document from the disk.
        /// </summary>
        /// <param name="fileSave">An instance to a <see cref="FileSave"/> class.</param>
        /// <param name="document">A ScintillaTabbedDocument to which contents should also be updated.</param>
        /// <returns>True if the operation was successful; otherwise false.</returns>
        public static bool ReloadFromDisk(this FileSave fileSave, ScintillaTabbedDocument document)
        {
            try
            {
                // can't reload what doesn't exist..
                if (File.Exists(fileSave.FileNameFull))
                {
                    // read the file contents from the file..
                    using (FileStream fileStream = new FileStream(fileSave.FileNameFull, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        // create a byte buffer the contain all the bytes if the file with an assumption
                        // no one wishes to open massive binary files..
                        byte[] fileContents = new byte[fileStream.Length];

                        // read the file contents to the buffer..
                        fileStream.Read(fileContents, 0, (int)fileStream.Length);

                        // set the file system's modified flag..
                        fileSave.FileSystemModified = new FileInfo(fileSave.FileNameFull).LastWriteTime;

                        fileSave.SetDatabaseModified(fileSave.FileSystemModified); // set the other DateTime flags to indicate the same..
                        fileSave.FileSystemSaved = fileSave.FileSystemModified;    // set the other DateTime flags to indicate the same..
                        fileSave.ResetPreviousDbModified();


                        // create a new memory stream to hold the file contents..
                        MemoryStream memoryStream = new MemoryStream(fileContents);

                        document.Scintilla.Text = StreamStringHelpers.MemoryStreamToText(memoryStream, fileSave.GetEncoding());

                        // a reload doesn't need to be undone..
                        document.Scintilla.EmptyUndoBuffer();

                        fileSave.SetFileContentsAsMemoryStream(memoryStream);

                        // set the saved position of the document's caret..
                        if (fileSave.CurrentCaretPosition > 0 && fileSave.CurrentCaretPosition < document.Scintilla.TextLength)
                        {
                            document.Scintilla.CurrentPosition = fileSave.CurrentCaretPosition;
                            document.Scintilla.SelectionStart  = fileSave.CurrentCaretPosition;
                            document.Scintilla.SelectionEnd    = fileSave.CurrentCaretPosition;
                            document.Scintilla.ScrollCaret();
                        }
                    }
                    return(true); // success..
                }
                else
                {
                    return(false); // the file didn't exists, so fail..
                }
            }
            catch (Exception ex)
            {
                // log the exception..
                ErrorHandlingBase.ExceptionLogAction?.Invoke(ex);

                return(false); // an exception occurred, so fail..
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds the or update file.
        /// </summary>
        /// <param name="fileSave">A <see cref="FileSave"/> class instance to be added or updated into the database.</param>
        /// <param name="document">An instance to a ScintillaTabbedDocument class.</param>
        /// <param name="commit">A value indicating whether to commit the changes to the
        /// database or to the file system cache depending on the setting.</param>
        /// <param name="saveToFileSystem">A value indicating whether to override existing copy of the file in the file system.</param>
        /// <param name="contentChanged">A value indicating whether the file contents have been changed.</param>
        /// <returns>An instance to a <see cref="FileSave"/> modified class.</returns>
        public static FileSave AddOrUpdateFile(this FileSave fileSave, ScintillaTabbedDocument document, bool commit,
                                               bool saveToFileSystem, bool contentChanged)
        {
            fileSave.SetFileContents(fileSave.GetEncoding().GetBytes(document.Scintilla.Text), commit, saveToFileSystem, contentChanged);
            fileSave.CurrentCaretPosition = document.Scintilla.CurrentPosition;
            fileSave.FilePath             = Path.GetDirectoryName(fileSave.FileNameFull);
            ScriptNotepadDbContext.DbContext.SaveChanges();

            if (!ScriptNotepadDbContext.DbContext.FileSaves.Any(f => f.Id == fileSave.Id))
            {
                return(ScriptNotepadDbContext.DbContext.FileSaves.Add(fileSave).Entity);
            }

            return(ScriptNotepadDbContext.DbContext.FileSaves.FirstOrDefault(f => f.Id == fileSave.Id));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds the or update file.
        /// </summary>
        /// <param name="fileSave">A <see cref="FileSave"/> class instance to be added or updated into the database.</param>
        /// <param name="document">An instance to a ScintillaTabbedDocument class.</param>
        /// <param name="isHistory">if set to <c>true</c> the file is to be considered as a closed/history file.</param>
        /// <param name="sessionName">Name of the session the file belongs to.</param>
        /// <param name="encoding">The encoding of the file.</param>
        /// <param name="commit">A value indicating whether to commit the changes to the
        /// database or to the file system cache depending on the setting.</param>
        /// <param name="saveToFileSystem">A value indicating whether to override existing copy of the file in the file system.</param>
        /// <param name="contentChanged">A value indicating whether the file contents have been changed.</param>
        /// <returns>An instance to a <see cref="FileSave"/> modified class.</returns>
        public static FileSave AddOrUpdateFile(this FileSave fileSave, ScintillaTabbedDocument document, bool isHistory,
                                               string sessionName, Encoding encoding, bool commit,
                                               bool saveToFileSystem, bool contentChanged)
        {
            fileSave.SetFileContents(fileSave.GetEncoding().GetBytes(document.Scintilla.Text), commit, saveToFileSystem, contentChanged);
            fileSave.CurrentCaretPosition = document.Scintilla.CurrentPosition;
            fileSave.FilePath             = Path.GetDirectoryName(fileSave.FileNameFull);
            fileSave.IsHistory            = isHistory;
            fileSave.Session =
                ScriptNotepadDbContext.DbContext.FileSessions.FirstOrDefault(f => f.SessionName == sessionName);

            fileSave.SetEncoding(encoding);

            ScriptNotepadDbContext.DbContext.SaveChanges();
            return(ScriptNotepadDbContext.DbContext.FileSaves.FirstOrDefault(f => f.Id == fileSave.Id));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TabbedDocumentSpellCheck"/> class.
        /// </summary>
        /// <param name="document">The <see cref="ScintillaTabbedDocument"/> to attach the spell checker.</param>
        /// <param name="loadDictionaryFiles">Specifies whether the dictionary files should be loaded to the instance.</param>
        public TabbedDocumentSpellCheck(ScintillaTabbedDocument document, bool loadDictionaryFiles)
        {
            // verify the settings and the fact that the document doesn't already have this instance..
            if (FormSettings.Settings.EditorUseSpellChecking &&
                File.Exists(FormSettings.Settings.EditorHunspellDictionaryFile) &&
                File.Exists(FormSettings.Settings.EditorHunspellAffixFile) &&
                document.Tag0 == null)
            {
                try
                {
                    SpellCheck = new ScintillaSpellCheck(document.Scintilla,
                                                         FormSettings.Settings.EditorHunspellDictionaryFile,
                                                         FormSettings.Settings.EditorHunspellAffixFile,
                                                         UserDictionaryFile,
                                                         UserIgnoreWordFile)
                    {
                        MenuIgnoreText = DBLangEngine.GetStatMessage("msgSpellCheckIgnoreWordMenuText",
                                                                     "Ignore word \"{0}\".|A context menu item for spell checking to ignore a word"),
                        MenuAddToDictionaryText = DBLangEngine.GetStatMessage(
                            "msgSpellCheckAddWordToDictionaryText",
                            "Add word \"{0}\" to the dictionary.|A context menu item for spell checking to add a word to the dictionary"),
                        MenuDictionaryTopItemText = DBLangEngine.GetStatMessage("msgSpellChecking",
                                                                                "Spell checking|A message displayed in a spelling correct menu's top item."),
                        ShowDictionaryTopMenuItem = true,
                        AddBottomSeparator        = true,
                        ShowIgnoreMenu            = true,
                        ShowAddToDictionaryMenu   = true,
                        ScintillaIndicatorColor   = FormSettings.Settings.EditorSpellCheckColor,
                    };

                    if (!loadDictionaryFiles)
                    {
                        SpellCheck.Dictionary = null;
                    }

                    // add this instance to the document's Tag0 property..
                    document.Tag0 = this;

                    // subscribe to the event where a user wishes to correct a
                    // misspelled word via the context menu..
                    SpellCheck.UserWordReplace += SpellCheck_UserWordReplace;

                    // subscribe to the Scintilla text changed event..
                    document.Scintilla.TextChanged += Scintilla_TextChanged;

                    // subscribe the event when a user is requesting a word to be added to the personal dictionary..
                    SpellCheck.WordAddDictionaryRequested += SpellCheck_WordAddDictionaryOrIgnoreRequested;

                    // subscribe to the event when a user is requesting to add a word to personal ignore list..
                    SpellCheck.WordIgnoreRequested += SpellCheck_WordAddDictionaryOrIgnoreRequested;

                    // save the Scintilla instance to unsubscribe the events..
                    Scintilla = document.Scintilla;

                    // spell check the document for the first time..
                    SpellCheck?.SpellCheckScintillaFast();

                    // save the time of the latest spell check..
                    LastSpellCheck = DateTime.Now;
                }
                catch (Exception ex)
                {
                    // log the exception..
                    ExceptionLogAction?.Invoke(ex);

                    try
                    {
                        ExceptionLogger.LogMessage($"Spell check state: '{FormSettings.Settings.EditorUseSpellChecking}'.");
                        ExceptionLogger.LogMessage(
                            $"File exists ({File.Exists(FormSettings.Settings.EditorHunspellDictionaryFile)}): '{FormSettings.Settings.EditorHunspellDictionaryFile}'.");
                        ExceptionLogger.LogMessage(
                            $"File exists ({File.Exists(FormSettings.Settings.EditorHunspellAffixFile)}): '{FormSettings.Settings.EditorHunspellAffixFile}'.");
                        ExceptionLogger.LogMessage($"Document Tag0: '{document.Tag0}'.");
                    }
                    catch (Exception ex2)
                    {
                        ExceptionLogger.LogError(ex2);
                    }
                }
            }
            else
            {
                try
                {
                    ExceptionLogger.LogMessage($"Spell check state: '{FormSettings.Settings.EditorUseSpellChecking}'.");
                    ExceptionLogger.LogMessage(
                        $"File exists ({File.Exists(FormSettings.Settings.EditorHunspellDictionaryFile)}): '{FormSettings.Settings.EditorHunspellDictionaryFile}'.");
                    ExceptionLogger.LogMessage(
                        $"File exists ({File.Exists(FormSettings.Settings.EditorHunspellAffixFile)}): '{FormSettings.Settings.EditorHunspellAffixFile}'.");
                    ExceptionLogger.LogMessage($"Document Tag0: '{document.Tag0}'.");
                }
                catch (Exception ex)
                {
                    ExceptionLogger.LogError(ex);
                }
            }
        }