Я пишу плагин для Eclipse, и я пытаюсь реализовать поддержку содержимого. Приведенный ниже код работает с исключением того, что он перезаписывает существующий текст, и я хочу, чтобы он вставлялся.Как сделать вставку содержимого для поддержки eclipse вместо перезаписи
Любая помощь с благодарностью получена.
ContentAssistantProvider:
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 i = offset-2;
try {
char ch = doc.getChar(i);
while (!set.contains(ch)) {
context = ch + context;
ch = doc.getChar(--i);
}
} catch (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());
try {
String moduleClass = ASTRACore.getModuleClass(helper, cls, context);
IType type = helper.resolveClass(moduleClass);
for (IMethod mthd : type.getMethods()) {
String template = mthd.getElementName() + "()";
list.add(new CompletionProposal(template, offset, template.length(), template.length()));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (JavaModelException e) {
e.printStackTrace();
}
}
return list.toArray(new ICompletionProposal[list.size()]);
}
public IContextInformation[] computeContextInformation(ITextViewer viewer,
int offset) {
return null;
}
public char[] getCompletionProposalAutoActivationCharacters() {
return new char[] { '.' };
}
public char[] getContextInformationAutoActivationCharacters() {
return null;
}
public String getErrorMessage() {
return null;
}
public IContextInformationValidator getContextInformationValidator() {
return null;
}
}
я добавил к SourceConfigurationViewer код:
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
ContentAssistant assistant = new ContentAssistant();
assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
assistant.setContentAssistProcessor(new ASTRAContentAssistantProcessor(),IDocument.DEFAULT_CONTENT_TYPE);
assistant.enableAutoActivation(true);
return assistant;
}
Спасибо! Работает как сон! – Rem