public static CacheIndentEngine CreateEngine(string text, CSharpFormattingOptions formatOptions = null, TextEditorOptions options = null) { if (formatOptions == null) { formatOptions = FormattingOptionsFactory.CreateMono(); formatOptions.AlignToFirstIndexerArgument = formatOptions.AlignToFirstMethodCallArgument = true; } var sb = new StringBuilder(); int offset = 0; for (int i = 0; i < text.Length; i++) { var ch = text [i]; if (ch == '$') { offset = i; continue; } sb.Append(ch); } var document = new ReadOnlyDocument(sb.ToString()); options = options ?? new TextEditorOptions { EolMarker = "\n" }; var result = new CacheIndentEngine(new CSharpIndentEngine(document, options, formatOptions)); result.Update(offset); return result; }
static void IndentSingleLine(CacheIndentEngine engine, IDocument document, IDocumentLine line) { engine.Update(line.EndOffset); if (engine.NeedsReindent) { var indentation = TextUtilities.GetWhitespaceAfter(document, line.Offset); // replacing the indentation in two steps is necessary to make the caret move accordingly. document.Replace(indentation.Offset, indentation.Length, ""); document.Replace(indentation.Offset, 0, engine.ThisLineIndent); engine.ResetEngineToPosition(line.Offset); } }
public static IDocumentIndentEngine CreateEngine(string text) { var sb = new StringBuilder (); int offset = 0; for (int i = 0; i < text.Length; i++) { var ch = text [i]; if (ch == '$') { offset = i; continue; } sb.Append (ch); } var data = new TextEditorData (); data.Text = sb.ToString (); var csi = new JSonIndentEngine (data); var result = new CacheIndentEngine (csi); result.Update (offset); return result; }
public static IDocumentIndentEngine CreateEngine(string text, CSharpFormattingOptions formatOptions = null, IEnumerable<string> symbols = null) { var policy = formatOptions; if ( policy == null) { policy = FormattingOptionsFactory.CreateMono(); policy.IndentPreprocessorDirectives = false; policy.AlignToFirstMethodCallArgument = policy.AlignToFirstIndexerArgument = true; } var sb = new StringBuilder(); int offset = 0; for (int i = 0; i < text.Length; i++) { var ch = text[i]; if (ch == '$') { offset = i; continue; } sb.Append(ch); } var document = new ReadOnlyDocument(sb.ToString()); var options = new TextEditorOptions(); var csi = new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true }; if (symbols != null) { foreach (var sym in symbols) { csi.DefineSymbol(sym); } } var result = new CacheIndentEngine(csi); result.Update(offset); return result; }
public static void RandomTests(string filePath, int count, CSharpFormattingOptions policy = null, TextEditorOptions options = null) { if (File.Exists(filePath)) { var code = File.ReadAllText(filePath); var document = new ReadOnlyDocument(code); policy = policy ?? FormattingOptionsFactory.CreateMono(); options = options ?? new TextEditorOptions { IndentBlankLines = false }; var engine = new CacheIndentEngine(new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true }); Random rnd = new Random(); for (int i = 0; i < count; i++) { int offset = rnd.Next(document.TextLength); engine.Update(offset); if (engine.CurrentIndent.Length == 0) continue; } } else { Assert.Fail("File " + filePath + " doesn't exist."); } }
IStateMachineIndentEngine CreateTracker(TextEditorData data) { var policy = PolicyService.InvariantPolicies.Get <CSharpFormattingPolicy> ("text/x-csharp").CreateOptions(); var textStylePolicy = data.CreateNRefactoryTextEditorOptions(); textStylePolicy.IndentBlankLines = true; var result = new CacheIndentEngine(new ICSharpCode.NRefactory.CSharp.CSharpIndentEngine(data.Document, textStylePolicy, policy)); result.Update (data.Caret.Offset); return result; }