public void TestInit()
        {
            try
            {
                workspace = new AdhocWorkspace();

                project = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), "TestProj", "TestProj",
                                             LanguageNames.CSharp)
                          .WithMetadataReferences(new[]
                {
                    MetadataReference.CreateFromFile(typeof(DotvvmConfiguration).Assembly.Location),
                    MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
                });
                workspace.AddProject(project);

                workspace.AddDocument(project.Id, "test", SourceText.From("class A {}"));

                context = new DothtmlCompletionContext()
                {
                    Configuration   = DotvvmConfiguration.CreateDefault(),
                    RoslynWorkspace = workspace
                };
            }
            catch (ReflectionTypeLoadException ex)
            {
                throw new Exception(string.Join("\r\n", ex.LoaderExceptions.Select(e => e.ToString())));
            }
        }
Пример #2
0
        protected override IEnumerable <SimpleDothtmlCompletion> GetItemsCore(DothtmlCompletionContext context, List <string> tagNameHierarchy, string attributeName)
        {
            var glyph = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupEnum, StandardGlyphItem.GlyphItemPublic);

            return(context.MetadataControlResolver.GetControlAttributeValues(context, tagNameHierarchy, attributeName)
                   .Select(n => new SimpleDothtmlCompletion(n.DisplayText, n.CompletionText, glyph)));
        }
Пример #3
0
        public DothtmlCompletionContext GetCompletionContext(ICompletionSession session)
        {
            var context = new DothtmlCompletionContext()
            {
                CompletionSession = session,
                Tokens            = classifier.Tokens,
                Parser            = parser,
                Tokenizer         = classifier.Tokenizer,
                RoslynWorkspace   = workspace,
                GlyphService      = glyphService,
                DTE                     = dte,
                Configuration           = configurationProvider.GetConfiguration(dte.ActiveDocument.ProjectItem.ContainingProject),
                MetadataControlResolver = MetadataControlResolver
            };

            try
            {
                parser.Parse(classifier.Tokens);
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
                throw;
            }

            return(context);
        }
        public IEnumerable <CompletionData> GetControlAttributeValues(DothtmlCompletionContext context, List <string> tagNameHierarchy, string attributeName)
        {
            allControls.GetOrRetrieve(() => ReloadAllControls(context));

            ControlMetadata         currentControl;
            ControlPropertyMetadata currentProperty;

            GetElementContext(tagNameHierarchy, out currentControl, out currentProperty);

            if (currentControl != null && currentProperty == null)
            {
                var property = currentControl.Properties.FirstOrDefault(p => p.Name == attributeName);
                if (property != null)
                {
                    // it is a real property
                    return(HintPropertyValues(property.Type));
                }
                else
                {
                    // it can be an attached property
                    return(GetAttachedPropertyValues(context, attributeName));
                }
            }
            return(Enumerable.Empty <CompletionData>());
        }
        protected override IEnumerable <SimpleDothtmlCompletion> GetItemsCore(DothtmlCompletionContext context, string directiveName)
        {
            if (string.Equals(directiveName, Constants.ViewModelDirectiveName, StringComparison.InvariantCultureIgnoreCase) ||
                string.Equals(directiveName, Constants.BaseTypeDirective, StringComparison.InvariantCultureIgnoreCase))
            {
                // get icons for intellisense
                var classGlyph     = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupClass, StandardGlyphItem.GlyphItemPublic);
                var interfaceGlyph = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupInterface, StandardGlyphItem.GlyphItemPublic);
                var nameFilter     = string.Empty;

                var currentToken = context.Tokens[context.CurrentTokenIndex];
                if (currentToken.Type == DothtmlTokenType.DirectiveValue)
                {
                    var currentPosition = context.CompletionSession.TextView.Caret.Position.BufferPosition.Position;
                    if (currentPosition != currentToken.StartPosition)
                    {
                        nameFilter = string.Concat(currentToken.Text.Take(currentPosition - currentToken.StartPosition));
                    }
                }

                // get list of all custom types
                var types = typeNames.GetOrRetrieve(() =>
                {
                    return(CompletionHelper.GetSyntaxTrees(context)
                           .SelectMany(i => i.Tree.GetRoot().DescendantNodes().OfType <TypeDeclarationSyntax>()
                                       .Select(n => new { Symbol = i.SemanticModel.GetDeclaredSymbol(n), Compilation = i.Compilation, Node = n })
                                       .Where(n => n.Symbol != null))
                           .Select(t => new CompletionDataWithGlyph()
                    {
                        CompletionData = new CompletionData(
                            $"{t.Symbol.Name} (in namespace {t.Symbol.ContainingNamespace})",
                            t.Symbol.ToString() + ", " + t.Compilation.AssemblyName),
                        Glyph = t.Node is ClassDeclarationSyntax ? classGlyph : interfaceGlyph,
                        Name = t.Symbol.Name,
                        Namespace = t.Symbol.ContainingNamespace.ToString()
                    })
                           .ToList());
                });

                if (!string.IsNullOrWhiteSpace(nameFilter))
                {
                    types = types.Where(w =>
                                        w.Name.StartsWith(nameFilter, StringComparison.OrdinalIgnoreCase) ||
                                        ($"{w.Namespace}.{w.Name}").StartsWith(nameFilter, StringComparison.OrdinalIgnoreCase)).ToList();
                }

                // return completion items
                return(types.Select(t => new[]
                {
                    new SimpleDothtmlCompletion(t.CompletionData.DisplayText, t.CompletionData.CompletionText, t.Glyph),
                    new SimpleDothtmlCompletion(t.CompletionData.CompletionText, t.CompletionData.CompletionText, t.Glyph)
                })
                       .SelectMany(sm => sm));
            }
            else
            {
                return(Enumerable.Empty <SimpleDothtmlCompletion>());
            }
        }
        public override IEnumerable <SimpleDothtmlCompletion> GetItems(DothtmlCompletionContext context)
        {
            var directives = new[] { Constants.BaseTypeDirective, Constants.MasterPageDirective, Constants.ViewModelDirectiveName };

            var glyph = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphKeyword, StandardGlyphItem.GlyphItemPublic);

            return(directives.Select(d => new SimpleDothtmlCompletion(d, d + " ", glyph)));
        }
        public override IEnumerable <SimpleDothtmlCompletion> GetItems(DothtmlCompletionContext context)
        {
            var bindingTypes            = new[] { Constants.CommandBinding, Constants.ValueBinding, Constants.ResourceBinding };
            var userControlBindingTypes = new[] { Constants.StaticCommandBinding, Constants.ControlCommandBinding, Constants.ControlPropertyBinding };

            var glyph = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphKeyword, StandardGlyphItem.GlyphItemPublic);

            return(Enumerable.Concat(bindingTypes, userControlBindingTypes).Select(b => new SimpleDothtmlCompletion(b, b + ": ", glyph)));
        }
 private List <INamedTypeSymbol> ReloadAllClasses(DothtmlCompletionContext context)
 {
     return(allClasses.GetOrRetrieve(() =>
     {
         var syntaxTrees = CompletionHelper.GetSyntaxTrees(context);
         var ownSymbols = syntaxTrees.SelectMany(t => t.Tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>()
                                                 .Select(c => t.SemanticModel.GetDeclaredSymbol(c))).ToList();
         var referencedSymbols = CompletionHelper.GetReferencedSymbols(context);
         return Enumerable.Concat(referencedSymbols, ownSymbols).OfType <INamedTypeSymbol>()
         .Where(c => c.DeclaredAccessibility == Accessibility.Public && !c.IsAbstract)
         .ToList();
     }));
 }
        public IEnumerable <CompletionData> GetControlAttributeNames(DothtmlCompletionContext context, List <string> tagNameHierarchy, out bool combineWithHtmlCompletions)
        {
            allControls.GetOrRetrieve(() => ReloadAllControls(context));

            ControlMetadata         currentControl;
            ControlPropertyMetadata currentProperty;

            GetElementContext(tagNameHierarchy, out currentControl, out currentProperty);

            combineWithHtmlCompletions = currentControl != null && currentControl.Name == typeof(HtmlGenericControl).Name && currentControl.Namespace == typeof(HtmlGenericControl).Namespace;

            if (currentControl != null && currentProperty == null)
            {
                return(currentControl.Properties.Where(p => !p.IsElement).Select(p => new CompletionData(p.Name)));
            }
            return(Enumerable.Empty <CompletionData>());
        }
        internal List <CompletionData> ReloadAllControls(DothtmlCompletionContext context)
        {
            // get all possible control symbols
            var allClasses     = ReloadAllClasses(context);
            var controlClasses = allClasses
                                 .Where(c => CompletionHelper.GetBaseTypes(c).Any(t => CheckType(t, typeof(DotvvmBindableObject))))
                                 .ToList();

            var result = new List <CompletionData>();

            metadata = new ConcurrentDictionary <string, ControlMetadata>(StringComparer.CurrentCultureIgnoreCase);
            htmlGenericControlMetadata = null;

            foreach (var rule in context.Configuration.Markup.Controls)
            {
                string tagName;
                if (!string.IsNullOrEmpty(rule.Src))
                {
                    // markup control
                    tagName = rule.TagPrefix + ":" + rule.TagName;

                    // TODO: parse markup, find base type and extract metadata

                    result.Add(new CompletionData(tagName));
                }
                else
                {
                    // find all classes declared in the project
                    var controls = controlClasses.Where(c => c.ContainingAssembly.Name == rule.Assembly && c.ContainingNamespace.ToDisplayString() == rule.Namespace);
                    foreach (var control in controls)
                    {
                        tagName = rule.TagPrefix + ":" + control.Name;
                        var controlMetadata = GetControlMetadata(control, rule.TagPrefix, control.Name);
                        metadata[tagName] = controlMetadata;
                        result.Add(new CompletionData(tagName));

                        if (CheckType(control, typeof(HtmlGenericControl)))
                        {
                            htmlGenericControlMetadata = controlMetadata;
                        }
                    }
                }
            }

            return(result);
        }
        protected override IEnumerable <SimpleDothtmlCompletion> GetItemsCore(DothtmlCompletionContext context, List <string> tagNameHierarchy)
        {
            var glyph = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphXmlItem, StandardGlyphItem.GlyphItemPublic);

            var tagNames = context.MetadataControlResolver.GetElementNames(context, tagNameHierarchy).ToList();

            foreach (var n in tagNames)
            {
                yield return(new SimpleDothtmlCompletion(n.DisplayText, n.CompletionText + " ", glyph));
            }

            if (tagNameHierarchy.Any())
            {
                var tagName = tagNameHierarchy[tagNameHierarchy.Count - 1];
                yield return(new SimpleDothtmlCompletion("/" + tagName, "/" + tagName + ">", null));
            }
        }
        protected override IEnumerable <SimpleDothtmlCompletion> GetItemsCore(DothtmlCompletionContext context, string directiveName)
        {
            if (string.Equals(directiveName, Constants.MasterPageDirective, StringComparison.InvariantCultureIgnoreCase))
            {
                var documents = CompletionHelper.GetCurrentProjectFiles(context)
                                .Where(i => i.Name.EndsWith(".dotmaster", StringComparison.CurrentCultureIgnoreCase))
                                .Select(DTEHelper.GetProjectItemRelativePath)
                                .ToList();

                var glyph = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphJSharpDocument, StandardGlyphItem.GlyphItemPublic);
                return(documents.Select(d => new SimpleDothtmlCompletion(d, d, glyph)));
            }
            else
            {
                return(Enumerable.Empty <SimpleDothtmlCompletion>());
            }
        }
        protected override void CommitCore(DothtmlCompletionContext context, Completion selectedCompletion)
        {
            var session = context.CompletionSession;

            session.Commit();

            if (session.TextView.TextBuffer.CheckEditAccess())
            {
                using (var edit = session.TextView.TextBuffer.CreateEdit())
                {
                    edit.Insert(session.TextView.Caret.Position.BufferPosition, "=\"\"");
                    edit.Apply();
                    session.TextView.Caret.MoveTo(session.TextView.Caret.Position.BufferPosition - 1);
                    CompletionTriggerRequested = true;
                }
            }
        }
        public IEnumerable <CompletionData> GetElementNames(DothtmlCompletionContext context, List <string> tagNameHierarchy)
        {
            // get all available allControls
            var controls = allControls.GetOrRetrieve(() => ReloadAllControls(context));

            // get element properties
            var                     elementProperties = new List <CompletionData>();
            ControlMetadata         currentControl;
            ControlPropertyMetadata currentProperty;

            GetElementContext(tagNameHierarchy, out currentControl, out currentProperty);
            if (currentControl != null)
            {
                if (currentProperty == null)
                {
                    elementProperties.AddRange(currentControl.Properties.Where(p => p.IsElement).Select(p => new CompletionData(p.Name)));

                    // get default property
                    var defaultContentProperty = currentControl.GetProperty(currentControl.DefaultContentProperty);
                    if (defaultContentProperty != null)
                    {
                        var filteredControls = GetElementNamesInPropertyContext(defaultContentProperty);
                        return(elementProperties.Concat(filteredControls));
                    }
                    else if (!currentControl.AllowContent)
                    {
                        // content is not allowed, return only inner properties
                        return(elementProperties);
                    }
                    else
                    {
                        // content is allowed - return all allControls
                        return(elementProperties.Concat(controls));
                    }
                }
                else
                {
                    return(GetElementNamesInPropertyContext(currentProperty));
                }
            }

            return(controls);
        }
Пример #15
0
        protected override void CommitCore(DothtmlCompletionContext context, Completion selectedCompletion)
        {
            var session = context.CompletionSession;

            var currentToken    = context.Tokens[context.CurrentTokenIndex];
            var currentPosition = session.TextView.Caret.Position.BufferPosition.Position;

            if (currentToken.StartPosition < currentPosition)
            {
                using (var edit = session.TextView.TextBuffer.CreateEdit(EditOptions.DefaultMinimalChange, null, this))
                {
                    edit.Replace(currentToken.StartPosition, currentPosition - currentToken.StartPosition, selectedCompletion.InsertionText);
                    edit.Apply();
                    session.Dismiss();
                }
            }
            else
            {
                session.Commit();
            }
        }
Пример #16
0
        protected override IEnumerable <SimpleDothtmlCompletion> GetItemsCore(DothtmlCompletionContext context, List <string> tagNameHierarchy)
        {
            var glyph  = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupProperty, StandardGlyphItem.GlyphItemPublic);
            var glyph2 = context.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupProperty, StandardGlyphItem.GlyphItemShortcut);

            var keepHtmlAttributes = false;
            var results            = Enumerable.Concat(
                context.MetadataControlResolver.GetControlAttributeNames(context, tagNameHierarchy, out keepHtmlAttributes)
                .Select(n => new SimpleDothtmlCompletion(n.DisplayText, n.CompletionText, glyph)
            {
                CustomCommit = new MainTagAttributeNameCustomCommit(context)
            }),
                context.MetadataControlResolver.GetAttachedPropertyNames(context)
                .Select(n => new SimpleDothtmlCompletion(n.DisplayText, n.CompletionText, glyph2)
            {
                CustomCommit = new MainTagAttributeNameCustomCommit(context)
            })
                );

            CombineWithHtmlCompletions = keepHtmlAttributes;

            return(results);
        }
        internal List <AttachedPropertyMetadata> ReloadAllAttachedProperties(DothtmlCompletionContext context)
        {
            // get all possible control symbols
            var allClasses = ReloadAllClasses(context);
            var attachedPropertyClasses = allClasses
                                          .Where(c => c.GetThisAndAllBaseTypes().Any(t => t.GetAttributes().Any(a => CheckType(a.AttributeClass, typeof(ContainsDotvvmPropertiesAttribute)))))
                                          .ToList();

            // find all attached properties
            var attachedProperties = attachedPropertyClasses
                                     .SelectMany(c => c.GetMembers().OfType <IFieldSymbol>())
                                     .Where(f => f.Type.GetThisAndAllBaseTypes().Any(t => CheckType(t, typeof(DotvvmProperty))))
                                     .Where(f => f.GetAttributes().Any(a => CheckType(a.AttributeClass, typeof(AttachedPropertyAttribute))));

            return(attachedProperties
                   .Select(f => new AttachedPropertyMetadata()
            {
                Name = ComposeAttachedPropertyName(f),
                Type = f.GetAttributes().First(a => CheckType(a.AttributeClass, typeof(AttachedPropertyAttribute)))
                       .ConstructorArguments[0].Value as ITypeSymbol
            })
                   .ToList());
        }
Пример #18
0
 public TrimStartCommit(DothtmlCompletionContext context) : base(context)
 {
 }
 public MainTagAttributeNameCustomCommit(DothtmlCompletionContext context) : base(context)
 {
 }
 public IEnumerable <CompletionData> GetAttachedPropertyNames(DothtmlCompletionContext context)
 {
     return(attachedProperties.GetOrRetrieve(() => ReloadAllAttachedProperties(context)).Select(a => new CompletionData(a.Name)));
 }
 public IEnumerable <CompletionData> GetAttachedPropertyValues(DothtmlCompletionContext context, string attachedPropertyName)
 {
     return(attachedProperties.GetOrRetrieve(() => ReloadAllAttachedProperties(context))
            .Where(a => a.Name == attachedPropertyName)
            .SelectMany(p => HintPropertyValues(p.Type)));
 }