private static string FormatLinqQuery(LambdaExpression expr, string querySource, string linqQuery) { var querySourceName = expr.Parameters.First(x => x.Type != typeof(IClientSideDatabase)).Name; var indexOfQuerySource = linqQuery.IndexOf(querySourceName, StringComparison.Ordinal); if (indexOfQuerySource == -1) { throw new InvalidOperationException("Cannot understand how to parse the query"); } linqQuery = linqQuery.Substring(0, indexOfQuerySource) + querySource + linqQuery.Substring(indexOfQuerySource + querySourceName.Length); linqQuery = ReplaceAnonymousTypeBraces(linqQuery); linqQuery = Regex.Replace(linqQuery, "<>([a-z])_", // replace <>h_ in transparent identifiers match => "__" + match.Groups[1].Value + "_"); linqQuery = Regex.Replace(linqQuery, @"__h__TransparentIdentifier(\w+)", match => "this" + match.Groups[1].Value); if (EnvironmentUtils.RunningOnPosix) { linqQuery = Regex.Replace(linqQuery, @"<>__TranspIdent(\w)+", "this$1"); } linqQuery = JSBeautify.Apply(linqQuery); return(linqQuery); }
private void jsEditor1_KeyTyped(object sender, KeyTypedEventArgs e) { if (e.KeyData == (Keys.Shift | Keys.Control | Keys.F)) { JSBeautify js = new JSBeautify(jsEditor1.Text, new JSBeautifyOptions()); jsEditor1.Text = js.GetResult(); } }
/// <summary> /// Perform the actual generation /// </summary> public static string PruneToFailureLinqQueryAsStringToWorkableCode <TQueryRoot, TReduceResult>( LambdaExpression expr, DocumentConvention convention, string querySource, bool translateIdentityProperty) { if (expr == null) { return(null); } var expression = expr.Body; string queryRootName = null; switch (expression.NodeType) { case ExpressionType.ConvertChecked: case ExpressionType.Convert: expression = ((UnaryExpression)expression).Operand; break; case ExpressionType.Call: var methodCallExpression = ((MethodCallExpression)expression); switch (methodCallExpression.Method.Name) { case "Select": queryRootName = TryCaptureQueryRoot(methodCallExpression.Arguments[0]); break; case "SelectMany": queryRootName = TryCaptureQueryRoot(methodCallExpression.Arguments[1]); break; } break; } var linqQuery = ExpressionStringBuilder.ExpressionToString(convention, translateIdentityProperty, typeof(TQueryRoot), queryRootName, expression); var querySourceName = expr.Parameters.First(x => x.Type != typeof(IClientSideDatabase)).Name; var indexOfQuerySource = linqQuery.IndexOf(querySourceName, StringComparison.InvariantCulture); if (indexOfQuerySource == -1) { throw new InvalidOperationException("Canot understand how to parse the query"); } linqQuery = linqQuery.Substring(0, indexOfQuerySource) + querySource + linqQuery.Substring(indexOfQuerySource + querySourceName.Length); linqQuery = ReplaceAnonymousTypeBraces(linqQuery); linqQuery = Regex.Replace(linqQuery, @"new ((VB\$)|(<>))[\w_]+(`\d+)?", "new "); // remove anonymous types linqQuery = Regex.Replace(linqQuery, @"new " + typeof(TReduceResult).Name, "new "); // remove reduce result type linqQuery = Regex.Replace(linqQuery, @"<>([a-z])_", "__$1_"); // replace <>h_ in transperant identifiers linqQuery = Regex.Replace(linqQuery, @"<>([a-z])_", "__$1_"); // replace <>h_ in transperant identifiers linqQuery = Regex.Replace(linqQuery, @"__h__TransparentIdentifier(\d)+", "this$1"); linqQuery = JSBeautify.Apply(linqQuery); return(linqQuery); }
private void dSkinButton3_Click(object sender, EventArgs e) { string script = fastColoredTextBox1.Text;// System.IO.File.ReadAllText(.Replace("\"","'")); JSBeautify jsb = new JSBeautify(script, new JSBeautifyOptions { preserve_newlines = false }); string scriptbeautfied = jsb.GetResult(); fastColoredTextBox1.Text = scriptbeautfied; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var js = new JSBeautify(value as String, new JSBeautifyOptions { indent_char = ' ', indent_level = 0, indent_size = 4, preserve_newlines = true }); return(js.GetResult()); }
public static string Compile(string source) { TopLevelJoopCompilerScope compiler = new TopLevelJoopCompilerScope(); StringBuilder builder = new StringBuilder(); compiler.Parse(null, builder, source); JSBeautifyOptions options = new JSBeautifyOptions(); options.preserve_newlines = true; options.indent_char = '\t'; options.indent_size = 1; JSBeautify beautify = new JSBeautify(builder.ToString(), options); return(beautify.GetResult()); }
public static void FormattJsCode(string inputPath, string outputPath) { string code = File.ReadAllText(inputPath, Encoding.UTF8); var options = new JSBeautifyOptions(); options.indent_size = 4; options.indent_char = ' '; options.indent_level = 0; options.preserve_newlines = true; var jsbeautify = new JSBeautify(code, options); code = jsbeautify.GetResult(); File.WriteAllText(outputPath, code, Encoding.UTF8); }
public async Task Format() { using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context)) { var json = await context.ReadForMemoryAsync(RequestBodyStream(), "studio-tasks/format"); if (json == null) { throw new BadRequestException("No JSON was posted."); } if (json.TryGet(nameof(FormattedExpression.Expression), out string expressionAsString) == false) { throw new BadRequestException("'Expression' property was not found."); } if (string.IsNullOrWhiteSpace(expressionAsString)) { NoContentStatus(); return; } var type = IndexDefinitionHelper.DetectStaticIndexType(expressionAsString, reduce: null); FormattedExpression formattedExpression; switch (type) { case IndexType.Map: case IndexType.MapReduce: using (var workspace = new AdhocWorkspace()) { var expression = SyntaxFactory .ParseExpression(expressionAsString) .NormalizeWhitespace(); var result = Formatter.Format(expression, workspace); if (result.ToString().IndexOf("Could not format:", StringComparison.Ordinal) > -1) { throw new BadRequestException(); } formattedExpression = new FormattedExpression { Expression = result.ToString() }; } break; case IndexType.JavaScriptMap: case IndexType.JavaScriptMapReduce: formattedExpression = new FormattedExpression { Expression = JSBeautify.Apply(expressionAsString) }; break; default: throw new NotSupportedException($"Unknown index type '{type}'."); } await using (var writer = new AsyncBlittableJsonTextWriter(context, ResponseBodyStream())) { context.Write(writer, formattedExpression.ToJson()); } } }
public static string Beautify(string Code) { JSBeautify JB = new JSBeautify(Code, new JSBeautifyOptions()); string BeautifiedCode = JB.GetResult(); return BeautifiedCode; }
private void ShowFileContent(WXAPKG_FILE file) { if ("" == txtWXAPKG.Text || null == file) { return; } FileStream fs = null; try { fs = new FileStream(txtWXAPKG.Text, FileMode.Open, FileAccess.Read); byte[] b_content = new byte[file.m_size]; fs.Seek((long)file.m_offset, SeekOrigin.Begin); fs.Read(b_content, 0, file.m_size); fs.Close(); string file_name = file.m_name.ToLower(); if (file_name.EndsWith(".js") || file_name.EndsWith(".json")) { string content = Encoding.UTF8.GetString(b_content); if (btnBeautifyJS.Checked) { JSBeautifyOptions jsbo = new JSBeautifyOptions(); JSBeautify jsb = new JSBeautify(content, jsbo); content = jsb.GetResult(); } txtContent.Text = content; txtContent.BringToFront(); tsContent.Enabled = true; btnBeautifyJS.Enabled = true; } else if (file_name.EndsWith(".png") || file_name.EndsWith(".jpg") || file_name.EndsWith(".gif") || file_name.EndsWith(".bmp")) { MemoryStream ms = new MemoryStream(b_content); Bitmap bmp = (Bitmap)Bitmap.FromStream(ms); ms.Close(); picPreview.BackgroundImage = bmp; picPreview.BringToFront(); tsContent.Enabled = false; } else { string content = Encoding.UTF8.GetString(b_content); txtContent.Text = content; txtContent.BringToFront(); tsContent.Enabled = true; btnBeautifyJS.Enabled = false; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (null != fs) { fs.Close(); } } }