ITextSelection sel = (ITextSelection)editor.getSelectionProvider().getSelection();
IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
if (editor != null && editor.isEditable()) {
SnipKeyCombos keyCombos = new SnipKeyCombos();
String sequence = "";
int cursorOffset = ((ITextSelection) sel).getOffset();
int lastSpaceOffset = -1;
// int nextSpaceOffset = -1;
FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(doc);
try {
IRegion lastSpace = finder.find(cursorOffset - 1, "[^\\*0-9a-zA-Z_-]", false, false, false, true);
if (lastSpace == null) {
lastSpaceOffset = 0;
} else {
lastSpaceOffset = lastSpace.getOffset() + 1;
}
// System.out.println("Last Space at" + lastSpaceOffset);
// System.out.println("Cursot at" + cursorOffset);
if (cursorOffset > lastSpaceOffset) {
// ok, it could be valid, but we need to check what comes
// after the cursor.
if (cursorOffset != doc.getLength()) {
//System.out.println("yep");
IRegion nextSpace = finder
.find(cursorOffset - 1, "[^\\*0-9a-zA-Z_-]", true, false, false, true);
if (nextSpace != null && nextSpace.getOffset() == cursorOffset) {
// System.out.println("Next space bit");
sequence = doc.get().substring(lastSpaceOffset, cursorOffset);
}
} else {
sequence = doc.get().substring(lastSpaceOffset, cursorOffset);
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (sequence.length() == 0) {
System.out.println("no trigger text has been passed in");
}
if (sequence.length() > 0) {
String[] stringArray = sequence.split("\\*");
String trigger = stringArray[0];
int loopcount = 1;
if (stringArray.length > 1) {
loopcount = Integer.parseInt(stringArray[1].trim());
}
// Here starts the actual triggering of a snippet using the
// trigger text
String fileName = keyCombos.getKeyCombo(trigger);
SnipReader snipReader = new SnipReader();
IFile activeFile = null;
if (editor.getEditorInput() instanceof IFileEditorInput) {
activeFile = ((IFileEditorInput) editor.getEditorInput()).getFile();
}
if(fileName == null) {
// nasty nasty hack to tell the user no snippet found
// InputDialog d = new InputDialog(editor.getSite().getShell(),
// "your title",
// "Please give me input ...",
// "Default",
// null);
// Text t = new Text(parent, SWT.BORDER); // your SWT text field
// ContentProposalAdapter adapter = new ContentProposalAdapter(
// t,
// new TextContentAdapter(),
// new JavaCompletionProcessor(),
// null,
// null);
//
// String result = d.getValue();
// ContentAssistant cast = new ContentAssistant();
// cast.setContentAssistProcessor(new JavaCompletionProcessor(), "");
// cast.showPossibleCompletions();
// MessageBox dialog = new MessageBox(shell,SWT.ICON_ERROR);
// dialog.setMessage("No snippet found for : "+sequence);
// dialog.open();
try {
SnipTreeView snipTreeView = (SnipTreeView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(SnipTreeView.ID_SNIPVIEWTREE);
snipTreeView.setSnipFilter(sequence);
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
snipReader.read(fileName);
String indentString = "";
try {
int lineNumber = doc.getLineOfOffset(lastSpaceOffset);
int lineOffset = doc.getLineOffset(lineNumber);
indentString = doc.get().substring(lineOffset, lastSpaceOffset);
} catch (Exception e) {
System.err.println(e);
// do nothing
// System.err.println("Insert snippet failed to get insert string.");
}
if (indentString.length() > 0) {
snipReader.performIndent(indentString);
}
String snippet = "";
int finalCursorOffset = -1;
for (int i = 0; i < loopcount; i++) {
start = SnipVarParser.parse(snipReader.getSnipStartBlock(), activeFile, shell);
end = SnipVarParser.parse(snipReader.getSnipEndBlock(), activeFile, shell);
if (start == null || end == null) {
snippet = null;
break;
} else {
snippet = start + end;
}
if (snippet != null && snippet.length() > 0) {
Encloser encloser = new Encloser();
encloser.enclose(doc, (ITextSelection) sel, snippet, "");
// move the cursor to before the end of the new insert
int offset = ((ITextSelection) sel).getOffset();
offset += ((ITextSelection) sel).getLength();
offset += snippet.length();
if (i == 0) {
try {
doc.replace(lastSpaceOffset, sequence.length(), "");
sel = new TextSelection(doc, offset - sequence.length(), 0);
// We only want the cursor coming back if there
// is something in the end block
if (end.length() > 0) {
finalCursorOffset = lastSpaceOffset + start.length();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
sel = new TextSelection(doc, offset, 0);
}
editor.setHighlightRange(offset, 0, true);
} else {
MessageBox dialog = new MessageBox(shell,SWT.ICON_ERROR);
dialog.setMessage("No key combo specified for : "+trigger + "in " + keyCombos.getKeyCombosFilePath());
dialog.open();
}
}
if (finalCursorOffset > 0) {