Snippet Manager

v1.8 now available!

  • (11 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • Last »

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

#41 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 27 August 2008 - 09:40 AM

Well I got quite a bit done last night gabehabe, but having a problem updating my TreeView from a separate form after adding a category. It adds the category to my XML file (I even search the XML file to make sure the category doesn't already exist) but no matter what I do the TreeView wont update until I close the form and reopen it.

Any ideas? Here's the code I use for adding the category to the XML file


private static bool AddNewNode(ref string category, ref string tag, ref string name)
{
    try
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(name);
        XmlElement newCat = xml.CreateElement("Category");

        //add language value
        XmlElement language = xml.CreateElement("Language");
        XmlText catText = xml.CreateTextNode(category);
        language.AppendChild(catText);
        newCat.AppendChild(language);
        xml.DocumentElement.AppendChild(newCat);

        //add tag value
        XmlElement tagValue = xml.CreateElement("Tag");
        XmlText catTag = xml.CreateTextNode(tag);
        tagValue.AppendChild(catTag);
        newCat.AppendChild(tagValue);
        xml.DocumentElement.AppendChild(newCat);

        //insert after last node
        xml.DocumentElement.InsertAfter(newCat, xml.DocumentElement.LastChild);

        //save the document
        xml.Save(name);

        return true;
    }
    catch (Exception ex)
    {
        _returnMessage = ex.Message;
        return false;
    }
}



Here's the code I use for populating my TreeView
public static void LoadCategoriesIntoTreeView(ref string file, TreeView tv)
{
    string name = AppPath + file;

    DataSet languages = new DataSet();
    if (!(DoesFileExist(ref name)))
    {
        tv.Nodes.Add("No categories available");
    }
    else
    {
        languages.ReadXml(file);

        XmlDataDocument doc = new XmlDataDocument(languages);
        string query = "/Categories/Category";

        string display = "";

        foreach (XmlNode node in doc.SelectNodes(query))
        {
            display = node.ChildNodes[0].InnerText.ToString();
            tv.Nodes.Add(display);
           
        }
    }
}



Then in my AddCategory form I do this


private void cmdAdd_Click(object sender, EventArgs e)
{
    
    string file = "Categories.xml";
    string name = cboCategories.Text;
    string tag = cboCategories.SelectedValue.ToString();
    if (!(CategoriesManager.AddNewCategory(ref name, ref tag, ref file)))
    {
        MessageBox.Show(CategoriesManager.ReturnMessage);
    }
    else
    {
        Form1 frm = new Form1();
        frm.tvLanguages.Nodes.Clear();
        CategoriesManager.LoadCategoriesIntoTreeView(ref file, frm.tvLanguages);
        this.Hide();
    }
}



But it's not updating the TreeView. Ill post when I come up with a solution
Was This Post Helpful? 0
  • +
  • -

#42 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




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

Re: Snippet Manager

Posted 27 August 2008 - 10:54 AM

After you clear it, does it repopulate it with the original stuff, or is it just completely empty after your call to the LoadCategoriesIntoTreeView() function?


As for me, I put a split container in, and it fucked it all up. So now, I've copied the code, and I'm remaking it in design view of a new project.

Pretty pissed off, to be honest...
Was This Post Helpful? 0
  • +
  • -

#43 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 27 August 2008 - 11:00 AM

It doesn't even clear the nodes, which is truly the strange part. Im getting frustrated, Ive never ran into this when trying to manipulate a control on a different form :crazy:

EDIT: I also have mine in a SplitContainer, wonder if that has something to do with it
Was This Post Helpful? 0
  • +
  • -

#44 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




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

Re: Snippet Manager

Posted 27 August 2008 - 11:14 AM

DAMN SPLIT CONTAINERS!

I dunno, I can't believe I'm even attempting to help you :unsure:

If you want to access your tree view inside a split container, would it be something like:
panel1.treeView1.Clear();
Where basically your tree view is inside the scope of that panel?
Was This Post Helpful? 0
  • +
  • -

#45 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 27 August 2008 - 11:25 AM

The only way to access a control in a SplitContainer on a different form

Form1 frm = new Form1()
frm.splitContainer1.Controls[*put index here]



But even using the Refresh() from there isnt doing anything. I'm gonna be so pissed if I have to get rid of this SplitContainer for this to work :crazy:
Was This Post Helpful? 0
  • +
  • -

#46 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




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

Re: Snippet Manager

Posted 27 August 2008 - 12:03 PM

Well good luck getting rid of it~ My project wouldn't allow it. I had to start over.

And now it can't seem to find the resources for the images for the buttons (default: new, open, etc)

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#47 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




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

Re: Snippet Manager

Posted 27 August 2008 - 12:52 PM

Never mind, I finally fixed it.
Was This Post Helpful? 0
  • +
  • -

#48 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Snippet Manager

Posted 27 August 2008 - 12:53 PM

The idea of a syntax highligher was too fascinating for me to pass up. Particularly with the idea of supporting many syntax schemas described in data driven files. And in one of my favorite languages.

I ended up coming up with a reasonably flexible solution that you may find interesting. I did quite know where to put it, so it landed here.

Here's a base class from the project:
public abstract class RuleBase {
	protected Color color;
	protected string pattern;
	protected bool ignoreCase;
	protected bool multiLine;

	public RuleBase(string pattern, bool ignoreCase, bool multiLine, System.Drawing.Color color) {
		this.pattern = pattern;
		this.color = color;
		this.ignoreCase = ignoreCase;
		this.multiLine = multiLine;
	}

	public abstract FormatRange Match(string text);

	public virtual string Pattern { get { return this.pattern; } }
	public virtual System.Drawing.Color Color { get { return this.color; } }
	public virtual bool IgnoreCase { get { return this.ignoreCase; } }
	public virtual bool Multiline { get { return this.multiLine; } }
}



Here's what one of the concrete classes looks like ( there are only two, but could be many more):
public class RuleRegex : RuleBase {
	protected Regex re;

	public RuleRegex(string pattern, bool ignoreCase, bool multiLine, System.Drawing.Color color)
		: base(pattern, ignoreCase, multiLine, color) 
	{
		RegexOptions opts = RegexOptions.None;
		if (ignoreCase) { opts = opts | RegexOptions.IgnoreCase; }
		if (multiLine) { opts = opts | RegexOptions.Multiline | RegexOptions.Singleline; }
		this.re = new Regex(pattern, opts);
	}

	public override FormatRange Match(string text) {
		Match match = re.Match(text);
		if (!match.Success) { return FormatRange.Empty; }
		return new FormatRange(match.Index, match.Length);
	}
}



A syntax files looks like this:
<HighlightRules>
	<Default Color="Black" />
	<RuleType TypeId="0" Type="Baavgai.Highlight.RuleKeyword" Color="Blue" IgnoreCase="False" Multiline="False" />
	<RuleType TypeId="1" Type="Baavgai.Highlight.RuleRegex" Color="Green" IgnoreCase="True" Multiline="True" />
	<RuleType TypeId="2" Type="Baavgai.Highlight.RuleRegex" Color="Green" IgnoreCase="True" Multiline="False" />
	<RuleType TypeId="3" Type="Baavgai.Highlight.RuleRegex" Color="Red" IgnoreCase="True" Multiline="False" />
	<Rule TypeId="1" Pattern="/\*.*\*/" />
	<Rule TypeId="2" Pattern="//.*" />
	<Rule TypeId="3" Pattern='"[^"]*"'/>
	<Rule Pattern="abstract" />
	<Rule Pattern="event" />
	<Rule Pattern="new" />
	<!-- you get the idea, it's big -->
</HighlightRules>




The text starts out as a single block. The formatting rules are applied in order. When a rule has a match the block it matches on is removed from the text scanned. This is based on the idea that rules have precedence and once one is applied future ones can't be reapplied. The result is a list of format spans. All this is done without any knowlege of GUI.

The application of the result looks like this:
public class RulesCollection : List<RuleBase> {
	protected Color defaultColor = Color.Black;

	public RulesCollection() { }

	public Color DefaultColor { 
		get { return this.defaultColor; } 
		set { this.defaultColor = value; } 
	}

	public void ApplyRules(RichTextBox rtbSource) {
		int selectionstartSave = rtbSource.Selectionstart;
		int selectionLengthSave = rtbSource.SelectionLength;

		RichTextBox rtb = new RichTextBox();
		rtb.Rtf = rtbSource.Rtf;

		HighLightMap map = new HighLightMap(rtb.Text, defaultColor, this);
		foreach (MapItem item in map) {
			rtb.Selectionstart = item.Start;
			rtb.SelectionLength = item.Len;
			rtb.SelectionColor = item.Color;
		}

		rtbSource.Rtf = rtb.Rtf;
		rtbSource.Selectionstart = selectionstartSave;
		rtbSource.SelectionLength = selectionLengthSave;
	}

}



After all was done, I wasn't sure what to do with this, but I wanted to share. Maybe it will help you project in some way.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#49 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 27 August 2008 - 01:02 PM

@gabehabe: I copied all my controls to a new form (which was deleted once I was done with it. I then deleted all GUI controls (except the SplitContainer). I then went to the Properties window and selected the SplitContainer from the DropDownList at the top. Once it was highlighted I clicked the small arrow on the upper left hand corner and un-docked it. It wasn't until I did all this that I could delete it.

But alas, even removing the SplitContainer repopulating the TreeView with the new categories isn't working. I don't think Ive ever ran into this before and it's getting rather frustrating :blink:

Nice code baavgai! I like the idea. I'm working on having it highlight as you type a new snippet, Ill post what I've come up with once I have it working.
Was This Post Helpful? 0
  • +
  • -

#50 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Snippet Manager

Posted 27 August 2008 - 01:15 PM

View PostPsychoCoder, on 27 Aug, 2008 - 04:02 PM, said:

I'm working on having it highlight as you type a new snippet, Ill post what I've come up with once I have it working.


The example does that. It's not blazingly fast, but much better than the first draft. :P

Speed is why the heavy lifting was removed from the GUI. Ultimately, the speed bottleneck is the refresh of the control. Ideally, the code would compare only changes and only apply changes. Also, not worry about anything until the user types white space or navigates somewhere. Unfortunately, the control is limited in the amount of inspection it will allow.

Note, this codes actually does a kind of double buffering. It copies the rtf to an off form control, changes that, then pushes the rtf back. This alone helped speed the thing up a lot.
Was This Post Helpful? 0
  • +
  • -

#51 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




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

Re: Snippet Manager

Posted 27 August 2008 - 01:42 PM

Show off. It works a treat, much better than mine did/does~

I don't want to use it, since I'm aiming to do this all by myself, but I'll definitely read it in great detail before making my own.

Thanks for the example, baavgai :^:
Was This Post Helpful? 0
  • +
  • -

#52 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




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

Re: Snippet Manager

Posted 27 August 2008 - 02:11 PM

@Psycho:
I'm not sure if this will help, but it might give you an idea. This is my updateTreeView() function:
		void updateTreeView ()
		{
			treeView1.Nodes.Clear();
			// get all folders within the current directory, to add to the tree menu
			System.IO.DirectoryInfo dir = new DirectoryInfo(System.Environment.CurrentDirectory);

			foreach (DirectoryInfo g in dir.GetDirectories())
			{ // load the parent nodes, aka "languages"
				TreeNode objNode = treeView1.Nodes.Add(g.Name);
				foreach (FileInfo h in g.GetFiles())
					// load the child nodes, aka "snippets"
					objNode.Nodes.Add (h.Name);
			}
			
			int count = 0;
			bool readmeExists = false;
			TreeNode unsortedNode = treeView1.Nodes.Add ("Unsorted"); // the parent node
			foreach (FileInfo g in dir.GetFiles())
			{
				if (g.Name != "readme.snippet")
				{
					unsortedNode.Nodes.Add (g.Name);
					count++;
				} else readmeExists = true;
			}
			if (count == 0)
				treeView1.Nodes.Remove(unsortedNode);
			if (readmeExists == true)
				treeView1.Nodes.Add("readme.snippet");
			if (readmeExists == false)
			{ // the sneaky bit. recreate the readme if it doesn't already exist!
				initialiseReadme();
				StreamWriter SW;
				SW = File.CreateText (@System.Environment.CurrentDirectory + @"\\readme.snippet");
				SW.Write (@readme);
				SW.Close();
				treeView1.Nodes.Add ("readme.snippet");
			}
		}

It has a big big big exemption for the readme, so that it doesn't go into a folder, and another one to put the files in the root inside a folder called "Unsorted" to keep it all a little more organised.

Hope it helps :^:
Was This Post Helpful? 0
  • +
  • -

#53 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 27 August 2008 - 02:17 PM

That is being called from the form the TreeView is in correct? The problem I'm having is updating the TreeView from a form other than the form the TreeView is on
Was This Post Helpful? 0
  • +
  • -

#54 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




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

Re: Snippet Manager

Posted 27 August 2008 - 02:47 PM

Well if I wanted to call that from another form, all I'd do is MainForm.updateTreeView();

You might have to use a this pointer, but you get the idea~

Basically, my method will clear the whole thing, then add them back in. It may not be the quickest way, but it's fast enough to work effectively :)
Was This Post Helpful? 0
  • +
  • -

#55 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




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

Re: Snippet Manager

Posted 27 August 2008 - 03:26 PM

I've added the automatic clipboard contents list beneath the tree view.

Also, I've added a "Last Modified" in the status bar.

I won't upload it yet, since I'm still testing it, but wait a sec and I'll upload a screenshot~

EDIT:
As promised, here's a screenshot:
http://www.dreamincode.net/forums/index.php?act=Attach&type=post&id=8130

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

  • (11 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • Last »