public PaintLineLocation[] ViewportLines(FormattedCode code, RectangleF viewport, float fontHeight)
        {
            List<PaintLineLocation> list = new List<PaintLineLocation>();
            int visibles = CountVisibleLines(viewport.Height, fontHeight);
            int topIndex = YCoordinateToLineIndex(viewport.Top, fontHeight);
            int lineIndex = YCoordinateToLineIndex(viewport.Top, fontHeight);
            int i;

            for (i = 0; i < visibles; ++i, lineIndex++)
            {
                if (lineIndex < 0)
                    continue;

                if (lineIndex >= code.LineCount)
                    break;

                list.Add(
                    new PaintLineLocation(lineIndex, code.GetTextAt(lineIndex),
                    new PointF(-viewport.Left,
                        LineIndexToYCoordinate(lineIndex, fontHeight) -
                        viewport.Top)));
            }

            return (list.ToArray());
        }
        public void DrawToGraphics(FormattedCode code, CodeRenderingContext args, Rectangle viewport)
        {
            UiExceptionHelper.CheckNotNull(code, "code");
            UiExceptionHelper.CheckNotNull(args, "args");

            ClassifiedTokenCollection line;
            PaintLineLocation[] lines;
            ClassifiedToken token;
            float fontHeight;
            string text;
            float tk_width;
            int i;
            float x;

            fontHeight = LineIndexToYCoordinate(1, args.Graphics, args.Font);
            lines = ViewportLines(code, viewport, fontHeight);

            foreach (PaintLineLocation paintLine in lines)
            {
                // All lines that differ from CurrentLine are displayed
                // in using different styles of Brush to make a distinction
                // between code, keyword, comments.
                if (paintLine.LineIndex != args.CurrentLine)
                {
                    line = code[paintLine.LineIndex];
                    x = 0;
                    text = line.Text;

                    for (i = 0; i < line.Count; ++i)
                    {
                        token = line[i];

                        args.Graphics.DrawString(token.Text, args.Font, args.GetBrush(token.Tag),
                            paintLine.Location.X + x, paintLine.Location.Y);

                        tk_width = measureStringWidth(args.Graphics, args.Font, text,
                            token.IndexStart, token.Text.Length);

                        x += tk_width;
                    }

                    continue;
                }

                // The current line is emphasized by using a 
                // specific couples of Background & Foreground colors

                args.Graphics.FillRectangle(
                    args.CurrentLineBackBrush,
                    0, paintLine.Location.Y,
                    viewport.Width, fontHeight);

                args.Graphics.DrawString(
                    paintLine.Text, args.Font,
                    args.CurrentLineForeBrush,
                    paintLine.Location.X, paintLine.Location.Y);
            }

            return;
        }
        public void SetUp()
        {
            _renderer = new DefaultCodeRenderer();

            ICodeFormatter formatter = new PlainTextCodeFormatter();

            _empty = formatter.Format("");

            _text3x7 = formatter.Format(
                    "111\r\n" +
                    "222\r\n" +
                    "333\r\n" +
                    "444\r\n" +
                    "555\r\n" +
                    "666\r\n" +
                    "777\r\n");

            _loremIpsum = formatter.Format(
                "Lorem ipsum dolor sit\r\n" +
                "amet, consectetur adipiscing elit.\r\n" +
                "Maecenas a nisi. In imperdiet, orci in\r\n" +
                "porta facilisis,\r\n" +
                "odio purus iaculis est, non varius urna turpis in mi.\r\n" + // longest line
                "Nullam dictum. Ut iaculis dignissim nulla.\r\n" +
                "Nullam condimentum porttitor leo.\r\n" +
                "Integer a odio et\r\n" +
                "velit suscipit pulvinar.");

            Image img = new Bitmap(100, 100);
            _args = new CodeRenderingContext();
            _args.Graphics = Graphics.FromImage(img);
            _args.Font = new Font("Courier New", 12);

            return;
        }
Example #4
0
        /// <summary>
        /// Interprets and highlight the given string as C# code
        /// and return the resulting FormattedCode instance.
        /// </summary>
        /// <param name="csharpCode">A string read as C# code.
        /// This parameter must not be null.</param>
        /// <returns>A FormattedCode instance containing data
        /// to highlight the text with basic syntax coloring.</returns>
        public FormattedCode Format(string csharpCode)
        {
            UiExceptionHelper.CheckNotNull(csharpCode, "csharpCode");

            _info      = FormattedCode.NewCodeInfo();
            csharpCode = PreProcess(csharpCode);
            Parse(csharpCode);

            return(CSCode);
        }
Example #5
0
        public CodeInfo CopyInfo()
        {
            FormattedCode copy;

            copy = new FormattedCode(_codeInfo.Text,
                                     _codeInfo.IndexArray.ToArray(),
                                     _codeInfo.TagArray.ToArray(),
                                     _codeInfo.LineArray.ToArray());

            return(copy._codeInfo);
        }
Example #6
0
        public CodeInfo CopyInfo()
        {
            FormattedCode copy;

            copy = new FormattedCode(_codeInfo.Text, 
                _codeInfo.IndexArray.ToArray(),
                _codeInfo.TagArray.ToArray(), 
                _codeInfo.LineArray.ToArray());

            return (copy._codeInfo);
        }
Example #7
0
        public void SetUp()
        {
            _mockFormatter = Substitute.For<IFormatterCatalog>();
            _mockRenderer = Substitute.For<ICodeRenderer>();

            _box = new TestingCodeBox(_mockFormatter, _mockRenderer);
            _box.Width = 150;
            _box.Height = 150;

            _someText = Format("some C# code", "");
            _someCode = Format("some C# code", "C#");

            return;
        }
Example #8
0
        TestingCodeBox SetupCodeBox(FormattedCode code, SizeF size)
        {
            TestingCodeBox box;

            box = new TestingCodeBox(_mockFormatter, _mockRenderer);

            _mockFormatter.Format(code.Text, "").Returns(code);
            _mockRenderer.GetDocumentSize(code, box.RenderingContext.Graphics, box.RenderingContext.Font).Returns(size);

            box.Text = code.Text;
            Assert.That(box.Text, Is.EqualTo(code.Text));

            return (box);
        }
        public void SetUp()
        {
            _mockFormatter = new DynamicMock(typeof(IFormatterCatalog));
            _mockRenderer = new DynamicMock(typeof(ICodeRenderer));

            _box = new TestingCodeBox((IFormatterCatalog)_mockFormatter.MockInstance,
                                (ICodeRenderer)_mockRenderer.MockInstance);
            _box.Width = 150;
            _box.Height = 150;

//            _emptyText = Format("", "");
            _someText = Format("some C# code", "");
            _someCode = Format("some C# code", "C#");

            return;
        }
        public void Format_HelloWorld()
        {
            FormattedCode res;
            FormattedCode exp;
            
            res = _formatter.Format("Hello world!");

            exp = new FormattedCode(
                "Hello world!",
                new int[] { 0 },
                new byte[] { 0 },
                new int[] { 0 });

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
Example #11
0
        public void Test_Format()
        {
            FormattedCode exp;
            FormattedCode res;

            res = _parser.Format("line 1\n  line 2\nline 3\n");

            exp = new FormattedCode(
                "line 1\n  line 2\nline 3\n",
                new int[] { 0, 7, 16 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 }
                );

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
        public void Test_ComplexCollection()
        {
            _code = new TestingCSCode(
                "int i; //comment\n" +
                "char c='a';\n",
                new int[] { 0, 4, 7, 17, 22, 24, 27 },
                new byte[] { 1, 0, 2,  1,  0,  3,  0 },
                new int[] { 0, 3 }
            );

            Assert.That(_code.Text, Is.EqualTo("int i; //comment\nchar c='a';\n"));
            Assert.That(_code.LineCount, Is.EqualTo(2));

            Assert.That(_code[0], Is.Not.Null);
            Assert.That(_code[0].Text, Is.EqualTo("int i; //comment"));

            Assert.That(_code[1], Is.Not.Null);
            Assert.That(_code[1].Text, Is.EqualTo("char c='a';"));

            // check internal data

            Assert.That(_code[0].Count, Is.EqualTo(3));
            Assert.That(_code[0][0].Text, Is.EqualTo("int "));
            Assert.That(_code[0][0].Tag, Is.EqualTo(ClassificationTag.Keyword));
            Assert.That(_code[0][1].Text, Is.EqualTo("i; "));
            Assert.That(_code[0][1].Tag, Is.EqualTo(ClassificationTag.Code));
            Assert.That(_code[0][2].Text, Is.EqualTo("//comment"));
            Assert.That(_code[0][2].Tag, Is.EqualTo(ClassificationTag.Comment));

            Assert.That(_code[1].Count, Is.EqualTo(4));
            Assert.That(_code[1][0].Text, Is.EqualTo("char "));
            Assert.That(_code[1][0].Tag, Is.EqualTo(ClassificationTag.Keyword));
            Assert.That(_code[1][1].Text, Is.EqualTo("c="));
            Assert.That(_code[1][1].Tag, Is.EqualTo(ClassificationTag.Code));
            Assert.That(_code[1][2].Text, Is.EqualTo("'a'"));
            Assert.That(_code[1][2].Tag, Is.EqualTo(ClassificationTag.String));
            Assert.That(_code[1][3].Text, Is.EqualTo(";"));
            Assert.That(_code[1][3].Tag, Is.EqualTo(ClassificationTag.Code));

            return;
        }
        TestingCodeBox SetupCodeBox(FormattedCode code, SizeF size)
        {
            TestingCodeBox box;

            box = new TestingCodeBox(
                (IFormatterCatalog)_mockFormatter.MockInstance,
                (ICodeRenderer)_mockRenderer.MockInstance);

            _mockFormatter.ExpectAndReturn("Format", code, new object[] { code.Text, "" });

            _mockRenderer.ExpectAndReturn("GetDocumentSize", size,
                new object[] { code, box.RenderingContext.Graphics, box.RenderingContext.Font });

            box.Text = code.Text;
            Assert.That(box.Text, Is.EqualTo(code.Text));

            _mockFormatter.Verify();
            _mockRenderer.Verify();

            return (box);
        }
Example #14
0
        /// <summary>
        /// An utility method that check data consistency. The operation
        /// raises an exception if an error is found.
        /// </summary>
        public static void CheckData(FormattedCode data)
        {
            List <int> lines;
            int        line;
            int        bound;
            int        i;

            UiExceptionHelper.CheckNotNull(data, "data");

            UiExceptionHelper.CheckTrue(
                data._codeInfo.IndexArray.Count == data._codeInfo.TagArray.Count,
                "IndexArray.Count and TagArray.Count must match.",
                "data");

            bound = data._codeInfo.IndexArray.Count;
            lines = data._codeInfo.LineArray;
            for (i = 0; i < lines.Count; ++i)
            {
                line = lines[i];

                UiExceptionHelper.CheckTrue(
                    line >= 0 && line < bound,
                    "Bad LineArray value at index " + i + ", value was: " + line + ", expected to be in: [0-" + bound + "[.",
                    "data");

                if (i == 0)
                {
                    continue;
                }

                UiExceptionHelper.CheckTrue(
                    lines[i] > lines[i - 1],
                    "Bad LineArray[" + i + "], value was: " + line + ", expected to be > than LineArray[" + (i - 1) + "]=" + lines[i - 1] + ".",
                    "data");
            }

            return;
        }
        public void Test_SimpleCollection()
        {
            _code = new TestingCSCode(
                "line 1\n  line 2\nline 3\n",
                new int[] { 0, 7, 16 },
                new byte[] { 0, 0,  0 },
                new int[] { 0, 1, 2 }
                );

            Assert.That(_code.Text, Is.EqualTo("line 1\n  line 2\nline 3\n"));
            Assert.That(_code.LineCount, Is.EqualTo(3));

            Assert.That(_code[0], Is.Not.Null);
            Assert.That(_code[0].Text, Is.EqualTo("line 1"));

            Assert.That(_code[1], Is.Not.Null);
            Assert.That(_code[1].Text, Is.EqualTo("  line 2"));

            Assert.That(_code[2], Is.Not.Null);
            Assert.That(_code[2].Text, Is.EqualTo("line 3"));

            // check internal data

            Assert.That(_code[0].Count, Is.EqualTo(1));
            Assert.That(_code[0][0].Text, Is.EqualTo("line 1"));
            Assert.That(_code[0][0].Tag, Is.EqualTo(ClassificationTag.Code));

            Assert.That(_code[1].Count, Is.EqualTo(1));
            Assert.That(_code[1][0].Text, Is.EqualTo("  line 2"));
            Assert.That(_code[1][0].Tag, Is.EqualTo(ClassificationTag.Code));

            Assert.That(_code[2].Count, Is.EqualTo(1));
            Assert.That(_code[2][0].Text, Is.EqualTo("line 3"));
            Assert.That(_code[2][0].Tag, Is.EqualTo(ClassificationTag.Code));

            return;
        }
Example #16
0
        protected CodeBox(IFormatterCatalog formatter, ICodeRenderer renderer)
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            DoubleBuffered = true;

            _formatter = formatter;
            _formattedCode = FormattedCode.Empty;

            _renderer = renderer;

            _currentLine = -1;
            _showCurrentLine = false;

            _language = "";

            this.Font = new Font("Courier New", 8);
            this.BackColor = Color.White;

            createGraphics();
            AutoScroll = true;

            return;
        }
Example #17
0
        public void Test_Format_3()
        {
            FormattedCode exp;
            FormattedCode res;

            // Ensure that escaping sequences are
            // handled correctly
            //                    0  2           14   17    21        
            res = _parser.Format("s=\"<font class=\\\"cls\\\">hi, there!</font>");

            exp = new FormattedCode(
                "s=\"<font class=\\\"cls\\\">hi, there!</font>",
                new int[] { 0, 2 },
                new byte[] { 0, 3 },
                new int[] { 0 });

            Assert.That(res, Is.EqualTo(exp));

            _parser = new TestingCSharpCodeFormatter();

            //                   0  2              
            res = _parser.Format("s=\"<font class=\\\\\"cls\\\">hi, there!</font>");
            exp = new FormattedCode(
                "s=\"<font class=\\\\\"cls\\\">hi, there!</font>",
                new int[] { 0, 2, 18, 22 },
                new byte[] { 0, 3, 0, 3 },
                new int[] { 0 });

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
        public void Test_Equals()
        {
            _code = new TestingCSCode(
               "line",
               new int[] { 0 },
               new byte[] { 0 },
               new int[] { 0 }
               );

            // Tests to fail

            Assert.That(_code.Equals(null), Is.False);
            Assert.That(_code.Equals("hello"), Is.False);
            Assert.That(_code.Equals(
                new TestingCSCode("a", new int[] { 0 }, new byte[] { 0 }, new int[] { 0 })),
                Is.False);
            Assert.That(_code.Equals(
                new TestingCSCode("line", new int[] { 1 }, new byte[] { 0 }, new int[] { 0 })),
                Is.False);
            Assert.That(_code.Equals(
                new TestingCSCode("line", new int[] { 0 }, new byte[] { 1 }, new int[] { 0 })),
                Is.False);
            Assert.That(_code.Equals(
                new TestingCSCode("line", new int[] { 0 }, new byte[] { 0 }, new int[] { 1 })),
                Is.False);

            Assert.That(_code.Equals(
                new TestingCSCode("line", new int[] { 0, 0 }, new byte[] { 0 }, new int[] { 0 })),
                Is.False);
            Assert.That(_code.Equals(
                new TestingCSCode("line", new int[] { 0 }, new byte[] { 0, 0 }, new int[] { 0 })),
                Is.False);
            Assert.That(_code.Equals(
                new TestingCSCode("line", new int[] { 0 }, new byte[] { 0 }, new int[] { 0, 0 })),
                Is.False);

            // NUnit.UiException.Tests to pass

            Assert.That(_code.Equals(
                new TestingCSCode("line", new int[] { 0 }, new byte[] { 0 }, new int[] { 0 })),
                Is.True);

            return;
        }
Example #19
0
        /// <summary>
        /// Builds a new instance of CSharpCodeFormatter.
        /// </summary>
        public CSharpCodeFormatter()
        {
            _info = FormattedCode.NewCodeInfo();

            return;
        }
Example #20
0
 public InternalFormattedCode(FormattedCode.CodeInfo info)
 {
     _codeInfo = info;
 }
        public void Test_MaxLength()
        {
            _code = new TestingCSCode(
                "", new int[] { }, new byte[] { }, new int[] { });
            Assert.That(_code.MaxLength, Is.EqualTo(0));

            _code = new TestingCSCode(
                "a\r\nabc\r\nab",
                new int[] { 0, 3, 8 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 });
            Assert.That(_code.MaxLength, Is.EqualTo(3));

            _code = new TestingCSCode(
                "a\r\nab\r\nabc",
                new int[] { 0, 3, 7 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 });
            Assert.That(_code.MaxLength, Is.EqualTo(3));

            return;
        }
Example #22
0
        /// <summary>
        /// An utility method that check data consistency. The operation
        /// raises an exception if an error is found.
        /// </summary>
        public static void CheckData(FormattedCode data)
        {
            List<int> lines;
            int line;
            int bound;
            int i;

            UiExceptionHelper.CheckNotNull(data, "data");

            UiExceptionHelper.CheckTrue(
                data._codeInfo.IndexArray.Count == data._codeInfo.TagArray.Count,
                "IndexArray.Count and TagArray.Count must match.",
                "data");

            bound = data._codeInfo.IndexArray.Count;
            lines = data._codeInfo.LineArray;
            for (i = 0; i < lines.Count; ++i)
            {
                line = lines[i];

                UiExceptionHelper.CheckTrue(
                    line >= 0 && line < bound,
                    "Bad LineArray value at index " + i + ", value was: " + line + ", expected to be in: [0-" + bound + "[.",
                    "data");

                if (i == 0)
                    continue;

                UiExceptionHelper.CheckTrue(
                    lines[i] > lines[i - 1],
                    "Bad LineArray[" + i + "], value was: " + line + ", expected to be > than LineArray[" + (i - 1) + "]=" + lines[i - 1] + ".",
                    "data");
            }

            return;
        }
Example #23
0
 public void DrawToGraphics(FormattedCode code, CodeRenderingContext args, Rectangle viewport)
 {
     CURRENTLINE_INDEX = args.CurrentLine;
 }
Example #24
0
 public SizeF GetDocumentSize(FormattedCode code, Graphics g, Font font)
 {
     return (new SizeF(200, 400));
 }
        public void Format_Lines()
        {
            FormattedCode res;
            FormattedCode exp;

            res = _formatter.Format(
                "line 1\r\n" +
                "line 2\r\n" +
                "line 3\r\n");

            exp = new FormattedCode(
                res.Text,
                new int[] { 0, 8, 16 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 });

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
Example #26
0
        public void Test_Format_2()
        {
            FormattedCode exp;
            FormattedCode res;

            res = _parser.Format(
                "int i; //comment\n" +
                "char c='a';\n");

            exp = new FormattedCode(
                "int i; //comment\n" +
                "char c='a';\n",
                new int[] { 0, 3, 7, 16, 17, 21, 24, 27 },
                new byte[] { 1, 0, 2, 0, 1, 0, 3, 0 },
                new int[] { 0, 4 }
            );

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
        public SizeF GetDocumentSize(FormattedCode code, Graphics g, Font font)
        {
            UiExceptionHelper.CheckNotNull(code, "code");
            UiExceptionHelper.CheckNotNull(g, "g");
            UiExceptionHelper.CheckNotNull(font, "font");

            StringBuilder sample;
            SizeF measure;
            int i;

            sample = new StringBuilder();
            for (i = code.MaxLength; i > 0; --i)
                sample.Append("m");

            measure = g.MeasureString(sample.ToString(), font);

            return (new SizeF(measure.Width, measure.Height * code.LineCount));
        }
Example #28
0
        /// <summary>
        /// Appends data in token at the end of output.
        /// </summary>
        /// <param name="token">Token to be merged with output.</param>
        /// <param name="output">Target location.</param>
        private void _flushToken(ClassifiedToken token, FormattedCode.CodeInfo output)
        {
            if (token == null)
                return;

            output.IndexArray.Add(token.IndexStart);
            output.TagArray.Add((byte)token.Tag);

            return;
        }