[Gtk-sharp-list] need help with TextBuffers and TextIters

Patrik Olterman patrik@olterman.se
Fri, 21 Nov 2003 19:23:25 +0100


Hello all 

I have for a while been working on a low end C# editor in GTK# 
I am struggling to make syntax highlighting to work due to the 
lack of documentation on TextIters and TextBuffers. The full code can be
found in a subversion repository on 
http://www.olterman.se:8080/repos/mono/ezsharp/ezsharp.cs

heres what I have so far as to syntax highlighting as you can see its
far from perfect, right now it highlights oneline comments and strings
if there is only one string on a line. Hopefully this will give us a
working little editor and some documentation on buffers and iters.

Please help 
----------------------------------------------------------------------
  class Hilite 
  {
	public TextTagTable tt;
        public TextTag string_tag, comment_tag;
	public bool commentBit;

	public Hilite()
	{
	tt = new TextTagTable();
	// text tags for syntax highlighting
	// strings
       string_tag = new TextTag(null);
	string_tag.Foreground = "blue";
      tt.Add(string_tag);
	// coments
       comment_tag = new TextTag(null);
	comment_tag.Foreground = "darkgreen";
      tt.Add(comment_tag);
	}

	public void hilite_all()
	{
		
		//Console.WriteLine("Hilite event");
		TextIter begin = new TextIter();
		begin = App.editor.buf.StartIter;
		TextIter stop = new TextIter();
		stop = App.editor.buf.StartIter;
		stop.ForwardLine();
		//Console.WriteLine(buf.GetText(begin, stop, false));
		for (int i = 1; i < App.editor.buf.LineCount; i++)
			{
			    // get the txt 
			     //Console.Write(buf.GetText(begin, stop, false));
			     string txt = App.editor.buf.GetText(begin, stop, true);
			    //
				if(txt != "") {

					// checking for strings
					Regex reg_string = new Regex("(\".*\")");
					Match mat_string = reg_string.Match(txt);
					if(mat_string.Success==true) {
						char fnutt= '"';
						TextIter begin_string = begin.Copy();
						TextIter stop_string = stop.Copy();
						while( true==true ) {
							begin_string.ForwardChar();
							string control = App.editor.buf.GetText(begin_string,
stop_string, true);
							if ( control[0] == fnutt ) {
								break;		
							}
						}
						while( true==true ) {
							stop_string.BackwardChar();
							string control = App.editor.buf.GetText(begin_string,
stop_string, true);
							int end = control.Length-1;
							if ( control[end] == fnutt ) {
								break;		
							}
						} 

						//applying string tag
						App.editor.buf.ApplyTag (string_tag, begin_string, stop_string);
					}	
					// checking for comments
					Regex reg_comment = new Regex("(\\s)*(//)"); 
					Match mat_comment = reg_comment.Match(txt);
					if(mat_comment.Success==true) {
						string slash = "//";
						TextIter begin_comment = begin.Copy();
						while( true==true ) {
							
							TextIter control_it = begin_comment.Copy();
							control_it.ForwardChar();
							control_it.ForwardChar();
							string control = App.editor.buf.GetText(begin_comment,
control_it, true);
							if ( control == slash ) {
								break;		
							}
						begin_comment.ForwardChar();
						}
						//applying comment tag
						App.editor.buf.ApplyTag (comment_tag, begin_comment, stop);
					}								
				}
			    // move iters forward one line 
				begin.ForwardLine();	
				stop.ForwardLine();
			}
		
		/*
		TextIter it_a = new TextIter();
		it_a = App.editor.buf.StartIter;
		TextIter it_b = new TextIter();
		it_b = App.editor.buf.StartIter;
		it_b.ForwardChar();
		TextIter it_end = new TextIter();
		it_end = App.editor.buf.EndIter;
		
		// looping through each char of the code
		while (it_b != it_end)
		{
		//Console.WriteLine(App.editor.buf.GetText(it_a, it_b, true));
		
		
		// moving iters forward
		it_a.ForwardChar();
		it_b.ForwardChar();
		}
		*/
	}
	
	public void hilite_last_word()
	{
	
	}

	public void hilite_remove()
	{
		TextIter first = new TextIter();
		first = App.editor.buf.StartIter;
		TextIter last = new TextIter();
		last = App.editor.buf.EndIter;

		App.editor.buf.RemoveAllTags(first, last);
	}
  }