Ejemplo n.º 1
0
        private void ProcessStyleSetter(string setterPropertyName, XmlParser state, List <Completion> completions, string currentAssemblyName)
        {
            const string selectorTypes = @"(?<type>([\w|])+)|([:\.#/]\w+)";

            var selector         = state.FindParentAttributeValue("Selector", 1, maxLevels: 0);
            var matches          = Regex.Matches(selector ?? "", selectorTypes);
            var types            = matches.OfType <Match>().Select(m => m.Groups["type"].Value).Where(v => !string.IsNullOrEmpty(v));
            var selectorTypeName = types.LastOrDefault()?.Replace('|', ':') ?? "Control";

            if (string.IsNullOrEmpty(selectorTypeName))
            {
                return;
            }

            if (setterPropertyName == "Property")
            {
                string value = state.AttributeValue ?? "";

                if (value.Contains("."))
                {
                    int curStart = state.CurrentValueStart ?? 0;
                    var dotPos   = value.IndexOf(".");
                    var typeName = value.Substring(0, dotPos);
                    var compName = value.Substring(dotPos + 1);
                    curStart = curStart + dotPos + 1;

                    var sameType = state.GetParentTagName(1) == typeName;

                    completions.AddRange(_helper.FilterPropertyNames(typeName, compName, attached: true, hasSetter: true)
                                         .Select(p => new Completion(p, $"{typeName}.{p}", p, CompletionKind.AttachedProperty)));
                }
                else
                {
                    completions.AddRange(_helper.FilterPropertyNames(selectorTypeName, value, attached: false, hasSetter: true)
                                         .Select(x => new Completion(x, CompletionKind.Property)));

                    completions.AddRange(_helper.FilterTypeNames(value, withAttachedPropertiesOrEventsOnly: true).Select(x => new Completion(x, CompletionKind.Class)));
                }
            }
            else if (setterPropertyName == "Value")
            {
                var setterProperty = state.FindParentAttributeValue("Property", maxLevels: 0);

                if (setterProperty.Contains("."))
                {
                    var vals = setterProperty.Split('.');
                    selectorTypeName = vals[0];
                    setterProperty   = vals[1];
                }

                var setterProp = _helper.LookupProperty(selectorTypeName, setterProperty);
                if (setterProp?.Type?.HasHintValues == true)
                {
                    completions.AddRange(GetHintCompletions(setterProp.Type, state.AttributeValue, currentAssemblyName));
                }
            }
        }
Ejemplo n.º 2
0
        private IEnumerable <Completion> FilterHintValuesForBindingPath(MetadataType bindingPathType, string entered, string currentAssemblyName, string fullText, XmlParser state)
        {
            IEnumerable <Completion> forPropertiesFromType(MetadataType filterType, string filter, Func <string, string> fmtInsertText = null)
            {
                if (filterType != null)
                {
                    foreach (var propertyName in _helper.FilterPropertyNames(filterType, filter, false, false))
                    {
                        yield return(new Completion(propertyName, fmtInsertText?.Invoke(propertyName) ?? propertyName, propertyName, CompletionKind.Property));
                    }
                }
            }

            IEnumerable <Completion> forProperties(string filterType, string filter, Func <string, string> fmtInsertText = null)
            => forPropertiesFromType(_helper.LookupType(filterType ?? ""), filter, fmtInsertText);

            if (string.IsNullOrEmpty(entered))
            {
                return(forProperties(state.FindParentAttributeValue("(x\\:)?DataType"), entered));
            }

            var values = entered.Split('.');

            if (values.Length == 1)
            {
                if (values[0].StartsWith("$parent["))
                {
                    return(_helper.FilterTypes(entered.Substring("$parent[".Length))
                           .Select(v => new Completion(v.Key, $"$parent[{v.Key}].", v.Key, CompletionKind.Class)));
                }
                else if (values[0].StartsWith("#"))
                {
                    var nameMatch = Regex.Matches(fullText, $"\\s(?:(x\\:)?Name)=\"(?<AttribValue>[\\w\\:\\s\\|\\.]+)\"");

                    if (nameMatch.Count > 0)
                    {
                        var result = new List <Completion>();
                        foreach (Match m in nameMatch)
                        {
                            if (m.Success)
                            {
                                var name = m.Groups["AttribValue"].Value;
                                result.Add(new Completion(name, $"#{name}", name, CompletionKind.Class));
                            }
                        }
                        return(result);
                    }

                    return(Array.Empty <Completion>());
                }

                return(forProperties(state.FindParentAttributeValue("(x\\:)?DataType"), entered));
            }

            string type = values[0];

            int i;

            if (values[0].StartsWith("$"))
            {
                i    = 1;
                type = "Control";
                if (values[0] == "$self") //current control type
                {
                    type = state.GetParentTagName(0);
                }
                else if (values[0] == "$parent") //parent control in the xaml
                {
                    type = state.GetParentTagName(1) ?? "Control";
                }
                else if (values[0].StartsWith("$parent[")) //extract parent type
                {
                    type = values[0].Substring("$parent[".Length, values[0].Length - "$parent[".Length - 1);
                }
            }
            else if (values[0].StartsWith("#"))
            {
                i = 1;
                //todo: find the control type etc ???
                type = "Control";
            }
            else
            {
                i    = 0;
                type = state.FindParentAttributeValue("(x\\:)?DataType");
            }

            var mdType = _helper.LookupType(type ?? "");

            while (mdType != null && i < values.Length - 1 && !string.IsNullOrEmpty(values[i]))
            {
                if (i <= 1 && values[i] == "DataContext")
                {
                    //assume parent.datacontext is x:datatype so we have some intelisence
                    type   = state.FindParentAttributeValue("(x\\:)?DataType");
                    mdType = _helper.LookupType(type);
                }
                else
                {
                    mdType = mdType.Properties.FirstOrDefault(p => p.Name == values[i])?.Type;
                    type   = mdType?.FullName;
                }
                i++;
            }

            return(forPropertiesFromType(mdType, values[i], p => $"{string.Join(".", values.Take(i).ToArray())}.{p}"));
        }