*/
package net.sourceforge.phpdt.internal.corext.template;
-//import org.eclipse.jface.text.BadLocationException;
+import net.sourceforge.phpdt.internal.corext.Assert;
+
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
-//import org.eclipse.jdt.internal.corext.Assert;
/**
* A typical text based document template context.
*/
public abstract class DocumentTemplateContext extends TemplateContext {
- /** The text of the document. */
- private final IDocument fDocument;
- /** The completion position. */
- private final int fCompletionPosition;
+ /** The text of the document. */
+ private final IDocument fDocument;
+ /** The completion offset. */
+ private final int fCompletionOffset;
+ /** The completion length. */
+ private final int fCompletionLength;
- /**
- * Creates a document template context.
- */
- protected DocumentTemplateContext(ContextType type, IDocument document, int completionPosition) {
- super(type);
+ /**
+ * Creates a document template context.
+ */
+ protected DocumentTemplateContext(ContextType type, IDocument document,
+ int completionOffset, int completionLength)
+ {
+ super(type);
-// Assert.isNotNull(document);
-// Assert.isTrue(completionPosition >= 0 && completionPosition <= document.getLength());
+ Assert.isNotNull(document);
+ Assert.isTrue(completionOffset >= 0 && completionOffset <= document.getLength());
+ Assert.isTrue(completionLength >= 0);
- fDocument= document;
- fCompletionPosition= completionPosition;
- }
+ fDocument= document;
+ fCompletionOffset= completionOffset;
+ fCompletionLength= completionLength;
+ }
- public IDocument getDocument() {
- return fDocument;
- }
+ public IDocument getDocument() {
+ return fDocument;
+ }
- /**
- * Returns the string of the context.
- */
-// public String getString() {
-// return fDocument.get();
-// }
+ /**
+ * Returns the completion offset within the string of the context.
+ */
+ public int getCompletionOffset() {
+ return fCompletionOffset;
+ }
- /**
- * Returns the completion position within the string of the context.
- */
- public int getCompletionPosition() {
- return fCompletionPosition;
- }
+ /**
+ * Returns the completion length within the string of the context.
+ */
+ public int getCompletionLength() {
+ return fCompletionLength;
+ }
- /**
- * Returns the keyword which triggered template insertion.
- */
- public String getKey() {
- int offset= getStart();
- int length= getEnd() - offset;
- try {
- return fDocument.get(offset, length);
- } catch (BadLocationException e) {
- return ""; //$NON-NLS-1$
- }
- }
+ /**
+ * Returns the keyword which triggered template insertion.
+ */
+ public String getKey() {
+ int offset= getStart();
+ int length= getEnd() - offset;
+ try {
+ return fDocument.get(offset, length);
+ } catch (BadLocationException e) {
+ return ""; //$NON-NLS-1$
+ }
+ }
- /**
- * Returns the beginning offset of the keyword.
- */
- public int getStart() {
- return fCompletionPosition;
- }
+ /**
+ * Returns the beginning offset of the keyword.
+ */
+ public int getStart() {
+ return fCompletionOffset;
+ }
- /**
- * Returns the end offset of the keyword.
- */
- public int getEnd() {
- return fCompletionPosition;
- }
+ /**
+ * Returns the end offset of the keyword.
+ */
+ public int getEnd() {
+ return fCompletionOffset + fCompletionLength;
+ }
}
+
protected IDocument fDocument;
/** the completion position within the document string */
- protected int fPosition;
-
+ protected int fOffset;
+
+ /** the completion length */
+ protected int fLength;
+
/** the associated compilation unit, may be <code>null</code> */
//protected ICompilationUnit fCompilationUnit;
/*
/**
* Sets context parameters. Needs to be called before createContext().
*/
- public void setContextParameters(IDocument document, int position) {//, ICompilationUnit compilationUnit) {
+ public void setContextParameters(IDocument document, int position, int length) {//, ICompilationUnit compilationUnit) {
fDocument= document;
- fPosition= position;
+ fOffset= position;
+ fLength= length;
// fCompilationUnit= compilationUnit;
}
package net.sourceforge.phpdt.internal.corext.template.php;
import java.text.DateFormat;
+import java.util.Calendar;
import net.sourceforge.phpdt.internal.corext.template.SimpleTemplateVariable;
import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
return DateFormat.getDateInstance().format(new java.util.Date());
}
}
-
+
+ /**
+ * The year variable evaluates to the current year.
+ */
+ static class Year extends SimpleTemplateVariable {
+ public Year() {
+ super(PHPTemplateMessages.getString("GlobalVariables.variable.name.year"), PHPTemplateMessages.getString("GlobalVariables.variable.description.year")); //$NON-NLS-1$ //$NON-NLS-2$
+ setResolved(true);
+ }
+ public String evaluate(TemplateContext context) {
+ return Integer.toString(Calendar.getInstance().get(Calendar.YEAR));
+ }
+ }
/**
* The time variable evaluates to the current time.
*/
addVariable(new GlobalVariables.Cursor());
addVariable(new GlobalVariables.Dollar());
addVariable(new GlobalVariables.Date());
+ addVariable(new GlobalVariables.Year());
addVariable(new GlobalVariables.Time());
addVariable(new GlobalVariables.User());
* @see ContextType#createContext()
*/
public TemplateContext createContext() {
- return new HTMLUnitContext(this, fDocument, fPosition); //, fCompilationUnit);
+ return new HTMLUnitContext(this, fDocument, fOffset); //, fCompilationUnit);
}
}
protected HTMLUnitContext(ContextType type, IDocument document, int completionPosition)
//,ICompilationUnit compilationUnit)
{
- super(type, document, completionPosition);
+ super(type, document, completionPosition, 0);
// fCompilationUnit= compilationUnit;
}
public int getStart() {
IDocument document = getDocument();
try {
- int start = getCompletionPosition();
+ int start = getCompletionOffset();
while (((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
|| ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
return start;
} catch (BadLocationException e) {
- return getCompletionPosition();
+ return getCompletionOffset();
}
}
addVariable(new GlobalVariables.Cursor());
addVariable(new GlobalVariables.Dollar());
addVariable(new GlobalVariables.Date());
+ addVariable(new GlobalVariables.Year());
addVariable(new GlobalVariables.Time());
addVariable(new GlobalVariables.User());
* @see ContextType#createContext()
*/
public TemplateContext createContext() {
- return new PHPUnitContext(this, fDocument, fPosition); //, fCompilationUnit);
+ return new PHPUnitContext(this, fDocument, fOffset); //, fCompilationUnit);
}
}
--- /dev/null
+/*
+ * (c) Copyright IBM Corp. 2000, 2001.
+ * All Rights Reserved.
+ */
+package net.sourceforge.phpdt.internal.corext.template.php;
+
+import net.sourceforge.phpdt.internal.corext.template.ContextType;
+import net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext;
+import net.sourceforge.phpdt.internal.corext.template.Template;
+import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
+import net.sourceforge.phpdt.internal.corext.template.TemplateTranslator;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+
+/**
+ * A context for phpdoc.
+ */
+public class PHPDocContext extends DocumentTemplateContext {
+
+ // tags
+ private static final char HTML_TAG_BEGIN= '<';
+ private static final char HTML_TAG_END= '>';
+ private static final char JAVADOC_TAG_BEGIN= '@';
+
+ /**
+ * Creates a phpdoc template context.
+ *
+ * @param type the context type.
+ * @param document the document.
+ * @param completionPosition the completion position within the document.
+ * @param unit the compilation unit (may be <code>null</code>).
+ */
+ public PHPDocContext(ContextType type, IDocument document, int completionOffset, int completionLength)
+ //, int completionOffset, int completionLength, ICompilationUnit compilationUnit)
+ {
+ super(type, document, completionOffset, completionLength);
+// super(type, document, completionOffset, completionLength, compilationUnit);
+ }
+
+ /*
+ * @see TemplateContext#canEvaluate(Template templates)
+ */
+ public boolean canEvaluate(Template template) {
+ String key= getKey();
+
+// if (fForceEvaluation)
+// return true;
+ return template.matches(getKey(), getContextType().getName());
+// return
+// template.matches(key, getContextType().getName()) &&
+// (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase());
+ }
+
+ /*
+ * @see DocumentTemplateContext#getStart()
+ */
+ public int getStart() {
+ try {
+ IDocument document= getDocument();
+
+ if (getCompletionLength() == 0) {
+ int start= getCompletionOffset();
+
+ if ((start != 0) && (document.getChar(start - 1) == HTML_TAG_END))
+ start--;
+
+ while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
+ start--;
+
+ if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
+ start--;
+
+ // include html and phpdoc tags
+ if ((start != 0) && (
+ (document.getChar(start - 1) == HTML_TAG_BEGIN) ||
+ (document.getChar(start - 1) == JAVADOC_TAG_BEGIN)))
+ {
+ start--;
+ }
+
+ return start;
+
+ } else {
+
+ int start= getCompletionOffset();
+ int end= getCompletionOffset() + getCompletionLength();
+
+ while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
+ start--;
+
+ while (start != end && Character.isWhitespace(document.getChar(start)))
+ start++;
+
+ if (start == end)
+ start= getCompletionOffset();
+
+ return start;
+ }
+
+ } catch (BadLocationException e) {
+ return getCompletionOffset();
+ }
+ }
+
+ /*
+ * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getEnd()
+ */
+ public int getEnd() {
+
+ if (getCompletionLength() == 0)
+ return super.getEnd();
+
+ try {
+ IDocument document= getDocument();
+
+ int start= getCompletionOffset();
+ int end= getCompletionOffset() + getCompletionLength();
+
+ while (start != end && Character.isWhitespace(document.getChar(end - 1)))
+ end--;
+
+ return end;
+
+ } catch (BadLocationException e) {
+ return super.getEnd();
+ }
+ }
+
+ /*
+ * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getKey()
+ */
+ public String getKey() {
+
+ if (getCompletionLength() == 0)
+ return super.getKey();
+
+ try {
+ IDocument document= getDocument();
+
+ int start= getStart();
+ int end= getCompletionOffset();
+ return start <= end
+ ? document.get(start, end - start)
+ : ""; //$NON-NLS-1$
+
+ } catch (BadLocationException e) {
+ return super.getKey();
+ }
+ }
+
+ /*
+ * @see TemplateContext#evaluate(Template)
+ */
+ public TemplateBuffer evaluate(Template template) throws CoreException {
+ TemplateTranslator translator= new TemplateTranslator();
+ TemplateBuffer buffer= translator.translate(template.getPattern());
+
+ getContextType().edit(buffer, this);
+
+ return buffer;
+ }
+
+}
+
--- /dev/null
+/*
+ * (c) Copyright IBM Corp. 2000, 2001.
+ * All Rights Reserved.
+ */
+package net.sourceforge.phpdt.internal.corext.template.php;
+
+import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
+
+/**
+ * A context type for phpdoc.
+ */
+public class PHPDocContextType extends CompilationUnitContextType {
+
+ /**
+ * Creates a php context type.
+ */
+ public PHPDocContextType() {
+ super("phpdoc"); //$NON-NLS-1$
+
+ // global
+ addVariable(new GlobalVariables.Cursor());
+ addVariable(new GlobalVariables.Dollar());
+ addVariable(new GlobalVariables.Date());
+ addVariable(new GlobalVariables.Year());
+ addVariable(new GlobalVariables.Time());
+ addVariable(new GlobalVariables.User());
+
+ // compilation unit
+/* addVariable(new File());
+ addVariable(new Method());
+ addVariable(new ReturnType());
+ addVariable(new Arguments());
+ addVariable(new Type());
+ addVariable(new Package());
+ addVariable(new Project()); */
+ }
+
+ /*
+ * @see ContextType#createContext()
+ */
+ public TemplateContext createContext() {
+ return new PHPDocContext(this, fDocument, fOffset, fLength); // , fCompilationUnit);
+ }
+
+}
GlobalVariables.variable.description.cursor=The cursor position after editing template variables
GlobalVariables.variable.description.dollar=The dollar symbol
GlobalVariables.variable.description.date=Current date
+GlobalVariables.variable.description.year=Current year
GlobalVariables.variable.description.time=Current time
GlobalVariables.variable.description.user=User name
GlobalVariables.variable.name.cursor=cursor
GlobalVariables.variable.name.dollar=dollar
GlobalVariables.variable.name.date=date
+GlobalVariables.variable.name.year=year
GlobalVariables.variable.name.time=time
GlobalVariables.variable.name.user=user
protected PHPUnitContext(ContextType type, IDocument document, int completionPosition)
//,ICompilationUnit compilationUnit)
{
- super(type, document, completionPosition);
+ super(type, document, completionPosition, 0);
// fCompilationUnit= compilationUnit;
}
return
// fEnabled &&
// fContextTypeName.equals(contextTypeName) &&
- (prefix.length() != 0) && identifier.toLowerCase().startsWith(prefix.toLowerCase());
+ (prefix.length() != 0) && identifier.toLowerCase().startsWith(prefix.toLowerCase());
}
/*
public int getStart() {
IDocument document = getDocument();
try {
- int start = getCompletionPosition();
+ int start = getCompletionOffset();
while (((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
|| ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
return start;
} catch (BadLocationException e) {
- return getCompletionPosition();
+ return getCompletionOffset();
}
}
import net.sourceforge.phpdt.internal.ui.PHPUiImages;
import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
+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.Region;
+import org.eclipse.swt.graphics.Point;
//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
public class BuiltInEngine {
if (!(fContextType instanceof CompilationUnitContextType))
return;
-
- ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition);//mpilationUnit);
+ Point selection= viewer.getSelectedRange();
+ // remember selected text
+ String selectedText= null;
+ if (selection.y != 0) {
+ try {
+ selectedText= document.get(selection.x, selection.y);
+ } catch (BadLocationException e) {}
+ }
+
+ ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);//mpilationUnit);
+
PHPUnitContext context= (PHPUnitContext) fContextType.createContext();
int start= context.getStart();
int end= context.getEnd();
import net.sourceforge.phpdt.internal.ui.PHPUiImages;
import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
+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.Region;
+import org.eclipse.swt.graphics.Point;
//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
public class IdentifierEngine {
if (!(fContextType instanceof CompilationUnitContextType))
return;
- ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition);//mpilationUnit);
+ Point selection= viewer.getSelectedRange();
+ // remember selected text
+ String selectedText= null;
+ if (selection.y != 0) {
+ try {
+ selectedText= document.get(selection.x, selection.y);
+ } catch (BadLocationException e) {}
+ }
+
+ ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);//mpilationUnit);
+
PHPUnitContext context= (PHPUnitContext) fContextType.createContext();
int start= context.getStart();
int end= context.getEnd();
import net.sourceforge.phpdt.internal.ui.PHPUiImages;
import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
+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.Region;
+import org.eclipse.swt.graphics.Point;
//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
public class TemplateEngine {
if (!(fContextType instanceof CompilationUnitContextType))
return;
-
- ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition);//mpilationUnit);
+
+ Point selection= viewer.getSelectedRange();
+ // remember selected text
+ String selectedText= null;
+ if (selection.y != 0) {
+ try {
+ selectedText= document.get(selection.x, selection.y);
+ } catch (BadLocationException e) {}
+ }
+
+ ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);//mpilationUnit);
DocumentTemplateContext context= (DocumentTemplateContext) fContextType.createContext();
int start= context.getStart();
int end= context.getEnd();
import org.eclipse.swt.graphics.RGB;
/**
- * Configuration for an <code>SourceViewer</code> which shows PHP code.
+ * Configuration for an <code>SourceViewer</code> which shows PHP code.
*/
public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
public String[] getPartitionManagingPositionCategories() {
return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
}
-// /**
+// /**
// * Returns the names of the document position categories used by the document
// * partitioners created by this object to manage their partition information.
// * If the partitioners don't use document position categories, the returned
};
}
- /* (non-Javadoc)
+ /* (non-Javadoc)
* Method declared on SourceViewerConfiguration
*/
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {