Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Xu2017-06-06 01:04:39 -0400
committerAlex Xu2017-06-06 21:20:47 -0400
commitb779826dafe27e7003f4a3277811b6284a728de3 (patch)
tree6e5b3da6482083a64bf4914fc1d7f4af1b63bbef
parent13bcaf50e39bcf6aa15e6f4e01290b07aa5a1026 (diff)
downloadorg.eclipse.pdt-b779826dafe27e7003f4a3277811b6284a728de3.tar.gz
org.eclipse.pdt-b779826dafe27e7003f4a3277811b6284a728de3.tar.xz
org.eclipse.pdt-b779826dafe27e7003f4a3277811b6284a728de3.zip
Bug 517836 - Fix some bugs for ImportRewrite
Change-Id: Ie93983785619ed319ad1ace74b9b330114c81817 Signed-off-by: Alex Xu <ibazzi@qq.com>
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewrite.java4
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewriteAnalyzer.java58
-rw-r--r--tests/org.eclipse.php.ui.tests/META-INF/MANIFEST.MF1
-rw-r--r--tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/actions/OrganizeUseStatementsActionTests.java40
-rw-r--r--tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement08.pdtt18
-rw-r--r--tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement09.pdtt18
6 files changed, 112 insertions, 27 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewrite.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewrite.java
index d6277a545..320da4636 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewrite.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewrite.java
@@ -363,9 +363,7 @@ public final class ImportRewrite {
String curr = imports.get(i);
int res = compareImport(prefix, qualifier, name, curr);
if (res != ImportRewriteContext.RES_NAME_UNKNOWN) {
- if (res == ImportRewriteContext.RES_NAME_FOUND) {
- return res;
- }
+ return res;
}
}
return ImportRewriteContext.RES_NAME_UNKNOWN;
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewriteAnalyzer.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewriteAnalyzer.java
index 409445dbf..3b47f33ea 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewriteAnalyzer.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ImportRewriteAnalyzer.java
@@ -15,6 +15,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
@@ -25,14 +26,17 @@ import org.eclipse.dltk.core.IBuffer;
import org.eclipse.dltk.core.IScriptProject;
import org.eclipse.dltk.core.ISourceModule;
import org.eclipse.dltk.core.ModelException;
+import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.php.core.ast.nodes.*;
import org.eclipse.php.core.compiler.ast.nodes.NamespaceReference;
+import org.eclipse.php.internal.core.format.FormatterUtils;
import org.eclipse.php.internal.core.typeinference.PHPModelUtils;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;
+import org.eclipse.wst.sse.core.StructuredModelManager;
public final class ImportRewriteAnalyzer {
@@ -49,6 +53,7 @@ public final class ImportRewriteAnalyzer {
private boolean filterImplicitImports;
private Map<NamespaceDeclaration, Integer> flags = new HashMap<>();
+ private final String indentationString;
private static final int F_NEEDS_LEADING_DELIM = 2;
private static final int F_NEEDS_TRAILING_DELIM = 4;
@@ -56,6 +61,7 @@ public final class ImportRewriteAnalyzer {
public ImportRewriteAnalyzer(ISourceModule sourceModule, Program root, String[] importOrder,
Map<NamespaceDeclaration, Boolean> restoreExistingImports) {
this.sourceModule = sourceModule;
+ this.indentationString = createIndentationString(1);
this.filterImplicitImports = true;
@@ -72,6 +78,27 @@ public final class ImportRewriteAnalyzer {
}
}
+ private String createIndentationString(int indentationUnits) {
+ int tabs = indentationUnits;
+ if (tabs == 0) {
+ return ""; //$NON-NLS-1$
+ }
+ try {
+ IDocument document = StructuredModelManager.getModelManager()
+ .getModelForRead((IFile) sourceModule.getResource()).getStructuredDocument();
+ char indentationChar = FormatterUtils.getFormatterCommonPreferences().getIndentationChar(document);
+ int indentWidth = FormatterUtils.getFormatterCommonPreferences().getIndentationSize(document);
+
+ StringBuilder buffer = new StringBuilder(tabs * indentWidth);
+ for (int i = 0; i < tabs * indentWidth; i++) {
+ buffer.append(indentationChar);
+ }
+ return buffer.toString();
+ } catch (Exception e) {
+ }
+ return ""; //$NON-NLS-1$
+ }
+
private void initialize(Program root, NamespaceDeclaration namespace, String[] importOrder,
Map<NamespaceDeclaration, Boolean> restoreExistingImports) {
this.packageEntries.put(namespace, new ArrayList<>(20));
@@ -204,7 +231,7 @@ public final class ImportRewriteAnalyzer {
// the next line begin offset
if (currEndLine < nextOffsetLine) {
currEndLine++;
- nextOffset = getPostion(root, currEndLine);
+ nextOffset = getPosition(root, currEndLine);
}
currPackage.add(new ImportDeclEntry(name, statementType, null));
currOffset = nextOffset;
@@ -255,7 +282,7 @@ public final class ImportRewriteAnalyzer {
// the next line begin offset
if (currEndLine < nextOffsetLine) {
currEndLine++;
- nextOffset = getPostion(root, currEndLine);
+ nextOffset = getPosition(root, currEndLine);
}
currPackage.add(new ImportDeclEntry(name, statementType, new Region(currOffset, nextOffset - currOffset)));
currOffset = nextOffset;
@@ -263,7 +290,7 @@ public final class ImportRewriteAnalyzer {
// add a comment entry for spacing between imports
if (currEndLine < nextOffsetLine) {
- nextOffset = getPostion(root, nextOffsetLine);
+ nextOffset = getPosition(root, nextOffsetLine);
currPackage = new PackageEntry(); // create a comment package
// entry for this
@@ -524,7 +551,7 @@ public final class ImportRewriteAnalyzer {
int endPos = root.getExtendedStartPosition(last) + root.getExtendedLength(last);
int endLine = root.getLineNumber(endPos);
if (endLine > 0) {
- int nextLinePos = getPostion(root, endLine + 1);
+ int nextLinePos = getPosition(root, endLine + 1);
if (nextLinePos >= 0) {
int firstStatementPos = getFirstStatementBeginPos(root, namespace);
if (firstStatementPos != -1 && firstStatementPos < nextLinePos) {
@@ -680,6 +707,9 @@ public final class ImportRewriteAnalyzer {
private String getNewImportString(NamespaceDeclaration namespace, String importName, int importType,
String lineDelim) {
StringBuilder buf = new StringBuilder();
+ if (namespace != null && namespace.isBracketed()) {
+ buf.append(indentationString);
+ }
buf.append("use "); //$NON-NLS-1$
if (importType == UseStatement.T_FUNCTION) {
buf.append("function "); //$NON-NLS-1$
@@ -708,7 +738,7 @@ public final class ImportRewriteAnalyzer {
isAfterUseStatements = true;
}
for (Statement s : statements) {
- if (s instanceof UseStatement) {
+ if (s instanceof UseStatement || s instanceof InLineHtml) {
isAfterUseStatements = true;
continue;
}
@@ -727,18 +757,12 @@ public final class ImportRewriteAnalyzer {
private int getNamespaceNameEndPos(Program root, NamespaceDeclaration namespace) {
int flags = this.flags.get(namespace);
if (namespace != null) {
- int offset = 0;
- if (namespace.getName() == null) {
- offset = namespace.getBody().getStart();
- } else {
- NamespaceName packDecl = namespace.getName();
- offset = packDecl.getStart() + packDecl.getLength();
- }
+ int offset = namespace.getBody().getStart() + 1;
int afterPackageStatementPos = -1;
int lineNumber = root.getLineNumber(offset);
if (lineNumber >= 0) {
int lineAfterPackage = lineNumber + 1;
- afterPackageStatementPos = getPostion(root, lineAfterPackage);
+ afterPackageStatementPos = getPosition(root, lineAfterPackage);
}
if (afterPackageStatementPos < 0) {
flags |= F_NEEDS_LEADING_DELIM;
@@ -763,14 +787,14 @@ public final class ImportRewriteAnalyzer {
}
flags |= F_NEEDS_TRAILING_DELIM;
this.flags.put(namespace, flags);
- return getPostion(root, 2);
+ return getFirstStatementBeginPos(root, null);
}
- private int getPostion(Program root, int line) {
- return getPostion(root, line, 0);
+ private int getPosition(Program root, int line) {
+ return getPosition(root, line, 0);
}
- private int getPostion(Program root, int line, int column) {
+ private int getPosition(Program root, int line, int column) {
return root.getPosition(line, column) - 1;
}
diff --git a/tests/org.eclipse.php.ui.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.php.ui.tests/META-INF/MANIFEST.MF
index d5e5272bc..22b8a3fc4 100644
--- a/tests/org.eclipse.php.ui.tests/META-INF/MANIFEST.MF
+++ b/tests/org.eclipse.php.ui.tests/META-INF/MANIFEST.MF
@@ -22,3 +22,4 @@ Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Vendor: Eclipse PDT
Export-Package: org.eclipse.php.ui.tests
+Import-Package: org.eclipse.php.formatter.ui.preferences
diff --git a/tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/actions/OrganizeUseStatementsActionTests.java b/tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/actions/OrganizeUseStatementsActionTests.java
index 8ce6a5590..2d85704c8 100644
--- a/tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/actions/OrganizeUseStatementsActionTests.java
+++ b/tests/org.eclipse.php.ui.tests/src/org/eclipse/php/ui/tests/actions/OrganizeUseStatementsActionTests.java
@@ -16,6 +16,8 @@ import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -26,7 +28,8 @@ import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.preferences.DefaultScope;
+import org.eclipse.core.runtime.preferences.IScopeContext;
+import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.dltk.internal.ui.editor.EditorUtility;
import org.eclipse.php.core.PHPVersion;
import org.eclipse.php.core.tests.PdttFile;
@@ -37,8 +40,11 @@ import org.eclipse.php.core.tests.runner.PDTTList;
import org.eclipse.php.core.tests.runner.PDTTList.AfterList;
import org.eclipse.php.core.tests.runner.PDTTList.BeforeList;
import org.eclipse.php.core.tests.runner.PDTTList.Parameters;
-import org.eclipse.php.internal.core.PHPCoreConstants;
-import org.eclipse.php.internal.ui.PHPUiPlugin;
+import org.eclipse.php.formatter.core.CodeFormatterConstants;
+import org.eclipse.php.formatter.core.profiles.PHPDefaultFormatterPreferences;
+import org.eclipse.php.formatter.ui.preferences.ProfileManager;
+import org.eclipse.php.formatter.ui.preferences.ProfileManager.CustomProfile;
+import org.eclipse.php.formatter.ui.preferences.ProfileManager.Profile;
import org.eclipse.php.internal.ui.actions.OrganizeUseStatementsAction;
import org.eclipse.php.internal.ui.editor.PHPStructuredEditor;
import org.eclipse.php.ui.tests.PHPTestEditor;
@@ -69,6 +75,8 @@ public class OrganizeUseStatementsActionTests {
protected IFile[] otherFiles = new IFile[0];
protected PHPVersion phpVersion;
protected PHPStructuredEditor fEditor;
+ protected IScopeContext scopeContext;
+ protected ProfileManager profileManager;
@Parameters
public static final Map<PHPVersion, String[]> TESTS = new LinkedHashMap<PHPVersion, String[]>();
@@ -89,18 +97,35 @@ public class OrganizeUseStatementsActionTests {
@BeforeList
public void setUpSuite() throws Exception {
- project = TestUtils.createProject("Content Assist_" + this.phpVersion);
- // set auto insert to true,if there are only one proposal in the CA,it
- // will insert the proposal,so we can test CA without UI interaction
- DefaultScope.INSTANCE.getNode(PHPUiPlugin.ID).putBoolean(PHPCoreConstants.CODEASSIST_AUTOINSERT, true);
+ project = TestUtils.createProject("OrganizeUseStatements_" + this.phpVersion);
+
+ Map<String, Object> settings = new HashMap<>();
+ settings.put("indentationChar", "\t");
+ settings.put(CodeFormatterConstants.FORMATTER_INDENTATION_SIZE, "1");
+ CustomProfile profile = new CustomProfile("test", settings);
+ scopeContext = InstanceScope.INSTANCE;
+ profileManager = new ProfileManager(new ArrayList<Profile>(), scopeContext);
+ profileManager.addProfile(profile);
+ profileManager.setSelected(profile);
+ profileManager.commitChanges(scopeContext);
+
TestUtils.setProjectPHPVersion(project, phpVersion);
}
@AfterList
public void tearDownSuite() throws Exception {
+ setDefaultFormatter(scopeContext, profileManager);
TestUtils.deleteProject(project);
}
+ private static void setDefaultFormatter(IScopeContext scopeContext, ProfileManager profileManager) {
+ profileManager.clearAllSettings(scopeContext);
+ if (profileManager.getSelected().getID() != PHPDefaultFormatterPreferences.ID) {
+ profileManager.setSelected(profileManager.getProfile(PHPDefaultFormatterPreferences.ID));
+ }
+ profileManager.commitChanges(scopeContext);
+ }
+
@After
public void tearDown() throws Exception {
if (testFile != null) {
@@ -125,6 +150,7 @@ public class OrganizeUseStatementsActionTests {
@Override
public void run() {
try {
+
createFile(pdttFile.getFile(), Long.toString(System.currentTimeMillis()),
prepareOtherStreams(pdttFile));
openEditor();
diff --git a/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement08.pdtt b/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement08.pdtt
new file mode 100644
index 000000000..60371032f
--- /dev/null
+++ b/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement08.pdtt
@@ -0,0 +1,18 @@
+--TEST--
+Bug 517836 - wrong use statement position with inline html
+--FILE0--
+<?php
+namespace bar\baz
+{
+ class Target { function bar_baz(); }
+}
+--FILE--
+<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<?php
+new Target();
+--EXPECT--
+<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<?php
+use bar\baz\Target;
+
+new Target();
diff --git a/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement09.pdtt b/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement09.pdtt
new file mode 100644
index 000000000..5bececdd2
--- /dev/null
+++ b/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement09.pdtt
@@ -0,0 +1,18 @@
+--TEST--
+Bug 517836 - incorrect indentation for braketed namespace
+--FILE--
+<?php
+namespace name {
+
+ new Exception();
+}
+?>
+--EXPECT--
+<?php
+namespace name {
+
+ use Exception;
+
+ new Exception();
+}
+?> \ No newline at end of file

Back to the top