Suneel Kumar

Suneel Kumar

  • NA
  • 15
  • 0

How to implement an IDE kind of editor

May 17 2006 1:30 PM
I am implementing an IDE for a programming language.
What the best way to implement IDE with formatting for the comments, keywords etc.

Currently i am using RichTextBox to display the text in IDE.
And i have a thread running in the background which will format the text.
In the thread function i am looking for the string match and changing the color.
But the problem with this is it is refreshing always.


Here is the code i implemented. (This looks for the comments and displays that in Green color)
In this program i have on richtextbox (richTextBoxIDE) and OpenFileDialog (openFileDialog1)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace NewIDE
{
public partial class Form1 : Form
{

delegate string GetTextCallback();
delegate void SelectionStartCallBack(int start);
delegate void SelectionLengthCallBack(int Length);
delegate void SelectionColorCallBack(Color c);

ThreadStart tStart;
Thread FormatThread;

public Form1()
{
InitializeComponent();
tStart = new ThreadStart(FormatIDE);
FormatThread = new Thread(tStart);
openFileDialog1.ShowDialog();

using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
string ch;
while ((ch = sr.ReadLine()) != null)
{
richTextBoxTPG.Text = richTextBoxTPG.Text + ch + "\n";
}
sr.Close();
}
}
private string GetText()
{
return richTextBoxTPG.Text;
}

private void SelectionStart(int start)
{
richTextBoxIDE.SelectionStart = start;
}

private void SelectionLength(int Length)
{
richTextBoxIDE.SelectionLength = Length;
}

private void SelectionColor(Color c)
{
richTextBoxIDE.SelectionColor = c;
}

private void FormatIDE()
{
CharEnumerator ce;
string temp;

GetTextCallback d = new GetTextCallback(GetText);
SelectionColorCallBack sc = new SelectionColorCallBack(SelectionColor);
SelectionLengthCallBack sl = new SelectionLengthCallBack(SelectionLength);
SelectionStartCallBack ss = new SelectionStartCallBack(SelectionStart);
if (this.richTextBoxIDE.InvokeRequired)
{
temp = richTextBoxIDE.Invoke(d).ToString();
ce = temp.GetEnumerator();
}
else
{
ce = richTextBoxIDE.Text.GetEnumerator();
temp = richTextBoxIDE.Text;
}
char eachChar;
int count = 0;
int commentCount = 0, commentStart = 0;
while (count<temp.Length)
{
eachChar = Convert.ToChar(temp[count]);
count++;
if (count<temp.Length)
{
if ((eachChar == '/') && (Convert.ToChar(temp[count]) == '*'))
{
if (commentCount == 0)
commentStart = count - 1;
commentCount++;
}
if ((eachChar == '*') && (Convert.ToChar(temp[count]) == '/'))
{
commentCount--;
if (commentCount < 0)
{
//Inform the user with mismatched comment.
commentCount = 0;
}
if (commentCount == 0)
{
if (this.richTextBoxIDE.InvokeRequired)
{
richTextBoxIDE.Invoke(ss, commentStart);
richTextBoxTPG.Invoke(sl, count - commentStart + 1);
richTextBoxTPG.Invoke(sc, Color.Green);
}
else
{
richTextBoxIDE.SelectionStart = commentStart;
richTextBoxIDE.SelectionLength = count - commentStart + 1;
richTextBoxIDE.SelectionColor = Color.Green;
}
}
}
}
 
}
if (this.richTextBoxIDE.InvokeRequired)
{
richTextBoxIDE.Invoke(ss, temp.Length);
}
else
{
richTextBoxIDE.SelectionStart = temp.Length;
}
}

private void richTextBoxTPG_KeyUp(object sender, KeyEventArgs e)
{
if (!FormatThread.IsAlive && e.KeyCode != Keys.Up && e.KeyCode != Keys.Down && e.KeyCode != Keys.Right && e.KeyCode != Keys.Left)
{
FormatThread.Start();
FormatThread = new Thread(tStart);
}
}

}

}