From 85eed6770603990135d718fe5cda096935bc25ed Mon Sep 17 00:00:00 2001 From: Junichi Yamamoto Date: Mon, 10 Jul 2023 22:50:04 +0900 Subject: [PATCH] Fix "Fix Imports" issue with declare statements #5578 - https://github.com/apache/netbeans/issues/5578 Example: ```php declareStatements) { + // e.g. + // declare(ticks=1) { + // $test = 1; // is this element in declare? + // } + for (DeclareStatement declareStatement : declareStatements) { + if (isInDeclare(offset, declareStatement)) { + return true; + } + } + return false; + } + + private static boolean isInDeclare(int offset, DeclareStatement declareStatement) { + return declareStatement.getStartOffset() < offset && offset < declareStatement.getEndOffset(); + } + + private static int getDeclareEndPosition(int offset, List declareStatements) { + for (DeclareStatement declareStatement : declareStatements) { + if (isInDeclare(offset, declareStatement)) { + return declareStatement.getEndOffset(); + } + } + return -1; + } + @CheckForNull private static ModelElement getLastUse(NamespaceScope namespaceScope, boolean group) { ModelElement offsetElement = null; diff --git a/php/php.editor/test/unit/data/testfiles/actions/testGH5578/declare03/testGH5578_declare03.php b/php/php.editor/test/unit/data/testfiles/actions/testGH5578/declare03/testGH5578_declare03.php new file mode 100644 index 000000000000..5df3a81f1e4c --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testGH5578/declare03/testGH5578_declare03.php @@ -0,0 +1,40 @@ + selections = new ArrayList<>(); + selections.add(new Selection("\\NS2\\DeclareTest2", ItemVariant.Type.CLASS)); + Options options = new Options.Builder(PhpVersion.PHP_81).build(); + performTest("class DeclareTest1 ^{", selections, true, options); + } + + public void testGH5578_declare04() throws Exception { + List selections = new ArrayList<>(); + selections.add(new Selection("\\NS2\\DeclareTest2", ItemVariant.Type.CLASS)); + Options options = new Options.Builder(PhpVersion.PHP_81).build(); + performTest("class DeclareTest1 ^{", selections, true, options); + } + + public void testGH5578_declare05() throws Exception { + List selections = new ArrayList<>(); + selections.add(new Selection("\\NS2\\DeclareTest2", ItemVariant.Type.CLASS)); + Options options = new Options.Builder(PhpVersion.PHP_81).build(); + performTest("class DeclareTest1 ^{", selections, true, options); + } + private String getTestResult(final String fileName, final String caretLine, final List selections, final boolean removeUnusedUses, final Options options) throws Exception { FileObject testFile = getTestFile(fileName);