Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect code completion for use function #7041 #7054

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ public static boolean insertOnlyMethodsName(CompletionRequest request) {
if (request.insertOnlyMethodsName != null) {
return request.insertOnlyMethodsName;
}
if (isUseFunctionContext(request.context)) {
// GH-7041
// e.g. use function Vendor\Package\myFunction;
return true;
}
boolean result = false;
TokenHierarchy<?> tokenHierarchy = request.result.getSnapshot().getTokenHierarchy();
TokenSequence<PHPTokenId> tokenSequence = (TokenSequence<PHPTokenId>) tokenHierarchy.tokenSequence();
Expand Down Expand Up @@ -595,6 +600,11 @@ public static boolean insertOnlyMethodsName(CompletionRequest request) {
return result;
}

private static boolean isUseFunctionContext(CompletionContext context) {
return context == CompletionContext.USE_FUNCTION_KEYWORD
|| context == CompletionContext.GROUP_USE_FUNCTION_KEYWORD;
}

private boolean isNewClassContext(CompletionContext context) {
return context.equals(CompletionContext.NEW_CLASS)
|| context.equals(CompletionContext.THROW_NEW)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace GH7041;

function gh7041_01(): void {}
function gh7041_02(int $param): void {}

namespace UseFunctionTest;

use function GH7041\{
// test
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name: gh7041_01
gh7041_01

Name: gh7041_02
gh7041_02
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace GH7041;

function gh7041_01(): void {}
function gh7041_02(int $param): void {}

namespace UseFunctionTest;

use function GH7041\

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name: gh7041_01
GH7041\gh7041_01

Name: gh7041_02
GH7041\gh7041_02
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ protected Map<String, ClassPath> createClassPathsForTest() {
);
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath(String fileName) {
return String.format("testfiles/completion/lib/php71/%s/%s.php", getTestDirName(), fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ protected Map<String, ClassPath> createClassPathsForTest() {
);
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath(String fileName) {
return String.format("testfiles/completion/lib/php72/%s/%s.php", getTestDirName(), fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ protected Map<String, ClassPath> createClassPathsForTest() {
);
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath(String fileName) {
return String.format("testfiles/completion/lib/php74/%s/%s.php", getTestDirName(), fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ protected FileObject[] createSourceClassPathsForTest() {
return new FileObject[]{FileUtil.toFileObject(new File(getDataDir(), "/testfiles/completion/lib/php80/" + getTestDirName()))};
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath() {
return String.format("testfiles/completion/lib/php80/%s/%s.php", getTestDirName(), getTestDirName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ protected Map<String, ClassPath> createClassPathsForTest() {
);
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath(String fileName) {
return String.format("testfiles/completion/lib/php81/%s/%s.php", getTestDirName(), fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ protected Map<String, ClassPath> createClassPathsForTest() {
);
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath(String fileName) {
return String.format("testfiles/completion/lib/php82/%s/%s.php", getTestDirName(), fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ protected Map<String, ClassPath> createClassPathsForTest() {
);
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath(String fileName) {
return String.format("testfiles/completion/lib/php83/%s/%s.php", getTestDirName(), fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ protected FileObject[] createSourceClassPathsForTest() {
return new FileObject[]{FileUtil.toFileObject(new File(getDataDir(), "/testfiles/completion/lib/autoImport/" + getTestDirName()))};
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath() {
return String.format("testfiles/completion/lib/autoImport/%s/%s.php", getTestDirName(), getTestDirName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.php.editor.completion;

import java.io.File;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;

public class PHPCodeCompletionGH7041Test extends PHPCodeCompletionTestBase {

public PHPCodeCompletionGH7041Test(String testName) {
super(testName);
}

@Override
protected FileObject[] createSourceClassPathsForTest() {
return new FileObject[]{FileUtil.toFileObject(new File(getDataDir(), "/testfiles/completion/lib/gh7041/" + getTestDirName()))};
}

private String getTestPath() {
return String.format("testfiles/completion/lib/gh7041/%s/%s.php", getTestDirName(), getTestDirName());
}

public void testUseFunction01() throws Exception {
checkCompletionCustomTemplateResult(getTestPath(), "use function GH7041\\^", null, true);
}

public void testGroupUseFunction01() throws Exception {
checkCompletionCustomTemplateResult(getTestPath(), " ^// test", null, true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,6 @@ protected Map<String, ClassPath> createClassPathsForTest() {
);
}

private String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

private String getTestPath(String fileName) {
return String.format("testfiles/completion/lib/magicMethods/%s/%s.php", getTestDirName(), fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ protected FileObject[] createSourceClassPathsForTest() {
return null;
}

protected String getTestDirName() {
String name = getName();
int indexOf = name.indexOf("_");
if (indexOf != -1) {
name = name.substring(0, indexOf);
}
return name;
}

protected void checkCompletionCustomTemplateResult(final String file, final String caretLine, CompletionProposalFilter filter, boolean checkAllItems) throws Exception {
checkCompletionCustomTemplateResult(file, caretLine, filter, checkAllItems, null);
}
Expand Down
Loading