internal async Task <long> CommitToPersistence(StringBuilder dmlCollector, long?position) { if (position.HasValue == false) { throw new ArgumentException("Commit's must have a position", nameof(position)); } using (var readDb = SqlExecution.OpenWriteConnection(_commitConnectionstring)) { //Consider a write-lock around _dmlCollector, is it necessary? //Consider pre-pending a "BEGIN TRANSACTION" and have a all-or-nothing write dmlCollector.PrependLine("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; BEGIN TRANSACTION;"); dmlCollector.AppendLine( $"UPDATE Inf_ReadSubscriptions SET ReadPosition = {position.Value} WHERE SchemaName = '{_projection.SchemaIdentifier.Name}';"); dmlCollector.AppendLine("COMMIT TRANSACTION;"); var dml = new SqlCommand(dmlCollector.ToString(), readDb); var effect = await dml.ExecuteNonQueryAsync(); if (effect == 0) { throw new InvalidOperationException( "Something went wrong while updating the state of a readservice. SQL:\r\n" + dmlCollector); } //todo: Error handling? return(position.Value); } }
public static StringBuilder WrapScript(this StringBuilder builder) { builder.PrependLine(@"<script type='text/javascript'>$(function() {"); builder.AppendLine(@"});</script>"); return(builder); }
public static string GetAllMessages(this Exception ex) { var message = new StringBuilder(); for (var e = ex; e != null; e = e.InnerException) message.PrependLine(e.Message); return message.ToString(); }
public static Result FindMemberDocumentationNRefactory(string code, int fromLine) { var result = new Result(); var syntaxTree = new CSharpParser().Parse(code, "demo.cs"); var comment = syntaxTree.Children .DeepAll(x => x is Comment) .Cast <Comment>() .Where(c => c.CommentType == CommentType.Documentation && c.StartLocation.Line <= fromLine && c.EndLocation.Line >= fromLine) //inside of the node .Select(t => new { Node = t, Size = t.EndLocation.Line - t.StartLocation.Line }) .OrderBy(x => x.Size) .FirstOrDefault(); if (comment != null) { var content = new StringBuilder(); content.AppendLine(comment.Node.Content); var prevComment = comment.Node.PrevSibling as Comment; while (prevComment != null && prevComment.CommentType == CommentType.Documentation) { content.PrependLine(prevComment.Content); prevComment = prevComment.PrevSibling as Comment; } result.XmlDocumentation = content.ToString(); var nextNode = comment.Node.NextSibling; while (nextNode != null && (nextNode.NodeType != NodeType.Member && nextNode.NodeType != NodeType.TypeDeclaration)) { nextNode = nextNode.NextSibling; } if (nextNode != null && (nextNode.NodeType == NodeType.Member || nextNode.NodeType == NodeType.TypeDeclaration || nextNode.NodeType == NodeType.TypeDeclaration)) { //<membertype>:<signature> var signatureInfo = (nextNode.GetMemberSignature(code) ?? ":").Split(':'); result.MemberTitle = signatureInfo[0]; result.MemberDefinition = signatureInfo[1]; } else { result.MemberTitle = "Member"; result.MemberDefinition = "[some member]"; } result.Success = true; } return(result); }
public void TestStringBuilder_PrependLine() { var endLine = "blablabla"; var startLine = "Something"; _sb.Append(endLine); _sb.PrependLine(startLine); var sb = new StringBuilder(); sb.AppendLine(startLine); sb.Append(endLine); Assert.AreEqual(sb.ToString(), _sb.ToString()); }
public GenerationResult Generate(IEnumerable <Type> types) { var typeBuilder = new StringBuilder(); var enumBuilder = new StringBuilder(); foreach (var t in types) { _typeStack.Push(t); } while (_typeStack.Any()) { var type = _typeStack.Pop(); if (_typesGenerated.Contains(type)) { continue; } if (type.IsEnum) { RenderEnum(enumBuilder, type); } else { RenderType(typeBuilder, type); } } if (!string.IsNullOrEmpty(_module)) { typeBuilder = new StringBuilder(typeBuilder.ToString().IndentEachLine(TabString)); typeBuilder.PrependLine($"declare module '{_module}' {{"); typeBuilder.AppendLine("}"); } else { typeBuilder.AppendLine(); } return(new GenerationResult(typeBuilder.ToString(), enumBuilder.ToString())); }
private void DoReset(string resetReason = "") { ControlsDelegate.SetText(this, tbTitle, ""); ControlsDelegate.SetText(this, labelNextSplit, ""); ControlsDelegate.SetText(this, labelScene, ""); ControlsDelegate.SetText(this, labelCheckpoint, ""); ControlsDelegate.SetText(this, labelGlove, ""); ControlsDelegate.SetText(this, labelBlink, ""); ControlsDelegate.SetText(this, labelSuit, ""); ControlsDelegate.SetText(this, labelStaff, ""); ControlsDelegate.SetText(this, labelBarriers, ""); ControlsDelegate.SetText(this, labelOrbs, ""); ControlsDelegate.SetText(this, labelScrollCount, ""); ControlsDelegate.SetText(this, labelBosses, ""); ControlsDelegate.SetText(this, labelComplete, ""); splitCounter = 0; scrollCounter = 0; sceneIndex = 0; checkpointIndex = 0; previousText = ""; sbText = new StringBuilder(resetReason); sbText.PrependLine(String.Format("Watching Save Directories:\r\n{0}\r\n{1}", fswLegacy == null ? "" : fswLegacy.Path, fswCloud == null ? "" : fswCloud.Path)); buttonReset.Enabled = false; if (fswLegacy != null && fswLegacy.EnableRaisingEvents) ControlsDelegate.SetText(this, tbTitle, sbText.ToString()); else if (fswCloud != null && fswCloud.EnableRaisingEvents) ControlsDelegate.SetText(this, tbTitle, sbText.ToString()); if (HamsterDance.playState == WMPPlayState.wmppsPlaying) HamsterDance.controls.stop(); }
public static Result FindMemberDocumentation(string code, int caretLine, string language = "CSharp") { /* * C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Xamarin\Xamarin\4.0.0.1689 * * Quite shockingly Xamarin VS plugin is using ICSharpCode.NRefactory.dll - AssemblyFileVersion("5.5.1") * While this plugin relies on NuGet assembly ICSharpCode.NRefactory.dll - AssemblyFileVersion("5.2.0"). * And the both assemblies are built as "Assembly ICSharpCode.NRefactory, Version 5.0.0.0" * SHOCKING!!!! * The assemblies are clearly different (file size and functionality wise) despite being both marked as the same version. * * CLR cannot distinguish them and assembly probing gets so screwed that UnitTesting loads * one assembly file and VS (runtime) another one. * * Xamarin should never ever distribute a conflicting assembly. * * The outcome is - using NRefactory v5.0.0.0 is too risky as it's not clear when and where the asm probing * will fail again. And also there is no warranty that Xamarin wouldn't release yet another edition of * ICSharpCode.NRefactory v5.0.0.0. * * Forcing (somehow) DocPreview to be loaded before Xamarin will fix the problem but it will * then screw Xamarin plugin. * * I had no choice but to implement my own "poor-man" parser. * */ //FindMemberDocumentationNRefactoryNew(code, caretLine); string[] statementDelimiters = new string[] { ";", "{" }; string xmlDocPreffix = GetXmlDocPrefix(language); var result = new Result(); int fromLine = caretLine - 1; //poor man C# syntax parser. Too bad NRefactory conflicts with Xamarin string[] lines = code.Lines().ToArray(); if (lines.Any() && lines.Last() == null) { lines = lines.Take(lines.Count() - 1).ToArray(); } if (lines.Length > fromLine && lines[fromLine].Trim().StartsWith(xmlDocPreffix)) // it is XML doc comment line { result.MemberTitle = "Member"; result.MemberDefinition = "[some member]"; var content = new StringBuilder(); for (int i = fromLine; i >= 0; i--) { var line = lines[i].Trim(); if (line.HasText()) { if (line.StartsWith(xmlDocPreffix)) { content.PrependLine(line.Substring(xmlDocPreffix.Length)); } else { break; } } } int endOfDoc = -1; for (int i = fromLine + 1; i < lines.Length; i++) { var line = lines[i].Trim(); if (line.HasText()) { if (line.StartsWith(xmlDocPreffix)) { content.AppendLine(line.Substring(xmlDocPreffix.Length)); } else { endOfDoc = i; break; } } } if (endOfDoc != -1) { char endofDeclChar = (char)0; var declaration = new StringBuilder(); for (int i = endOfDoc; i < lines.Length; i++) { var line = lines[i].TrimEnd(); var endofDecl = line.IndexOfAny(statementDelimiters); if (endofDecl == -1) { var temp = line.Trim(); if (temp.StartsWith("/// <summary>")) // start of the next member } { break; } if (!temp.EndsWith("[") && !temp.EndsWith("]")) //not an attribute declaration { declaration.AppendLine(line); if (language != "CSharp") { break; } } } else { endofDeclChar = line[endofDecl]; declaration.AppendLine(line.Substring(0, endofDecl)); break; } } if (declaration.Length > 0) { result.MemberDefinition = declaration.ToString().FormatApiSignature(); if (language == "CSharp") { result.MemberTitle = (result.MemberDefinition + " " + endofDeclChar).ToMemberTitle(); } else { if (result.MemberDefinition.Contains("delegate ")) { result.MemberTitle = "Delegate"; } else if (result.MemberDefinition.Contains("class ")) { result.MemberTitle = "Class"; } else if (result.MemberDefinition.Contains("event ")) { result.MemberTitle = "Event"; } else if (result.MemberDefinition.Contains("struct ")) { result.MemberTitle = "Struct"; } else if (result.MemberDefinition.Contains("enum ")) { result.MemberTitle = "Enum"; } else if (endofDeclChar == '{') { if (result.MemberDefinition.Last() == ')') { result.MemberTitle = "Method"; } else { result.MemberTitle = "Property"; } } } } } result.XmlDocumentation = content.ToString().Trim(); result.Success = true; } return(result); }