Skip to content

Fixed #1631: PhpConstGotoCompletionProvider throws IndexOutOfBoundsException when cursor is before scope operator #1645

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

Merged
merged 3 commits into from
May 18, 2021
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 @@ -42,12 +42,16 @@ public void getLookupElements(@NotNull GotoCompletionProviderLookupArguments arg
PhpIndex phpIndex = PhpIndex.getInstance(this.getProject());
CompletionResultSet resultSet = arguments.getResultSet();

final String prefix = getElement().getText().replace(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED, "");
var elementText = getElement().getText();
var scopeOperatorPos = elementText.indexOf(SCOPE_OPERATOR);
var cursorPos = elementText.indexOf(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED);

// Class constants: !php/const Foo::<caret>
if (prefix.contains(SCOPE_OPERATOR)) {
String classFQN = prefix.substring(0, getElement().getText().indexOf(SCOPE_OPERATOR));
PhpClass phpClass = PhpElementsUtil.getClassInterface(this.getProject(), classFQN);
if (scopeOperatorPos > -1 && scopeOperatorPos < cursorPos) {
var prefix = elementText.replace(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED, "");
var classFQN = prefix.substring(0, scopeOperatorPos);
var phpClass = PhpElementsUtil.getClassInterface(this.getProject(), classFQN);

if (phpClass != null) {
// reset the prefix matcher, starting after ::
resultSet = resultSet.withPrefixMatcher(prefix.substring(prefix.indexOf(SCOPE_OPERATOR) + 2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,35 @@ public void testThatDecoratesPrioritizeLookupElementOnInstance() {
lookupElement -> "foo".equals(lookupElement.getItemText()) && lookupElement.isItemTextBold() && lookupElement.isItemTextBold()
);
}

public void testThatPhpConstAreCompletedAndNavigable() {
assertCompletionContains(
YAMLFileType.YML,
"services:\n" +
" foo:\n" +
" class: Foo\\Foobar\n" +
" arguments: \n" +
" - !php/const <caret>\n",
"Bar", "Foobar"
);

assertCompletionContains(
YAMLFileType.YML,
"services:\n" +
" foo:\n" +
" class: Foo\\Foobar\n" +
" arguments: \n" +
" - !php/const Foo\\Bar::<caret>\n",
"BAZ"
);

assertCompletionIsEmpty(
YAMLFileType.YML,
"services:\n" +
" foo:\n" +
" class: Foo\\Foobar\n" +
" arguments: \n" +
" - !php/const Foo\\Bar<caret>::BAZ\n"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
{
class Bar
{
public const BAZ = 'baz';

public function create() {}
}

class Foobar extends Bar
{
public function __construct($arg = null) {}
}
}