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); }
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 tww = new TestWorkbenchWindow(); var content = new TestViewContent(); tww.ViewContent = content; content.ContentName = "/a.json"; content.Data.MimeType = "application/json"; content.Data.Text = sb.ToString(); var doc = new MonoDevelop.Ide.Gui.Document(tww); var csi = new JSonIndentEngine(content.Data, doc); var result = new CacheIndentEngine(csi); result.Update(content.Data, offset); return(result); }
internal static CacheIndentEngine CreateEngine(string text, out SourceText sourceText, OptionSet options = null) { if (options == null) { options = FormattingOptionsFactory.CreateMono(); // options.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); } sourceText = SourceText.From(sb.ToString()); var result = new CacheIndentEngine(new CSharpIndentEngine(options)); result.Update(sourceText, offset); return(result); }
public static void RandomTests(string filePath, int count, OptionSet options = null) { if (File.Exists(filePath)) { var code = File.ReadAllText(filePath); var document = SourceText.From(code); options = options ?? FormattingOptionsFactory.CreateMono(); var engine = new CacheIndentEngine(new CSharpIndentEngine(options) { EnableCustomIndentLevels = true }); Random rnd = new Random(); for (int i = 0; i < count; i++) { int offset = rnd.Next(document.Length); engine.Update(document, offset); if (engine.CurrentIndent.Length == 0) { continue; } } } else { Assert.Fail("File " + filePath + " doesn't exist."); } }
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 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."); } }
internal void SafeUpdateIndentEngine(int offset) { try { stateTracker.Update(Editor, offset); } catch (Exception e) { LoggingService.LogError("Error while updating the indentation engine", e); } }
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); }
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, OptionSet 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 = SourceText.From(sb.ToString()); var csi = new CSharpIndentEngine(policy) { EnableCustomIndentLevels = true }; if (symbols != null) { foreach (var sym in symbols) { csi.DefineSymbol(sym); } } var result = new CacheIndentEngine(csi); result.Update(document, offset); return(result); }
public void TestIncreaseIndent() { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = @"do "; var engine = new CacheIndentEngine(new TextMateDocumentIndentEngine( editor, new Regex("^\\s*do\\s*$"), null, null, null )); engine.Update(editor, 2); Assert.AreEqual("", engine.CurrentIndent); Assert.AreEqual("\t", engine.ThisLineIndent); Assert.AreEqual("\t", engine.NextLineIndent); }
public static async Task <TestCase> Create(string text, bool tabsToSpaces = false) { var test = new TestCase(); 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 content = new TestViewContent(); await content.Initialize(new FileDescriptor ("/a.json", null, null)); content.Editor.MimeType = "application/json"; content.Editor.Text = sb.ToString(); test.testCase = await TextEditorExtensionTestCase.Create(content, null, false); test.testCase.Document.Editor.Options = new CustomEditorOptions { TabsToSpaces = tabsToSpaces, TabSize = 4 }; var csi = new JSonIndentEngine(content.Editor); var result = new CacheIndentEngine(csi); result.Update(content.Editor, offset); test.Engine = result; return(test); }
string GetIndentationString(DocumentLocation loc) { var line = data.Document.GetLine(loc.Line); if (line == null) { return(""); } // Get context to the end of the line w/o changing the main engine's state var offset = line.Offset; string curIndent = line.GetIndentation(data.Document); try { stateTracker.Update(Math.Min(data.Length, offset + Math.Min(line.Length, loc.Column - 1))); int nlwsp = curIndent.Length; if (!stateTracker.LineBeganInsideMultiLineComment || (nlwsp < line.LengthIncludingDelimiter && data.Document.GetCharAt(offset + nlwsp) == '*')) { return(stateTracker.ThisLineIndent); } } catch (Exception e) { LoggingService.LogError("Error while indenting at " + loc, e); } return(curIndent); }
public override string GetIndentationString(int lineNumber) { var line = data.GetLine(lineNumber); if (line == null) { return(""); } // Get context to the end of the line w/o changing the main engine's state var offset = line.Offset; string curIndent = line.GetIndentation(data); try { stateTracker.Update(data, Math.Min(data.Length, offset + line.Length)); int nlwsp = curIndent.Length; if (!stateTracker.LineBeganInsideMultiLineComment || (nlwsp < line.LengthIncludingDelimiter && data.GetCharAt(offset + nlwsp) == '*')) { return(stateTracker.ThisLineIndent); } } catch (Exception e) { LoggingService.LogError("Error while indenting at line " + lineNumber, e); } return(curIndent); }