SnipMatchbeta

Search-based code repository

Snippet Creation Help




Tutorial
To practice creating snippets, you will create a snippet that inserts code to generate a random number between 0 and an Integer value specified in the search box. Follow these instructions:

1) Test the following code. It should run:
import java.util.Random;

public class test {
	public static void main(String[] args) {
		
		Random generator = new Random();
		int randomIndex = generator.nextInt(5);
		
		System.out.print(randomIndex);		
	}
}

2) To create the snippet, open the SnipMatch menu and select "Submit a Snippet". If you haven't already registered for an account, please perform that step now.

3) Once the snippet creation dialog appears, select the tab named "Input".

4) Next, click on the button "Add Pattern" and type "generate a random number between 0 and $int" in the text box, then press "OK". This is the text that is searchable and will appear in the search results. "$int" is a parameter. When a user searches for this snippet, he/she will provide an argument for this parameter before it is inserted.

5) Now click on the button "Add Parameter" and enter "$int" in the name text box and "int" in the type text box, then press "OK". Note that "$int" appears in the pattern. In this step we are specifying the type for this parameter.

6) Next, select the "Code" tab and enter the following into the text area:
${import(java.util.Random)}
Random generator = new Random();
int randomIndex = generator.nextInt(${int}+1);

7) Note that the import (java.util.Random) and the parameter in the pattern (int) are surrounded by a special markup. When someone searches for and inserts this snippet, this markup specifies that the import should be added (if it is missing) and the user-specified argument should be inserted in place of the parameter "$int". You can find more markup examples by pressing the "Insert formula" button.

8) Finally, select the "Privacy" tab and click to check the check box. After submitting a snippet, you are immediately able to search for it. Checking this box allows other SnipMatch users to see your snippet in their search results (once a moderator has approved your snippet).

Now press the "Submit" button to create your snippet! You can test it by searching SnipMatch. For example, search for "random" or "generate random".



Questions? Comments? Suggestions?
contact@snipmatch.com