public static IDisposable With(
            [CanBeNull] this ITextTemplatingComponents components,
            [CanBeNull] IVsHierarchy hierarchy,
            [CanBeNull] FileSystemPath inputFilePath
            )
        {
            if (components == null)
            {
                return(Disposable.Empty);
            }

            object oldHierarchy     = components.Hierarchy;
            string oldInputFileName = components.InputFile;

            return(Disposable.CreateBracket(
                       () => {
                components.Hierarchy = hierarchy;
                components.InputFile = inputFilePath.IsNullOrEmpty() ? null : inputFilePath.FullPath;
            },
                       () => {
                components.Hierarchy = oldHierarchy;
                components.InputFile = oldInputFileName;
            },
                       false
                       ));
        }
Example #2
0
        /// <summary>
        /// Called when the associated data file changed: added/removed assemblies or includes.
        /// </summary>
        /// <param name="dataDiff">The difference between the old and new data.</param>
        private void OnDataFileChanged([NotNull] T4FileDataDiff dataDiff)
        {
            _shellLocks.AssertWriteAccessAllowed();

            bool hasFileChanges = ResolveMacros(dataDiff.AddedMacros);
            bool hasChanges     = hasFileChanges;

            ITextTemplatingComponents components = _t4Environment.Components.CanBeNull;

            using (components.With(TryGetVsHierarchy(), _projectFile.Location)) {
                // removes the assembly references from the old assembly directives
                foreach (string removedAssembly in dataDiff.RemovedAssemblies)
                {
                    string assembly = removedAssembly;
                    if (components != null)
                    {
                        assembly = components.Host.ResolveAssemblyReference(assembly);
                    }

                    IAssemblyCookie cookie;
                    if (!_assemblyReferences.TryGetValue(assembly, out cookie))
                    {
                        continue;
                    }

                    _assemblyReferences.Remove(assembly);
                    hasChanges = true;
                    cookie.Dispose();
                }

                // adds assembly references from the new assembly directives
                foreach (string addedAssembly in dataDiff.AddedAssemblies)
                {
                    string assembly = addedAssembly;
                    if (components != null)
                    {
                        assembly = components.Host.ResolveAssemblyReference(assembly);
                    }

                    if (assembly == null)
                    {
                        continue;
                    }

                    if (_assemblyReferences.ContainsKey(assembly))
                    {
                        continue;
                    }

                    IAssemblyCookie cookie = TryAddReference(assembly);
                    if (cookie != null)
                    {
                        hasChanges = true;
                    }
                }
            }

            if (!hasChanges)
            {
                return;
            }

            // tells the world the module has changed
            var changeBuilder = new PsiModuleChangeBuilder();

            changeBuilder.AddModuleChange(this, PsiModuleChange.ChangeType.Modified);

            if (hasFileChanges)
            {
                GetPsiServices().MarkAsDirty(SourceFile);
            }

            _shellLocks.ExecuteOrQueue("T4PsiModuleChange",
                                       () => _changeManager.ExecuteAfterChange(
                                           () => _shellLocks.ExecuteWithWriteLock(
                                               () => _changeManager.OnProviderChanged(this, changeBuilder.Result, SimpleTaskExecutor.Instance))
                                           )
                                       );
        }