/*******************************************************************************
* 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.BadLocationException;
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.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;
/**
* create content assist proposals for tml comment partitions
*
*
*/
public class TMLCommentPartitionContentAssistProcessor implements IContentAssistProcessor {
private String _errorMessage;
public TMLCommentPartitionContentAssistProcessor() {
}
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
setErrorMessage(null);
IDocument document = viewer.getDocument();
try {
IRegion region = document.getPartition(offset);
// fallback to current edit line if partition has length 0 - for e.g. end of document
if (region.getLength() == 0) {
region = document.getLineInformationOfOffset(offset);
}
ITypedRegion previousPartition = document.getPartition(offset - 1);
// check if tml comment partition is open
String content = document.get(region.getOffset(), region.getLength());
if (!content.trim().endsWith("</tml:comment>") && (content.trim().startsWith("<tml:comment>") || previousPartition.getType().equals(TMLPartitionScanner.TML_COMMENT))) {
String replacement = "</tml:comment>";
String alreadyTyped = document.get(region.getOffset(), offset-region.getOffset());
if (alreadyTyped.contains("\n")) {
alreadyTyped = alreadyTyped.substring(alreadyTyped.lastIndexOf("\n"));
}
int completionStartPos = -1;
if (alreadyTyped.indexOf(">") == -1) {
completionStartPos = alreadyTyped.lastIndexOf("<");
if (completionStartPos == -1) {
completionStartPos = offset;
} else {
completionStartPos = offset - (alreadyTyped.length() - completionStartPos);
}
} else {
completionStartPos = offset;
}
ICompletionProposal proposals[] = new ICompletionProposal[1];
proposals[0] = new CompletionProposal(replacement, completionStartPos, offset - completionStartPos, replacement.length());
return proposals;
}
} catch (BadLocationException 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[] { ' ', ':' , '/' };
}
public char[] getContextInformationAutoActivationCharacters() {
return null;
}
public IContextInformationValidator getContextInformationValidator() {
return null;
}
public String getErrorMessage() {
return _errorMessage;
}
}