Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichele Locati2018-12-06 05:17:50 -0500
committerThierry BLIND2018-12-10 12:04:24 -0500
commitfe340a1047d7f84272ada34ca3f7048921127e46 (patch)
treed8cc1dcc4c81d93de9500caeaeab571d58e4e6cd
parentfb62cd29656a587b0a28b86dcaa41a32b3fd50b8 (diff)
downloadorg.eclipse.pdt-fe340a1047d7f84272ada34ca3f7048921127e46.tar.gz
org.eclipse.pdt-fe340a1047d7f84272ada34ca3f7048921127e46.tar.xz
org.eclipse.pdt-fe340a1047d7f84272ada34ca3f7048921127e46.zip
Bug 542456 - Fully-qualified class names in generated PHPDoc
Let's add an option to use fully.qualified class names in generated PHPDocs. Change-Id: I18082f512dc499ad0eb4efdb7aa1d96a469502cd Signed-off-by: Michele Locati <michele@locati.it>
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/PHPCoreConstants.java12
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/preferences/CorePreferenceConstants.java1
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.java1
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.properties1
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPContentAssistOptionsConfigurationBlock.java5
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/ui/CodeGeneration.java69
-rw-r--r--tests/org.eclipse.php.core.tests/src/org/eclipse/php/core/tests/PdttFile.java58
-rw-r--r--tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/formatter/autoedit/FormatterAutoEditTests.java7
-rw-r--r--tests/org.eclipse.php.ui.tests/workspace/phpdoc-generation/php53/fullyQualifiedNamesTest01.pdtt54
9 files changed, 203 insertions, 5 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/PHPCoreConstants.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/PHPCoreConstants.java
index eb20fa6b7..6addf6f64 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/PHPCoreConstants.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/PHPCoreConstants.java
@@ -198,6 +198,18 @@ public interface PHPCoreConstants {
String CODEASSIST_INSERT_FULL_QUALIFIED_NAME_FOR_NAMESPACE = "contentAssistInsertFullyQualifiedNameForNamespaceCheckBox"; //$NON-NLS-1$
/**
+ * A named preference that controls if code assist insert full qualified
+ * name in comments.
+ * <p>
+ * Value is of type <code>Boolean</code>. if
+ * <code>false<code> code assist may insert class names without a fully qualified class name. If
+ * <code>true</code> code assist always insert use statement with the full
+ * qualified name.
+ * </p>
+ */
+ String CODEASSIST_INSERT_FULL_QUALIFIED_NAME_IN_COMMENTS = "contentAssistInsertFullyQualifiedNameInCommentsCheckBox"; //$NON-NLS-1$
+
+ /**
* A named preference that controls if code assist also contains proposals from
* other files
* <p>
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/preferences/CorePreferenceConstants.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/preferences/CorePreferenceConstants.java
index fab2b8bac..459572a28 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/preferences/CorePreferenceConstants.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/preferences/CorePreferenceConstants.java
@@ -63,6 +63,7 @@ public class CorePreferenceConstants {
node.putBoolean(PHPCoreConstants.CODEASSIST_SHOW_VARIABLES_FROM_OTHER_FILES, false);
node.putBoolean(PHPCoreConstants.CODEASSIST_SHOW_VARIABLES_FROM_REFERENCED_FILES, true);
node.putBoolean(PHPCoreConstants.CODEASSIST_INSERT_FULL_QUALIFIED_NAME_FOR_NAMESPACE, true);
+ node.putBoolean(PHPCoreConstants.CODEASSIST_INSERT_FULL_QUALIFIED_NAME_IN_COMMENTS, false);
node.putBoolean(PHPCoreConstants.CODEASSIST_SHOW_STRICT_OPTIONS, false);
node.putBoolean(PHPCoreConstants.CODEASSIST_AUTOACTIVATION, true);
node.putBoolean(PHPCoreConstants.CODEASSIST_ASYNC, true);
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.java
index 235ed1c9d..fc49d4547 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.java
@@ -709,6 +709,7 @@ public final class PHPUIMessages extends NLS {
public static String ShowInExplorerViewAction_description;
public static String PHPFileCreationWizardPage_14;
public static String CodeAssistPreferencePage_insertFullyQualifiedNameForNamespace;
+ public static String CodeAssistPreferencePage_insertFullyQualifiedNameInComments;
public static String PHPFileCreationWizardPage_12;
public static String ResourceTransferDragAdapter_moving_resource;
public static String PHPFileCreationWizardPage_11;
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.properties b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.properties
index 4589ecb86..176077d77 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.properties
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/PHPUIMessages.properties
@@ -320,6 +320,7 @@ CodeAssistPreferencePage_showStrictOptions=Show strict options
CodeAssistPreferencePage_showVariablesFromOtherFiles=Show variables from other files
CodeAssistPreferencePage_showVariablesFromReferencedFiles=Show variables from referenced files
CodeAssistPreferencePage_insertFullyQualifiedNameForNamespace=Insert full qualified name for use statement
+CodeAssistPreferencePage_insertFullyQualifiedNameInComments=Insert full qualified names in comments
CodeAssistPreferencePage_fillParameterNamesOnMethodCompletion=I&nsert parameter names
CodeAssistPreferencePage_insertGlobalPrefixForFunctionCallInsideNamespace=Insert global ('\\') prefix for function call inside namespace
CoreUtility_buildall_taskname=Build all...
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPContentAssistOptionsConfigurationBlock.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPContentAssistOptionsConfigurationBlock.java
index 23ff4789a..049af1537 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPContentAssistOptionsConfigurationBlock.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/preferences/PHPContentAssistOptionsConfigurationBlock.java
@@ -44,6 +44,7 @@ public class PHPContentAssistOptionsConfigurationBlock extends AbstractPHPConten
protected Button showVariableFromOtherFilesCheckBox;
protected Button showVariableFromReferencedFilesCheckBox;
protected Button insertFullyQualifiedNameForNamespaceCheckBox;
+ protected Button insertFullyQualifiedNameInCommentsCheckBox;
protected Button fInsertParameterNamesRadioButton;
protected Button fPrefixGlobalFunctionCallRadioButton;
@@ -99,6 +100,10 @@ public class PHPContentAssistOptionsConfigurationBlock extends AbstractPHPConten
PHPUIMessages.CodeAssistPreferencePage_insertFullyQualifiedNameForNamespace,
PHPCoreConstants.CODEASSIST_INSERT_FULL_QUALIFIED_NAME_FOR_NAMESPACE, 0);
+ insertFullyQualifiedNameInCommentsCheckBox = addCheckBox(composite,
+ PHPUIMessages.CodeAssistPreferencePage_insertFullyQualifiedNameInComments,
+ PHPCoreConstants.CODEASSIST_INSERT_FULL_QUALIFIED_NAME_IN_COMMENTS, 0);
+
fInsertParameterNamesRadioButton = addCheckBox(composite,
PHPUIMessages.CodeAssistPreferencePage_fillParameterNamesOnMethodCompletion,
PHPCoreConstants.CODEASSIST_FILL_ARGUMENT_NAMES, 0);
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/ui/CodeGeneration.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/ui/CodeGeneration.java
index e5563cc8e..84da29750 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/ui/CodeGeneration.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/ui/CodeGeneration.java
@@ -17,6 +17,7 @@ import java.util.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.dltk.core.*;
import org.eclipse.dltk.evaluation.types.AmbiguousType;
import org.eclipse.dltk.evaluation.types.MultiTypeType;
@@ -25,8 +26,12 @@ import org.eclipse.dltk.ti.types.IEvaluatedType;
import org.eclipse.dltk.ui.ScriptElementLabels;
import org.eclipse.php.core.ast.nodes.*;
import org.eclipse.php.core.ast.visitor.AbstractVisitor;
+import org.eclipse.php.core.compiler.ast.nodes.NamespaceReference;
import org.eclipse.php.core.project.ProjectOptions;
import org.eclipse.php.internal.core.Constants;
+import org.eclipse.php.internal.core.PHPCoreConstants;
+import org.eclipse.php.internal.core.PHPCorePlugin;
+import org.eclipse.php.internal.core.typeinference.PHPClassType;
import org.eclipse.php.internal.core.typeinference.PHPSimpleTypes;
import org.eclipse.php.internal.core.typeinference.evaluators.PHPEvaluationUtils;
import org.eclipse.php.internal.ui.corext.codemanipulation.StubUtility;
@@ -583,6 +588,39 @@ public class CodeGeneration {
*/
public static String getMethodComment(IMethod method, IMethod overridden, String lineDelimiter)
throws CoreException {
+ boolean useFQN = Platform.getPreferencesService().getBoolean(PHPCorePlugin.ID,
+ PHPCoreConstants.CODEASSIST_INSERT_FULL_QUALIFIED_NAME_IN_COMMENTS, false, null);
+ return getMethodComment(method, overridden, lineDelimiter, useFQN);
+ }
+
+ /**
+ * Returns the comment for a method or constructor using the comment code
+ * templates (constructor / method / overriding method). <code>null</code>
+ * is returned if the template is empty.
+ * <p>
+ * The returned string is unformatted and not indented.
+ *
+ * @param method
+ * The method to be documented. The method must exist.
+ * @param overridden
+ * The method that will be overridden by the created method or
+ * <code>null</code> for non-overriding methods. If not
+ * <code>null</code>, the method must exist.
+ * @param lineDelimiter
+ * The line delimiter to be used.
+ * @param useFQN
+ * Generate fully-qualified type names.
+ * @return Returns the constructed comment or <code>null</code> if the
+ * comment code template is empty. The returned string is
+ * unformatted and and has no indent (formatting required).
+ * @throws CoreException
+ * Thrown when the evaluation of the code template fails.
+ * Contributed by zhaozw - bug #255204 [regression] Parameters
+ * type is not displayed in Generated element comments doc block
+ */
+ public static String getMethodComment(IMethod method, IMethod overridden, String lineDelimiter, boolean useFQN)
+ throws CoreException {
+
// FIXME - 'retType' should be initialized to null after the
// 'getReturnType will be functional, so void/c'tor will not have
// 'return' tag
@@ -719,7 +757,12 @@ public class CodeGeneration {
FormalParameter formalParameter = (FormalParameter) node;
Expression parameterType = formalParameter.getParameterType();
if (parameterType != null) {
- String typeName = ((Identifier) parameterType).getName();
+ String typeName;
+ if (useFQN) {
+ typeName = resolveFQN((Identifier) parameterType, method.getSourceModule());
+ } else {
+ typeName = ((Identifier) parameterType).getName();
+ }
parameterTypes[i++] = typeName;
continue;
}
@@ -778,6 +821,10 @@ public class CodeGeneration {
}
}
+ if (retType != null && useFQN) {
+ retType = resolveFQN(retType, method.getSourceModule(), method.getSourceRange().getOffset());
+ }
+
typeParametersTypes = resolvedBinding.getParameterTypes();
if (null != typeParametersTypes) {
@@ -841,6 +888,26 @@ public class CodeGeneration {
return program;
}
+ private static String resolveFQN(Identifier identifier, ISourceModule sourceModule) {
+ return resolveFQN(identifier.getName(), sourceModule, identifier.getStart());
+ }
+
+ private static String resolveFQN(String typeName, ISourceModule sourceModule, int sourceLocation) {
+ if (typeName.indexOf(Constants.TYPE_SEPARATOR_CHAR) >= 0) {
+ String[] typeNames = typeName.split("\\" + Constants.TYPE_SEPARATOR_CHAR); //$NON-NLS-1$
+ for (int index = typeNames.length - 1; index >= 0; index--) {
+ typeNames[index] = resolveFQN(typeNames[index], sourceModule, sourceLocation);
+ }
+ return String.join(String.valueOf(Constants.TYPE_SEPARATOR_CHAR), typeNames);
+ }
+ if (typeName.length() == 0 || typeName.charAt(0) == NamespaceReference.NAMESPACE_SEPARATOR) {
+ return typeName;
+ }
+ PHPClassType classType = PHPClassType.fromTypeName(typeName, sourceModule, sourceLocation);
+
+ return classType.getTypeName();
+ }
+
private static List<ITypeBinding> removeDuplicateTypes(ITypeBinding[] returnTypes) {
List<ITypeBinding> types = new ArrayList<>();
diff --git a/tests/org.eclipse.php.core.tests/src/org/eclipse/php/core/tests/PdttFile.java b/tests/org.eclipse.php.core.tests/src/org/eclipse/php/core/tests/PdttFile.java
index 3db7b360c..ef61fc35b 100644
--- a/tests/org.eclipse.php.core.tests/src/org/eclipse/php/core/tests/PdttFile.java
+++ b/tests/org.eclipse.php.core.tests/src/org/eclipse/php/core/tests/PdttFile.java
@@ -50,6 +50,8 @@ import org.osgi.framework.Bundle;
* Custom configuration
* --PREFERENCES--
* Eclipse preference options to set before running this test
+ * --RESTORE_PREFERENCES--
+ * Eclipse preference options to set after running this test
* --FILE--
* &lt;? some PHP code ?&gt;
* --EXPECT--
@@ -60,8 +62,9 @@ public class PdttFile {
protected enum STATES {
- TEST("--TEST--"), CONFIG("--CONFIG--"), PREFERENCES("--PREFERENCES--"), FILE("--FILE--"), EXPECT(
- "--EXPECT--"), OTHER("--OTHER--"), OTHER_FILE("--FILE([0-9]+)--");
+ TEST("--TEST--"), CONFIG("--CONFIG--"), PREFERENCES("--PREFERENCES--"), RESTORE_PREFERENCES(
+ "--RESTORE_PREFERENCES--"), FILE(
+ "--FILE--"), EXPECT("--EXPECT--"), OTHER("--OTHER--"), OTHER_FILE("--FILE([0-9]+)--");
private static class Names {
private static Map<String, STATES> map = new HashMap<>();
@@ -87,6 +90,7 @@ public class PdttFile {
private Bundle testBundle;
private Map<String, String> config = new HashMap<>();
private String preferences;
+ private String restorePreferences;
private String description;
private String file = "";
/**
@@ -178,11 +182,42 @@ public class PdttFile {
}
/**
+ * Returns the preferences to be restored after test executions (--RESTORE
+ * PREFERENCES-- section contents)
+ *
+ * @return
+ */
+ public String getRestorePreferences() {
+ return restorePreferences;
+ }
+
+ /**
* Applies Eclipse preferences specified in --PREFERENCES-- option.
*
* @throws CoreException
*/
public void applyPreferences() throws CoreException {
+ applyPreferences(this.preferences);
+ }
+
+ /**
+ * Applies Eclipse preferences specified in --RESTORE_PREFERENCES-- option.
+ *
+ * @throws CoreException
+ */
+ public void restorePreferences() throws CoreException {
+ applyPreferences(this.restorePreferences);
+ }
+
+ /**
+ * Applies Eclipse preferences specified in --PREFERENCES-- or --RESTORE
+ * PREFERENCES-- options.
+ *
+ * @param preferences
+ * The preferences to be applied
+ * @throws CoreException
+ */
+ private static void applyPreferences(String preferences) throws CoreException {
if (preferences != null) {
Platform.getPreferencesService().importPreferences(new ByteArrayInputStream(preferences.getBytes()));
}
@@ -264,7 +299,7 @@ public class PdttFile {
String line = bReader.readLine();
STATES state = null;
while (line != null) {
- if (line.matches("--[A-Z0-9]+--")) {
+ if (line.matches("--[A-Z0-9]+(_[A-Z0-9]+)*--")) {
state = parseStateLine(line);
if (state == null) {
throw new Exception("Wrong state: " + line);
@@ -286,6 +321,10 @@ public class PdttFile {
this.preferences = preferences;
}
+ public void setRestorePreferences(String preferences) {
+ this.restorePreferences = preferences;
+ }
+
public void setDescription(String description) {
this.description = description;
}
@@ -324,6 +363,10 @@ public class PdttFile {
w.println(STATES.PREFERENCES.getName());
w.println(preferences.trim());
}
+ if (restorePreferences != null) {
+ w.println(STATES.RESTORE_PREFERENCES.getName());
+ w.println(restorePreferences.trim());
+ }
w.println(STATES.FILE.getName());
w.println(file.trim());
w.println(STATES.EXPECT.getName());
@@ -407,6 +450,15 @@ public class PdttFile {
}
}
break;
+ case RESTORE_PREFERENCES:
+ if (line != null && line.length() > 0) {
+ if (restorePreferences == null) {
+ this.restorePreferences = (line + "\n");
+ } else {
+ this.restorePreferences += (line + "\n");
+ }
+ }
+ break;
default:
break;
}
diff --git a/tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/formatter/autoedit/FormatterAutoEditTests.java b/tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/formatter/autoedit/FormatterAutoEditTests.java
index 54e5b7574..f002ed977 100644
--- a/tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/formatter/autoedit/FormatterAutoEditTests.java
+++ b/tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/formatter/autoedit/FormatterAutoEditTests.java
@@ -138,6 +138,7 @@ public class FormatterAutoEditTests {
@Test
public void formatter(String fileName) throws Exception {
final PdttFile pdttFile = new PdttFile(PHPUiTests.getDefault().getBundle(), fileName);
+ pdttFile.applyPreferences();
final String cursor = getCursor(pdttFile) != null ? getCursor(pdttFile) : DEFAULT_CURSOR;
final DocumentCommand cmd = createFile(pdttFile.getFile().trim(), cursor);
final Exception[] err = new Exception[1];
@@ -159,7 +160,11 @@ public class FormatterAutoEditTests {
throw err[0];
}
// Compare contents
- PDTTUtils.assertContents(pdttFile.getExpected(), document.get());
+ try {
+ PDTTUtils.assertContents(pdttFile.getExpected(), document.get());
+ } finally {
+ pdttFile.restorePreferences();
+ }
}
protected DocumentCommand createFile(String data, String cursor) throws Exception {
diff --git a/tests/org.eclipse.php.ui.tests/workspace/phpdoc-generation/php53/fullyQualifiedNamesTest01.pdtt b/tests/org.eclipse.php.ui.tests/workspace/phpdoc-generation/php53/fullyQualifiedNamesTest01.pdtt
new file mode 100644
index 000000000..f53c50f5c
--- /dev/null
+++ b/tests/org.eclipse.php.ui.tests/workspace/phpdoc-generation/php53/fullyQualifiedNamesTest01.pdtt
@@ -0,0 +1,54 @@
+--TEST--
+Tests creating fully qualified class names.
+--PREFERENCES--
+org.eclipse.php.core/contentAssistInsertFullyQualifiedNameInCommentsCheckBox=true
+--RESTORE_PREFERENCES--
+org.eclipse.php.core/contentAssistInsertFullyQualifiedNameInCommentsCheckBox=false
+--FILE--
+<?php
+namespace TestNamespace
+{
+ use \Other\Namespace1\ClassName;
+ use \Other\Namespace2\ClassName as AliasName;
+ use \stdClass as StdClassAlias;
+ /**|
+ function test(ClassName $param1, array $param2, AliasName $param3)
+ {
+ return new StdClassAlias();
+ }
+}
+namespace Other\Namespace1
+{
+ class ClassName{}
+}
+namespace Other\Namespace2
+{
+ class ClassName{}
+}
+--EXPECT--
+<?php
+namespace TestNamespace
+{
+ use \Other\Namespace1\ClassName;
+ use \Other\Namespace2\ClassName as AliasName;
+ use \stdClass as StdClassAlias;
+ /**
+ *
+ * @param \Other\Namespace1\ClassName $param1
+ * @param array $param2
+ * @param \Other\Namespace2\ClassName $param3
+ * @return \stdClass
+ */
+ function test(ClassName $param1, array $param2, AliasName $param3)
+ {
+ return new StdClassAlias();
+ }
+}
+namespace Other\Namespace1
+{
+ class ClassName{}
+}
+namespace Other\Namespace2
+{
+ class ClassName{}
+} \ No newline at end of file

Back to the top