Exemplo n.º 1
0
 private void AddParameterField (ParameterFieldData data)
 {
     var textField = new LineTextField(data.Id, data.Value ?? string.Empty);
     if (data.Nameless) textField.tooltip = data.Name;
     else textField.AddToClassList("NamedParameterLabel");
     parameterFields.Add(textField);
     // Show the un-assigned named parameters only when hovered or focused.
     if (data.Nameless || !hideParameters || !string.IsNullOrEmpty(data.Value))
         Content.Add(textField);
 }
Exemplo n.º 2
0
        public static ScriptLineView CreateOrError(CommandScriptLine scriptLine, VisualElement container, bool hideParameters, bool @default = false)
        {
            ErrorLineView Error(string error) => new ErrorLineView(scriptLine, container, error);

            if (!scriptLine.Valid)
            {
                return(Error("Incorrect syntax."));
            }

            var commandType = Command.FindCommandType(scriptLine.CommandName);

            if (commandType is null)
            {
                return(Error($"Unknown command `{scriptLine.CommandName}`."));
            }

            var commandLineView = new CommandLineView(scriptLine, container);

            commandLineView.hideParameters = hideParameters;
            commandLineView.CommandId      = scriptLine.CommandName;
            var nameLabel = new Label(scriptLine.CommandName);

            nameLabel.name = "InputLabel";
            nameLabel.AddToClassList("Inlined");
            commandLineView.Content.Add(nameLabel);

            var paramaterFieldInfos = commandType.GetProperties()
                                      .Where(property => property.IsDefined(typeof(Command.CommandParameterAttribute), false)).ToList();
            var parameterAttributes = paramaterFieldInfos
                                      .Select(f => f.GetCustomAttributes(typeof(Command.CommandParameterAttribute), false).First() as Command.CommandParameterAttribute).ToList();

            Debug.Assert(paramaterFieldInfos.Count == parameterAttributes.Count);

            for (int i = 0; i < paramaterFieldInfos.Count; i++)
            {
                var paramFieldInfo = paramaterFieldInfos[i];
                var paramAttribute = parameterAttributes[i];

                var paramName = paramAttribute.Alias != null && scriptLine.CommandParameters.ContainsKey(paramAttribute.Alias) ? paramAttribute.Alias : paramFieldInfo.Name;
                if (!scriptLine.CommandParameters.ContainsKey(paramName) && !paramAttribute.Optional && !@default)
                {
                    return(Error($"Missing `{paramName}` parameter."));
                }

                scriptLine.CommandParameters.TryGetValue(paramName, out var paramValue);
                var textField = new LineTextField(paramAttribute.Alias ?? char.ToLowerInvariant(paramName[0]) + paramName.Substring(1), paramValue);
                // Show parameter ID of the nameless parameters via tooltip.
                if (string.IsNullOrEmpty(textField.label))
                {
                    textField.tooltip = paramFieldInfo.Name;
                }
                else
                {
                    textField.AddToClassList("NamedParameterLabel");
                }
                commandLineView.parameterFields.Add(textField);
                // Show the un-assigned named parameters only when hovered or focused.
                if (string.IsNullOrEmpty(textField.label) || !hideParameters || !string.IsNullOrEmpty(textField.value))
                {
                    commandLineView.Content.Add(textField);
                }
            }

            foreach (var paramId in scriptLine.CommandParameters.Keys)
            {
                if (parameterAttributes.Exists(a => a.Alias?.EqualsFastIgnoreCase(paramId) ?? false))
                {
                    continue;
                }
                if (paramaterFieldInfos.Exists(f => f.Name.EqualsFastIgnoreCase(paramId)))
                {
                    continue;
                }
                return(Error($"Unsupported `{paramId}` parameter."));
            }

            return(commandLineView);
        }