/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.tmlscript;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.bindings.keys.KeySequence;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
import org.eclipse.jface.text.quickassist.IQuickAssistProcessor;
import org.eclipse.jface.text.quickassist.QuickAssistAssistant;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
import org.eclipse.ui.texteditor.MarkerAnnotation;
import de.innovationgate.eclipse.editors.ResourceIDs;
import de.innovationgate.eclipse.editors.all.InvalidCharacterCompletionProposal;
import de.innovationgate.eclipse.editors.helpers.MarkerFactory;
import de.innovationgate.eclipse.editors.helpers.TextStyles;
import de.innovationgate.eclipse.editors.tml.TMLScriptContentAssistProcessor;
public class TMLScriptEditorConfiguration extends TextSourceViewerConfiguration {
private ContentAssistant _contentAssistent;
@Override
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
_contentAssistent = new ContentAssistant();
_contentAssistent.enableAutoActivation(true);
_contentAssistent.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
_contentAssistent.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
_contentAssistent.setInformationControlCreator(getInformationControlCreator(sourceViewer));
_contentAssistent.setRepeatedInvocationMode(true);
_contentAssistent.setRepeatedInvocationTrigger(KeySequence.getInstance(KeyStroke.getInstance('\r')));
TMLScriptContentAssistProcessor tmlScriptContentAssistProcessor = new TMLScriptContentAssistProcessor();
_contentAssistent.setContentAssistProcessor(tmlScriptContentAssistProcessor, TMLScriptPartitionScanner.TMLSCRIPT);
_contentAssistent.setContentAssistProcessor(tmlScriptContentAssistProcessor, IDocument.DEFAULT_CONTENT_TYPE);
_contentAssistent.setAutoActivationDelay(250);
return _contentAssistent;
}
private TMLScriptScanner scanner;
public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
List<String> contentTypes = new ArrayList<String>();
contentTypes.addAll(Arrays.asList(TMLScriptPartitionScanner.PARTITIONS));
return contentTypes.toArray(new String[0]);
}
protected TMLScriptScanner getTMLScriptScanner() {
if (scanner == null) {
scanner = new TMLScriptScanner();
scanner.setDefaultReturnToken(new Token(TextStyles.STYLE_DEFAULT));
}
return scanner;
}
public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
PresentationReconciler reconciler = new PresentationReconciler();
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getTMLScriptScanner());
reconciler.setDamager(dr, TMLScriptPartitionScanner.TMLSCRIPT);
reconciler.setRepairer(dr, TMLScriptPartitionScanner.TMLSCRIPT);
NonRuleBasedDamagerRepairer ndr = new NonRuleBasedDamagerRepairer(TextStyles.COMMENT);
reconciler.setDamager(ndr, TMLScriptPartitionScanner.MCOMMENT);
reconciler.setRepairer(ndr, TMLScriptPartitionScanner.MCOMMENT);
ndr = new NonRuleBasedDamagerRepairer(TextStyles.COMMENT);
reconciler.setDamager(ndr, TMLScriptPartitionScanner.SCOMMENT);
reconciler.setRepairer(ndr, TMLScriptPartitionScanner.SCOMMENT);
return reconciler;
}
@Override
public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
IQuickAssistAssistant assistant = new QuickAssistAssistant();
assistant.setQuickAssistProcessor(new IQuickAssistProcessor() {
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
return true;
}
public boolean canFix(Annotation annotation) {
boolean canFix = false;
if (annotation instanceof MarkerAnnotation) {
MarkerAnnotation markerAnnotation = (MarkerAnnotation) annotation;
try {
if (markerAnnotation.getMarker().getType().equals(ResourceIDs.MARKER_INVALID_CHARACTER)) {
canFix = InvalidCharacterCompletionProposal.canCorrect(markerAnnotation);
}
} catch (CoreException e) {;
}
}
return canFix;
}
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
MarkerAnnotation markerAnnotation = MarkerFactory.findMarkerAnnotationByTypeAndOffset(ResourceIDs.MARKER_INVALID_CHARACTER, invocationContext.getOffset(), invocationContext.getSourceViewer());
if (markerAnnotation != null && markerAnnotation.isQuickFixable()) {
InvalidCharacterCompletionProposal proposal = new InvalidCharacterCompletionProposal(markerAnnotation);
proposals.add(proposal);
}
return proposals.toArray(new ICompletionProposal[0]);
}
public String getErrorMessage() {
return null;
}
});
assistant.install(sourceViewer);
return assistant;
}
}