public async Task TestVSTS634797() { using (var testCase = await Create(@" class Foo { void Bar() { $ int a; } } ", createWithProject: true)) { var indent = new CSharpTextEditorIndentation(); indent.Initialize(testCase.Document.Editor, testCase.Document.DocumentContext); var offset = testCase.Document.Editor.CaretOffset; indent.SafeUpdateIndentEngine(offset); var pasteHandler = new CSharpTextPasteHandler(indent, null); await pasteHandler.PostFomatPastedText(offset, " int a;".Length); CheckOutput(testCase, @" class Foo { void Bar() { int a; } } ", indent); } }
public async Task TestVSTS567503() { using (var testCase = await Create(@" class Foo { /// <exception cref="""">$ void Bar () { } } ", createWithProject: true)) { var indent = new CSharpTextEditorIndentation(); indent.Initialize(testCase.Document.Editor, testCase.Document.DocumentContext); indent.KeyPress(KeyDescriptor.FromGtk((Gdk.Key) '>', '>', Gdk.ModifierType.None)); CheckOutput(testCase, @" class Foo { /// <exception cref="""">$</exception> void Bar () { } } ", indent); } }
void TestGuessSemicolonInsertionOffset(string fooBar, bool expected = true) { StringBuilder sb = new StringBuilder(); int semicolonOffset = 0; int guessedOffset = 0; for (int i = 0; i < fooBar.Length; i++) { char ch = fooBar [i]; if (ch == '$') { semicolonOffset = sb.Length - 1; } else if (ch == '~') { guessedOffset = sb.Length - 1; } else { sb.Append(ch); } } var data = TextEditorFactory.CreateNewEditor(); data.Text = sb.ToString(); int guessed; Assert.AreEqual(expected, CSharpTextEditorIndentation.GuessSemicolonInsertionOffset(data, data.GetLineByOffset(semicolonOffset), semicolonOffset, out guessed)); if (expected) { Assert.AreEqual(guessedOffset, guessed); } }
void CheckOutput(TextEditorExtensionTestCase testCase, string output, CSharpTextEditorIndentation engine = null) { TextEditor data = testCase.Document.Editor; if (engine == null) { engine = new CSharpTextEditorIndentation(); } engine.FixLineStart(data, CreateTracker(data), data.CaretLine); int idx = output.IndexOf('$'); if (idx > 0) { output = output.Substring(0, idx) + output.Substring(idx + 1); } if (output != data.Text) { Console.WriteLine("expected:"); Console.WriteLine(output.Replace("\t", "\\t").Replace(" ", ".")); Console.WriteLine("was:"); Console.WriteLine(data.Text.Replace("\t", "\\t").Replace(" ", ".")); } Assert.AreEqual(output, data.Text); if (idx >= 0) { Assert.AreEqual(idx, data.CaretOffset, "Caret offset mismatch."); } }
public void TestBug17766() { var data = Create(@" class Foo { $void Bar () { } } "); var engine = new CSharpTextEditorIndentation(); var tww = new TestWorkbenchWindow(); var content = new TestViewContent(data); tww.ViewContent = content; content.ContentName = "a.cs"; engine.Initialize(new Document(tww)); MiscActions.RemoveTab(data); engine.KeyPress(Gdk.Key.Tab, '\t', Gdk.ModifierType.ShiftMask); CheckOutput(data, @" class Foo { $void Bar () { } } ", engine); }
public async Task TestBug17896Async() { using (var data = await Create("\t\t\"This is a long test string.$ It contains spaces.\"")) { EditActions.InsertNewLine(data.Document.Editor); var engine = new CSharpTextEditorIndentation { wasInStringLiteral = true }; CheckOutput(data, "\t\t\"This is a long test string.\" +" + eolMarker + "\t\t\"$ It contains spaces.\"", engine); } }
public async Task TestStringContinationAsync() { using (var data = await Create("\t\t\"Hello$World\"")) { EditActions.InsertNewLine(data.Document.Editor); var engine = new CSharpTextEditorIndentation { wasInStringLiteral = true }; CheckOutput(data, "\t\t\"Hello\" +" + eolMarker + "\t\t\"$World\"", engine); } }
public void TestBug17896() { var data = Create("\t\t\"This is a long test string.$ It contains spaces.\""); MiscActions.InsertNewLine(data); var engine = new CSharpTextEditorIndentation { wasInStringLiteral = true }; CheckOutput(data, "\t\t\"This is a long test string.\" +" + eolMarker + "\t\t\"$ It contains spaces.\"", engine); }
public void TestStringContination() { var data = Create("\t\t\"Hello$World\""); MiscActions.InsertNewLine(data); var engine = new CSharpTextEditorIndentation { wasInStringLiteral = true }; CheckOutput(data, "\t\t\"Hello\" +" + eolMarker + "\t\t\"$World\"", engine); }
void CheckOutput(TextEditorData data, string output) { CSharpTextEditorIndentation.FixLineStart(data, CreateTracker(data), data.Caret.Line); int idx = output.IndexOf('$'); if (idx > 0) { output = output.Substring(0, idx) + output.Substring(idx + 1); } if (output != data.Text) { Console.WriteLine(data.Text.Replace("\t", "\\t").Replace(" ", ".")); } Assert.AreEqual(output, data.Text); Assert.AreEqual(idx, data.Caret.Offset, "Caret offset mismatch."); }
static CSharpTextEditorIndentation Setup(string input, out TestViewContent content) { TestWorkbenchWindow tww = new TestWorkbenchWindow(); content = new TestViewContent(); content.Data.Options.IndentStyle = IndentStyle.Auto; tww.ViewContent = content; content.ContentName = "a.cs"; content.GetTextEditorData().Document.MimeType = "text/x-csharp"; Document doc = new Document(tww); var text = input; int endPos = text.IndexOf('$'); if (endPos >= 0) { text = text.Substring(0, endPos) + text.Substring(endPos + 1); } content.Text = text; content.CursorPosition = System.Math.Max(0, endPos); var compExt = new CSharpCompletionTextEditorExtension(); compExt.Initialize(doc); content.Contents.Add(compExt); var ext = new CSharpTextEditorIndentation(); CSharpTextEditorIndentation.OnTheFlyFormatting = true; ext.Initialize(doc); content.Contents.Add(ext); doc.UpdateParseDocument(); return(ext); }
public async Task TestIssue5951() { using (var data = await Create(@" using System; namespace MyLibrary { public class MyClass { public MyClass() { } $ } }")) { data.Document.Editor.Options = new CustomEditorOptions(data.Document.Editor.Options) { IndentStyle = IndentStyle.Smart, RemoveTrailingWhitespaces = false }; var indent = new CSharpTextEditorIndentation(); indent.Initialize(data.Document.Editor, data.Document); indent.KeyPress(KeyDescriptor.FromGtk(Gdk.Key.Return, '\n', Gdk.ModifierType.None)); CheckOutput(data, @" using System; namespace MyLibrary { public class MyClass { public MyClass() { } $ } }", indent); } }
void AppendParameterList(StringBuilder result, CodeGenerationOptions options, IList <IParameter> parameters) { for (int i = 0; i < parameters.Count; i++) { if (i > 0) { result.Append(", "); } var p = parameters[i]; if (p.IsOut) { result.Append("out "); } if (p.IsRef) { result.Append("ref "); } if (p.IsParams) { result.Append("params "); } AppendReturnType(result, options, p.Type); result.Append(" "); result.Append(CSharpAmbience.FilterName(p.Name)); if (p.ConstantValue != null) { result.Append(" = "); if (p.Type.Kind == TypeKind.Enum) { bool found = false; foreach (var literal in GetEnumLiterals(p.Type)) { if (literal.ConstantValue.Equals(p.ConstantValue)) { AppendReturnType(result, options, p.Type); result.Append("." + literal.Name); found = true; break; } } if (!found) { result.Append("("); AppendReturnType(result, options, p.Type); result.Append(")" + p.ConstantValue); } } else if (p.ConstantValue is char) { result.Append("'" + p.ConstantValue + "'"); } else if (p.ConstantValue is string) { result.Append("\"" + CSharpTextEditorIndentation.ConvertToStringLiteral((string)p.ConstantValue) + "\""); } else if (p.ConstantValue is bool) { result.Append((bool)p.ConstantValue ? "true" : "false"); } else { result.Append(p.ConstantValue); } } } }
static async Task Simulate(string input, Action <TestViewContent, CSharpTextEditorIndentation> act) { TestWorkbenchWindow tww = new TestWorkbenchWindow(); var content = new TestViewContent(); content.Data.Options = new CustomEditorOptions { IndentStyle = IndentStyle.Auto }; tww.ViewContent = content; content.ContentName = "/a.cs"; content.Data.MimeType = "text/x-csharp"; var doc = new Document(tww); var sb = new StringBuilder(); int cursorPosition = 0, selectionStart = -1, selectionEnd = -1; for (int i = 0; i < input.Length; i++) { var ch = input [i]; switch (ch) { case '$': cursorPosition = sb.Length; break; case '<': if (i + 1 < input.Length) { if (input [i + 1] == '-') { selectionStart = sb.Length; i++; break; } } goto default; case '-': if (i + 1 < input.Length) { var next = input [i + 1]; if (next == '>') { selectionEnd = sb.Length; i++; break; } } goto default; default: sb.Append(ch); break; } } content.Text = sb.ToString(); content.CursorPosition = cursorPosition; var project = Services.ProjectService.CreateProject("C#"); project.Name = "test"; project.FileName = "test.csproj"; project.Files.Add(new ProjectFile(content.ContentName, BuildAction.Compile)); project.Policies.Set(Projects.Policies.PolicyService.InvariantPolicies.Get <CSharpFormattingPolicy> (), CSharpFormatter.MimeType); var solution = new MonoDevelop.Projects.Solution(); solution.AddConfiguration("", true); solution.DefaultSolutionFolder.AddItem(project); using (var monitor = new ProgressMonitor()) await TypeSystemService.Load(solution, monitor); content.Project = project; doc.SetProject(project); var compExt = new CSharpCompletionTextEditorExtension(); compExt.Initialize(doc.Editor, doc); content.Contents.Add(compExt); var ext = new CSharpTextEditorIndentation(); CSharpTextEditorIndentation.OnTheFlyFormatting = true; ext.Initialize(doc.Editor, doc); content.Contents.Add(ext); await doc.UpdateParseDocument(); if (selectionStart >= 0 && selectionEnd >= 0) { content.GetTextEditorData().SetSelection(selectionStart, selectionEnd); } try { act(content, ext); } finally { TypeSystemService.Unload(solution); } }
// class ThrowsExceptionVisitor : DepthFirstAstVisitor // { // public bool Throws = false; // // public override void VisitBlockStatement (BlockStatement blockStatement) // { // if (blockStatement.Statements.Count == 1 && blockStatement.Statements.First () is ThrowStatement) // Throws = true; // } // } static void AppendParameterList(StringBuilder result, CodeGenerationOptions options, IList <IParameterSymbol> parameters, bool asParameterList) { for (int i = 0; i < parameters.Count; i++) { if (i > 0) { result.Append(", "); } var p = parameters [i]; if (p.RefKind == RefKind.Out) { result.Append("out "); } if (p.RefKind == RefKind.Ref) { result.Append("ref "); } if (asParameterList) { if (p.IsParams) { result.Append("params "); } AppendReturnType(result, options, p.Type); result.Append(" "); } result.Append(CSharpAmbience.FilterName(p.Name)); if (p.HasExplicitDefaultValue) { result.Append(" = "); if (p.ExplicitDefaultValue is Enum) { var name = Enum.GetName(p.ExplicitDefaultValue.GetType(), p.ExplicitDefaultValue); if (name != null) { AppendReturnType(result, options, p.Type); result.Append("." + name); } else { result.Append("("); AppendReturnType(result, options, p.Type); result.Append(")" + p.ExplicitDefaultValue); } } else if (p.ExplicitDefaultValue is char) { result.Append("'" + p.ExplicitDefaultValue + "'"); } else if (p.ExplicitDefaultValue is string) { result.Append("\"" + CSharpTextEditorIndentation.ConvertToStringLiteral((string)p.ExplicitDefaultValue) + "\""); } else if (p.ExplicitDefaultValue is bool) { result.Append((bool)p.ExplicitDefaultValue ? "true" : "false"); } else { result.Append(p.ExplicitDefaultValue); } } } }
static CSharpTextEditorIndentation Setup(string input, out TestViewContent content) { TestWorkbenchWindow tww = new TestWorkbenchWindow(); content = new TestViewContent(); content.Data.Options.IndentStyle = IndentStyle.Auto; tww.ViewContent = content; content.ContentName = "a.cs"; content.GetTextEditorData().Document.MimeType = "text/x-csharp"; Document doc = new Document(tww); var sb = new StringBuilder(); int cursorPosition = 0, selectionStart = -1, selectionEnd = -1; for (int i = 0; i < input.Length; i++) { var ch = input [i]; switch (ch) { case '$': cursorPosition = sb.Length; break; case '<': if (i + 1 < input.Length) { if (input [i + 1] == '-') { selectionStart = sb.Length; i++; break; } } goto default; case '-': if (i + 1 < input.Length) { var next = input [i + 1]; if (next == '>') { selectionEnd = sb.Length; i++; break; } } goto default; default: sb.Append(ch); break; } } content.Text = sb.ToString(); content.CursorPosition = cursorPosition; var compExt = new CSharpCompletionTextEditorExtension(); compExt.Initialize(doc); content.Contents.Add(compExt); var ext = new CSharpTextEditorIndentation(); CSharpTextEditorIndentation.OnTheFlyFormatting = true; ext.Initialize(doc); content.Contents.Add(ext); doc.UpdateParseDocument(); if (selectionStart >= 0 && selectionEnd >= 0) { content.GetTextEditorData().SetSelection(selectionStart, selectionEnd); } return(ext); }