Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MonoDevelopMetadataReference.cs « MonoDevelop.Ide.TypeSystem « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e38bcb05ce56099ba2267a335dba46b9857abbd6 (plain)
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using Microsoft.CodeAnalysis;
using MonoDevelop.Core;
using MonoDevelop.FSW;
using MonoDevelop.Projects;

namespace MonoDevelop.Ide.TypeSystem
{
	[DebuggerDisplay ("{GetDebuggerDisplay(),nq}")]
	sealed partial class MonoDevelopMetadataReference : IDisposable
	{
		readonly MonoDevelopMetadataReferenceManager _provider;
		readonly MetadataReferenceProperties _properties;

		Snapshot _currentSnapshot;
		public FilePath FilePath { get; }

		public event EventHandler<MetadataReferenceUpdatedEventArgs> SnapshotUpdated;

		public MonoDevelopMetadataReference (
			MonoDevelopMetadataReferenceManager provider,
			string filePath,
			MetadataReferenceProperties properties)
		{
			Contract.Requires (properties.Kind == MetadataImageKind.Assembly);

			FilePath = filePath;
			_provider = provider;
			_properties = properties;

			FileWatcherService.WatchDirectories (this, new [] { FilePath.ParentDirectory }).Ignore ();
			FileService.FileChanged += OnUpdatedOnDisk;
		}

		public MetadataReferenceProperties Properties => _properties;

		public PortableExecutableReference CurrentSnapshot {
			get {
				if (_currentSnapshot == null) {
					UpdateSnapshot ();
				}

				return _currentSnapshot;
			}
		}

		void OnUpdatedOnDisk (object sender, FileEventArgs e)
		{
			foreach (var file in e) {
				if (file.FileName == FilePath) {
					var args = new MetadataReferenceUpdatedEventArgs (this);
					SnapshotUpdated?.Invoke (this, args);
					return;
				}
			}
		}

		public void Dispose ()
		{
			FileService.FileChanged -= OnUpdatedOnDisk;
			FileWatcherService.WatchDirectories (this, null).Ignore ();
		}

		internal void UpdateSnapshot () => _currentSnapshot = new Snapshot (_provider, Properties, FilePath);

		string GetDebuggerDisplay () => Path.GetFileName (FilePath);
	}

	class MetadataReferenceUpdatedEventArgs : EventArgs
	{
		public PortableExecutableReference OldSnapshot { get; }
		public Lazy<PortableExecutableReference> NewSnapshot { get; }

		public MetadataReferenceUpdatedEventArgs (MonoDevelopMetadataReference reference)
		{
			OldSnapshot = reference.CurrentSnapshot;
			NewSnapshot = new Lazy<PortableExecutableReference> (() => {
				reference.UpdateSnapshot ();
				return reference.CurrentSnapshot;
			});
		}
	}
}