Tuesday, 15 September 2015

How to make eclipse content assist insert instead of overwrite -



How to make eclipse content assist insert instead of overwrite -

i writing plugin eclipse , trying implement content assist. code below works exception overwrites existing text , want insert instead.

any help gratefully received.

the contentassistantprovider:

class="lang-java prettyprint-override">package astra.ide.editor.astra; import java.util.arraylist; import java.util.hashset; import java.util.list; import java.util.set; import org.eclipse.core.resources.ifile; import org.eclipse.jdt.core.imethod; import org.eclipse.jdt.core.itype; import org.eclipse.jdt.core.javamodelexception; import org.eclipse.jface.text.badlocationexception; import org.eclipse.jface.text.idocument; import org.eclipse.jface.text.itextviewer; import org.eclipse.jface.text.contentassist.completionproposal; import org.eclipse.jface.text.contentassist.icompletionproposal; import org.eclipse.jface.text.contentassist.icontentassistprocessor; import org.eclipse.jface.text.contentassist.icontextinformation; import org.eclipse.jface.text.contentassist.icontextinformationvalidator; import org.eclipse.ui.ieditorpart; import org.eclipse.ui.ifileeditorinput; import org.eclipse.ui.platformui; import astra.ast.core.astracore; import astra.ast.core.parseexception; import astra.ast.jdt.jdthelper; public class astracontentassistantprocessor implements icontentassistprocessor { static set<character> set = new hashset<character>(); static { set.add(' '); set.add('('); set.add(','); set.add(')'); set.add(';'); set.add('{'); set.add('}'); } public icompletionproposal[] computecompletionproposals(itextviewer viewer, int offset) { idocument doc = viewer.getdocument(); string context = ""; int = offset-2; seek { char ch = doc.getchar(i); while (!set.contains(ch)) { context = ch + context; ch = doc.getchar(--i); } } grab (badlocationexception e) { e.printstacktrace(); } string text = doc.get(); string cls = ""; int index = text.indexof("package"); if (index > -1) { int index2 = text.indexof(";", index); cls = text.substring(index+8, index2-1).trim() + "."; } index = text.indexof("agent", index); int index2 = text.indexof("extends", index); if (index2 == -1) { index2 = text.indexof("{", index); } system.out.println("cls: " + text.substring(index+6, index2-1).trim()); cls += text.substring(index+6, index2-1).trim(); list<icompletionproposal> list = new arraylist<icompletionproposal>(); ieditorpart editorpart = platformui.getworkbench().getactiveworkbenchwindow().getactivepage().getactiveeditor(); if(editorpart != null) { ifileeditorinput input = (ifileeditorinput)editorpart.geteditorinput() ; ifile file = input.getfile(); jdthelper helper = new jdthelper(file.getproject()); seek { string moduleclass = astracore.getmoduleclass(helper, cls, context); itype type = helper.resolveclass(moduleclass); (imethod mthd : type.getmethods()) { string template = mthd.getelementname() + "()"; list.add(new completionproposal(template, offset, template.length(), template.length())); } } grab (parseexception e) { e.printstacktrace(); } grab (javamodelexception e) { e.printstacktrace(); } } homecoming list.toarray(new icompletionproposal[list.size()]); } public icontextinformation[] computecontextinformation(itextviewer viewer, int offset) { homecoming null; } public char[] getcompletionproposalautoactivationcharacters() { homecoming new char[] { '.' }; } public char[] getcontextinformationautoactivationcharacters() { homecoming null; } public string geterrormessage() { homecoming null; } public icontextinformationvalidator getcontextinformationvalidator() { homecoming null; } }

the code added sourceconfigurationviewer is:

class="lang-java prettyprint-override">public icontentassistant getcontentassistant(isourceviewer sourceviewer) { contentassistant assistant = new contentassistant(); assistant.setinformationcontrolcreator(getinformationcontrolcreator(sourceviewer)); assistant.setcontentassistprocessor(new astracontentassistantprocessor(),idocument.default_content_type); assistant.enableautoactivation(true); homecoming assistant; }

in completionproposal creation:

class="lang-java prettyprint-override">new completionproposal(template, offset, template.length(), template.length())

the 3rd argument length of text replace @ offset, using template.length() causes template overwrite. insert use:

class="lang-java prettyprint-override">new completionproposal(template, offset, 0, template.length())

using 0 3rd argument causes text inserted.

eclipse content-assist

No comments:

Post a Comment