diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionHandler.java b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionHandler.java index cee2cc5167f7..26d84bea25cf 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionHandler.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionHandler.java @@ -29,6 +29,7 @@ import javax.swing.text.Document; import javax.swing.text.JTextComponent; import org.antlr.v4.runtime.Token; +import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.project.Project; import org.netbeans.modules.csl.api.CodeCompletionContext; import org.netbeans.modules.csl.api.CodeCompletionHandler; @@ -46,7 +47,6 @@ import org.netbeans.modules.php.blade.csl.elements.NamedElement; import org.netbeans.modules.php.blade.csl.elements.PhpFunctionElement; import org.netbeans.modules.php.blade.csl.elements.TagElement; -import org.netbeans.modules.php.blade.editor.completion.BladeCompletionProposal.CompletionRequest; import org.netbeans.modules.php.blade.editor.directives.CustomDirectives; import org.netbeans.modules.php.blade.editor.parser.BladeParserResult; import org.netbeans.modules.php.blade.editor.preferences.ModulePreferences; @@ -149,7 +149,7 @@ private void completePhpSnippet(final List completionProposa PhpCodeCompletionService phpCodeCompletion = new PhpCodeCompletionService(); for (CompletionProposal proposal : phpCodeCompletion.getCompletionProposal(offset, currentToken)) { String proposalPrefix = proposal.getInsertPrefix(); - if (proposalPrefix.startsWith(phpCodeCompletion.prefix)) { + if (proposalPrefix.startsWith(phpCodeCompletion.getPrefix())) { completionProposals.add(proposal); } } @@ -162,19 +162,17 @@ private void completeScopedVariables(final List completionPr FileObject fo = completionContext.getParserResult().getSnapshot().getSource().getFileObject(); if (scopedVariables != null && !scopedVariables.isEmpty()) { - CompletionRequest request = new CompletionRequest(); - request.anchorOffset = completionContext.getCaretOffset() - variablePrefix.length(); - request.carretOffset = completionContext.getCaretOffset(); - request.prefix = variablePrefix; + int anchorOffset = completionContext.getCaretOffset() - variablePrefix.length(); + if (BladeVariables.LOOP_VAR.startsWith(variablePrefix)) { String variableName = BladeVariables.LOOP_VAR; NamedElement variableElement = new NamedElement(variableName, fo, ElementType.VARIABLE); - completionProposals.add(new BladeCompletionProposal.BladeVariableItem(variableElement, request, variableName)); + completionProposals.add(new BladeCompletionProposal.BladeVariableItem(variableElement, anchorOffset, variableName)); } for (String variableName : scopedVariables) { if (variableName.startsWith(variablePrefix)) { NamedElement variableElement = new NamedElement(variableName, fo, ElementType.VARIABLE); - completionProposals.add(new BladeCompletionProposal.VariableItem(variableElement, request, variableName)); + completionProposals.add(new BladeCompletionProposal.VariableItem(variableElement, anchorOffset, variableName)); } } } @@ -192,13 +190,13 @@ private void completeBladeTags(final List completionProposal CodeCompletionContext completionContext, Token currentToken) { String tagStart = currentToken.getText(); - CompletionRequest request = completionRequest(tagStart, completionContext.getCaretOffset()); + int anchorOffset = computeAnchorOffset(tagStart, completionContext.getCaretOffset()); BladeTags tagsContainer = new BladeTags(); Tag[] tags = tagsContainer.getTags(); for (Tag tag : tags) { if (tag.openTag().startsWith(tagStart)) { TagElement tagElement = new TagElement(tag.closeTag()); - completionProposals.add(new BladeCompletionProposal.BladeTag(tagElement, request, tag)); + completionProposals.add(new BladeCompletionProposal.BladeTag(tagElement, anchorOffset, tag)); } } } @@ -208,7 +206,7 @@ private void completeDirectives(final List completionProposa String prefix = currentToken.getText(); DirectiveCompletionList completionList = new DirectiveCompletionList(); - CompletionRequest request = completionRequest(prefix, completionContext.getCaretOffset()); + int anchorOffset = computeAnchorOffset(prefix, completionContext.getCaretOffset()); for (Directive directive : completionList.getDirectives()) { String directiveName = directive.name(); @@ -216,15 +214,15 @@ private void completeDirectives(final List completionProposa DirectiveElement directiveEl = new DirectiveElement(directiveName, fo); if (directive.params()) { - completionProposals.add(new BladeCompletionProposal.DirectiveWithArg(directiveEl, request, directive)); + completionProposals.add(new BladeCompletionProposal.DirectiveWithArg(directiveEl, anchorOffset, directive)); if (!directive.endtag().isEmpty()) { - completionProposals.add(new BladeCompletionProposal.BlockDirectiveWithArg(directiveEl, request, directive)); + completionProposals.add(new BladeCompletionProposal.BlockDirectiveWithArg(directiveEl, anchorOffset, directive)); } } else { - completionProposals.add(new BladeCompletionProposal.InlineDirective(directiveEl, request, directive)); + completionProposals.add(new BladeCompletionProposal.InlineDirective(directiveEl, anchorOffset, directive)); if (!directive.endtag().isEmpty()) { - completionProposals.add(new BladeCompletionProposal.BlockDirective(directiveEl, request, directive)); + completionProposals.add(new BladeCompletionProposal.BlockDirective(directiveEl, anchorOffset, directive)); } } } @@ -236,11 +234,11 @@ private void completeDirectives(final List completionProposa public void filterDirectiveName(CustomDirectives.CustomDirective directive, FileObject file) { DirectiveElement directiveEl = new DirectiveElement(directive.name, file); if (directive.name.startsWith(prefix)) { - CompletionRequest request = completionRequest(prefix, completionContext.getCaretOffset()); + int anchorOffset = computeAnchorOffset(prefix, completionContext.getCaretOffset()); completionProposals.add( new BladeCompletionProposal.CustomDirective( directiveEl, - request, + anchorOffset, directive.name )); } @@ -317,13 +315,8 @@ public Documentation documentElement(ParserResult parserResult, ElementHandle el } return result; } - - public static CompletionRequest completionRequest(String prefix, int offset) { - CompletionRequest request = new CompletionRequest(); - request.anchorOffset = offset - prefix.length(); - request.carretOffset = offset; - request.prefix = prefix; - - return request; + + public static int computeAnchorOffset(@NonNull String prefix, int offset){ + return offset - prefix.length(); } } diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionProposal.java b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionProposal.java index 1514c460a43e..7394f019fa75 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionProposal.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionProposal.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.Set; import javax.swing.ImageIcon; +import org.netbeans.api.annotations.common.NonNull; import org.netbeans.modules.csl.api.CompletionProposal; import org.netbeans.modules.csl.api.ElementHandle; import org.netbeans.modules.csl.api.ElementKind; @@ -28,6 +29,7 @@ import org.netbeans.modules.csl.api.Modifier; import org.netbeans.modules.php.blade.csl.elements.ClassElement; import org.netbeans.modules.php.blade.editor.ResourceUtilities; +import org.netbeans.modules.php.blade.syntax.BladeDirectivesUtils; import org.netbeans.modules.php.blade.syntax.annotation.Directive; import org.netbeans.modules.php.blade.syntax.annotation.Tag; @@ -40,27 +42,27 @@ */ public class BladeCompletionProposal implements CompletionProposal { - final CompletionRequest request; - protected final ElementHandle element; - final String previewValue; - protected Directive directive; + private final ElementHandle element; + private final String previewValue; + private int anchorOffset; + private Directive directive; - public BladeCompletionProposal(ElementHandle element, CompletionRequest request, String previewValue) { + public BladeCompletionProposal(ElementHandle element, int anchorOffset, String previewValue) { this.element = element; - this.request = request; + this.anchorOffset = anchorOffset; this.previewValue = previewValue; } - public BladeCompletionProposal(ElementHandle element, CompletionRequest request, Directive directive) { + public BladeCompletionProposal(ElementHandle element, int anchorOffset, Directive directive) { this.element = element; - this.request = request; + this.anchorOffset = anchorOffset; this.previewValue = directive.name(); this.directive = directive; } @Override public int getAnchorOffset() { - return request.anchorOffset; + return anchorOffset; } @Override @@ -86,12 +88,12 @@ public int getSortPrioOverride() { @Override public String getLhsHtml(HtmlFormatter formatter) { formatter.name(getKind(), true); - formatter.appendHtml(""); - formatter.appendHtml(""); + formatter.appendHtml(""); // NOI18N + formatter.appendHtml(""); // NOI18N formatter.appendText(previewValue); - formatter.appendHtml(""); - formatter.appendHtml(""); - formatter.name(getKind(), false); + formatter.appendHtml(""); // NOI18N + formatter.appendHtml(""); // NOI18N + formatter.name(getKind(), false); // NOI18N return formatter.getText(); } @@ -141,11 +143,15 @@ public ElementKind getKind() { public boolean isSmart() { return true; } + + public Directive getDirective(){ + return directive; + } public static class PhpElementItem extends BladeCompletionProposal { - public PhpElementItem(ElementHandle element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public PhpElementItem(ElementHandle element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); } @Override @@ -165,8 +171,8 @@ public String getRhsHtml(HtmlFormatter formatter) { public static class NamespaceItem extends PhpElementItem { - public NamespaceItem(ElementHandle element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public NamespaceItem(ElementHandle element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); } @Override @@ -182,8 +188,8 @@ public int getSortPrioOverride() { public static class DirectiveItem extends BladeCompletionProposal { - public DirectiveItem(ElementHandle element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public DirectiveItem(ElementHandle element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); } } @@ -192,8 +198,8 @@ public static class ClassItem extends PhpElementItem { protected String namespace = null; - public ClassItem(ClassElement element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public ClassItem(ClassElement element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); this.namespace = element.getNamespace(); } @@ -218,9 +224,9 @@ public int getSortPrioOverride() { @Override public String getCustomInsertTemplate() { if (namespace != null && namespace.length() > 0) { - return "\\" + namespace + "\\" + element.getName(); + return "\\" + namespace + "\\" + getElement().getName(); } - return element.getName(); + return getElement().getName(); } } @@ -228,15 +234,15 @@ public static class FunctionItem extends PhpElementItem { protected final String namespace; - public FunctionItem(ElementHandle element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public FunctionItem(ElementHandle element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); this.namespace = null; } - public FunctionItem(ElementHandle element, CompletionRequest request, + public FunctionItem(ElementHandle element, int anchorOffset, String namespace, String previewValue) { - super(element, request, previewValue); + super(element, anchorOffset, previewValue); this.namespace = namespace; } @@ -262,8 +268,8 @@ public String getRhsHtml(HtmlFormatter formatter) { public static class ConstantItem extends PhpElementItem { - public ConstantItem(ElementHandle element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public ConstantItem(ElementHandle element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); } @Override @@ -275,8 +281,8 @@ public ElementKind getKind() { public static class VariableItem extends BladeCompletionProposal { - public VariableItem(ElementHandle element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public VariableItem(ElementHandle element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); } @Override @@ -288,8 +294,8 @@ public ElementKind getKind() { public static class BladeVariableItem extends BladeCompletionProposal { - public BladeVariableItem(ElementHandle element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public BladeVariableItem(ElementHandle element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); } @Override @@ -299,29 +305,22 @@ public ElementKind getKind() { @Override public String getRhsHtml(HtmlFormatter formatter) { - return "blade"; + return "blade"; // NOI18N } } - public static class CompletionRequest { - - public int anchorOffset; - public int carretOffset; - public String prefix; - } - public static class BladeTag extends BladeCompletionProposal { - protected Tag tag; + private Tag tag; - public BladeTag(ElementHandle element, CompletionRequest request, Tag tag) { - super(element, request, ""); + public BladeTag(ElementHandle element, int anchorOffset, Tag tag) { + super(element, anchorOffset, ""); // NOI18N this.tag = tag; } @Override public String getCustomInsertTemplate() { - return tag.openTag() + " ${cursor} " + tag.closeTag(); + return tag.openTag() + " ${cursor} " + tag.closeTag(); // NOI18N } @Override @@ -342,12 +341,12 @@ public int getSortPrioOverride() { public static class DirectiveProposal extends BladeCompletionProposal { - public DirectiveProposal(ElementHandle element, CompletionRequest request, Directive directive) { - super(element, request, directive); + public DirectiveProposal(ElementHandle element, int anchorOffset, Directive directive) { + super(element, anchorOffset, directive); } - public DirectiveProposal(ElementHandle element, CompletionRequest request, String previewValue) { - super(element, request, previewValue); + public DirectiveProposal(ElementHandle element, int anchorOffset, String previewValue) { + super(element, anchorOffset, previewValue); } @Override @@ -358,22 +357,22 @@ public ImageIcon getIcon() { @Override public String getRhsHtml(HtmlFormatter formatter) { - if (this.directive == null) { + if (this.getDirective() == null) { return null; } - if (directive.description().isEmpty() && !this.directive.since().isEmpty()) { - return "v" + this.directive.since(); + if (getDirective().description().isEmpty() && !getDirective().since().isEmpty()) { + return "v" + getDirective().since(); // NOI18N } - return this.directive.description(); + return getDirective().description(); } } public static class CustomDirective extends DirectiveProposal { - public CustomDirective(ElementHandle element, CompletionRequest request, String preview) { - super(element, request, preview); + public CustomDirective(ElementHandle element, int anchorOffset, String preview) { + super(element, anchorOffset, preview); } @Override @@ -381,87 +380,78 @@ public String getRhsHtml(HtmlFormatter formatter) { if (this.getElement().getFileObject() != null) { return this.getElement().getFileObject().getNameExt(); } - return "custom directive"; + return "custom directive"; // NOI18N } } public static class InlineDirective extends DirectiveProposal { - public InlineDirective(ElementHandle element, CompletionRequest request, Directive directive) { - super(element, request, directive); + public InlineDirective(ElementHandle element, int anchorOffset, Directive directive) { + super(element, anchorOffset, directive); } } public static class DirectiveWithArg extends InlineDirective { - public DirectiveWithArg(ElementHandle element, CompletionRequest request, Directive directive) { - super(element, request, directive); + public DirectiveWithArg(ElementHandle element, int anchorOffset, Directive directive) { + super(element, anchorOffset, directive); } @Override public String getCustomInsertTemplate() { String template = getName() + "($$${arg})"; switch (getName()) { - case "@include": - case "@extends": - template = getName() + "('${path}')"; - break; + case BladeDirectivesUtils.DIRECTIVE_INCLUDE, BladeDirectivesUtils.DIRECTIVE_EXTENDS -> template = getName() + "('${path}')"; // NOI18N } return template; } @Override public String getLhsHtml(HtmlFormatter formatter) { - return getName() + "()"; + return getName() + "()"; // NOI18N } } public static class BlockDirective extends DirectiveProposal { - public BlockDirective(ElementHandle element, CompletionRequest request, Directive directive) { - super(element, request, directive); + public BlockDirective(ElementHandle element, int anchorOffset, Directive directive) { + super(element, anchorOffset, directive); } @Override public String getLhsHtml(HtmlFormatter formatter) { - return getName() + " ... " + directive.endtag(); + return getName() + " ... " + getDirective().endtag(); // NOI18N } @Override public String getCustomInsertTemplate() { - return getName() + "\n ${selection} ${cursor}\n" + directive.endtag(); + return getName() + "\n ${selection} ${cursor}\n" + getDirective().endtag(); // NOI18N } } public static class BlockDirectiveWithArg extends DirectiveProposal { - public BlockDirectiveWithArg(ElementHandle element, CompletionRequest request, Directive directive) { - super(element, request, directive); + public BlockDirectiveWithArg(ElementHandle element, int anchorOffset, Directive directive) { + super(element, anchorOffset, directive); } @Override public String getLhsHtml(HtmlFormatter formatter) { - return getName() + "() ... " + directive.endtag(); + return getName() + "() ... " + getDirective().endtag(); // NOI18N } @Override public String getCustomInsertTemplate() { - String template = getName() + "($$${arg})\n ${cursor}\n" + directive.endtag(); - - switch (getName()) { - case "@foreach": // NOI18N - template = getName() + "($$${array} as $$${item})\n ${selection}${cursor}\n" + directive.endtag(); - break; - case "@section": // NOI18N - case "@session": // NOI18N - template = getName() + "('${id}')\n ${cursor}\n" + directive.endtag(); - break; - } - - return template; + return switch (getName()) { + case BladeDirectivesUtils.DIRECTIVE_FOREACH -> // NOI18N + getName() + "($$${array} as $$${item})\n ${selection}${cursor}\n" + getDirective().endtag(); // NOI18N + case BladeDirectivesUtils.DIRECTIVE_SECTION, BladeDirectivesUtils.DIRECTIVE_SESSION -> // NOI18N + getName() + "('${id}')\n ${cursor}\n" + getDirective().endtag(); // NOI18N + default -> getName() + "($$${arg})\n ${cursor}\n" + getDirective().endtag(); // NOI18N + }; } } diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionProvider.java b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionProvider.java index 4fb960ec83bd..f79e213448c8 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionProvider.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladeCompletionProvider.java @@ -62,7 +62,7 @@ */ @MimeRegistrations(value = { @MimeRegistration(mimeType = "text/html", service = CompletionProvider.class), - @MimeRegistration(mimeType = "text/x-blade", service = CompletionProvider.class) + @MimeRegistration(mimeType = BladeLanguage.MIME_TYPE, service = CompletionProvider.class) }) public class BladeCompletionProvider implements CompletionProvider { @@ -182,7 +182,7 @@ protected void query(CompletionResultSet resultSet, Document doc, int caretOffse } finally { long time = System.currentTimeMillis() - startTime; if (time > 2000) { - LOGGER.log(Level.INFO, "Slow completion time detected. {0}ms", time); + LOGGER.log(Level.INFO, "Slow completion time detected. {0}ms", time); // NOI18N } resultSet.finish(); } diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladePhpCompletionProvider.java b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladePhpCompletionProvider.java index 1234989407b6..58799632cac4 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladePhpCompletionProvider.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/BladePhpCompletionProvider.java @@ -31,7 +31,9 @@ import org.netbeans.api.editor.document.EditorDocumentUtils; import org.netbeans.api.editor.mimelookup.MimeRegistration; import org.netbeans.api.editor.mimelookup.MimeRegistrations; +import static org.netbeans.modules.php.blade.syntax.antlr4.v10.BladeAntlrLexer.*; import org.netbeans.api.project.Project; +import org.netbeans.modules.php.api.util.FileUtils; import org.netbeans.modules.php.blade.editor.BladeLanguage; import org.netbeans.modules.php.blade.editor.EditorStringUtils; import org.netbeans.modules.php.blade.editor.ResourceUtilities; @@ -40,7 +42,6 @@ import org.netbeans.modules.php.blade.editor.path.BladePathUtils; import org.netbeans.modules.php.blade.project.ProjectUtils; import org.netbeans.modules.php.blade.syntax.antlr4.v10.BladeAntlrLexer; -import static org.netbeans.modules.php.blade.syntax.antlr4.v10.BladeAntlrLexer.*; import org.netbeans.modules.php.blade.syntax.antlr4.v10.BladeAntlrUtils; import org.netbeans.spi.editor.completion.CompletionItem; import org.netbeans.spi.editor.completion.CompletionProvider; @@ -58,7 +59,7 @@ * @author bhaidu */ @MimeRegistrations(value = { - @MimeRegistration(mimeType = "text/x-php5", service = CompletionProvider.class, position = 102), + @MimeRegistration(mimeType = FileUtils.PHP_MIME_TYPE, service = CompletionProvider.class, position = 102), } ) public class BladePhpCompletionProvider implements CompletionProvider { @@ -99,15 +100,10 @@ public int getAutoQueryTypes(JTextComponent component, String typedText) { } char lastChar = typedText.charAt(typedText.length() - 1); - switch (lastChar) { - case ')': - case '\n': - case '<': - case '>': - return 0; - } - - return COMPLETION_QUERY_TYPE; + return switch (lastChar) { + case ')', '\n', '<', '>' -> 0; + default -> COMPLETION_QUERY_TYPE; + }; } private class BladeCompletionQuery extends AsyncCompletionQuery { @@ -121,7 +117,7 @@ protected void query(CompletionResultSet resultSet, Document doc, int caretOffse doQuery(resultSet, doc, caretOffset); long time = System.currentTimeMillis() - startTime; if (time > 2000) { - LOGGER.log(Level.INFO, "Slow completion time detected. {0}ms", time); + LOGGER.log(Level.INFO, "Slow completion time detected. {0}ms", time); // NOI18N } resultSet.finish(); } @@ -212,7 +208,7 @@ private void doQuery(CompletionResultSet resultSet, Document doc, int caretOffse for (FileObject file : childrenFiles) { String pathFileName = file.getName(); if (!file.isFolder()) { - pathFileName = pathFileName.replace(".blade", ""); + pathFileName = pathFileName.replace(".blade", ""); // NOI18N } completeBladePath(pathFileName, file, pathOffset, resultSet); } @@ -232,10 +228,8 @@ private void doQuery(CompletionResultSet resultSet, Document doc, int caretOffse case EXPR_STRING: { String pathName = EditorStringUtils.stripSurroundingQuotes(currentToken.getText()); - if (!pathName.contains("resources")) { - if (!"resources".startsWith(pathName)) { - break; - } + if (!pathName.contains(BladePathUtils.LARAVEL_RESOURCES)) { + break; } int lastSlash = pathName.lastIndexOf("/"); diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/PhpCodeCompletionService.java b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/PhpCodeCompletionService.java index 1d3c146ee7f1..f91f70e8cdbd 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/PhpCodeCompletionService.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/editor/completion/PhpCodeCompletionService.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.List; import org.antlr.v4.runtime.Token; +import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.project.Project; import org.netbeans.modules.csl.api.CodeCompletionContext; import org.netbeans.modules.csl.api.CodeCompletionHandler; @@ -33,7 +34,6 @@ import org.netbeans.modules.php.blade.csl.elements.ElementType; import org.netbeans.modules.php.blade.csl.elements.NamedElement; import org.netbeans.modules.php.blade.editor.EditorStringUtils; -import static org.netbeans.modules.php.blade.editor.completion.BladeCompletionHandler.completionRequest; import org.netbeans.modules.php.blade.editor.indexing.PhpIndexFunctionResult; import org.netbeans.modules.php.blade.editor.indexing.PhpIndexResult; import org.netbeans.modules.php.blade.editor.indexing.PhpIndexUtils; @@ -50,9 +50,13 @@ * * @author bogdan */ -public class PhpCodeCompletionService { +public final class PhpCodeCompletionService { - public String prefix = ""; + private String prefix = ""; + + public String getPrefix() { + return prefix; + } public List getCompletionProposal(int offset, Token currentToken) { List proposals = new ArrayList<>(); @@ -100,7 +104,7 @@ public static void completePhpCode(final List completionProp BladeParserResult.FieldAccessReference fieldAccessReference = parserResult.findFieldAccessRefrence(offset); FileObject fo = parserResult.getSnapshot().getSource().getFileObject(); - + if (fieldAccessReference != null) { completeClassConstants(prefix, fieldAccessReference.ownerClass.identifier, offset, completionProposals, fo); completeClassMethods(prefix, fieldAccessReference, offset, completionProposals, fo); @@ -139,42 +143,41 @@ public static void completePhpCode(final List completionProp Collection indexedNamespaces = PhpIndexUtils.queryNamespaces( fo, namespacePath, QuerySupport.Kind.PREFIX ); - BladeCompletionProposal.CompletionRequest request = completionRequest(prefix, offset); + + int anchorOffset = computeAnchorOffset(prefix, offset); for (PhpIndexResult indexResult : indexedNamespaces) { completionProposals.add(new BladeCompletionProposal.NamespaceItem( - namespaceElement(indexResult), request, indexResult.name)); + namespaceElement(indexResult), anchorOffset, indexResult.name)); } } else if (prefix.endsWith("\\")) { //the identifier is the namespace int substringOffset = elementReference.identifier.startsWith("\\") ? 1 : 0; String namespacePath = elementReference.identifier.substring(substringOffset, elementReference.identifier.length() - 1); Collection indexClassResults = PhpIndexUtils.queryAllNamespaceClasses(fo, namespacePath); - BladeCompletionProposal.CompletionRequest request = completionRequest(namespacePath, offset + namespacePath.length()); + int anchorOffset = computeAnchorOffset(namespacePath, offset + namespacePath.length()); for (PhpIndexResult indexResult : indexClassResults) { - completionProposals.add(new BladeCompletionProposal.ClassItem(classElement(indexResult), request, indexResult.name)); + completionProposals.add(new BladeCompletionProposal.ClassItem(classElement(indexResult), anchorOffset, indexResult.name)); } - //completeNamespacedPhpClasses("", namespace, offset, completionProposals, parserResult); } break; } } - private static void completePhpClasses(String prefix, int offset, + private static void completePhpClasses(String prefix, int offset, final List completionProposals, - FileObject fo) - { + FileObject fo) { Collection indexClassResults = PhpIndexUtils.queryClass(fo, prefix); if (indexClassResults.isEmpty()) { return; } - BladeCompletionProposal.CompletionRequest request = completionRequest(prefix, offset); + int anchorOffset = computeAnchorOffset(prefix, offset); for (PhpIndexResult indexResult : indexClassResults) { completionProposals.add(new BladeCompletionProposal.ClassItem( - classElement(indexResult), request, indexResult.name)); + classElement(indexResult), anchorOffset, indexResult.name)); } } @@ -186,14 +189,14 @@ private static void completePhpFunctions(String prefix, int offset, if (indexedFunctions.isEmpty()) { return; } - BladeCompletionProposal.CompletionRequest request = completionRequest(prefix, offset); + int anchorOffset = computeAnchorOffset(prefix, offset); for (PhpIndexFunctionResult indexResult : indexedFunctions) { //to be completed //might add syntax completion cursor String preview = indexResult.name + indexResult.getParamsAsString(); completionProposals.add(new BladeCompletionProposal.FunctionItem( functionElement(indexResult), - request, + anchorOffset, preview) ); } @@ -219,7 +222,7 @@ private static void completeClassMethods(String prefix, BladeParserResult.FieldA return; } - BladeCompletionProposal.CompletionRequest request = completionRequest(prefix, offset); + int anchorOffset = computeAnchorOffset(prefix, offset); for (PhpIndexFunctionResult indexResult : indexedFunctions) { //to be completed @@ -227,7 +230,7 @@ private static void completeClassMethods(String prefix, BladeParserResult.FieldA String preview = indexResult.name + indexResult.getParamsAsString(); completionProposals.add(new BladeCompletionProposal.FunctionItem( functionElement(indexResult), - request, + anchorOffset, indexResult.getClassNamespace(), preview) ); @@ -256,34 +259,33 @@ private static void completeNamespace(String prefix, int offset, return; } - BladeCompletionProposal.CompletionRequest request = completionRequest(prefix, offset + substringOffset); + int anchorOffset = computeAnchorOffset(prefix, offset + substringOffset); for (PhpIndexResult indexResult : indexClassResults) { completionProposals.add(new BladeCompletionProposal.NamespaceItem( - namespaceElement(indexResult), request, indexResult.name)); + namespaceElement(indexResult), anchorOffset, indexResult.name)); } } private static void completeConstants(String prefix, int offset, final List completionProposals, - FileObject fo) - { + FileObject fo) { Collection indexClassResults = PhpIndexUtils.queryConstants(fo, prefix); //treat only uppercase strings - if (!Character.isUpperCase(prefix.charAt(0))){ + if (!Character.isUpperCase(prefix.charAt(0))) { return; } - + if (indexClassResults.isEmpty()) { return; } - BladeCompletionProposal.CompletionRequest request = completionRequest(prefix, offset); + int anchorOffset = computeAnchorOffset(prefix, offset); for (PhpIndexResult indexResult : indexClassResults) { completionProposals.add(new BladeCompletionProposal.ConstantItem( - constantElement(indexResult), request, indexResult.name)); + constantElement(indexResult), anchorOffset, indexResult.name)); } } @@ -295,30 +297,29 @@ private static void completeClassConstants(String prefix, String ownerClass, int fo, prefix, ownerClass); //treat only uppercase strings - if (!Character.isUpperCase(prefix.charAt(0))){ + if (!Character.isUpperCase(prefix.charAt(0))) { return; } - + if (indexClassResults.isEmpty()) { return; } - BladeCompletionProposal.CompletionRequest request = completionRequest(prefix, offset); + int anchorOffset = computeAnchorOffset(prefix, offset); for (PhpIndexResult indexResult : indexClassResults) { completionProposals.add(new BladeCompletionProposal.ConstantItem( - constantElement(indexResult), request, indexResult.name)); + constantElement(indexResult), anchorOffset, indexResult.name)); } } //TODO might move in a factory for NamedElement - private static ClassElement classElement(PhpIndexResult indexResult) { return new ClassElement(indexResult.name, indexResult.namespace, indexResult.declarationFile); } - + private static NamedElement namespaceElement(PhpIndexResult indexResult) { return namedElement(indexResult, ElementType.PHP_NAMESPACE); } @@ -331,12 +332,16 @@ private static NamedElement functionElement(PhpIndexResult indexResult) { private static NamedElement constantElement(PhpIndexResult indexResult) { return namedElement(indexResult, ElementType.PHP_CONSTANT); } - + private static NamedElement namedElement(PhpIndexResult indexResult, ElementType type) { return new NamedElement(indexResult.name, indexResult.declarationFile, type); } - + private static NamedElement namedElement(String preview, PhpIndexResult indexResult, ElementType type) { return new NamedElement(preview, indexResult.declarationFile, type); } + + public static int computeAnchorOffset(@NonNull String prefix, int offset) { + return offset - prefix.length(); + } } diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/editor/embedding/BladePhpEmbeddingProvider.java b/php/php.blade/src/org/netbeans/modules/php/blade/editor/embedding/BladePhpEmbeddingProvider.java index 7d88ddc83b16..7493ad3274bd 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/editor/embedding/BladePhpEmbeddingProvider.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/editor/embedding/BladePhpEmbeddingProvider.java @@ -28,6 +28,7 @@ import org.netbeans.modules.parsing.api.Embedding; import org.netbeans.modules.parsing.api.Snapshot; import org.netbeans.modules.parsing.spi.EmbeddingProvider; +import org.netbeans.modules.php.api.util.FileUtils; import org.netbeans.modules.php.blade.editor.BladeLanguage; import org.netbeans.modules.php.blade.editor.lexer.BladeTokenId; /** @@ -36,9 +37,9 @@ */ @EmbeddingProvider.Registration( mimeType = BladeLanguage.MIME_TYPE, - targetMimeType = "text/x-php5") + targetMimeType = FileUtils.PHP_MIME_TYPE) public class BladePhpEmbeddingProvider extends EmbeddingProvider { - public static final String TARGET_MIME_TYPE = "text/x-php5"; //NOI18N + public static final String TARGET_MIME_TYPE = FileUtils.PHP_MIME_TYPE; //NOI18N @Override public List getEmbeddings(final Snapshot snapshot) { diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/editor/parser/ParsingUtils.java b/php/php.blade/src/org/netbeans/modules/php/blade/editor/parser/ParsingUtils.java index 16c6fd6e8e66..7d9e2974419d 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/editor/parser/ParsingUtils.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/editor/parser/ParsingUtils.java @@ -34,6 +34,7 @@ import org.netbeans.modules.parsing.api.UserTask; import org.netbeans.modules.parsing.spi.ParseException; import org.netbeans.modules.parsing.spi.Parser; +import org.netbeans.modules.php.api.util.FileUtils; import org.netbeans.modules.php.editor.parser.PHPParseResult; import org.openide.filesystems.AbstractFileSystem; import org.openide.filesystems.DefaultAttributes; @@ -299,7 +300,7 @@ public boolean readOnly(String name) { @Override public String mimeType(String name) { - return "text/x-php5"; + return FileUtils.PHP_MIME_TYPE; } @Override diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/editor/path/BladePathUtils.java b/php/php.blade/src/org/netbeans/modules/php/blade/editor/path/BladePathUtils.java index 6d660dbb8e3d..05d10e00d11a 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/editor/path/BladePathUtils.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/editor/path/BladePathUtils.java @@ -28,6 +28,7 @@ import org.netbeans.api.project.Project; import org.netbeans.modules.php.blade.project.BladeProjectProperties; import org.netbeans.modules.php.blade.project.ProjectUtils; +import org.netbeans.modules.php.blade.syntax.StringUtils; import org.netbeans.spi.project.ui.support.ProjectConvertors; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; @@ -38,6 +39,7 @@ */ public final class BladePathUtils { + public static final String LARAVEL_RESOURCES = "resources"; //NOI18N public static final String LARAVEL_VIEW_PATH = "resources/views"; //NOI18N public static final String BLADE_EXT = ".blade.php"; //NOI18N @@ -133,11 +135,11 @@ public static List getParentChildrenFromPrefixPath(FileObject contex return list; } - String unixPath = prefixViewPath.replace(".", "/"); + String unixPath = prefixViewPath.replace(StringUtils.DOT, StringUtils.FORWARD_SLASH); int relativeSlash; //fix issues with lastIndexOf search - relativeSlash = unixPath.lastIndexOf("/"); + relativeSlash = unixPath.lastIndexOf(StringUtils.FORWARD_SLASH); HashSet filteredViewRoots = new HashSet<>(); @@ -179,7 +181,7 @@ public static List getParentChildrenFromPrefixPath(FileObject contex //filter by filename in relative context for (FileObject rootFolder : filteredViewRoots) { for (FileObject file : rootFolder.getChildren()) { - String filePath = file.getPath().replace(rootFolder.getPath() + "/", ""); + String filePath = file.getPath().replace(rootFolder.getPath() + StringUtils.FORWARD_SLASH, ""); if (filePath.startsWith(relativePrefixToCompare)) { list.add(file); } @@ -269,7 +271,7 @@ public static String toBladeViewPath(FileObject file) { String viewFileAbsPath = viewFile.getPath(); if (filePath.startsWith(viewFileAbsPath)) { String relativePath = filePath.replace(viewFileAbsPath, ""); - if (!relativePath.startsWith("/")) { + if (!relativePath.startsWith(StringUtils.FORWARD_SLASH)) { //it doesn't belong to the folder continue; } @@ -298,11 +300,11 @@ public static String getRelativeProjectPath(FileObject currentFile) { } public static String toBladeViewPath(String filePath) { - return filePath.replace(BLADE_EXT, "").replace("/", "."); + return filePath.replace(BLADE_EXT, "").replace(StringUtils.FORWARD_SLASH, StringUtils.DOT); } public static String viewPathToFilePath(String viewPath) { - return viewPath.replace(".", "/") + BLADE_EXT; + return viewPath.replace(StringUtils.DOT, StringUtils.FORWARD_SLASH) + BLADE_EXT; } public static HashSet getDefaultRoots(Project project) { @@ -317,6 +319,6 @@ public static HashSet getDefaultRoots(Project project) { } public static String toBladeToProjectFilePath(String path) { - return LARAVEL_VIEW_PATH + "/" + viewPathToFilePath(path); + return LARAVEL_VIEW_PATH + StringUtils.FORWARD_SLASH + viewPathToFilePath(path); } } diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/syntax/BladeDirectivesUtils.java b/php/php.blade/src/org/netbeans/modules/php/blade/syntax/BladeDirectivesUtils.java index 40f5c037c103..ceaad59fc130 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/syntax/BladeDirectivesUtils.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/syntax/BladeDirectivesUtils.java @@ -41,6 +41,7 @@ public final class BladeDirectivesUtils { public static final String DIRECTIVE_FOREACH = "@foreach"; // NOI18N public static final String DIRECTIVE_INCLUDE = "@include"; // NOI18N public static final String DIRECTIVE_EXTENDS = "@extends"; // NOI18N + public static final String DIRECTIVE_SESSION = "@session"; // NOI18N public static String[] directiveStart2EndPair(String directive){ if (directive.equals(DIRECTIVE_SECTION)){ diff --git a/php/php.blade/src/org/netbeans/modules/php/blade/syntax/StringUtils.java b/php/php.blade/src/org/netbeans/modules/php/blade/syntax/StringUtils.java index 42b6b9eda961..bfd222d49388 100644 --- a/php/php.blade/src/org/netbeans/modules/php/blade/syntax/StringUtils.java +++ b/php/php.blade/src/org/netbeans/modules/php/blade/syntax/StringUtils.java @@ -24,6 +24,9 @@ */ public final class StringUtils { + public static final String DOT = "."; + public static final String FORWARD_SLASH = "/"; //NOI18N + private StringUtils() { }