-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTedApp.FileOperations.cs
More file actions
250 lines (198 loc) · 7.02 KB
/
TedApp.FileOperations.cs
File metadata and controls
250 lines (198 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using Terminal.Gui.Document;
using Terminal.Gui.Highlighting;
using Terminal.Gui.Resources;
using Terminal.Gui.Views;
namespace Ted;
public sealed partial class TedApp
{
/// <summary>The path currently associated with <see cref="Editor" />, or <see langword="null" /> for an untitled buffer.</summary>
public string? CurrentFilePath { get; private set; }
/// <summary>Dialog hook used by <see cref="OpenFile" />. Tests can replace it to avoid interactive UI.</summary>
public Func<string?> ShowOpenDialog { get; set; }
/// <summary>Dialog hook used by <see cref="SaveFileAs" />. Tests can replace it to avoid interactive UI.</summary>
public Func<string?> ShowSaveDialog { get; set; }
/// <summary>Dialog hook used by <see cref="QuitFile" />. Tests can replace it to avoid interactive UI.</summary>
public Func<SaveChangesChoice> ShowSaveChangesDialog { get; set; }
/// <summary>File read hook used by <see cref="OpenFile" />. Tests can replace it with an in-memory fake.</summary>
public Func<string, string> ReadAllText { get; set; } = File.ReadAllText;
/// <summary>
/// File write hook used by <see cref="SaveFile" /> and <see cref="SaveFileAs" />. Tests can replace it with an
/// in-memory fake.
/// </summary>
public Action<string, string> WriteAllText { get; set; } = File.WriteAllText;
/// <summary>Gets whether the current editor document has unsaved changes.</summary>
public bool IsDocumentModified => Editor.Document?.UndoStack.IsOriginalFile == false;
/// <summary>Clears the editor and makes the buffer untitled.</summary>
public void NewFile ()
{
SetDocument (string.Empty, null);
}
/// <summary>Prompts for a file path, then loads that file into the editor.</summary>
public bool OpenFile ()
{
var filePath = ShowOpenDialog ();
if (string.IsNullOrWhiteSpace (filePath))
{
return false;
}
SetDocument (ReadAllText (filePath), filePath);
return true;
}
/// <summary>Opens a CLI-requested missing file path as an empty, modified document bound to that path.</summary>
public void OpenMissingFile (string filePath)
{
if (string.IsNullOrWhiteSpace (filePath))
{
throw new ArgumentException ("Path must not be null, empty, or whitespace.", nameof (filePath));
}
SetDocument (string.Empty, filePath);
TextDocument document = Editor.Document
?? throw new InvalidOperationException ("ted cannot open a missing file because the editor has no document.");
document.UndoStack.DiscardOriginalFileMarker ();
}
/// <summary>Saves the editor text to the current file, or prompts for a path if the buffer is untitled.</summary>
public bool SaveFile ()
{
if (CurrentFilePath is null)
{
return SaveFileAs ();
}
WriteAllText (CurrentFilePath, GetEditorText ());
Editor.Document!.UndoStack.MarkAsOriginalFile ();
return true;
}
/// <summary>Prompts for a file path, then saves the editor text to that path.</summary>
public bool SaveFileAs ()
{
var filePath = ShowSaveDialog ();
if (string.IsNullOrWhiteSpace (filePath))
{
return false;
}
CurrentFilePath = filePath;
WriteAllText (filePath, GetEditorText ());
Editor.Document!.UndoStack.MarkAsOriginalFile ();
UpdateFileNameShortcut ();
UpdatePreviewVisibility ();
return true;
}
/// <summary>Quits ted, prompting to save first when the current document has unsaved changes.</summary>
public bool QuitFile ()
{
if (!ConfirmSaveChanges ())
{
return false;
}
RequestStop ();
return true;
}
internal void SetDocument (string text, string? filePath)
{
Editor.ClearSelection ();
Editor.Document = new TextDocument (text);
Editor.CaretOffset = 0;
CurrentFilePath = filePath;
// Auto-detect highlighting from file extension.
IHighlightingDefinition? def = null;
if (filePath is not null)
{
var ext = Path.GetExtension (filePath);
if (!string.IsNullOrEmpty (ext))
{
def = HighlightingManager.Instance.GetDefinitionByExtension (ext);
}
}
Editor.HighlightingDefinition = def;
LanguageShortcut.Title = def?.Name ?? "Plain Text";
UpdateFileNameShortcut ();
UpdatePreviewVisibility ();
InstallFolding ();
Editor.SetNeedsDraw ();
}
private string? ShowDefaultOpenDialog ()
{
using OpenDialog dialog = new ();
dialog.AllowsMultipleSelection = false;
dialog.MustExist = true;
dialog.OpenMode = OpenMode.File;
if (App is null)
{
throw new InvalidOperationException ("ted must be running before showing the open dialog.");
}
App.Run (dialog);
return dialog.FilePaths.FirstOrDefault ();
}
private string? ShowDefaultSaveDialog ()
{
using SaveDialog dialog = new ();
dialog.AllowsMultipleSelection = false;
dialog.OpenMode = OpenMode.File;
if (App is null)
{
throw new InvalidOperationException ("ted must be running before showing the save dialog.");
}
App.Run (dialog);
return dialog.FileName;
}
private SaveChangesChoice ShowDefaultSaveChangesDialog ()
{
if (App is null)
{
throw new InvalidOperationException ("ted must be running before showing the save-changes dialog.");
}
var result = MessageBox.Query (
App,
"Save changes?",
"The document has unsaved changes. Save before quitting?",
Strings.btnCancel,
"Do_n't Save",
Strings.btnSave);
return result switch
{
1 => SaveChangesChoice.Discard,
2 => SaveChangesChoice.Save,
_ => SaveChangesChoice.Cancel
};
}
private string GetEditorText ()
{
return Editor.Document is null
? throw new InvalidOperationException ("ted cannot save because the editor has no document.")
: Editor.Document.Text;
}
private void New ()
{
if (!ConfirmSaveChanges ())
{
return;
}
NewFile ();
}
private void Open ()
{
if (!ConfirmSaveChanges ())
{
return;
}
OpenFile ();
}
private void Save () { SaveFile (); }
private void SaveAs () { SaveFileAs (); }
private void Quit ()
{
QuitFile ();
}
private bool ConfirmSaveChanges ()
{
if (!IsDocumentModified)
{
return true;
}
return ShowSaveChangesDialog () switch
{
SaveChangesChoice.Discard => true,
SaveChangesChoice.Save => SaveFile (),
_ => false
};
}
}