/*******************************************************************************
* 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.tml;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
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 de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.tmlscript.parsing.TMLScriptRegion;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.wga.model.VersionCompliance;
/**
* create content assist proposals for tags with tmlscript body
* for e.g. tml:script, tml:action, tml:eventscript
*/
public class TMLScriptContentAssistProcessor implements IContentAssistProcessor {
private String _errorMessage;
public TMLScriptContentAssistProcessor() {
}
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
setErrorMessage(null);
IDocument document = viewer.getDocument();
try {
IRegion region = document.getPartition(offset);
// search backwards to document start or partition of type TML - this is neccessary bc. our current script may contain TMLscript comments
ITypedRegion prevPartition = null;
int previousOffset = region.getOffset() - 1;
while (previousOffset > 0) {
prevPartition = document.getPartition(previousOffset);
if (prevPartition.getType().equals(TMLPartitionScanner.TML_TAG_START)) {
break;
}
previousOffset = prevPartition.getOffset() - 1;
}
if (prevPartition != null) {
region = new Region(previousOffset+1, region.getOffset() - previousOffset + region.getLength() - 1);
}
VersionCompliance versionCompliance = WGADesignStructureHelper.getWGAVersionCompliance(Plugin.getDefault().getActiveFile());
TMLScriptRegion tmlScriptRegion = TMLScriptRegion.parse(region, document, offset, versionCompliance);
System.out.println(tmlScriptRegion.toString());
return tmlScriptRegion.createProposals().toArray(new ICompletionProposal[0]);
}
catch (Exception e) {
Plugin.getDefault().logError("Unable to compute completion proposal", e);
setErrorMessage(e.getMessage());
}
return null;
}
private void setErrorMessage(String message) {
_errorMessage = message;
}
public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
return null;
}
public char[] getCompletionProposalAutoActivationCharacters() {
//return new char[] { ' ', ':' , '/' };
return new char[] {'.'};
}
public char[] getContextInformationAutoActivationCharacters() {
return null;
}
public IContextInformationValidator getContextInformationValidator() {
return null;
}
public String getErrorMessage() {
return _errorMessage;
}
}