Snippet Manager

v1.8 now available!

  • (11 Pages)
  • +
  • « First
  • 5
  • 6
  • 7
  • 8
  • 9
  • Last »

157 Replies - 29802 Views - Last Post: 02 March 2009 - 11:13 AM

#86 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Snippet Manager

Posted 31 August 2008 - 07:40 AM

For the record here's how Im doing my syntax highlighting (I feel a tutorial coming from this part alone. It consist of two class files
  • SyntaxHighlighter.cs
  • Syntax.cs

SyntaxHighlighter.cs calls Syntax.cs, so here they are

SyntaxHighlighter.cs
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;

namespace RLM.SnippetManager
{
    public class SyntaxHighlighter
    {
        public bool populating = true;
        private Syntax syntaxReader;
        private Color commentColor = Color.Green;
        private string _syntaxFile;

        public SyntaxHighlighter()
        {
            syntaxReader = new Syntax();
            syntaxReader.SyntaxFile = "SyntaxFiles/C#.syntax";
            syntaxReader.OpenSyntaxFile();
        }

        public SyntaxHighlighter(string file)
        {
            _syntaxFile = file;
            syntaxReader = new Syntax();
            syntaxReader.SyntaxFile = _syntaxFile;
            syntaxReader.OpenSyntaxFile();
        }

        public void OpenSyntaxFile()
        {
            syntaxReader.SyntaxFile = _syntaxFile;
            syntaxReader.OpenSyntaxFile();
        }

        public string SyntaxFile
        {
            get { return _syntaxFile; }
            set { _syntaxFile = value; }
        }

        public void ColorCurrentLine(RichTextBox rtb)
        {
            int selStart = rtb.Selectionstart;
            int selLength = rtb.SelectionLength;

            // find start of line
            int pos = selStart;
            while ((pos > 0) && (rtb.Text[pos - 1] != '\n')) pos--;

            int pos2 = selStart;

            while ((pos2 < rtb.Text.Length) && (rtb.Text[pos2] != '\n')) pos2++;

            string str = rtb.Text.Substring(pos, pos2 - pos);

            if (FindComment(str) == true)
            {
                rtb.Select(pos, pos2 - pos);
                rtb.SelectionColor = commentColor;
            }
            else
            {
                string previousWord = "";
                int count = ParseLine(str);
                for (int i = 0; i < count; i++)
                {
                    WordPosition wp = buffer[*];

                    // check for comment
                    if (wp.word == "/" && previousWord == "/")
                    {
                        // color until end of line
                        int commentStart = wp.pos - 1;
                        int commentEnd = pos2;
                        while (wp.word != "\n" && i < count)
                        {
                            wp = buffer[*];
                            i++;
                        }

                        i--;

                        commentEnd = pos2;
                        rtb.Select(commentStart + pos, commentEnd - (commentStart + pos));
                        rtb.SelectionColor = commentColor;

                    }
                    else
                    {

                        Color color = DetermineColor(wp.word);
                        rtb.Select(wp.pos + pos, wp.len);
                        rtb.SelectionColor = color;
                      
                    }

                    previousWord = wp.word;

                }
            }

            if (selStart >= 0)
                rtb.Select(selStart, selLength);


        }

        public void ColorAllText(string s, RichTextBox rtb)
        {
            populating = true;

            int selStart = rtb.Selectionstart;
            int selLength = rtb.SelectionLength;

            int count = ParseLine(s);
            string previousWord = "";
            for (int i = 0; i < count; i++)
            {
                WordPosition wp = buffer[*];

                // check for comment
                if (wp.word == "/" && previousWord == "/")
                {
                    // color until end of line
                    int posCommentStart = wp.pos - 1;
                    int posCommentEnd = i;
                    while (wp.word != "\n" && i < count)
                    {







                        wp = buffer[*];
                        i++;
                    }

                    i--;

                    posCommentEnd = wp.pos;
                    rtb.Select(posCommentStart, posCommentEnd - posCommentStart);
                    rtb.SelectionColor = commentColor;

                }
                else
                {

                    Color c = DetermineColor(wp.word);
                    rtb.Select(wp.pos, wp.len);
                    rtb.SelectionColor = c;
                }

                previousWord = wp.word;

                //				Console.WriteLine(wp.ToString());
            }

            if (selStart >= 0)
                rtb.Select(selStart, selLength);

            populating = false;

        }

        private struct WordPosition
        {
            public string word;
            public int pos;
            public int len;

            public override string ToString()
            {
                string str = "Word = " + word + ", Position = " + pos + ", Length = " + len + "\n";
                return str;
            }
        }

        private WordPosition[] buffer = new WordPosition[4000];

        public bool FindComment(string s)
        {
            string testString = s.Trim();
            if ((testString.Length >= 2) && (testString[0] == '/') && (testString[1] == '/'))
                return true;
            return false;
        }

        public int ParseLine(string s)
        {
            buffer.Initialize();
            int count = 0;
            Regex reg = new Regex(@"\w+|[^A-Za-z0-9_ \f\t\v]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            Match match;

            for (match = reg.Match(s); match.Success; match = match.NextMatch())
            {
                buffer[count].word = match.Value;
                buffer[count].pos = match.Index;
                buffer[count].len = match.Length;
                count++;
            }


            return count;
        }

        public Color DetermineColor(string s)
        {
            Color theColor = Color.Black;

            if (syntaxReader.IsOperator(s))
            {
                theColor = Color.DarkRed;
            }

            if (syntaxReader.IsFunction(s))
            {
                theColor = Color.Blue;
            }

            if (syntaxReader.IsKeyword(s))
            {
                theColor = Color.Blue;
            }

            if (syntaxReader.IsDirective(s))
            {
                theColor = Color.Gray;
            }

            if (syntaxReader.IsDataTypes(s))
            {
                theColor = Color.Navy;
            }
            return theColor;
        }
    }
}





Syntax.cs
using System;
using System.Collections;
using System.IO;

namespace RLM.SnippetManager
{
    class Syntax
    {
        private string _syntaxFile;
        private ArrayList _functions = new ArrayList();
        private ArrayList _keywords = new ArrayList();
        private ArrayList _directives = new ArrayList();
        private ArrayList _dataTypes = new ArrayList();
        private ArrayList _operators = new ArrayList();
        private ArrayList _comments = new ArrayList();

        public string SyntaxFile
        {
            get { return _syntaxFile; }
            set { _syntaxFile = value; }
        }

        public Syntax()
		{
			//
			// TODO: Add constructor logic here
			//
            
		}

        public void OpenSyntaxFile()
        {
            FileStream stream = new FileStream(_syntaxFile, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(stream);
            _syntaxFile = reader.ReadToEnd();
            reader.Close();
            stream.Close();
            Fill();
        }

		public void Fill()
		{
            StringReader reader = new StringReader(_syntaxFile);

			string next = reader.ReadLine();
			next = next.Trim();

			//find the keywords header
			while (next != null)
			{
				if (next == "[KEYWORDS]")
				{
					//read all keywords into our ArrayList
					next = reader.ReadLine();
					if (next != null)
						next = next.Trim();
					while (next != null && next[0] != '[')
					{
						_keywords.Add(next);
						next = "";
						while (next != null && next == "")
						{
							next = reader.ReadLine();
							if (next != null)
								next = next.Trim();
						}
					}
				}

                //find the functions header
                if (next == "[FUNCTIONS]")
                {
                    //read all directives into our ArrayList
                    next = reader.ReadLine();
                    if (next != null)
                        next = next.Trim();
                    while (next != null && next[0] != '[')
                    {
                        _functions.Add(next);
                        next = "";
                        while (next != null && next == "")
                        {
                            next = reader.ReadLine();
                            if (next != null)
                                next = next.Trim();
                        }

                    }
                }

                //find the directives header
                if (next == "[DIRECTIVES]")
				{
					//read all directives into our ArrayList
					next = reader.ReadLine();
					if (next != null)
						next = next.Trim();
					while (next != null && next[0] != '[')
					{
						_directives.Add(next);
						next = "";
						while (next != null && next == "")
						{
							next = reader.ReadLine();
							if (next != null)
							next = next.Trim();
						}
						
					}
				}

                //find the operators header
                if (next == "[OPERATORS]")
				{
					//read all operators into our ArrayList
					next = reader.ReadLine();
					if (next != null)
						next = next.Trim();
					while (next != null && next[0] != '[')
					{
						_operators.Add(next);

						next = "";
						while (next != null && next == "")
						{
							next = reader.ReadLine();
							if (next != null)
								next = next.Trim();
						}
						
					}
				}

                //find the data types header
                if (next == "[DATATYPES]")
                {
                    //read all comments into our ArrayList
                    next = reader.ReadLine();
                    if (next != null)
                        next = next.Trim();
                    while (next != null && next[0] != '[')
                    {
                        _dataTypes.Add(next);
                        next = "";
                        while (next != null && next == "")
                        {
                            next = reader.ReadLine();
                            if (next != null) 
                                next = next.Trim();
                        }

                    }
                }

                //find the comments header
                if (next == "[COMMENTS]")
                {
                    //read all comments into our ArrayList
                    next = reader.ReadLine();
                    if (next != null)
                        next = next.Trim();
                    while (next != null && next[0] != '[')
                    {
                        _comments.Add(next);
                        next = "";
                        while (next != null && next == "")
                        {
                            next = reader.ReadLine();
                            if (next != null) 
                                next = next.Trim();
                        }

                    }
                }

				if (next != null && next.Length > 0 && next[0] == '[')
				{
				}
				else
				{
					next = reader.ReadLine();
					if (next != null) 
                        next = next.Trim();
				}
			}

            //sort our ArrayList's
            _functions.Sort();
			_keywords.Sort();
			_directives.Sort();
            _dataTypes.Sort();
			_operators.Sort();
            _comments.Sort();	
		}

		public bool IsKeyword(string s)
		{
			int index = _keywords.BinarySearch(s);
			if (index >= 0) return true;

			return false;



		}


        public bool IsFunction(string s)
        {
            int index = _functions.BinarySearch(s);
            if (index >= 0) return true;

            return false;
        }

		public bool IsDirective(string s)
		{
			int index = _directives.BinarySearch(s);
			if (index >= 0) return true;

			return false;
		}

        public bool IsOperator(string s)
        {
            int index = _operators.BinarySearch(s);
            if (index >= 0) return true;

            return false;
        }

        public bool IsDataTypes(string s)
        {
            int index = _dataTypes.BinarySearch(s);
            if (index >= 0) return true;

            return false;
        }

		public bool IsComment(string s)
		{
			int index = _comments.BinarySearch(s);
			if (index >= 0)
				return true;

			return false;
		}

    }
}



To prevent the RichTextBox from flickering when the syntax is highlighted (as you type) I eat the Pain message in my custom RichTextBox control with this

const short  WM_PAINT = 0x00f;
public static bool doPaint = true;

protected override void WndProc(ref System.Windows.Forms.Message m) 
{
	//eat the Paint message to prevent seeing the flickering
    //when we select the text to set the color

	if (m.Msg == WM_PAINT) 
	{
		if (_Paint)
			base.WndProc(ref m);
		else
			m.Result = IntPtr.Zero;
	}
    else
		base.WndProc (ref m);
}



Ill post more teasers as I go along

EDIT: Where you see buffer[*] is supposed to be buffer[i], but it was messing with the formatting of the post
Was This Post Helpful? 0
  • +
  • -

#87 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Snippet Manager

Posted 31 August 2008 - 09:01 AM

awww now I'm the only one who hasn't written a super cool syntax highlighter, and it was my project :(

Oh well~ I'll start working on it properly tonight.
Was This Post Helpful? 0
  • +
  • -

#88 jacobjordan   User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Snippet Manager

Posted 31 August 2008 - 05:39 PM

Nice. I must say, i am making a bunch of great code, and when i remember about it later when i actually need it, i completely forget where i put it, so something like this would be great. However, yours is a bit limited for what i want, so i got started on making my own. I am making mine so it can support unlimited levels of categories, and all the snippets and category information are stored in one main database file, not over a folder tree like yours. Every snippet contains data about it's author, summary, and even a star rating.
Was This Post Helpful? 0
  • +
  • -

#89 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Snippet Manager

Posted 31 August 2008 - 08:52 PM

The information and ratings would be a great idea. gabehabe you should add that as well.

EDIT:
You should associate .snippet files to your program.

This post has been edited by gbertoli3: 01 September 2008 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#90 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Snippet Manager

Posted 01 September 2008 - 10:54 AM

Yeah, I ain't keeping it as *.snippet

It's just for now~ until I get it sorted with different types (customisable)

I've come back to this thread now because...

I finally got some good syntax highlighting of my own!

There are a lot less bugs than my original, and it's all contained in a class called SyntaxBox

And the class itself is only about 50 lines, compared to those big lengthy ones~ not bad, huh? ;)

EDIT:
Oh, it's 100~ it's still got some work, it'll probably be between 150-250 lines by the time it's done. :)
Was This Post Helpful? 0
  • +
  • -

#91 abgorn   User is offline

  • sudo apt install brain -y
  • member icon

Reputation: 31
  • View blog
  • Posts: 1,423
  • Joined: 05-June 08

Re: Snippet Manager

Posted 01 September 2008 - 11:31 AM

Wow! Last time I looked at this thread you had just finished highlighting, but now look at it! It's kick ass! Hats off to you Gabehabe. ^_^
Was This Post Helpful? 0
  • +
  • -

#92 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Snippet Manager

Posted 01 September 2008 - 11:40 AM

So what file type will they be saved as?
Was This Post Helpful? 0
  • +
  • -

#93 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Snippet Manager

Posted 01 September 2008 - 11:55 AM

The actual language's filetypes~ cpp, c, h, cs, etc~

Now, I'm gonna need some help from people. If you know a language, other than C/C++ do you want to write a syntax file? I'll give credit to you :)

And, here's a screen shot of the syntax box:
http://www.dreamincode.net/forums/index.php?act=Attach&type=post&id=8155

There's still a way to go, it needs to handle comments. BUT, that's where I'm asking for help, too. Add the comment types to the file:
/* block
comment */
// line comment

And I'll sort it from there.

Any takers? Let me know, and I'll explain the format of the files better (I'm not working with XML, it's a really simple layout)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#94 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Snippet Manager

Posted 01 September 2008 - 01:30 PM

Finally advanced it a little more.

Updates:
-Reduced even more flashing.
-Enabled single line comments
-Enabled block comments

http://www.dreamincode.net/forums/index.php?act=Attach&type=post&id=8156

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#95 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Snippet Manager

Posted 01 September 2008 - 02:28 PM

Last showcase of the night.

Updates:
-Added support for quotes
-Added support for preprocessors (# is a prefix for comments in other languages, so it can be portable)
-Added support for nested quotes

http://www.dreamincode.net/forums/index.php?act=Attach&type=post&id=8157

Thoughts/comments?

Here's the demo: Attached File  Demo.zip (4.63K)
Number of downloads: 33
It's got pretty much full C++ highlighting, for the most parts. :)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#96 jacobjordan   User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Snippet Manager

Posted 02 September 2008 - 02:19 PM

I got a lot of time on my hands. I'd be happy to write a VB or VB.NET syntax file. Also, i downloaded a programming notepad off the web a few weeks back that has syntax files for about 20 languages. Using those, i could probably make quite a few fully accurate syntax files.

This post has been edited by jacobjordan: 02 September 2008 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#97 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Snippet Manager

Posted 03 September 2008 - 03:10 AM

Thanks :^:

I'm gonna get a new format sorted for it first (most likely in XML) so I'll sort that and explain it as soon as it's done.

:)

I didn't get anything done on this yesterday, I was just playing around in Google Chrome all night ^_^
Tonight, I'm gonna try and finish the syntax box fully, sort the format, and add it to the snippet manager.
Was This Post Helpful? 0
  • +
  • -

#98 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Snippet Manager

Posted 03 September 2008 - 06:20 AM

Yeah I was playing with Chrome too.
Was This Post Helpful? 0
  • +
  • -

#99 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Snippet Manager

Posted 03 September 2008 - 11:39 AM

Well here are some more screens of the application. Ive implemented Find & Replace, custom icons for each language, context menu's, move a snippet to a different language, etc:


Attached Image Attached Image

Attached Image Attached Image

Attached Image
Was This Post Helpful? 0
  • +
  • -

#100 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Snippet Manager

Posted 03 September 2008 - 12:19 PM

That's brilliant!

<n00b moment>
How did you get the right click menus with that little blue strip?
</n00b moment>


*feels less accomplished now, after comparing to Psycho's version* :(
Was This Post Helpful? 0
  • +
  • -

  • (11 Pages)
  • +
  • « First
  • 5
  • 6
  • 7
  • 8
  • 9
  • Last »