Beispiel #1
0
        public StringBuilder GetUsingBuilder()
        {
#if DEBUG
            Stopwatch stopwatch = new();
            stopwatch.Start();
#endif
            var usingScript = new StringBuilder();

            //如果用户想使用自定义的Using
            if (!_useGlobalUsing)
            {
                foreach (var @using in UsingRecorder._usings)
                {
                    usingScript.AppendLine($"using {@using};");
                }
                //使用全局Using
                if (_autoLoadDomainUsing)
                {
                    foreach (var @using in AssemblyBuilder.Domain.UsingRecorder._usings)
                    {
                        if (!UsingRecorder.HasUsing(@using))
                        {
                            usingScript.AppendLine($"using {@using};");
                        }
                    }
                }
            }
            else
            {
                //使用全局Using
                if (_autoLoadDomainUsing && AssemblyBuilder.Domain.Name != "Default")
                {
                    foreach (var @using in AssemblyBuilder.Domain.UsingRecorder._usings)
                    {
                        if (!DefaultUsing.HasElement(@using) && !UsingRecorder.HasUsing(@using))
                        {
                            usingScript.AppendLine($"using {@using};");
                        }
                    }
                }

                //把当前域中的using全部加上
                foreach (var @using in UsingRecorder._usings)
                {
                    //如果全局已经存在using了,就不加了
                    if (!DefaultUsing.HasElement(@using))
                    {
                        usingScript.AppendLine($"using {@using};");
                    }
                }
                usingScript.Append(DefaultUsing.UsingScript);
            }
#if DEBUG
            stopwatch.StopAndShowCategoreInfo("[using]", "using 组合", 3);
#endif
            return(usingScript);
        }
Beispiel #2
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);
        }