Author: mkrueger
Date: 2008-02-04 12:47:05 -0500 (Mon, 04 Feb 2008)
New Revision: 94767

Added:
   trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextUtil.cs
Modified:
   trunk/monodevelop/main/src/addins/Mono.Texteditor/ChangeLog
   trunk/monodevelop/main/src/addins/Mono.Texteditor/Makefile.am
   
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.Highlighting/Style.cs
   
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.Highlighting/SyntaxModeService.cs
   trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.mdp
   
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextEditor.cs
   
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextEditorOptions.cs
   trunk/monodevelop/main/src/addins/Mono.Texteditor/OblivionStyle.xml
   trunk/monodevelop/main/src/addins/Mono.Texteditor/TangoStyle.xml
Log:
* Mono.TextEditor.mdp, TangoStyle.xml, OblivionStyle.xml,
  Mono.TextEditor/TextUtil.cs, Mono.TextEditor/TextEditor.cs,
  Mono.TextEditor/TextEditorOptions.cs,
  Mono.TextEditor.Highlighting/SyntaxModeService.cs,
  Mono.TextEditor.Highlighting/Style.cs, Makefile.am: Implemented
  bracket matching.

Modified: trunk/monodevelop/main/src/addins/Mono.Texteditor/ChangeLog
===================================================================
--- trunk/monodevelop/main/src/addins/Mono.Texteditor/ChangeLog 2008-02-04 
17:26:19 UTC (rev 94766)
+++ trunk/monodevelop/main/src/addins/Mono.Texteditor/ChangeLog 2008-02-04 
17:47:05 UTC (rev 94767)
@@ -1,5 +1,14 @@
 2008-02-04  Mike Krüger <[EMAIL PROTECTED]> 
 
+       * Mono.TextEditor.mdp, TangoStyle.xml, OblivionStyle.xml,
+         Mono.TextEditor/TextUtil.cs, Mono.TextEditor/TextEditor.cs,
+         Mono.TextEditor/TextEditorOptions.cs,
+         Mono.TextEditor.Highlighting/SyntaxModeService.cs,
+         Mono.TextEditor.Highlighting/Style.cs, Makefile.am: Implemented 
bracket
+         matching.
+
+2008-02-04  Mike Krüger <[EMAIL PROTECTED]> 
+
        * Mono.TextEditor/DefaultEditActions.cs: Implemented Bug 354129 - Auto 
add
          second brackets, quotation mark.
 

Modified: trunk/monodevelop/main/src/addins/Mono.Texteditor/Makefile.am
===================================================================
--- trunk/monodevelop/main/src/addins/Mono.Texteditor/Makefile.am       
2008-02-04 17:26:19 UTC (rev 94766)
+++ trunk/monodevelop/main/src/addins/Mono.Texteditor/Makefile.am       
2008-02-04 17:47:05 UTC (rev 94767)
@@ -3,11 +3,13 @@
 ASSEMBLY = $(top_builddir)/build/bin/Mono.TextEditor.dll
 REFS =  \
        $(GTK_SHARP_LIBS) \
+       -r:Mono.Posix \
        -r:System \
        -r:System.Xml
 
 FILES =  \
        AssemblyInfo.cs \
+       gtk-gui/generated.cs \
        Mono.TextEditor.Highlighting/Chunk.cs \
        Mono.TextEditor.Highlighting/ChunkStyle.cs \
        Mono.TextEditor.Highlighting/DefaultStyle.cs \
@@ -44,7 +46,8 @@
        Mono.TextEditor/TextEditor.cs \
        Mono.TextEditor/TextEditorData.cs \
        Mono.TextEditor/TextEditorOptions.cs \
-       Mono.TextEditor/TextMarker.cs 
+       Mono.TextEditor/TextMarker.cs \
+       Mono.TextEditor/TextUtil.cs 
        Mono.TextEditor.Highlighting/Match.cs \
        Mono.TextEditor.Highlighting/Style.cs \
        Mono.TextEditor.Highlighting/SyntaxMode.cs \
@@ -61,6 +64,7 @@
        CPPSyntaxMode.xml \
        CSharpSyntaxMode.xml \
        CSyntaxMode.xml \
+       gtk-gui/gui.stetic \
        gtk-gui/objects.xml \
        JavaSyntaxMode.xml \
        OblivionStyle.xml \

Modified: 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextEditor.cs
===================================================================
--- 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextEditor.cs 
    2008-02-04 17:26:19 UTC (rev 94766)
+++ 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextEditor.cs 
    2008-02-04 17:47:05 UTC (rev 94767)
@@ -281,6 +281,37 @@
                                        RedrawLine (Caret.Line);
                                }
                        };
+                       Caret.PositionChanged += delegate {
+                               int offset = Caret.Offset - 1;
+                               if (!TextUtil.IsBracket 
(Document.Buffer.GetCharAt (offset)))
+                                       offset++;
+                               if (offset < 0)
+                                       offset = 0;
+                               if (offset >= Document.Buffer.Length)
+                                       return;
+                               char ch = Document.Buffer.GetCharAt (offset);
+                               int bracket = TextUtil.openBrackets.IndexOf 
(ch);
+                               int oldIndex = bracketIndex;
+                               if (bracket >= 0) {
+                                       bracketIndex = 
TextUtil.SearchMatchingBracketForward (Document, offset + 1, bracket);
+                               } else {
+                                       bracket = 
TextUtil.closingBrackets.IndexOf (ch);
+                                       if (bracket >= 0) {
+                                               bracketIndex = 
TextUtil.SearchMatchingBracketBackward (Document, offset - 1, bracket);
+                                       } else {
+                                               bracketIndex = -1;
+                                       }
+                               }
+                               if (bracketIndex != oldIndex) {
+                                       int line1 = oldIndex >= 0 ? 
Document.Splitter.OffsetToLineNumber (oldIndex) : -1;
+                                       int line2 = bracketIndex >= 0 ? 
Document.Splitter.OffsetToLineNumber (bracketIndex) : -1;
+                                       if (line1 >= 0)
+                                               this.RedrawLine (line1);
+                                       if (line1 != line2 && line2 >= 0)
+                                               this.RedrawLine (line2);
+                               }
+                       };
+                               
                        this.ColorStyle = new DefaultStyle (this);
                        defaultCursor = null;
                        textCursor = new Gdk.Cursor (Gdk.CursorType.Xterm);
@@ -292,6 +323,8 @@
                        Gtk.Drag.DestSet (this, DestDefaults.All, 
(TargetEntry[])list, DragAction.Move | DragAction.Copy);
                }
                
+               int bracketIndex = -1;
+               
                protected virtual void OptionsChanged (object sender, EventArgs 
args)
                {
                        layout.FontDescription = TextEditorOptions.Options.Font;
@@ -789,7 +822,7 @@
                        Chunk[] chunks = mode.GetChunks (Document, 
TextEditorData.ColorStyle, line, offset, length);
 //                     int start  = offset;
                        int xStart = xPos;
-                       offset = line.Offset + line.EditableLength - offset;
+                       int index = line.Offset + line.EditableLength - offset;
                        Gdk.Color selectedTextColor = ColorStyle.SelectedFg;
                        int selectionStart = textEditorData.SelectionStart != 
null ? textEditorData.SelectionStart.Segment.Offset + 
textEditorData.SelectionStart.Column : -1;
                        int selectionEnd = textEditorData.SelectionEnd != null 
? textEditorData.SelectionEnd.Segment.Offset + 
textEditorData.SelectionEnd.Column : -1;
@@ -798,6 +831,19 @@
                                selectionEnd = selectionStart;
                                selectionStart = tmp;
                        }
+                       
+                       if (offset <= this.bracketIndex && this.bracketIndex < 
offset + length) {
+                               int bracketMarkerColumn = this.bracketIndex - 
line.Offset; 
+                               int width, height;
+                               layout.SetText (Buffer.GetTextAt (offset, 
bracketMarkerColumn).Replace ("\t", new string (' ', 
TextEditorOptions.Options.TabSize)));
+                               layout.GetPixelSize (out width, out height);
+                               Gdk.Rectangle bracketMatch = new Gdk.Rectangle 
(xStart + width, y, charWidth, LineHeight - 1);
+                               gc.RgbFgColor = 
this.ColorStyle.BracketHighlightBg;
+                               win.DrawRectangle (gc, true, bracketMatch);
+                               gc.RgbFgColor = 
this.ColorStyle.BracketHighlightRectangle;
+                               win.DrawRectangle (gc, false, bracketMatch);
+                       }
+                       
 //                             Console.WriteLine ("#" + chunks.Length);
                        foreach (Chunk chunk in chunks) {
 //                                     Console.WriteLine (chunk + " style:" + 
chunk.Style);
@@ -818,10 +864,9 @@
                                } else 
                                        DrawTextWithHighlightedWs (win, gc, 
chunk.Style.Color, ref xPos, y, Document.Buffer.GetTextAt (chunk));
                        }
-                       
                        if (line.Markers != null) {
                                foreach (TextMarker marker in line.Markers) {
-                                       marker.Draw (this, win, offset, offset 
+ length, y, xStart, xPos);
+                                       marker.Draw (this, win, index, index + 
length, y, xStart, xPos);
                                }
                        }
                }

Modified: 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextEditorOptions.cs
===================================================================
--- 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextEditorOptions.cs
      2008-02-04 17:26:19 UTC (rev 94766)
+++ 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextEditorOptions.cs
      2008-02-04 17:47:05 UTC (rev 94767)
@@ -60,6 +60,7 @@
                bool showSpaces = false;
                bool showEolMarkers = false;
                bool enableSyntaxHighlighting = true;
+               bool highlightMatchingBracket = true;
                bool highlightCaretLine = false;
                string fontName = DEFAULT_FONT;
                
@@ -69,6 +70,18 @@
                        }
                }
                
+               public virtual bool HighlightMatchingBracket {
+                       get {
+                               return highlightMatchingBracket;
+                       }
+                       set {
+                               if (value != HighlightMatchingBracket) {
+                                       highlightMatchingBracket = value;
+                                       OnChanged (EventArgs.Empty);
+                               }
+                       }
+               }
+               
                public virtual bool TabsToSpaces {
                        get {
                                return tabsToSpaces;

Added: 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextUtil.cs
===================================================================
--- 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextUtil.cs   
    2008-02-04 17:26:19 UTC (rev 94766)
+++ 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor/TextUtil.cs   
    2008-02-04 17:47:05 UTC (rev 94767)
@@ -0,0 +1,94 @@
+//
+// TextUtil.cs
+//
+// Author:
+//   Mike Krüger <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+namespace Mono.TextEditor
+{
+       public sealed class TextUtil
+       {
+               public const string openBrackets    = "([{<";
+               public const string closingBrackets = ")]}>";
+               
+               public static bool IsBracket (char ch)
+               {
+                       return (openBrackets + closingBrackets).IndexOf (ch) >= 
0;
+               }
+               
+               public static int SearchMatchingBracketForward (Document 
document, int offset, int bracket)
+               {
+                       return SearchMatchingBracket (document, offset, 
closingBrackets[bracket], openBrackets[bracket], 1);
+               }
+               
+               public static int SearchMatchingBracketBackward (Document 
document, int offset, int bracket)
+               {
+                       return SearchMatchingBracket (document, offset, 
openBrackets[bracket], closingBrackets[bracket], -1);
+               }
+               
+               static int SearchMatchingBracket (Document document, int 
offset, char openBracket, char closingBracket, int direction)
+               {
+                       bool isInString       = false;
+                       bool isInChar         = false;  
+                       bool isInBlockComment = false;
+                       int depth = -1;
+                       while (offset >= 0 && offset < document.Buffer.Length) {
+                               char ch = document.Buffer.GetCharAt (offset);
+                               switch (ch) {
+                                       case '/':
+                                               if (isInBlockComment) 
+                                                       isInBlockComment = 
document.Buffer.GetCharAt (offset + direction) != '*';
+                                               if (!isInString && !isInChar && 
offset - direction < document.Buffer.Length) 
+                                                       isInBlockComment = 
offset > 0 && document.Buffer.GetCharAt (offset - direction) == '*';
+                                               break;
+                                       case '"':
+                                               if (!isInChar && 
!isInBlockComment) 
+                                                       isInString = 
!isInString;
+                                               break;
+                                       case '\'':
+                                               if (!isInString && 
!isInBlockComment) 
+                                                       isInChar = !isInChar;
+                                               break;
+                                       default :
+                                               if (ch == closingBracket) {
+                                                       if (!(isInString || 
isInChar || isInBlockComment)) 
+                                                               --depth;
+                                               } else if (ch == openBracket) {
+                                                       if (!(isInString || 
isInChar || isInBlockComment)) {
+                                                               ++depth;
+                                                               if (depth == 0) 
+                                                                       return 
offset;
+                                                       }
+                                               }
+                                               break;
+                               }
+                               offset += direction;
+                       }
+                       return -1;                      
+               }
+       }
+}

Modified: 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.Highlighting/Style.cs
===================================================================
--- 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.Highlighting/Style.cs
     2008-02-04 17:26:19 UTC (rev 94766)
+++ 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.Highlighting/Style.cs
     2008-02-04 17:47:05 UTC (rev 94767)
@@ -43,6 +43,7 @@
                
                Color lineNumberFg, lineNumberBg, lineNumberFgHighlighted;
                Color iconBarBg, iconBarSeperator;
+               Color bracketHighlightBg, bracketHighlightRectangle;
                Color foldLine, foldLineHighlighted, foldBg, foldToggleMarker;
                List<ChunkStyle>        styles           = new 
List<Mono.TextEditor.ChunkStyle> ();
                Dictionary<string, int> styleLookupTable = new 
Dictionary<string, int> (); 
@@ -155,6 +156,18 @@
                        }
                }
 
+               public virtual Color BracketHighlightBg {
+                       get {
+                               return bracketHighlightBg;
+                       }
+               }
+
+               public virtual Color BracketHighlightRectangle {
+                       get {
+                               return bracketHighlightRectangle;
+                       }
+               }
+
                public string Name {
                        get {
                                return name;
@@ -191,6 +204,9 @@
                        whitespaceMarker  = new Gdk.Color (187, 187, 187);
                        invalidLineMarker = new Gdk.Color (210, 0, 0);
                        
+                       bracketHighlightBg        = new Gdk.Color (196, 196, 
196);
+                       bracketHighlightRectangle = new Gdk.Color (128, 128, 
128);
+                       
                        SetStyle ("comment", new Mono.TextEditor.ChunkStyle 
(new Gdk.Color (0, 0, 255), false, false));
                        SetStyle ("altcomment", new Mono.TextEditor.ChunkStyle 
(new Gdk.Color (128, 128, 128), true, false));
                        SetStyle ("todocomment", new Mono.TextEditor.ChunkStyle 
(new Gdk.Color (0, 0, 255), true, false));
@@ -271,11 +287,13 @@
                        case "foldBg":
                        case "foldLineHighlighted":
                        case "foldToggleMarker":
+                       case "bracketHighlightRectangle":
+                       case "bracketHighlightBg":
                                return false;
                        }
                        return true;
                }
-               
+                                       
                Gdk.Color GetColor (string colorString)
                {
                        if (customPalette.ContainsKey (colorString))
@@ -346,6 +364,12 @@
                        case "foldToggleMarker":
                                this.foldToggleMarker = color;
                                break;
+                       case "bracketHighlightRectangle":
+                               this.bracketHighlightRectangle = color;
+                               break;
+                       case "bracketHighlightBg":
+                               this.bracketHighlightBg = color;
+                               break;
                        default:
                                throw new Exception ("color  " + name + " 
invalid.");
                        }

Modified: 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.Highlighting/SyntaxModeService.cs
===================================================================
--- 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.Highlighting/SyntaxModeService.cs
 2008-02-04 17:26:19 UTC (rev 94766)
+++ 
trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.Highlighting/SyntaxModeService.cs
 2008-02-04 17:47:05 UTC (rev 94767)
@@ -46,7 +46,7 @@
                                List<string> result = new List<string> ();
                                foreach (string style in styles.Keys) 
                                        result.Add (style);
-                               foreach (string style in styleLookup.Values) 
+                               foreach (string style in styleLookup.Keys) 
                                        result.Add (style);
                                return result.ToArray ();
                        }

Modified: trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.mdp
===================================================================
--- trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.mdp       
2008-02-04 17:26:19 UTC (rev 94766)
+++ trunk/monodevelop/main/src/addins/Mono.Texteditor/Mono.TextEditor.mdp       
2008-02-04 17:47:05 UTC (rev 94767)
@@ -64,6 +64,9 @@
     <File name="Mono.TextEditor.Highlighting/SyntaxModeService.cs" 
subtype="Code" buildaction="Compile" />
     <File name="Mono.TextEditor.Highlighting/XmlReadHelper.cs" subtype="Code" 
buildaction="Compile" />
     <File name="AssemblyInfo.cs" subtype="Code" buildaction="Compile" />
+    <File name="gtk-gui/gui.stetic" subtype="Code" 
buildaction="EmbedAsResource" />
+    <File name="gtk-gui/generated.cs" subtype="Code" buildaction="Compile" />
+    <File name="Mono.TextEditor/TextUtil.cs" subtype="Code" 
buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
@@ -72,6 +75,7 @@
     <ProjectReference type="Gac" localcopy="False" refto="gdk-sharp, 
Version=2.10.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
     <ProjectReference type="Gac" localcopy="True" refto="pango-sharp, 
Version=2.10.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
     <ProjectReference type="Gac" localcopy="True" refto="atk-sharp, 
Version=2.10.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
+    <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
   </References>
   <GtkDesignInfo isWidgetLibrary="True" gtkVersion="2.10.2">
     <ExportedWidgets>
@@ -87,4 +91,4 @@
     <AsmRefVar Sync="True" Name="REFS" Prefix="-r:" />
     <ProjectRefVar Sync="True" Name="REFS" Prefix="-r:" />
   </MonoDevelop.Autotools.MakefileInfo>
-</Project>
+</Project>
\ No newline at end of file

Modified: trunk/monodevelop/main/src/addins/Mono.Texteditor/OblivionStyle.xml
===================================================================
--- trunk/monodevelop/main/src/addins/Mono.Texteditor/OblivionStyle.xml 
2008-02-04 17:26:19 UTC (rev 94766)
+++ trunk/monodevelop/main/src/addins/Mono.Texteditor/OblivionStyle.xml 
2008-02-04 17:47:05 UTC (rev 94767)
@@ -77,6 +77,9 @@
        <Style name="foldLineHighlighted" color="aluminium2"/>
        <Style name="foldToggleMarker" color="aluminium1"/>
        
+       <Style name="bracketHighlightRectangle" color="aluminium4"/>
+       <Style name="bracketHighlightBg" color="aluminium6"/>
+
        <!-- highlighting -->
        <Style name="comment" color="aluminium4"/>
        <Style name="altcomment" color="aluminium4"/>

Modified: trunk/monodevelop/main/src/addins/Mono.Texteditor/TangoStyle.xml
===================================================================
--- trunk/monodevelop/main/src/addins/Mono.Texteditor/TangoStyle.xml    
2008-02-04 17:26:19 UTC (rev 94766)
+++ trunk/monodevelop/main/src/addins/Mono.Texteditor/TangoStyle.xml    
2008-02-04 17:47:05 UTC (rev 94767)
@@ -77,6 +77,9 @@
        <Style name="foldLineHighlighted" color="aluminium5"/>
        <Style name="foldToggleMarker" color="black"/>
        
+       <Style name="bracketHighlightRectangle" color="orange3"/>
+       <Style name="bracketHighlightBg" color="orange1"/>
+
        <!-- highlighting -->
        <Style name="comment" color="chameleon3"/>
        <Style name="altcomment" color="chameleon3"/>

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to