public ScriptLineView CreateLineView(string scriptLineText, int lineIndex, bool @default = false) { var lineType = Script.ResolveLineType(scriptLineText); var lineView = default(ScriptLineView); switch (lineType.Name) { case nameof(CommentScriptLine): var commentScriptLine = new CommentScriptLine(null, lineIndex, scriptLineText, null, true); lineView = new CommentLineView(commentScriptLine, linesContainer); break; case nameof(LabelScriptLine): var labelScriptLine = new LabelScriptLine(null, lineIndex, scriptLineText, null, true); lineView = new LabelLineView(labelScriptLine, linesContainer); break; case nameof(DefineScriptLine): var defineScriptLine = new DefineScriptLine(null, lineIndex, scriptLineText, null, true); lineView = new DefineLineView(defineScriptLine, linesContainer); break; case nameof(CommandScriptLine): var commandScriptLine = new CommandScriptLine(null, lineIndex, scriptLineText, null, true); lineView = CommandLineView.CreateOrError(commandScriptLine, linesContainer, config.HideUnusedParameters, @default); break; case nameof(GenericTextScriptLine): var genericTextScriptLine = new GenericTextScriptLine(null, lineIndex, scriptLineText, null, true); lineView = new GenericTextLineView(genericTextScriptLine, linesContainer); break; } return(lineView); }
public ScriptLineView CreateLineView(int lineIndex, string lineText) { tokens.Clear(); var lineType = lexer.TokenizeLine(lineText, tokens); var lineView = default(ScriptLineView); switch (lineType) { case LineType.Comment: lineView = new CommentLineView(lineIndex, lineText, linesContainer); break; case LineType.Label: lineView = new LabelLineView(lineIndex, lineText, linesContainer); break; case LineType.Command: lineView = CommandLineView.CreateOrError(lineIndex, lineText, tokens, linesContainer, config.HideUnusedParameters); break; default: lineView = new GenericTextLineView(lineIndex, lineText, linesContainer); break; } return(lineView); }
public static ScriptLineView CreateOrError(int lineIndex, string lineText, IReadOnlyList <Token> tokens, VisualElement container, bool hideParameters) { errors.Clear(); var model = commandLineParser.Parse(lineText, tokens, errors)?.Command; if (model is null || errors.Count > 0) { return(Error(errors.FirstOrDefault())); } var meta = projectMeta.commands.FirstOrDefault(c => (c.id?.EqualsFastIgnoreCase(model.Identifier) ?? false) || (c.alias?.EqualsFastIgnoreCase(model.Identifier) ?? false)); if (meta is null) { return(Error($"Unknown command: `{model.Identifier}`")); } var nameLabel = new Label(model.Identifier.Text.FirstToLower()); nameLabel.name = "InputLabel"; nameLabel.AddToClassList("Inlined"); var commandLineView = new CommandLineView(lineIndex, container); commandLineView.Content.Add(nameLabel); commandLineView.CommandId = model.Identifier; commandLineView.hideParameters = hideParameters; foreach (var paramMeta in meta.@params) { var data = new ParameterFieldData { Id = string.IsNullOrEmpty(paramMeta.alias) ? paramMeta.id.FirstToLower() : paramMeta.alias, Value = GetValueFor(paramMeta), Nameless = paramMeta.nameless }; if (commandLineView.ShouldShowParameter(data)) { commandLineView.AddParameterField(data); } else { commandLineView.delayedAddFields.Add(data); } } return(commandLineView); ErrorLineView Error(string e) => new ErrorLineView(lineIndex, lineText, container, model?.Identifier, e); string GetValueFor(ProjectMetadata.ParameterMetadata m) { var param = model.Parameters.FirstOrDefault(p => p.Identifier.Text.EqualsFastIgnoreCase(m.id) || p.Identifier.Text.EqualsFastIgnoreCase(m.alias)); return(param?.Value); } }
public static ScriptLineView CreateOrError(int lineIndex, string lineText, VisualElement container, bool hideParameters) { var commandBodyText = lineText.GetAfterFirst(CommandScriptLine.IdentifierLiteral); var commandId = commandBodyText.GetBefore(" ") ?? commandBodyText.GetBefore("\t") ?? commandBodyText.Trim(); var commandType = Command.ResolveCommandType(commandId); if (commandType is null) { return(Error("Failed to resolve command type.")); } var commandLineView = new CommandLineView(lineIndex, container); var nameLabel = new Label(commandId); nameLabel.name = "InputLabel"; nameLabel.AddToClassList("Inlined"); commandLineView.Content.Add(nameLabel); commandLineView.CommandId = commandId; commandLineView.hideParameters = hideParameters; var paramFields = commandType.GetFields(BindingFlags.Public | BindingFlags.Instance) .Where(f => typeof(ICommandParameter).IsAssignableFrom(f.FieldType)); var paramValues = typeof(Command).GetMethod("ExtractParameters", BindingFlags.Static | BindingFlags.NonPublic) .Invoke(null, new[] { commandBodyText, string.Empty }) as LiteralMap <string>; foreach (var paramField in paramFields) { var paramAlias = paramField.GetCustomAttribute <Command.ParameterAliasAttribute>()?.Alias; var paramId = paramAlias ?? char.ToLowerInvariant(paramField.Name[0]) + paramField.Name.Substring(1); var nameless = string.IsNullOrEmpty(paramId); paramValues.TryGetValue(paramId, out var valueText); if (valueText is null) { paramValues.TryGetValue(paramField.Name, out valueText); } var data = new ParameterFieldData { Id = paramId, Name = paramField.Name, Value = valueText, Nameless = nameless }; if (!data.Nameless && hideParameters && string.IsNullOrEmpty(data.Value)) { commandLineView.delayedAddFields.Add(data); // Add un-assigned fields on hover for better init performance. } else { commandLineView.AddParameterField(data); } } return(commandLineView); ErrorLineView Error(string error) => new ErrorLineView(lineIndex, lineText, container, commandId, error); }
public ScriptLineView CreateLineView(int lineIndex, string lineText) { var lineView = default(ScriptLineView); switch (Script.ResolveLineType(lineText?.TrimFull()).Name) { case nameof(CommentScriptLine): lineView = new CommentLineView(lineIndex, lineText, linesContainer); break; case nameof(LabelScriptLine): lineView = new LabelLineView(lineIndex, lineText, linesContainer); break; case nameof(CommandScriptLine): lineView = CommandLineView.CreateOrError(lineIndex, lineText, linesContainer, config.HideUnusedParameters); break; case nameof(GenericTextScriptLine): lineView = new GenericTextLineView(lineIndex, lineText, linesContainer); break; } return(lineView); }
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); }