|
1 | 1 | using Cody.Core.DocumentSync; |
2 | 2 | using Cody.Core.Logging; |
| 3 | +using Cody.Core.Settings; |
3 | 4 | using Cody.Core.Trace; |
4 | 5 | using Microsoft.VisualStudio; |
5 | 6 | using Microsoft.VisualStudio.Editor; |
|
12 | 13 | using System.Collections.Generic; |
13 | 14 | using System.Diagnostics; |
14 | 15 | using System.Linq; |
15 | | -using Cody.Core.Settings; |
| 16 | +using System.Threading; |
16 | 17 |
|
17 | 18 | namespace Cody.VisualStudio.Services |
18 | 19 | { |
@@ -248,67 +249,90 @@ int IVsRunningDocTableEvents.OnAfterSave(uint docCookie) |
248 | 249 |
|
249 | 250 | int IVsRunningDocTableEvents.OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame) |
250 | 251 | { |
251 | | - trace.TraceEvent("OnBeforeDocumentWindowShow"); |
252 | | - if (lastShowDocCookie != docCookie) |
| 252 | + try |
253 | 253 | { |
254 | | - var path = rdt.GetDocumentInfo(docCookie).Moniker; |
255 | | - if (path == null) return VSConstants.S_OK; |
256 | | - |
257 | | - if (!isSubscribed.Contains(docCookie)) |
| 254 | + trace.TraceEvent("OnBeforeDocumentWindowShow"); |
| 255 | + if (lastShowDocCookie != docCookie) |
258 | 256 | { |
259 | | - trace.TraceEvent("OnSubscribeDocument", path); |
| 257 | + var path = rdt.GetDocumentInfo(docCookie).Moniker; |
| 258 | + if (path == null) return VSConstants.S_OK; |
260 | 259 |
|
261 | | - var content = rdt.GetRunningDocumentContents(docCookie); |
262 | | - if (content == null) return VSConstants.S_OK; |
263 | | - |
264 | | - var textView = GetVsTextView(pFrame); |
265 | | - if (textView != null) |
| 260 | + if (!isSubscribed.Contains(docCookie)) |
266 | 261 | { |
267 | | - var wpfTextView = editorAdaptersFactoryService.GetWpfTextView(textView); |
268 | | - if (wpfTextView != null) |
269 | | - { |
270 | | - Subscribe(wpfTextView); |
| 262 | + trace.TraceEvent("OnSubscribeDocument", path); |
271 | 263 |
|
272 | | - if (!openNotificationSend.Contains(docCookie)) |
273 | | - { |
274 | | - |
275 | | - var docRange = GetDocumentSelection(wpfTextView); |
276 | | - var visibleRange = GetVisibleRange(wpfTextView); |
| 264 | + var content = rdt.GetRunningDocumentContents(docCookie); |
| 265 | + if (content == null) return VSConstants.S_OK; |
277 | 266 |
|
278 | | - documentActions.OnOpened(path, content, visibleRange, docRange); |
279 | | - openNotificationSend.Add(docCookie); |
| 267 | + var textView = GetVsTextView(pFrame); |
| 268 | + if (textView != null) |
| 269 | + { |
| 270 | + var wpfTextView = editorAdaptersFactoryService.GetWpfTextView(textView); |
| 271 | + if (wpfTextView != null) |
| 272 | + { |
| 273 | + Subscribe(wpfTextView, path, docCookie, content); |
280 | 274 | } |
281 | | - |
282 | | - isSubscribed.Add(docCookie); |
283 | 275 | } |
284 | 276 | } |
285 | | - } |
286 | 277 |
|
287 | | - trace.TraceEvent("OnDocumentFocus", path); |
288 | | - documentActions.OnFocus(path); |
| 278 | + trace.TraceEvent("OnDocumentFocus", path); |
| 279 | + documentActions.OnFocus(path); |
289 | 280 |
|
290 | | - lastShowDocCookie = docCookie; |
| 281 | + lastShowDocCookie = docCookie; |
| 282 | + } |
| 283 | + } |
| 284 | + catch (Exception ex) |
| 285 | + { |
| 286 | + log.Error("OnBeforeDocumentWindowShow failed", ex); |
291 | 287 | } |
292 | 288 |
|
293 | 289 | return VSConstants.S_OK; |
294 | 290 | } |
295 | 291 |
|
296 | | - private void Subscribe(IWpfTextView textView) |
| 292 | + private void Subscribe(IWpfTextView textView, string path, uint docCookie, string content) |
297 | 293 | { |
298 | | - textView.TextBuffer.Properties.AddProperty(typeof(IWpfTextView), textView); |
299 | | - textView.Selection.SelectionChanged += OnSelectionChanged; |
300 | | - textView.TextBuffer.ChangedLowPriority += OnTextBufferChanged; |
301 | | - textView.Closed += UnsubscribeOnClosed; |
| 294 | + try |
| 295 | + { |
| 296 | + |
| 297 | + textView.TextBuffer.Properties.AddProperty(typeof(IWpfTextView), textView); |
| 298 | + textView.Selection.SelectionChanged += OnSelectionChanged; |
| 299 | + textView.TextBuffer.ChangedLowPriority += OnTextBufferChanged; |
| 300 | + textView.Closed += UnsubscribeOnClosed; |
| 301 | + |
| 302 | + if (!openNotificationSend.Contains(docCookie)) |
| 303 | + { |
| 304 | + |
| 305 | + var docRange = GetDocumentSelection(textView); |
| 306 | + var visibleRange = GetVisibleRange(textView); |
| 307 | + |
| 308 | + documentActions.OnOpened(path, content, visibleRange, docRange); |
| 309 | + openNotificationSend.Add(docCookie); |
| 310 | + } |
| 311 | + |
| 312 | + isSubscribed.Add(docCookie); |
| 313 | + |
| 314 | + } |
| 315 | + catch (Exception ex) |
| 316 | + { |
| 317 | + log.Error("Subscribe failed", ex); |
| 318 | + } |
302 | 319 | } |
303 | 320 |
|
304 | 321 | private void UnsubscribeOnClosed(object sender, EventArgs e) |
305 | 322 | { |
306 | | - var textView = (IWpfTextView)sender; |
| 323 | + try |
| 324 | + { |
| 325 | + var textView = (IWpfTextView)sender; |
307 | 326 |
|
308 | | - textView.TextBuffer.Properties.RemoveProperty(typeof(IWpfTextView)); |
309 | | - textView.Selection.SelectionChanged -= OnSelectionChanged; |
310 | | - textView.TextBuffer.ChangedLowPriority -= OnTextBufferChanged; |
311 | | - textView.Closed -= UnsubscribeOnClosed; |
| 327 | + textView.TextBuffer.Properties.RemoveProperty(typeof(IWpfTextView)); |
| 328 | + textView.Selection.SelectionChanged -= OnSelectionChanged; |
| 329 | + textView.TextBuffer.ChangedLowPriority -= OnTextBufferChanged; |
| 330 | + textView.Closed -= UnsubscribeOnClosed; |
| 331 | + } |
| 332 | + catch (Exception ex) |
| 333 | + { |
| 334 | + log.Error("Unsubscribe failed", ex); |
| 335 | + } |
312 | 336 | } |
313 | 337 |
|
314 | 338 | int IVsRunningDocTableEvents.OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame) => VSConstants.S_OK; |
@@ -422,7 +446,7 @@ public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarch |
422 | 446 | { |
423 | 447 | trace.TraceEvent("OnRename"); |
424 | 448 | documentActions.OnRename(pszMkDocumentOld, pszMkDocumentNew); |
425 | | - } |
| 449 | + } |
426 | 450 | return VSConstants.S_OK; |
427 | 451 | } |
428 | 452 | } |
|
0 commit comments