-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMainWindow.FileManagement.vb
More file actions
executable file
·401 lines (333 loc) · 15.4 KB
/
Copy pathMainWindow.FileManagement.vb
File metadata and controls
executable file
·401 lines (333 loc) · 15.4 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
' MainWindow.FileManagement.vb - File management operations for MainWindow
Imports Gtk
Imports System
Imports System.IO
Imports System.Threading.Tasks
Imports SimpleIDE.Models
Imports SimpleIDE.Interfaces
Imports SimpleIDE.Utilities
Partial Public Class MainWindow
' ===== File Management Operations =====
' Note: ReloadF ile is already implemented in MainWindow.Editor.vb as an Async Sub
''' <summary>
''' Show notification when external file change is detected
''' </summary>
Private Sub ShowFileChangedNotification(vFilePath As String)
Try
If Not pOpenTabs.ContainsKey(vFilePath) Then Return
Dim lTabInfo As TabInfo = pOpenTabs(vFilePath)
' Build message and appropriate buttons based on whether the file has unsaved changes
Dim lMessage As String = $"The file '{System.IO.Path.GetFileName(vFilePath)}' has been modified outside the editor."
Dim lResponse As ResponseType
If lTabInfo.Modified Then
lMessage &= $"{Environment.NewLine}{Environment.NewLine}You have unsaved changes. What would you Like To Do?"
lResponse = ShowCustomButtonDialog(
MessageType.Warning, lMessage,
New String() {"Keep My Changes", "Reload from Disk", "Compare"},
New ResponseType() {ResponseType.No, ResponseType.Yes, ResponseType.Apply})
Else
lMessage &= $"{Environment.NewLine}{Environment.NewLine}Would you Like To reload it?"
lResponse = ShowCustomButtonDialog(
MessageType.Warning, lMessage,
New String() {"Keep Current", "Reload"},
New ResponseType() {ResponseType.No, ResponseType.Yes})
End If
Select Case lResponse
Case ResponseType.Yes
' User wants to reload from disk - go through IEditor.LoadContent (not
' SourceFileInfo.LoadContent directly), since the editor's version also
' clamps/resets the cursor, clears selection/undo history, and redraws;
' skipping that left the cursor able to point past the reloaded content's
' end and the view stale until an unrelated redraw happened to occur
lTabInfo.Editor.LoadContent()
Case ResponseType.No
' User wants to keep current version
#If DEBUG Then
Console.WriteLine($"Keeping current version Of {vFilePath}")
#End If
' Mark as modified if it wasn't already
If Not lTabInfo.Modified AndAlso lTabInfo.Editor IsNot Nothing Then
' The file differs from disk, so mark it as modified
lTabInfo.Modified = True
lTabInfo.Editor.IsModified = True
UpdateTabLabel(lTabInfo)
End If
Case ResponseType.Apply
' Future: Show diff/compare window
ShowInfo("Compare", "File comparison feature coming soon!")
End Select
Catch ex As Exception
Console.WriteLine($"ShowFileChangedNotification error: {ex.Message}")
End Try
End Sub
' Show notification when external file is deleted
Private Sub ShowFileDeletedNotification(vFilePath As String)
Try
If Not pOpenTabs.ContainsKey(vFilePath) Then Return
Dim lTabInfo As TabInfo = pOpenTabs(vFilePath)
' Show warning dialog
Dim lResponse As ResponseType = ShowCustomButtonDialog(
MessageType.Warning,
$"the file '{System.IO.Path.GetFileName(vFilePath)}' has been deleted outside the Editor.",
New String() {"Keep in Editor", "Close Tab"},
New ResponseType() {ResponseType.Yes, ResponseType.No})
If lResponse = ResponseType.No Then
CloseTab(lTabInfo)
Else
' Mark as modified since it no longer exists on disk
lTabInfo.Modified = True
UpdateTabLabel(lTabInfo)
End If
Catch ex As Exception
Console.WriteLine($"ShowFileDeletedNotification error: {ex.Message}")
End Try
End Sub
' Rename a tab when file is renamed externally
Private Sub RenameTab(vOldPath As String, vNewPath As String)
Try
If Not pOpenTabs.ContainsKey(vOldPath) Then Return
Dim lTabInfo As TabInfo = pOpenTabs(vOldPath)
' Update tab info
lTabInfo.FilePath = vNewPath
' Update the tab label using the existing UpdateTabLabel method
UpdateTabLabel(lTabInfo)
' Update dictionary
pOpenTabs.Remove(vOldPath)
pOpenTabs(vNewPath) = lTabInfo
' Update UI
UpdateWindowTitle()
' Update file watcher - a rename into a different directory needs its own
' Watch/Unwatch pair, since each directory has its own underlying watcher
If pFileSystemWatcher IsNot Nothing AndAlso
Not String.Equals(System.IO.Path.GetDirectoryName(vOldPath), System.IO.Path.GetDirectoryName(vNewPath), StringComparison.Ordinal) Then
pFileSystemWatcher.UnwatchFile(vOldPath)
pFileSystemWatcher.WatchFile(vNewPath)
End If
' Show notification
Dim lStatusContext As UInteger = pStatusBar.GetContextId("rename")
pStatusBar.Pop(lStatusContext)
pStatusBar.Push(lStatusContext, $"File renamed: {System.IO.Path.GetFileName(vOldPath)} → {System.IO.Path.GetFileName(vNewPath)}")
Catch ex As Exception
Console.WriteLine($"RenameTab error: {ex.Message}")
End Try
End Sub
' Get tab info for a file path
Public Function GetTabInfoForFile(vFilePath As String) As TabInfo
If pOpenTabs.ContainsKey(vFilePath) Then
Return pOpenTabs(vFilePath)
End If
Return Nothing
End Function
' Update LastSaved timestamp when saving files
Private Sub UpdateLastSavedTimestamp(vTabInfo As TabInfo)
Try
If Not String.IsNullOrEmpty(vTabInfo.FilePath) AndAlso File.Exists(vTabInfo.FilePath) Then
vTabInfo.LastSaved = File.GetLastWriteTime(vTabInfo.FilePath)
Else
vTabInfo.LastSaved = DateTime.Now
End If
Catch ex As Exception
Console.WriteLine($"UpdateLastSavedTimestamp error: {ex.Message}")
vTabInfo.LastSaved = DateTime.Now
End Try
End Sub
Private Sub SetupFileSystemWatcher()
Try
pFileSystemWatcher = New Utilities.FileSystemWatcher(pSettingsManager)
AddHandler pFileSystemWatcher.FileChanged, AddressOf OnExternalFileChanged
AddHandler pFileSystemWatcher.FileDeleted, AddressOf OnExternalFileDeleted
AddHandler pFileSystemWatcher.FileRenamed, AddressOf OnExternalFileRenamed
Catch ex As Exception
Console.WriteLine($"SetupFileSystemWatcher error: {ex.Message}")
End Try
End Sub
Private Sub OnExternalFileChanged(vFilePath As String)
Try
' Handle external file changes
If pOpenTabs.ContainsKey(vFilePath) Then
' Show file changed notification
Application.Invoke(Sub()
ShowFileChangedNotification(vFilePath)
End Sub)
End If
Catch ex As Exception
Console.WriteLine($"OnExternalFileChanged error: {ex.Message}")
End Try
End Sub
Private Sub OnExternalFileDeleted(vFilePath As String)
Try
' Handle external file deletion
If pOpenTabs.ContainsKey(vFilePath) Then
Application.Invoke(Sub()
ShowFileDeletedNotification(vFilePath)
End Sub)
End If
Catch ex As Exception
Console.WriteLine($"OnExternalFileDeleted error: {ex.Message}")
End Try
End Sub
Private Sub OnExternalFileRenamed(vOldPath As String, vNewPath As String)
Try
' Handle external file rename
If pOpenTabs.ContainsKey(vOldPath) Then
Application.Invoke(Sub()
RenameTab(vOldPath, vNewPath)
End Sub)
End If
Catch ex As Exception
Console.WriteLine($"OnExternalFileRenamed error: {ex.Message}")
End Try
End Sub
' Save current file
Private Sub OnSave(vSender As Object, vArgs As EventArgs)
Try
Dim lCurrentTab As TabInfo = GetCurrentTabInfo()
If lCurrentTab IsNot Nothing Then SaveFile(lCurrentTab)
Catch ex As Exception
Console.WriteLine($"OnSave error: {ex.Message}")
ShowError("Save failed", ex.Message)
End Try
End Sub
' Quit application
Private Sub OnQuit(vSender As Object, vArgs As EventArgs)
Try
' Check for unsaved changes
If CheckForUnsavedChanges() Then
Application.Quit()
End If
Catch ex As Exception
Console.WriteLine($"OnQuit error: {ex.Message}")
ShowError("Quit failed", ex.Message)
End Try
End Sub
' Navigate to next build error
Private Sub OnNavigateToNextError(vSender As Object, vArgs As EventArgs)
Try
NavigateToNextError()
Catch ex As Exception
Console.WriteLine($"OnNavigateToNextError error: {ex.Message}")
End Try
End Sub
' Navigate to previous build error
Private Sub OnNavigateToPreviousError(vSender As Object, vArgs As EventArgs)
Try
NavigateToPreviousError()
Catch ex As Exception
Console.WriteLine($"NavigateToPreviousError error: {ex.Message}")
End Try
End Sub
''' <summary>
''' Index into the current build's error list of the last error navigated to via
''' NavigateToNextError/NavigateToPreviousError, -1 meaning "none yet" (the next call
''' lands on the first error). Deliberately not reset on rebuild - GetErrors() always
''' reflects the latest build, and the modulo wraparound in both navigators tolerates
''' the list having shrunk or grown since this index was last set
''' </summary>
Private pCurrentErrorIndex As Integer = -1
Private Sub NavigateToNextError()
Try
If pBuildOutputPanel Is Nothing Then Return
Dim lErrors As List(Of BuildError) = pBuildOutputPanel.GetErrors()
If lErrors Is Nothing OrElse lErrors.Count = 0 Then
UpdateStatusBar("No build errors")
Return
End If
pCurrentErrorIndex = ((pCurrentErrorIndex + 1) Mod lErrors.Count + lErrors.Count) Mod lErrors.Count
NavigateToBuildError(lErrors(pCurrentErrorIndex), lErrors.Count)
Catch ex As Exception
Console.WriteLine($"NavigateToNextError error: {ex.Message}")
End Try
End Sub
Private Sub NavigateToPreviousError()
Try
If pBuildOutputPanel Is Nothing Then Return
Dim lErrors As List(Of BuildError) = pBuildOutputPanel.GetErrors()
If lErrors Is Nothing OrElse lErrors.Count = 0 Then
UpdateStatusBar("No build errors")
Return
End If
pCurrentErrorIndex = ((pCurrentErrorIndex - 1) Mod lErrors.Count + lErrors.Count) Mod lErrors.Count
NavigateToBuildError(lErrors(pCurrentErrorIndex), lErrors.Count)
Catch ex As Exception
Console.WriteLine($"NavigateToPreviousError error: {ex.Message}")
End Try
End Sub
''' <summary>
''' Opens (or switches to) the tab for a build error and places the cursor at its
''' location, reusing the same navigation logic OnFindResultSelected already uses for
''' Find Results
''' </summary>
Private Sub NavigateToBuildError(vError As BuildError, vTotalCount As Integer)
Try
If String.IsNullOrEmpty(vError.FilePath) Then Return
OnFindResultSelected(vError.FilePath, vError.Line, vError.Column)
UpdateStatusBar($"Error {pCurrentErrorIndex + 1} of {vTotalCount}: {vError.Message}")
Catch ex As Exception
Console.WriteLine($"NavigateToBuildError error: {ex.Message}")
End Try
End Sub
' Open specific file at line/column
Private Sub OpenSpecificFile(vFilePath As String, vLine As Integer, vColumn As Integer)
Try
If String.IsNullOrEmpty(vFilePath) Then Return
' Open the file
OpenFile(vFilePath)
' Navigate to line/column
Dim lEditor As IEditor = GetCurrentEditor()
If lEditor IsNot Nothing AndAlso lEditor.FilePath = vFilePath Then
lEditor.GoToPosition(New EditorPosition(vLine, vColumn))
lEditor.GrabFocus()
End If
Catch ex As Exception
Console.WriteLine($"OpenSpecificFile error: {ex.Message}")
End Try
End Sub
Private Function CheckForUnsavedChanges() As Boolean
Try
' Check all open tabs for unsaved changes
for each lTabEntry in pOpenTabs
Dim lTabInfo As TabInfo = lTabEntry.Value
If lTabInfo.Modified Then
Dim lResponse As Integer = ShowQuestion(
"Unsaved Changes",
$"You have unsaved changes in '{System.IO.Path.GetFileName(lTabInfo.FilePath)}'. Do you want to save them?"
)
If lResponse = CInt(ResponseType.Yes) Then
If Not SaveFile(lTabInfo) Then
Return False ' Cancel if save fails
End If
ElseIf lResponse = CInt(ResponseType.Cancel) Then
Return False ' Cancel the operation
End If
End If
Next
Return True ' All files handled, okay to proceed
Catch ex As Exception
Console.WriteLine($"CheckForUnsavedChanges error: {ex.Message}")
Return False
End Try
End Function
' Replace: SimpleIDE.MainWindow.SaveFile
''' <summary>
''' Save a file through ProjectManager/SourceFileInfo system
''' </summary>
Private Function SaveFile(vTabInfo As TabInfo) As Boolean
Try
' Save through the editor which will sync states properly
Dim lResult As Boolean = vTabInfo.Editor.SaveContent()
' CRITICAL: If save was successful, update TabInfo state
If lResult Then
vTabInfo.Modified = False
UpdateTabLabel(vTabInfo)
#If DEBUG Then
Console.WriteLine($"SaveFile: Saved and updated tab state for {vTabInfo.FilePath}")
#End If
End If
Return lResult
Catch ex As Exception
Console.WriteLine($"SaveFile error: {ex.Message}")
ShowError("Save File error", $"Failed to save file: {ex.Message}")
Return False
End Try
End Function
End Class