Example #1
0
 public GlobalUsingTemplate()
 {
     _useGlobalUsing      = true;
     _autoLoadDomainUsing = true;
     UsingRecorder        = new();
     _script = new StringBuilder(200);
 }
Example #2
0
 private NatashaReferenceDomain() : base()
 {
     References    = new();
     UsingRecorder = new();
     LoadAssemblyReferencsWithPath   += NatashaReferenceDomain_LoadAssemblyReferencsWithPath;
     LoadAssemblyReferenceWithStream += NatashaReferenceDomain_LoadAssemblyReferenceWithStream;
 }
Example #3
0
 public NatashaReferenceDomain(string key) : base(key)
 {
     References    = new();
     UsingRecorder = new();
     LoadAssemblyReferencsWithPath   += NatashaReferenceDomain_LoadAssemblyReferencsWithPath;
     LoadAssemblyReferenceWithStream += NatashaReferenceDomain_LoadAssemblyReferenceWithStream;
 }
Example #4
0
 internal T RecordUsing(NatashaUsingCache usingCache)
 {
     UsingRecorder.Using(usingCache._usings);
     return(Link);
 }
Example #5
0
        //internal static readonly ConcurrentDictionary<CSharpCompilation, HashSet<string>> _usingsCache;
        //static AmbiguityUsings()
        //{
        //    _usingsCache = new ConcurrentDictionary<CSharpCompilation, HashSet<string>>();
        //}

        private static CSharpCompilation HandlerCS0104(SyntaxTree syntaxTree, CSharpCompilation compilation, NatashaUsingCache usings)
        {
            var semantiModel = compilation.GetSemanticModel(syntaxTree);
            var errors       = semantiModel.GetDiagnostics();

            if (errors.Length > 0)
            {
                CompilationUnitSyntax root = syntaxTree.GetCompilationUnitRoot();
                var removeCache            = new HashSet <UsingDirectiveSyntax>();
                foreach (var diagnostic in errors)
                {
                    if (diagnostic.Id == "CS0104")
                    {
                        var  info        = semantiModel.GetSymbolInfo(diagnostic.GetSyntaxNode(root));
                        bool stepLastest = false;
                        for (int i = 0; i < info.CandidateSymbols.Length - 1; i++)
                        {
                            var spaceName = info.CandidateSymbols[i] !.OriginalDefinition.ContainingNamespace.Name;
                            if (!usings.HasUsing(spaceName))
                            {
                                var node = (from usingDeclaration in root.Usings
                                            where usingDeclaration.Name.ToString() == spaceName
                                            select usingDeclaration).FirstOrDefault();
                                if (node != null)
                                {
                                    removeCache.Add(node);
                                }
                            }
                            else
                            {
                                stepLastest = true;
                            }
                        }
                        if (stepLastest)
                        {
                            var spaceName = info.CandidateSymbols[info.CandidateSymbols.Length - 1] !.OriginalDefinition.ContainingNamespace.ToString();

                            var node = (from usingDeclaration in root.Usings
                                        where usingDeclaration.Name.ToString() == spaceName
                                        select usingDeclaration).FirstOrDefault();
                            if (node != null)
                            {
                                removeCache.Add(node);
                            }
                        }
                    }
                }

                if (removeCache.Count > 0)
                {
                    compilation = compilation.ReplaceSyntaxTree(syntaxTree, root.RemoveNodes(removeCache, SyntaxRemoveOptions.KeepNoTrivia) !.SyntaxTree);
                }
            }
            return(compilation);
        }