aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzzhongwei2013-03-12 02:12:43 (EDT)
committerzzhongwei2013-03-12 02:12:43 (EDT)
commitb429c5bf8e75aa3bcb86f09ed549e20423dcbdb9 (patch)
treeb61f2780bb7d8432198857525bd21a57f29bae35
parentdbcee7c1adf7f3dc97662a0c8719f96ce0f8933c (diff)
downloadorg.eclipse.pdt-b429c5bf8e75aa3bcb86f09ed549e20423dcbdb9.zip
org.eclipse.pdt-b429c5bf8e75aa3bcb86f09ed549e20423dcbdb9.tar.gz
org.eclipse.pdt-b429c5bf8e75aa3bcb86f09ed549e20423dcbdb9.tar.bz2
402733: ${cursor} in php template doesn't work with a line delimiter of
other than default https://bugs.eclipse.org/bugs/show_bug.cgi?id=402733
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/templates/PhpTemplateContext.java48
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPTemplateStore.java29
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/NewGenericFileTemplatesWizardPage.java12
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/PHPFileCreationWizard.java12
4 files changed, 88 insertions, 13 deletions
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/templates/PhpTemplateContext.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/templates/PhpTemplateContext.java
index 79184d9..a4bce0b 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/templates/PhpTemplateContext.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/templates/PhpTemplateContext.java
@@ -14,16 +14,15 @@ package org.eclipse.php.internal.ui.editor.templates;
import java.util.*;
import org.eclipse.dltk.core.ISourceModule;
+import org.eclipse.dltk.ui.templates.IScriptTemplateIndenter;
import org.eclipse.dltk.ui.templates.ScriptTemplateContext;
import org.eclipse.dltk.ui.templates.ScriptTemplateContextType;
import org.eclipse.dltk.utils.TextUtils;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
-import org.eclipse.jface.text.templates.Template;
-import org.eclipse.jface.text.templates.TemplateBuffer;
-import org.eclipse.jface.text.templates.TemplateException;
-import org.eclipse.jface.text.templates.TemplateVariable;
+import org.eclipse.jface.text.TextUtilities;
+import org.eclipse.jface.text.templates.*;
import org.eclipse.php.internal.core.PHPCoreConstants;
import org.eclipse.php.internal.core.PHPCorePlugin;
import org.eclipse.php.internal.core.format.FormatPreferencesSupport;
@@ -38,6 +37,8 @@ public class PhpTemplateContext extends ScriptTemplateContext {
public static final char BLANK = ' ';
public static final char TAB = '\t';
+ private String fLineDelimiter;
+
public PhpTemplateContext(ScriptTemplateContextType phpTemplateContextType,
IDocument document, int offset, int length,
ISourceModule sourceModule) {
@@ -77,7 +78,39 @@ public class PhpTemplateContext extends ScriptTemplateContext {
newPattern, template.isAutoInsertable());
}
- TemplateBuffer result = super.evaluate(template);
+ if (!canEvaluate(template))
+ return null;
+
+ final String[] lines = TextUtils.splitLines(template.getPattern());
+ if (lines.length > 1) {
+ String delimeter;
+ if (fLineDelimiter == null) {
+ delimeter = TextUtilities
+ .getDefaultLineDelimiter(getDocument());
+ } else {
+ delimeter = fLineDelimiter;
+ }
+ final String indent = calculateIndent(getDocument(), getStart());
+ final IScriptTemplateIndenter indenter = getIndenter();
+ final StringBuffer buffer = new StringBuffer(lines[0]);
+
+ // Except first line
+ for (int i = 1; i < lines.length; i++) {
+ buffer.append(delimeter);
+ indenter.indentLine(buffer, indent, lines[i]);
+ }
+
+ template = new Template(template.getName(),
+ template.getDescription(), template.getContextTypeId(),
+ buffer.toString(), template.isAutoInsertable());
+ }
+
+ TemplateTranslator translator = new TemplateTranslator();
+ TemplateBuffer buffer = translator.translate(template);
+
+ getContextType().resolve(buffer, this);
+
+ TemplateBuffer result = buffer;
// 300533: Switching through template keys? in fore and forek template
// is corrupter
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=300533
@@ -146,4 +179,9 @@ public class PhpTemplateContext extends ScriptTemplateContext {
return templateVariable.isUnambiguous()
&& DOLLAR.equals(templateVariable.getType());
}
+
+ public void setLineDelimiter(String lineDelimiter) {
+ this.fLineDelimiter = lineDelimiter;
+ }
+
}
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPTemplateStore.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPTemplateStore.java
index 14a7f56..9a1ab93 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPTemplateStore.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPTemplateStore.java
@@ -26,6 +26,7 @@ import org.eclipse.jface.text.templates.*;
import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
import org.eclipse.php.internal.ui.PHPUIMessages;
import org.eclipse.php.internal.ui.PHPUiPlugin;
+import org.eclipse.php.internal.ui.editor.templates.PhpTemplateContext;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
@@ -58,12 +59,23 @@ public class PHPTemplateStore extends ContributionTemplateStore {
public static CompiledTemplate compileTemplate(
ContextTypeRegistry contextTypeRegistry, Template template,
String containerName, String fileName) {
+ return compileTemplate(contextTypeRegistry, template, containerName,
+ fileName, null);
+ }
+
+ public static CompiledTemplate compileTemplate(
+ ContextTypeRegistry contextTypeRegistry, Template template,
+ String containerName, String fileName, String lineDelimiter) {
String string = null;
int offset = 0;
if (template != null) {
IDocument document = new Document();
DocumentTemplateContext context = getContext(contextTypeRegistry,
template, containerName, fileName, document);
+ if (context instanceof PhpTemplateContext) {
+ PhpTemplateContext phpTemplateContext = (PhpTemplateContext) context;
+ phpTemplateContext.setLineDelimiter(lineDelimiter);
+ }
TemplateBuffer buffer = null;
try {
buffer = context.evaluate(template);
@@ -91,6 +103,13 @@ public class PHPTemplateStore extends ContributionTemplateStore {
return compileTemplate(contextTypeRegistry, template, null, null);
}
+ public static CompiledTemplate compileTemplate(
+ ContextTypeRegistry contextTypeRegistry, Template template,
+ String lineDelimiter) {
+ return compileTemplate(contextTypeRegistry, template, null, null,
+ lineDelimiter);
+ }
+
/**
* @param contextTypeRegistry
* @param template
@@ -104,14 +123,14 @@ public class PHPTemplateStore extends ContributionTemplateStore {
String containerName, String fileName, IDocument document) {
if (fileName == null) {
- return new DocumentTemplateContext(contextTypeRegistry
- .getContextType(template.getContextTypeId()), document, 0,
- 0);
+ return new DocumentTemplateContext(
+ contextTypeRegistry.getContextType(template
+ .getContextTypeId()), document, 0, 0);
}
- IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(
- new Path(containerName + "/" + fileName));
+ IFile file = ResourcesPlugin.getWorkspace().getRoot()
+ .getFile(new Path(containerName + "/" + fileName));
ISourceModule sourceModule = DLTKCore.createSourceModuleFrom(file);
TemplateContextType type = contextTypeRegistry.getContextType(template
.getContextTypeId());
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/NewGenericFileTemplatesWizardPage.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/NewGenericFileTemplatesWizardPage.java
index bea7f4c..0229a5c 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/NewGenericFileTemplatesWizardPage.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/NewGenericFileTemplatesWizardPage.java
@@ -297,8 +297,8 @@ public abstract class NewGenericFileTemplatesWizardPage extends WizardPage {
}
resetTableViewerInput();
Dialog.applyDialogFont(parent);
- PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
- IPHPHelpContextIds.NEW);
+ PlatformUI.getWorkbench().getHelpSystem()
+ .setHelp(parent, IPHPHelpContextIds.NEW);
setControl(parent);
}
@@ -449,6 +449,14 @@ public abstract class NewGenericFileTemplatesWizardPage extends WizardPage {
fileName);
}
+ public CompiledTemplate compileTemplate(String containerName,
+ String fileName, String lineDelimiter) {
+ Template template = getSelectedTemplate();
+ return PHPTemplateStore.compileTemplate(
+ getTemplatesContextTypeRegistry(), template, containerName,
+ fileName, lineDelimiter);
+ }
+
public TemplateProposal createTemplateProposal() {
TemplateProposal proposal = null;
Template template = getSelectedTemplate();
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/PHPFileCreationWizard.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/PHPFileCreationWizard.java
index fbcd5e3..fc95fa3 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/PHPFileCreationWizard.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/PHPFileCreationWizard.java
@@ -21,12 +21,15 @@ import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.dltk.core.DLTKCore;
+import org.eclipse.dltk.core.IScriptProject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.osgi.util.NLS;
+import org.eclipse.php.internal.core.ast.util.Util;
import org.eclipse.php.internal.ui.Logger;
import org.eclipse.php.internal.ui.PHPUIMessages;
import org.eclipse.php.internal.ui.editor.PHPStructuredEditor;
@@ -70,8 +73,15 @@ public class PHPFileCreationWizard extends Wizard implements INewWizard {
.getContainerName();
final String fileName = phpFileCreationWizardPage.getFileName();
newPhpTemplatesWizardPage.resetTableViewerInput();
+ IScriptProject project = null;
+ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+ IResource resource = root.findMember(new Path(containerName));
+ if (!resource.exists() || !(resource instanceof IContainer)) {
+ project = DLTKCore.create(resource.getProject());
+ }
+ String lineSeparator = Util.getLineSeparator(null, project);
final PHPTemplateStore.CompiledTemplate template = this.newPhpTemplatesWizardPage
- .compileTemplate(containerName, fileName);
+ .compileTemplate(containerName, fileName, lineSeparator);
IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor)