public static ConditionalKeywords FromJObject(JObject rawConfiguration) { ConditionalKeywords keywords = new ConditionalKeywords(); string ifKeyword = rawConfiguration.ToString("ifKeyword"); if (!string.IsNullOrWhiteSpace(ifKeyword)) { keywords.IfKeyword = ifKeyword; } string elseIfKeyword = rawConfiguration.ToString("elseIfKeyword"); if (!string.IsNullOrWhiteSpace(elseIfKeyword)) { keywords.ElseIfKeyword = elseIfKeyword; } string elseKeyword = rawConfiguration.ToString("elseKeyword"); if (!string.IsNullOrWhiteSpace(elseKeyword)) { keywords.ElseKeyword = elseKeyword; } string endIfKeyword = rawConfiguration.ToString("endIfKeyword"); if (!string.IsNullOrWhiteSpace(endIfKeyword)) { keywords.EndIfKeyword = endIfKeyword; } return(keywords); }
public static List <IOperationProvider> ConfigureFromJObject(JObject rawConfiguration) { string startToken = rawConfiguration.ToString("startToken"); string endToken = rawConfiguration.ToString("endToken"); if (string.IsNullOrWhiteSpace(startToken)) { throw new TemplateAuthoringException($"Template authoring error. StartToken must be defined", "StartToken"); } else if (string.IsNullOrWhiteSpace(endToken)) { throw new TemplateAuthoringException($"Template authoring error. EndToken must be defined", "EndToken"); } string pseudoEndToken = rawConfiguration.ToString("pseudoEndToken"); ConditionalKeywords keywords = ConditionalKeywords.FromJObject(rawConfiguration); ConditionalOperationOptions options = ConditionalOperationOptions.FromJObject(rawConfiguration); if (string.IsNullOrWhiteSpace(pseudoEndToken)) { return(GenerateConditionalSetup(startToken, endToken, keywords, options)); } else { return(GenerateConditionalSetup(startToken, endToken, pseudoEndToken, keywords, options)); } }
public static IReadOnlyList <IOperationProvider> ConditionalSetup(ConditionalType style, string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id) { List <IOperationProvider> setup; switch (style) { case ConditionalType.MSBuild: setup = MSBuildConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id); break; case ConditionalType.Xml: setup = ConditionalBlockCommentConfig.GenerateConditionalSetup("<!--", "-->"); break; case ConditionalType.Razor: setup = ConditionalBlockCommentConfig.GenerateConditionalSetup("@*", "*@"); break; case ConditionalType.CLineComments: setup = ConditionalLineCommentConfig.GenerateConditionalSetup("//"); break; case ConditionalType.CNoComments: setup = CStyleNoCommentsConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id); break; case ConditionalType.CBlockComments: setup = ConditionalBlockCommentConfig.GenerateConditionalSetup("/*", "*/"); break; case ConditionalType.HashSignLineComment: // Most line comment conditional tags use: <comment symbol><keyword prefix><keyword> // But for this one, the '#' comment symbol is all that's needed, so it uses an empty keyword prefix. // So we end up with regular conditionals suchs as '#if', '#else' // and actionables such as '##if' ConditionalKeywords keywords = new ConditionalKeywords() { KeywordPrefix = string.Empty }; setup = ConditionalLineCommentConfig.GenerateConditionalSetup("#", keywords, new ConditionalOperationOptions()); break; case ConditionalType.RemLineComment: setup = ConditionalLineCommentConfig.GenerateConditionalSetup("rem "); break; case ConditionalType.HamlLineComment: setup = ConditionalLineCommentConfig.GenerateConditionalSetup("-#"); break; case ConditionalType.JsxBlockComment: setup = ConditionalBlockCommentConfig.GenerateConditionalSetup("{/*", "*/}"); break; default: throw new Exception($"Unrecognized conditional type {style}"); } return(setup); }
public static List <IOperationProvider> ConfigureFromJObject(JObject rawConfiguration) { string token = rawConfiguration.ToString("token"); if (string.IsNullOrWhiteSpace(token)) { // this is the only required data, all the rest is optional throw new TemplateAuthoringException("Template authoring error. Token must be defined", "token"); } ConditionalKeywords keywords = ConditionalKeywords.FromJObject(rawConfiguration); ConditionalOperationOptions options = ConditionalOperationOptions.FromJObject(rawConfiguration); return(GenerateConditionalSetup(token, keywords, options)); }
// Nice to have: Generalize this type of setup similarly to Line, Block, & Custom public static List <IOperationProvider> CStyleNoCommentsConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id) { ConditionalKeywords defaultKeywords = new ConditionalKeywords(); List <ITokenConfig> ifTokens = new List <ITokenConfig>(); List <ITokenConfig> elseifTokens = new List <ITokenConfig>(); List <ITokenConfig> elseTokens = new List <ITokenConfig>(); List <ITokenConfig> endifTokens = new List <ITokenConfig>(); foreach (string ifKeyword in defaultKeywords.IfKeywords) { ifTokens.Add($"{defaultKeywords.KeywordPrefix}{ifKeyword}".TokenConfig()); } foreach (string elseifKeyword in defaultKeywords.ElseIfKeywords) { elseifTokens.Add($"{defaultKeywords.KeywordPrefix}{elseifKeyword}".TokenConfig()); } foreach (string elseKeyword in defaultKeywords.ElseKeywords) { elseTokens.Add($"{defaultKeywords.KeywordPrefix}{elseKeyword}".TokenConfig()); } foreach (string endifKeyword in defaultKeywords.EndIfKeywords) { endifTokens.Add($"{defaultKeywords.KeywordPrefix}{endifKeyword}".TokenConfig()); } ConditionalTokens tokens = new ConditionalTokens { IfTokens = ifTokens, ElseTokens = elseTokens, ElseIfTokens = elseifTokens, EndIfTokens = endifTokens }; ConditionEvaluator evaluator = EvaluatorSelector.Select(evaluatorType); IOperationProvider conditional = new Conditional(tokens, wholeLine, trimWhiteSpace, evaluator, id, true); return(new List <IOperationProvider>() { conditional }); }
// TODO: Allow the rawConfiguration elements to be either strings (as-is) or arrays of strings. // The code that consumes instances of this class is already setup to deal with multiple forms of each keyword type. internal static ConditionalKeywords FromJObject(JObject rawConfiguration) { ConditionalKeywords keywords = new ConditionalKeywords(); string ifKeyword = rawConfiguration.ToString("ifKeyword"); if (!string.IsNullOrWhiteSpace(ifKeyword)) { keywords.IfKeywords = new[] { ifKeyword }; } string elseIfKeyword = rawConfiguration.ToString("elseIfKeyword"); if (!string.IsNullOrWhiteSpace(elseIfKeyword)) { keywords.ElseIfKeywords = new[] { elseIfKeyword }; } string elseKeyword = rawConfiguration.ToString("elseKeyword"); if (!string.IsNullOrWhiteSpace(elseKeyword)) { keywords.ElseKeywords = new[] { elseKeyword }; } string endIfKeyword = rawConfiguration.ToString("endIfKeyword"); if (!string.IsNullOrWhiteSpace(endIfKeyword)) { keywords.EndIfKeywords = new[] { endIfKeyword }; } string prefixString = rawConfiguration.ToString("keywordPrefix"); if (prefixString != null) { // Empty string is a valid value for keywordPrefix, null is not. // If the "keywordPrefix" key isn't present in the config, the value will be null and not used. keywords.KeywordPrefix = prefixString; } return(keywords); }
public static List <IOperationProvider> GenerateConditionalSetup(string token, ConditionalKeywords keywords, ConditionalOperationOptions options) { string uncommentOperationId = $"Uncomment (line): {token} -> ()"; string reduceCommentOperationId = $"Reduce comment (line): ({token}{token}) -> ({token})"; IOperationProvider uncomment = new Replacement(token.TokenConfig(), string.Empty, uncommentOperationId, options.OnByDefault); IOperationProvider reduceComment = new Replacement($"{token}{token}".TokenConfig(), token, reduceCommentOperationId, options.OnByDefault); List <ITokenConfig> ifTokens = new List <ITokenConfig>(); List <ITokenConfig> actionableIfTokens = new List <ITokenConfig>(); foreach (string ifKeyword in keywords.IfKeywords) { ifTokens.Add($"{token}{keywords.KeywordPrefix}{ifKeyword}".TokenConfig()); actionableIfTokens.Add($"{token}{token}{keywords.KeywordPrefix}{ifKeyword}".TokenConfig()); } List <ITokenConfig> elseTokens = new List <ITokenConfig>(); List <ITokenConfig> actionableElseTokens = new List <ITokenConfig>(); foreach (string elseKeyword in keywords.ElseKeywords) { elseTokens.Add($"{token}{keywords.KeywordPrefix}{elseKeyword}".TokenConfig()); actionableElseTokens.Add($"{token}{token}{keywords.KeywordPrefix}{elseKeyword}".TokenConfig()); } List <ITokenConfig> elseIfTokens = new List <ITokenConfig>(); List <ITokenConfig> actionalElseIfTokens = new List <ITokenConfig>(); foreach (string elseIfKeyword in keywords.ElseIfKeywords) { elseIfTokens.Add($"{token}{keywords.KeywordPrefix}{elseIfKeyword}".TokenConfig()); actionalElseIfTokens.Add($"{token}{token}{keywords.KeywordPrefix}{elseIfKeyword}".TokenConfig()); } List <ITokenConfig> endIfTokens = new List <ITokenConfig>(); foreach (string endIfKeyword in keywords.EndIfKeywords) { endIfTokens.Add($"{token}{keywords.KeywordPrefix}{endIfKeyword}".TokenConfig()); endIfTokens.Add($"{token}{token}{keywords.KeywordPrefix}{endIfKeyword}".TokenConfig()); } ConditionalTokens conditionalTokens = new ConditionalTokens { IfTokens = ifTokens, ElseTokens = elseTokens, ElseIfTokens = elseIfTokens, EndIfTokens = endIfTokens, ActionableIfTokens = actionableIfTokens, ActionableElseTokens = actionableElseTokens, ActionableElseIfTokens = actionalElseIfTokens, ActionableOperations = new[] { uncommentOperationId, reduceCommentOperationId } }; ConditionEvaluator evaluator = EvaluatorSelector.Select(options.EvaluatorType); IOperationProvider conditional = new Conditional(conditionalTokens, options.WholeLine, options.TrimWhitespace, evaluator, options.Id, options.OnByDefault); return(new List <IOperationProvider>() { conditional, reduceComment, uncomment }); }
public static List <IOperationProvider> GenerateConditionalSetup(string startToken, string endToken, string pseudoEndToken, ConditionalKeywords keywords, ConditionalOperationOptions options) { ConditionEvaluator evaluator = EvaluatorSelector.Select(options.EvaluatorType); ConditionalTokens tokens = new ConditionalTokens { EndIfTokens = new[] { $"{keywords.KeywordPrefix}{keywords.EndIfKeyword}".TokenConfig(), $"{startToken}{keywords.KeywordPrefix}{keywords.EndIfKeyword}".TokenConfig() }, ActionableIfTokens = new[] { $"{startToken}{keywords.KeywordPrefix}{keywords.IfKeyword}".TokenConfig() }, ActionableElseTokens = new[] { $"{keywords.KeywordPrefix}{keywords.ElseKeyword}".TokenConfig(), $"{startToken}{keywords.KeywordPrefix}{keywords.ElseKeyword}".TokenConfig() }, ActionableElseIfTokens = new[] { $"{keywords.KeywordPrefix}{keywords.ElseIfKeyword}".TokenConfig(), $"{startToken}{keywords.KeywordPrefix}{keywords.ElseIfKeyword}".TokenConfig() }, }; if (!string.IsNullOrWhiteSpace(pseudoEndToken)) { Guid operationIdGuid = new Guid(); string commentFixOperationId = $"Fix pseudo tokens ({pseudoEndToken} {operationIdGuid})"; string commentFixResetId = $"Reset pseudo token fixer ({pseudoEndToken} {operationIdGuid})"; tokens.ActionableOperations = new[] { commentFixOperationId, commentFixResetId }; IOperationProvider balancedComments = new BalancedNesting(startToken.TokenConfig(), endToken.TokenConfig(), pseudoEndToken.TokenConfig(), commentFixOperationId, commentFixResetId, options.OnByDefault); IOperationProvider conditional = new Conditional(tokens, options.WholeLine, options.TrimWhitespace, evaluator, options.Id, options.OnByDefault); return(new List <IOperationProvider>() { conditional, balancedComments }); } else { IOperationProvider conditional = new Conditional(tokens, options.WholeLine, options.TrimWhitespace, evaluator, options.Id, options.OnByDefault); return(new List <IOperationProvider>() { conditional }); } }
public static List <IOperationProvider> GenerateConditionalSetup(string startToken, string endToken, ConditionalKeywords keywords, ConditionalOperationOptions options) { string pseudoEndComment; if (endToken.Length < 2) { // end comment must be at least two characters to have a programmatically determined pseudo-comment pseudoEndComment = null; } else { // add a space just before the final character of the end comment pseudoEndComment = endToken.Substring(0, endToken.Length - 1) + " " + endToken.Substring(endToken.Length - 1); } return(GenerateConditionalSetup(startToken, endToken, pseudoEndComment, keywords, options)); }
public static List <IOperationProvider> GenerateConditionalSetup(string token, ConditionalKeywords keywords, ConditionalOperationOptions options) { string uncommentOperationId = $"Uncomment (line): {token} -> ()"; string reduceCommentOperationId = $"Reduce comment (line): ({token}{token}) -> ({token})"; IOperationProvider uncomment = new Replacement(token, string.Empty, uncommentOperationId); IOperationProvider reduceComment = new Replacement($"{token}{token}", token, reduceCommentOperationId); ConditionalTokens conditionalTokens = new ConditionalTokens { IfTokens = new[] { $"{token}{keywords.KeywordPrefix}{keywords.IfKeyword}" }, ElseTokens = new[] { $"{token}{keywords.KeywordPrefix}{keywords.ElseKeyword}" }, ElseIfTokens = new[] { $"{token}{keywords.KeywordPrefix}{keywords.ElseIfKeyword}" }, EndIfTokens = new[] { $"{token}{keywords.KeywordPrefix}{keywords.EndIfKeyword}", $"{token}{token}{keywords.KeywordPrefix}{keywords.EndIfKeyword}" }, ActionableIfTokens = new[] { $"{token}{token}{keywords.KeywordPrefix}{keywords.IfKeyword}" }, ActionableElseTokens = new[] { $"{token}{token}{keywords.KeywordPrefix}{keywords.ElseKeyword}" }, ActionableElseIfTokens = new[] { $"{token}{token}{keywords.KeywordPrefix}{keywords.ElseIfKeyword}" }, ActionableOperations = new[] { uncommentOperationId, reduceCommentOperationId } }; ConditionEvaluator evaluator = EvaluatorSelector.Select(options.EvaluatorType); IOperationProvider conditional = new Conditional(conditionalTokens, options.WholeLine, options.TrimWhitespace, evaluator, options.Id); return(new List <IOperationProvider>() { conditional, reduceComment, uncomment }); }