예제 #1
0
        public static IEnumerable <string> GetCodeAutocompletionList(string Code, int CarretPosition)
        {
            CodeAutocompletion_AlreadyEnteredStrLen = 0;
            string FullCode = DynamicCompile.GetCodeTemplate(Code, false);

            SyntaxTree        syntaxTree    = CSharpSyntaxTree.ParseText(FullCode);
            CSharpCompilation compilation   = CSharpCompilation.Create("SharpSmartMath.cs").AddReferences(DynamicCompile.GetMetadataReferences()).AddSyntaxTrees(syntaxTree);
            SemanticModel     semanticModel = compilation.GetSemanticModel(syntaxTree);

            int   TemplateOffset = DynamicCompile.CodeTemplate.IndexOf(DynamicCompile.Template_CodeString);
            Match match          = Regex.Match(Code.Substring(0, CarretPosition), @"\S*", RegexOptions.RightToLeft);

            if (!match.Success || match.Length <= 0 || match.Index + match.Length < CarretPosition)
            {
                return(null);
            }

            TextSpan   MatchSpan  = new TextSpan(TemplateOffset + match.Index, match.Length);
            SyntaxNode syntaxNode = syntaxTree.GetRoot().DescendantNodes(MatchSpan).Where(s => !(s is IdentifierNameSyntax)).Last();

            if (syntaxNode is MemberAccessExpressionSyntax)
            {
                MemberAccessExpressionSyntax memberAccessNode = (MemberAccessExpressionSyntax)syntaxNode;
                ITypeSymbol          lhsType   = semanticModel.GetTypeInfo(memberAccessNode.Expression).Type;
                IEnumerable <string> WordsList = lhsType.GetMembers().Where(
                    s => s.CanBeReferencedByName &&
                    s.DeclaredAccessibility == Accessibility.Public &&
                    s.Name.StartsWith(memberAccessNode.Name.ToString())
                    ).Select(s => s.Name).Distinct();

                CodeAutocompletion_AlreadyEnteredStrLen = memberAccessNode.Name.GetLocation().SourceSpan.Length;
                return(WordsList);
            }

            return(null);
        }
예제 #2
0
        public static string FormatCode(string Code)
        {
            string FullCode = DynamicCompile.GetCodeTemplate(Code, false);

            Workspace workspace = new AdhocWorkspace();
            Solution  solution  = workspace.CurrentSolution;
            Project   project   = solution.AddProject("SharpSmartMath", "SharpSmartMath", LanguageNames.CSharp);

            Document DocumentTemplate  = project.AddDocument("SharpSmartMath.cs", DynamicCompile.CodeTemplate);
            string   FormattedTemplate = Formatter.FormatAsync(DocumentTemplate).Result.GetTextAsync().Result.ToString();

            Document DocumentCode  = project.AddDocument("SharpSmartMath.cs", FullCode);
            string   FormattedCode = Formatter.FormatAsync(DocumentCode).Result.GetTextAsync().Result.ToString();

            int CodeStartPos = FormattedTemplate.IndexOf(DynamicCompile.Template_CodeString);
            int CodeEndLen   = FormattedTemplate.Length - CodeStartPos - DynamicCompile.Template_CodeString.Length;

            FormattedCode = FormattedCode.Remove(FormattedCode.Length - CodeEndLen, CodeEndLen).Remove(0, CodeStartPos);

            string[] Lines       = FormattedCode.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            int      SpacesCount = Lines.Min(str => str.Length > 0 ? str.Length - str.TrimStart().Length : int.MaxValue);

            return(string.Join(Environment.NewLine, Lines.Select(str => str.Remove(0, Math.Min(SpacesCount, str.Length)))));
        }