Skip to content

Commit

Permalink
Setup conditional config for file types only implementing line-level
Browse files Browse the repository at this point in the history
comments indicated by a '#'
TODO: setup the fileglobs that should be processed by this style.
  • Loading branch information
seancpeters committed Sep 22, 2016
1 parent 56fa5ca commit 684be95
Show file tree
Hide file tree
Showing 4 changed files with 350 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ public enum ConditionalType
None,
Xml,
Razor,
CWithComments,
CNoComments,
CBlockComments
CLineComments,
CBlockComments,
HashSignLineComment
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ public static IReadOnlyList<IOperationProvider> ConditionalSetup(ConditionalType
case ConditionalType.Razor:
setup = RazorConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
break;
case ConditionalType.CWithComments:
setup = CStyleWithCommentsConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
case ConditionalType.CLineComments:
setup = CStyleLineCommentsConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
break;
case ConditionalType.CNoComments:
setup = CStyleNoCommentsConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
break;
case ConditionalType.CBlockComments:
setup = CBlockCommentConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
setup = CStyleBlockCommentConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
break;
case ConditionalType.HashSignLineComment:
setup = HashSignLineCommentConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
break;
default:
throw new Exception($"Unrecognized conditional type {style}");
Expand Down Expand Up @@ -138,7 +141,7 @@ public static List<IOperationProvider> RazorConditionalSetup(string evaluatorTyp
};
}

public static List<IOperationProvider> CBlockCommentConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
public static List<IOperationProvider> CStyleBlockCommentConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
// This is the operationId (flag) for the balanced nesting
string commentFixingOperationId = "Fix pseudo comments (C Block)";
Expand Down Expand Up @@ -167,7 +170,7 @@ public static List<IOperationProvider> CBlockCommentConditionalSetup(string eval
};
}

public static List<IOperationProvider> CStyleWithCommentsConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
public static List<IOperationProvider> CStyleLineCommentsConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
string replaceOperationId = "Replacement (C style): (//) -> ()";
string uncommentOperationId = "Uncomment (C style): (////) -> (//)";
Expand Down Expand Up @@ -215,5 +218,37 @@ public static List<IOperationProvider> CStyleNoCommentsConditionalSetup(string e
conditional
};
}

// TODO: test
// this should work for nginx.conf, Perl, bash, etc.
public static List<IOperationProvider> HashSignLineCommentConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
string uncommentOperationId = "Uncomment (hash line): (##) -> (#)";
string replaceOperationId = "Replacement (hash line): (#) -> ()";
IOperationProvider uncomment = new Replacement("##", "#", uncommentOperationId);
IOperationProvider commentReplace = new Replacement("#", "", replaceOperationId);

ConditionalTokens tokens = new ConditionalTokens
{
IfTokens = new[] { "#if" },
ElseTokens = new[] { "#else" },
ElseIfTokens = new[] { "#elseif" },
EndIfTokens = new[] { "#endif" },
ActionableIfTokens = new[] { "##if" },
ActionableElseIfTokens = new[] { "##elseif" },
ActionableElseTokens = new[] { "##else" },
ActionableOperations = new[] { replaceOperationId, uncommentOperationId }
};

ConditionEvaluator evaluator = EvaluatorSelector.Select(evaluatorType);
IOperationProvider conditional = new Conditional(tokens, wholeLine, trimWhiteSpace, evaluator, id);

return new List<IOperationProvider>()
{
conditional,
uncomment,
commentReplace
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ IReadOnlyList<FileSource> IRunnableProjectConfig.Sources

private readonly Dictionary<Guid, string> _guidToGuidPrefixMap = new Dictionary<Guid, string>();

private static readonly SpecialOperationConfigParams DefaultOperationParams = new SpecialOperationConfigParams(string.Empty, "//", ConditionalType.CWithComments);
private static readonly SpecialOperationConfigParams DefaultOperationParams = new SpecialOperationConfigParams(string.Empty, "//", ConditionalType.CLineComments);

// operation info read from the config
private ICustomFileGlobModel CustomOperations = new CustomFileGlobModel();
Expand Down Expand Up @@ -239,7 +239,7 @@ IReadOnlyList<KeyValuePair<string, IGlobalRunConfig>> IRunnableProjectConfig.Spe
if (_specialOperationConfig == null)
{
List<SpecialOperationConfigParams> defaultSpecials = new List<SpecialOperationConfigParams>();
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.json", "//", ConditionalType.CWithComments));
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.json", "//", ConditionalType.CLineComments));
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.css.min", "/*", ConditionalType.CBlockComments));
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.css", "/*", ConditionalType.CBlockComments));
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.cs", "//", ConditionalType.CNoComments));
Expand Down
Loading

0 comments on commit 684be95

Please sign in to comment.