public void TestBug58764() { var tww = new TestWorkbenchWindow(); var content = new TestViewContent(); tww.ViewContent = content; var document = new Document(tww); var editor = TextEditorFactory.CreateNewEditor(document); editor.MimeType = "text/xml"; const string originalText = @"<?xml version=""1.0"" encoding=""utf-8"" ?> <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" x:Class=""XamlSamples.HelloXamlPage""> <Grid HeightRequest= </ContentPage>"; editor.Text = originalText; var offset = editor.Text.IndexOf("HeightRequest=", StringComparison.Ordinal) + "HeightRequest=".Length; editor.GetContent <ITextEditorImpl> ().CaretOffset = offset; //Reason why we use GetNativeWidget, and navigate to ExtensibleTextEditor child //and execute KeyPress on it instead something more abstract is... //EditSession key processing is done inside ExtensibleTextEditor. var extensibleEditor = FindChild <ExtensibleTextEditor> (editor.GetNativeWidget <Container> ()); extensibleEditor.OnIMProcessedKeyPressEvent((Gdk.Key) '"', '"', Gdk.ModifierType.None); extensibleEditor.OnIMProcessedKeyPressEvent(Gdk.Key.BackSpace, '\0', Gdk.ModifierType.None); extensibleEditor.OnIMProcessedKeyPressEvent((Gdk.Key) '"', '"', Gdk.ModifierType.None); Assert.AreEqual(originalText.Insert(offset, "\"\""), editor.Text); }
public void SortSimpleTest() { var editor = TextEditorFactory.CreateNewEditor(); editor.FileName = "a.cs"; editor.MimeType = "text/x-csharp"; CodeCommentTests.SetupInput(editor, @" 9 <-8 7 6 5 4 3 2-> 1 "); EditActions.SortSelectedLines(editor); Assert.AreEqual(@" 9 2 3 4 5 6 7 8 1 ", editor.Text); }
internal static void RunHighlightingTest(SyntaxHighlightingDefinition highlighting, string inputText) { var editor = TextEditorFactory.CreateNewEditor(); var sb = new StringBuilder(); int lineNumber = 0; var expectedSegments = new List <Tuple <DocumentLocation, string> > (); using (var sr = new StringReader(inputText)) { while (true) { var lineText = sr.ReadLine(); if (lineText == null) { break; } var idx = lineText.IndexOf('^'); if (idx >= 0) { expectedSegments.Add(Tuple.Create(new DocumentLocation(lineNumber, idx + 1), lineText.Substring(idx + 1).Trim())); } else { lineNumber++; sb.AppendLine(lineText); } } } editor.Text = sb.ToString(); highlighting.PrepareMatches(); editor.SyntaxHighlighting = new SyntaxHighlighting(highlighting, editor); //var line = editor.GetLine (6); { foreach (var line in editor.GetLines()) { var coloredSegments = editor.SyntaxHighlighting.GetHighlightedLineAsync(line, CancellationToken.None).WaitAndGetResult(CancellationToken.None).Segments; for (int i = 0; i < expectedSegments.Count; i++) { var seg = expectedSegments [i]; if (seg.Item1.Line == line.LineNumber) { var matchedSegment = coloredSegments.FirstOrDefault(s => s.Contains(seg.Item1.Column - 1)); Assert.NotNull(matchedSegment, "No segment found at : " + seg.Item1); foreach (var segi in seg.Item2.Split(new [] { " " }, StringSplitOptions.RemoveEmptyEntries)) { Console.WriteLine("line " + line.LineNumber + " : " + editor.GetTextAt(line)); Console.WriteLine(segi); Console.WriteLine(string.Join(", ", matchedSegment.ScopeStack.ToArray())); string mk = null; int d = 0; var expr = StackMatchExpression.Parse(segi); var matchResult = expr.MatchesStack(matchedSegment.ScopeStack, ref mk); Assert.IsTrue(matchResult.Item1, "Wrong color at " + seg.Item1 + " expected " + segi + " was " + string.Join(", ", matchedSegment.ScopeStack.ToArray())); } expectedSegments.RemoveAt(i); i--; } } } Assert.AreEqual(0, expectedSegments.Count, "Not all segments matched."); }
public void TestHRefSearching() { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = "My favorite web sites include:</P>" + "<A HREF=\"http://msdn2.microsoft.com\">" + "MSDN Home Page</A></P>" + "<A HREF=\"http://www.microsoft.com\">" + "Microsoft Corporation Home Page</A></P>" + "<A HREF=\"http://blogs.msdn.com/bclteam\">" + ".NET Base Class Library blog</A></P>";; var HRefPattern = "href\\s*=\\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\\S+))"; var m = Regex.Match(editor.Text, HRefPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(1)); int matches = 0; while (m.Success) { matches++; m = m.NextMatch(); } Assert.AreEqual(3, matches); }
void TestFoldings(string text) { var editor = TextEditorFactory.CreateNewEditor(); var sb = new StringBuilder(); var foldStack = new Stack <int> (); var expected = new List <ISegment> (); foreach (var ch in text) { if (ch == '+') { foldStack.Push(sb.Length); continue; } if (ch == '-') { expected.Add(TextSegment.FromBounds(foldStack.Pop(), sb.Length)); continue; } sb.Append(ch); } editor.Text = sb.ToString(); var ext = new TextMateFoldingTextEditorExtension(); var foldings = ext.GetFoldingsAsync(editor, default(CancellationToken)).Result.ToList(); Assert.AreEqual(expected.Count, foldings.Count); for (int i = 0; i < foldings.Count; i++) { Assert.AreEqual(expected [i].Offset, foldings [i].Offset); Assert.AreEqual(expected [i].Length, foldings [i].Length); } }
static void InvokeEditConfigTest(string editConfig, Action <TextEditor> test) { string tempPath; int i = 0; while (true) { tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); if (!File.Exists(tempPath)) { break; } if (i++ > 100) { Assert.Fail("Can't create random path."); } } Directory.CreateDirectory(tempPath); try { string editorConfigFile = Path.Combine(tempPath, ".editorconfig"); File.WriteAllText(editorConfigFile, editConfig); var editor = TextEditorFactory.CreateNewEditor(); var viewContent = editor.GetViewContent(); editor.OptionsChanged += delegate { test(editor); }; viewContent.ContentName = Path.Combine(tempPath, "a.cs"); } finally { Directory.Delete(tempPath, true); } }
static async Task InvokeEditConfigTest(string editConfig, Action <TextEditor> test) { string tempPath; int i = 0; while (true) { tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); if (!File.Exists(tempPath)) { break; } if (i++ > 100) { Assert.Fail("Can't create random path."); } } Directory.CreateDirectory(tempPath); try { string editorConfigFile = Path.Combine(tempPath, ".editorconfig"); File.WriteAllText(editorConfigFile, editConfig); var editor = TextEditorFactory.CreateNewEditor(); var viewContent = editor.GetViewContent(); string fileName = Path.Combine(tempPath, "a.cs"); var ctx = await EditorConfigService.GetEditorConfigContext(fileName); ((DefaultSourceEditorOptions)editor.Options).SetContext(ctx); test(editor); } finally { Directory.Delete(tempPath, true); } }
protected override IReadonlyTextDocument CreateReadonlyTextDocument(string text, Encoding enc = null) { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = text; editor.Encoding = enc; return(editor.CreateDocumentSnapshot()); }
protected override TextEditor CreateTextEditor(string text, Encoding enc = null) { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = text; editor.Encoding = enc; return(editor); }
protected override TextEditor CreateTextEditor(string text, Encoding enc = null, bool useBom = false) { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = text; editor.Encoding = enc; editor.UseBOM = useBom; return(editor); }
internal static TextEditor CreateTextEditor(string input) { var editor = TextEditorFactory.CreateNewEditor(); editor.FileName = "a.cs"; editor.MimeType = "text/x-csharp"; 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; } } editor.Text = sb.ToString(); editor.CaretOffset = cursorPosition; if (selectionStart >= 0 && selectionEnd >= 0) { editor.SetSelection(selectionStart, selectionEnd); } return(editor); }
public IViewContent CreateContent(FilePath fileName, string mimeType, Project ownerProject) { var editor = TextEditorFactory.CreateNewEditor(); editor.MimeType = mimeType; editor.GetViewContent().Project = ownerProject; editor.GetViewContent().ContentName = fileName; return(editor.GetViewContent()); }
public void TestProjectionCompletion() { var editor = TextEditorFactory.CreateNewEditor(); var options = new CustomEditorOptions(editor.Options); options.EditorTheme = "Tango"; editor.Options = options; editor.Text = "12345678901234567890"; var projectedDocument = TextEditorFactory.CreateNewDocument( new StringTextSource("__12__34__56__78__90"), "a" ); var segments = new List <ProjectedSegment> (); for (int i = 0; i < 5; i++) { segments.Add(new ProjectedSegment(i * 2, 2 + i * 4, 2)); } var projection = new Projection.Projection(projectedDocument, segments); var tww = new TestWorkbenchWindow(); var content = new TestViewContent(); tww.ViewContent = content; var originalContext = new Document(tww); var projectedEditor = projection.CreateProjectedEditor(originalContext); TestCompletionExtension orignalExtension; editor.SetExtensionChain(originalContext, new [] { orignalExtension = new TestCompletionExtension(editor) { CompletionWidget = new EmptyCompletionWidget(editor) } }); TestCompletionExtension projectedExtension; projectedEditor.SetExtensionChain(originalContext, new [] { projectedExtension = new TestCompletionExtension(editor) { CompletionWidget = new EmptyCompletionWidget(projectedEditor) } }); editor.SetOrUpdateProjections(originalContext, new [] { projection }, TypeSystem.DisabledProjectionFeatures.None); editor.CaretOffset = 1; var service = new CommandManager(); service.LoadCommands("/MonoDevelop/Ide/Commands"); service.DispatchCommand(TextEditorCommands.ShowCompletionWindow, null, editor.CommandRouter); Assert.IsFalse(orignalExtension.CompletionRun); Assert.IsTrue(projectedExtension.CompletionRun); editor.CaretOffset = 15; CompletionWindowManager.HideWindow(); service.DispatchCommand(TextEditorCommands.ShowCompletionWindow, null, editor.CommandRouter); Assert.IsTrue(orignalExtension.CompletionRun); }
void ToggleCodeCommentWithBlockComments() { var blockStarts = TextEditorFactory.GetSyntaxProperties(textEditor.MimeType, "BlockCommentStart"); var blockEnds = TextEditorFactory.GetSyntaxProperties(textEditor.MimeType, "BlockCommentEnd"); if (blockStarts == null || blockEnds == null || blockStarts.Length == 0 || blockEnds.Length == 0) { return; } string blockStart = blockStarts[0]; string blockEnd = blockEnds[0]; using (var undo = textEditor.OpenUndoGroup()) { IDocumentLine startLine; IDocumentLine endLine; if (textEditor.IsSomethingSelected) { startLine = textEditor.GetLineByOffset(textEditor.SelectionRange.Offset); endLine = textEditor.GetLineByOffset(textEditor.SelectionRange.EndOffset); // If selection ends at begining of line... This is visible as previous line // is selected, hence we want to select previous line Bug 26287 if (endLine.Offset == textEditor.SelectionRange.EndOffset) { endLine = endLine.PreviousLine; } } else { startLine = endLine = textEditor.GetLine(textEditor.CaretLine); } string startLineText = textEditor.GetTextAt(startLine.Offset, startLine.Length); string endLineText = textEditor.GetTextAt(endLine.Offset, endLine.Length); if (startLineText.StartsWith(blockStart, StringComparison.Ordinal) && endLineText.EndsWith(blockEnd, StringComparison.Ordinal)) { textEditor.RemoveText(endLine.Offset + endLine.Length - blockEnd.Length, blockEnd.Length); textEditor.RemoveText(startLine.Offset, blockStart.Length); if (textEditor.IsSomethingSelected) { textEditor.SelectionAnchorOffset -= blockEnd.Length; } } else { textEditor.InsertText(endLine.Offset + endLine.Length, blockEnd); textEditor.InsertText(startLine.Offset, blockStart); if (textEditor.IsSomethingSelected) { textEditor.SelectionAnchorOffset += blockEnd.Length; } } } }
public void MatchingBracketTests(string text, int offset, int expectedOffsetMatch) { var editor = TextEditorFactory.CreateNewEditor(); editor.MimeType = "text/x-csharp"; editor.Text = text; int actualOffset = SimpleBracketMatcher.GetMatchingBracketOffset(editor, offset); Assert.AreEqual(expectedOffsetMatch, actualOffset); }
public void TestPositionalMatch() { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = "FooBarFoo"; var r = new Regex("B.r"); var match = r.Match(editor.Text, "Foo".Length, editor.Length - "Foo".Length); Assert.IsTrue(match.Success); Assert.AreEqual("Foo".Length, match.Groups [0].Index); }
bool TryGetLineCommentTag(out string commentTag) { var lineComments = TextEditorFactory.GetSyntaxProperties(textEditor.MimeType, "LineComment"); if (lineComments == null || lineComments.Length == 0) { commentTag = null; return(false); } commentTag = lineComments [0]; return(true); }
public void TestUsingMatch() { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = "// FooBar\nusing System;"; var r = new Regex("^\\s*(using)\\s+([^ ;]*);", RegexOptions.Multiline); var line = editor.GetLine(2); var match = r.Match(editor.Text, line.Offset, line.Length); Assert.IsTrue(match.Success); }
void OnUpdateToggleComment(CommandInfo info) { var lineComments = TextEditorFactory.GetSyntaxProperties(textEditor.MimeType, "LineComment"); if (lineComments != null && lineComments.Length > 0) { info.Visible = true; return; } var blockStarts = TextEditorFactory.GetSyntaxProperties(textEditor.MimeType, "BlockCommentStart"); var blockEnds = TextEditorFactory.GetSyntaxProperties(textEditor.MimeType, "BlockCommentEnd"); info.Visible = blockStarts != null && blockStarts.Length > 0 && blockEnds != null && blockEnds.Length > 0; }
public ViewContent CreateContent(FilePath fileName, string mimeType, Project ownerProject) { TextEditor editor; // HACK: CreateNewEditor really needs to know whether the document exists (& should be loaded) // or we're creating an empty document with the given file name & mime type. // // That information could be added to FilePath but fileName is converted to a string below // which means the information is lost. editor = TextEditorFactory.CreateNewEditor(fileName, mimeType); editor.GetViewContent().Project = ownerProject; return(editor.GetViewContent()); }
public void EmptyTreeList() { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = "1\n2\n3\n4\n5\n"; var editor2 = TextEditorFactory.CreateNewEditor(); editor2.Text = "4\n1\n5\n2\n3\n"; var diff = editor.GetDiffAsString(editor2); Assert.AreEqual("--- \n+++ \n@@ -1,5 +1,5 @@\n+4\n 1\n+5\n 2\n 3\n-4\n-5\n", diff); }
public void Test677780() { var formatter = new TestAbstractCodeFormatter(); var editor = TextEditorFactory.CreateNewEditor(); editor.Text = new string ('\n', 9); try { formatter.CorrectIndenting(PolicyService.InvariantPolicies, editor, 0); Assert.Fail(); } catch (ArgumentOutOfRangeException) { } formatter.CorrectIndenting(PolicyService.InvariantPolicies, editor, 1); formatter.CorrectIndenting(PolicyService.InvariantPolicies, editor, 10); try { formatter.CorrectIndenting(PolicyService.InvariantPolicies, editor, 11); Assert.Fail(); } catch (ArgumentOutOfRangeException) { } }
protected override async Task <Control> OnGetViewControlAsync(CancellationToken token, DocumentViewContent view) { if (textEditor == null) { await Model.Load(); var editor = TextEditorFactory.CreateNewEditor((TextBufferFileModel)Model); var impl = editor.Implementation; await Init(editor, impl); HasUnsavedChanges = impl.IsDirty; // Editor extensions can provide additional content NotifyContentChanged(); } return(textEditor); }
public ViewContent CreateContent(FilePath fileName, string mimeType, Project ownerProject) { TextEditor editor; // HACK: this is a very poor test for whether to load the document. Maybe add an IsPlaceholder property to FilePath. // Another alternative would be to add a parameter to CreateContent. if (File.Exists(fileName)) { editor = TextEditorFactory.CreateNewEditor(fileName, mimeType); } else { editor = TextEditorFactory.CreateNewEditor(); editor.FileName = fileName; editor.MimeType = mimeType; } editor.GetViewContent().Project = ownerProject; return(editor.GetViewContent()); }
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 void TestProjectionUpdate() { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = "1234567890"; var projectedDocument = TextEditorFactory.CreateNewDocument( new StringTextSource("__12__34__56__78__90"), "a" ); var segments = new List <ProjectedSegment> (); for (int i = 0; i < 5; i++) { segments.Add(new ProjectedSegment(i * 2, 2 + i * 4, 2)); } var projection = new Projection.Projection(projectedDocument, segments); var tww = new TestWorkbenchWindow(); var content = new TestViewContent(); tww.ViewContent = content; var originalContext = new Document(tww); using (var testCase = new TextEditorExtensionTestCase(originalContext, content, tww, null, false)) { var projectedEditor = projection.CreateProjectedEditor(originalContext); editor.SetOrUpdateProjections(originalContext, new [] { projection }, TypeSystem.DisabledProjectionFeatures.All); editor.InsertText(1, "foo"); Assert.AreEqual("__1foo2__34__56__78__90", projectedEditor.Text); Assert.AreEqual(2, projection.ProjectedSegments.ElementAt(0).ProjectedOffset); Assert.AreEqual(2 + "foo".Length, projection.ProjectedSegments.ElementAt(0).Length); for (int i = 1; i < 5; i++) { Assert.AreEqual(2 + i * 4 + "foo".Length, projection.ProjectedSegments.ElementAt(i).ProjectedOffset); Assert.AreEqual(2, projection.ProjectedSegments.ElementAt(i).Length); } } }
public void TestProjectionHighlighting() { var editor = TextEditorFactory.CreateNewEditor(); var options = new CustomEditorOptions(editor.Options); options.EditorTheme = "Tango"; editor.Options = options; editor.Text = "1234567890"; var projectedDocument = TextEditorFactory.CreateNewDocument( new StringTextSource("__12__34__56__78__90"), "a" ); var segments = new List <ProjectedSegment> (); for (int i = 0; i < 5; i++) { segments.Add(new ProjectedSegment(i * 2, 2 + i * 4, 2)); } var projection = new Projection.Projection(projectedDocument, segments); var tww = new TestWorkbenchWindow(); var content = new TestViewContent(); tww.ViewContent = content; var originalContext = new Document(tww); using (var testCase = new TextEditorExtensionTestCase(originalContext, content, tww, null, false)) { var projectedEditor = projection.CreateProjectedEditor(originalContext); projectedEditor.SemanticHighlighting = new TestSemanticHighlighting(projectedEditor, originalContext); editor.SetOrUpdateProjections(originalContext, new [] { projection }, TypeSystem.DisabledProjectionFeatures.None); var markup = editor.GetMarkup(0, editor.Length, new MarkupOptions(MarkupFormat.Pango)); var color = "#3363a4"; Assert.AreEqual("<span foreground=\"" + color + "\">1</span><span foreground=\"#222222\">234</span><span foreground=\"" + color + "\">5</span><span foreground=\"#222222\">678</span><span foreground=\"" + color + "\">9</span><span foreground=\"#222222\">0</span>", markup); } }
public async Task TestProjectionUpdate() { var editor = TextEditorFactory.CreateNewEditor(); editor.Text = "1234567890"; var projectedDocument = TextEditorFactory.CreateNewDocument( new StringTextSource("__12__34__56__78__90"), "a" ); var segments = new List <ProjectedSegment> (); for (int i = 0; i < 5; i++) { segments.Add(new ProjectedSegment(i * 2, 2 + i * 4, 2)); } var projection = new Projection.Projection(projectedDocument, segments); var content = new TestViewContent(); await content.Initialize(new FileDescriptor ("Foo.cs", null, null)); using (var testCase = await TextEditorExtensionTestCase.Create(content, null, false)) { var originalContext = testCase.Document.DocumentContext; var projectedEditor = projection.CreateProjectedEditor(originalContext); editor.SetOrUpdateProjections(originalContext, new [] { projection }, TypeSystem.DisabledProjectionFeatures.All); editor.InsertText(1, "foo"); Assert.AreEqual("__1foo2__34__56__78__90", projectedEditor.Text); Assert.AreEqual(2, projection.ProjectedSegments.ElementAt(0).ProjectedOffset); Assert.AreEqual(2 + "foo".Length, projection.ProjectedSegments.ElementAt(0).Length); for (int i = 1; i < 5; i++) { Assert.AreEqual(2 + i * 4 + "foo".Length, projection.ProjectedSegments.ElementAt(i).ProjectedOffset); Assert.AreEqual(2, projection.ProjectedSegments.ElementAt(i).Length); } } }
public void TestVSTS615849() { DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket = true; var tww = new TestWorkbenchWindow(); var content = new TestViewContent(); tww.ViewContent = content; var document = new Document(tww); var editor = TextEditorFactory.CreateNewEditor(document); editor.MimeType = "text/xml"; const string originalText = @""; editor.Text = originalText; var extensibleEditor = FindChild <ExtensibleTextEditor> (editor.GetNativeWidget <Container> ()); extensibleEditor.OnIMProcessedKeyPressEvent((Gdk.Key) '"', '"', Gdk.ModifierType.None); extensibleEditor.OnIMProcessedKeyPressEvent(Gdk.Key.Right, '\0', Gdk.ModifierType.None); extensibleEditor.OnIMProcessedKeyPressEvent(Gdk.Key.BackSpace, '\0', Gdk.ModifierType.None); Assert.AreEqual("\"", editor.Text); }
public void TestVSTS615849() { DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket = true; var tww = new TestWorkbenchWindow(); var content = new TestViewContent(); tww.ViewContent = content; var document = new Document(tww); using (var testCase = new TextEditorExtensionTestCase(document, content, tww, null, false)) { var editor = TextEditorFactory.CreateNewEditor(document); editor.MimeType = "text/xml"; const string originalText = @""; editor.Text = originalText; var extensibleEditor = editor.GetContent <SourceEditorView> ().TextEditor; extensibleEditor.OnIMProcessedKeyPressEvent((Gdk.Key) '"', '"', Gdk.ModifierType.None); extensibleEditor.OnIMProcessedKeyPressEvent(Gdk.Key.Right, '\0', Gdk.ModifierType.None); extensibleEditor.OnIMProcessedKeyPressEvent(Gdk.Key.BackSpace, '\0', Gdk.ModifierType.None); Assert.AreEqual("\"", editor.Text); } }