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

CommentTasksProvider.cs « MonoDevelop.Ide.Tasks « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f42d994eaf4ae5ed04b13bf67d68556b747f2a1 (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
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
//
// CommentTasksProvider.cs
//
// Author:
//       Marius Ungureanu <maungu@microsoft.com>
//
// Copyright (c) 2018
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Threading;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Implementation.TodoComments;
using MonoDevelop.Core;
using MonoDevelop.Ide.TypeSystem;
using MonoDevelop.Projects;
using System.Linq;
using System.Threading.Tasks;
using MonoDevelop.Ide.Composition;

namespace MonoDevelop.Ide.Tasks
{
	partial class CommentTasksProvider
	{
		TaskService taskService;

		public static void Initialize ()
		{
			Runtime.ServiceProvider.WhenServiceInitialized<CompositionManager> (compositionManager => {
				var todoListProvider = compositionManager.GetExportedValue<ITodoListProvider> ();
				todoListProvider.TodoListUpdated += OnTodoListUpdated;
			});

			Runtime.ServiceProvider.WhenServiceInitialized<RootWorkspace> (workspace => {
				workspace.SolutionLoaded += OnSolutionLoaded;
				workspace.WorkspaceItemClosed += OnWorkspaceItemClosed;
				Legacy.Initialize ();
			});
			CommentTag.SpecialCommentTagsChanged += OnSpecialTagsChanged;
		}

		static bool TryGetDocument (Microsoft.CodeAnalysis.Common.UpdatedEventArgs args, out Document doc, out MonoDevelop.Projects.Project project)
		{
			doc = null;
			project = null;

			if (!(args.Workspace is MonoDevelopWorkspace ws))
				return false;

			doc = ws.GetDocument (args.DocumentId);
			if (doc == null)
				return false;

			project = ws.GetMonoProject (args.ProjectId);
			if (project == null)
				return false;

			return true;
		}

		static object lockObject = new object ();
		static Dictionary<object, TodoItemsUpdatedArgs> cachedUntilViewCreated = new Dictionary<object, TodoItemsUpdatedArgs> ();

		internal static void LoadCachedContents ()
		{
			lock (lockObject) {
				if (cachedUntilViewCreated == null)
					return;

				if (triggerLoad == null || triggerLoad.Invoke (cachedUntilViewCreated.Count)) {
					var changes = cachedUntilViewCreated.Values.Select (x => ToCommentTaskChange (x)).Where (x => x != null).ToList ();
					IdeServices.TaskService.InformCommentTasks (new CommentTasksChangedEventArgs (changes));
					cachedUntilViewCreated = null;
					triggerLoad = null;
				}
			}
		}

		public static CommentTaskChange ToCommentTaskChange (TodoItemsUpdatedArgs args)
		{
			if (!TryGetDocument (args, out var doc, out var project))
				return null;

			var file = doc.Name;
			var tags = args.TodoItems.Length == 0 ? (IReadOnlyList<Tag>)null : args.TodoItems.SelectAsArray (x => x.ToTag ());
			return new CommentTaskChange (file, tags, project);
		}

		// Test helper utilities so we can test multiple tests for cached
		#region Test Helpers
		static Func<int, bool> triggerLoad;
		internal static void ResetCachedContents (Func<int, bool> shouldTriggerLoad)
		{
			lock (lockObject) {
				cachedUntilViewCreated = new Dictionary<object, TodoItemsUpdatedArgs> ();
				triggerLoad = shouldTriggerLoad;
			}
		}

		internal static int GetCachedContentsCount ()
		{
			lock (lockObject) {
				if (cachedUntilViewCreated == null)
					return -1;
				return cachedUntilViewCreated.Count;
			}
		}

		#endregion

		static bool TryCache (TodoItemsUpdatedArgs args)
		{
			if (cachedUntilViewCreated == null)
				return false;

			lock (lockObject) {
				if (cachedUntilViewCreated == null)
					return false;

				cachedUntilViewCreated [args.Id] = args;

				if (triggerLoad != null)
					LoadCachedContents ();
				return true;
			}
		}

		static void OnTodoListUpdated (object sender, TodoItemsUpdatedArgs args)
		{
			if (TryCache (args))
				return;

			var change = ToCommentTaskChange (args);
			if (change != null)
				IdeServices.TaskService.InformCommentTasks (new CommentTasksChangedEventArgs (new [] { change }));
		}

		static async void OnSolutionLoaded (object sender, SolutionEventArgs args)
		{
			var ws = await IdeApp.TypeSystemService.GetWorkspaceAsync (args.Solution);
			UpdateWorkspaceOptions (ws);
		}

		static void OnWorkspaceItemClosed (object sender, WorkspaceItemEventArgs args)
		{
			if (args.Item is MonoDevelop.Projects.Solution sol) {
				var ws = IdeApp.TypeSystemService.GetWorkspace (sol);

				lock (lockObject) {
					if (cachedUntilViewCreated == null)
						return;

					cachedUntilViewCreated = cachedUntilViewCreated.Where (x => x.Value.Workspace != ws).ToDictionary (x => x.Key, x => x.Value);
				}
			}
		}

		static void UpdateWorkspaceOptions (Microsoft.CodeAnalysis.Workspace ws)
		{
			// Roslyn uses | as separator.
			ws.Options = ws.Options.WithChangedOption (TodoCommentOptions.TokenList, CommentTag.ToString (CommentTag.SpecialCommentTags, "|"));
		}

		static void OnSpecialTagsChanged (object sender, EventArgs args)
		{
			foreach (var ws in IdeApp.TypeSystemService.AllWorkspaces)
				UpdateWorkspaceOptions (ws);
		}
	}
}