Beispiel #1
0
        private async Task <TextEditorSessionDataV1> GetTextEditorSessionData(ITextEditor textEditor)
        {
            if (_sessionDataCache.TryGetValue(textEditor.Id, out TextEditorSessionDataV1 textEditorSessionData))
            {
                // We will not create new backup files for this text editor unless it has content changes
                // But we should update latest TextEditor state meta data
                textEditorSessionData.StateMetaData = textEditor.GetTextEditorStateMetaData();
            }
            else // Text content has been changed or editor has not backed up yet
            {
                textEditorSessionData = await BuildTextEditorSessionData(textEditor);

                if (textEditorSessionData == null)
                {
                    return(null);
                }

                _sessionDataCache.TryAdd(textEditor.Id, textEditorSessionData);
            }

            return(textEditorSessionData);
        }
Beispiel #2
0
        private async Task <TextEditorSessionDataV1> BuildTextEditorSessionData(ITextEditor textEditor)
        {
            TextEditorSessionDataV1 textEditorData = new TextEditorSessionDataV1
            {
                Id = textEditor.Id,
            };

            if (textEditor.EditingFile != null)
            {
                // Add the opened file to FutureAccessList so we can access it next launch
                var futureAccessToken = ToToken(textEditor.Id);
                await FileSystemUtility.TryAddOrReplaceTokenInFutureAccessList(futureAccessToken, textEditor.EditingFile);

                textEditorData.EditingFileFutureAccessToken = futureAccessToken;
                textEditorData.EditingFileName = textEditor.EditingFileName;
                textEditorData.EditingFilePath = textEditor.EditingFilePath;
            }

            if (textEditor.IsModified)
            {
                if (textEditor.EditingFile != null)
                {
                    // Persist the last save known to the app, which might not be up-to-date (if the file was modified outside the app)
                    var lastSavedBackupFile = await SessionUtility.CreateNewFileInBackupFolderAsync(
                        ToToken(textEditor.Id) + "-LastSaved",
                        CreationCollisionOption.ReplaceExisting,
                        _backupFolderName);

                    if (!await BackupTextAsync(textEditor.LastSavedSnapshot.Content,
                                               textEditor.LastSavedSnapshot.Encoding,
                                               textEditor.LastSavedSnapshot.LineEnding,
                                               lastSavedBackupFile))
                    {
                        return(null); // Error: Failed to write backup text to file
                    }

                    textEditorData.LastSavedBackupFilePath = lastSavedBackupFile.Path;
                }

                if (textEditor.EditingFile == null ||
                    !string.Equals(textEditor.LastSavedSnapshot.Content, textEditor.GetText()))
                {
                    // Persist pending changes relative to the last save
                    var pendingBackupFile = await SessionUtility.CreateNewFileInBackupFolderAsync(
                        ToToken(textEditor.Id) + "-Pending",
                        CreationCollisionOption.ReplaceExisting,
                        _backupFolderName);

                    if (!await BackupTextAsync(textEditor.GetText(),
                                               textEditor.LastSavedSnapshot.Encoding,
                                               textEditor.LastSavedSnapshot.LineEnding,
                                               pendingBackupFile))
                    {
                        return(null); // Error: Failed to write backup text to file
                    }

                    textEditorData.PendingBackupFilePath = pendingBackupFile.Path;
                }
            }

            textEditorData.StateMetaData = textEditor.GetTextEditorStateMetaData();

            return(textEditorData);
        }