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

{if !inject:beanName}{/if} is marked as invalid #835

Merged
merged 1 commit into from
Mar 29, 2023
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 @@ -13,6 +13,9 @@

import com.redhat.qute.parser.expression.Parts.PartKind;
import com.redhat.qute.parser.template.ASTVisitor;
import com.redhat.qute.parser.template.Parameter;
import com.redhat.qute.parser.template.Section;
import com.redhat.qute.parser.template.SectionKind;

/**
* Namespace part.
Expand All @@ -28,6 +31,8 @@ public class NamespacePart extends Part {

public static final String DATA_NAMESPACE = "data";

private int startName = -1;

public NamespacePart(int start, int end) {
super(start, end);
}
Expand All @@ -36,6 +41,27 @@ public NamespacePart(int start, int end) {
public PartKind getPartKind() {
return PartKind.Namespace;
}

@Override
public int getStartName() {
if (startName != -1) {
return startName;
}
startName = super.getStartName();
Parameter parameter = getOwnerParameter();
if (parameter != null) {
Section section = parameter.getOwnerSection();
if (section != null && section.getSectionKind() == SectionKind.IF) {
String text = getOwnerTemplate().getText();
if (text.charAt(startName) == '!') {
// ex : !inject:
// !inject --> inject
startName++;
datho7561 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
return startName;
}

@Override
protected void accept0(ASTVisitor visitor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import com.redhat.qute.parser.expression.Parts.PartKind;
import com.redhat.qute.parser.template.ASTVisitor;
import com.redhat.qute.parser.template.Expression;
import com.redhat.qute.parser.template.JavaTypeInfoProvider;
import com.redhat.qute.parser.template.Parameter;
import com.redhat.qute.parser.template.Section;
Expand Down Expand Up @@ -148,24 +147,6 @@ protected boolean canBeOptional() {
return true;
}

/**
* Returns the owner parameter of the object part and null otherwise.
*
* <p>
* {#if foo?? }
* </p>
*
* <p>
* {#let foo='bar' }
* </p>
*
* @return the owner parameter of the object part and null otherwise.
*/
public Parameter getOwnerParameter() {
Expression expression = getParent().getParent();
return expression != null ? expression.getOwnerParameter() : null;
}

@Override
protected void accept0(ASTVisitor visitor) {
visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.redhat.qute.parser.template.Expression;
import com.redhat.qute.parser.template.Node;
import com.redhat.qute.parser.template.NodeKind;
import com.redhat.qute.parser.template.Parameter;
import com.redhat.qute.parser.template.Section;

/**
Expand Down Expand Up @@ -198,6 +199,23 @@ public String toString() {
return getPartName();
}

/**
* Returns the owner parameter of the object part and null otherwise.
*
* <p>
* {#if foo?? }
* </p>
*
* <p>
* {#let foo='bar' }
* </p>
*
* @return the owner parameter of the object part and null otherwise.
*/
public Parameter getOwnerParameter() {
Expression expression = getParent().getParent();
return expression != null ? expression.getOwnerParameter() : null;
}
/**
* Returns the part kind.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,9 @@ private CompletableFuture<CompletionList> doCompleteExpressionForObjectPart(Comp
QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings,
QuteNativeSettings nativeImagesSettings, CancelChecker cancelChecker) {
// Completion for root object
int partStart = part != null && part.getKind() != NodeKind.Text ? part.getStart() : offset;
int partStart = part != null && part.getKind() != NodeKind.Text
? ((part.getKind() == NodeKind.ExpressionPart) ? ((Part) part).getStartName() : part.getStart())
: offset;
int partEnd = part != null && part.getKind() != NodeKind.Text ? part.getEnd() : offset;
Range range = QutePositionUtility.createRange(partStart, partEnd, template);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private ServerCapabilitiesConstants() {
/* Default Options */
public static final CodeLensOptions DEFAULT_CODELENS_OPTIONS = new CodeLensOptions();
public static final CompletionOptions DEFAULT_COMPLETION_OPTIONS = new CompletionOptions(false,
Arrays.asList("{", "@", "#", ".", ":", "$"));
Arrays.asList("{", "@", "#", ".", ":", "$", "!"));
public static final DocumentLinkOptions DEFAULT_DOCUMENT_LINK_OPTIONS = new DocumentLinkOptions(true);
public static final CodeActionOptions DEFAULT_CODE_ACTION_OPTIONS = new CodeActionOptions();
static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ public void optionalParameter() throws Exception {
c("foo", "foo", r(2, 2, 2, 2)), //
c("item", "item", r(2, 2, 2, 2)));
}

@Test
public void objectPartAfterNot() throws Exception {
String template = "{@org.acme.Item item}\r\n" + //
"{#if !|}";
testCompletionFor(template, //
c("item", "item", r(1, 6, 1, 6)));

template = "{@org.acme.Item item}\r\n" + //
"{#if abcd != '' && !|it }";
testCompletionFor(template, //
c("item", "item", r(1, 20, 1, 22)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,15 @@ public void conditionWithMethod() throws Exception {
}

@Test
public void notOperator() {
public void notOperatorWithObjectPart() {
String template = "{#if !true}NOK{#else}OK{/if}";
testDiagnosticsFor(template);

Diagnostic d = d(0, 6, 0, 9, QuteErrorCode.UndefinedObject, "`foo` cannot be resolved to an object.",
DiagnosticSeverity.Warning);

template = "{#if !foo}NOK{#else}OK{/if}";
testDiagnosticsFor(template, d);
testDiagnosticsFor(template, d(0, 6, 0, 9, QuteErrorCode.UndefinedObject, "`foo` cannot be resolved to an object.",
DiagnosticSeverity.Warning));
}

@Test
public void onlyNotOperator() {
String template = "{#if !}{/if}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ public void cdi() {
template = "{cdi:bean.isEmpty()}";
testDiagnosticsFor(template);
}

@Test
public void notOperatorWithNamespacePart() {
String template = "{#if !cdi:bean}NOK{#else}OK{/if}";
testDiagnosticsFor(template);

template = "{#if !cdi:foo}NOK{#else}OK{/if}";
testDiagnosticsFor(template, d(0, 10, 0, 13, QuteErrorCode.UndefinedObject, "`foo` cannot be resolved to an object.",
DiagnosticSeverity.Warning));

template = "{#if !foo:bar}NOK{#else}OK{/if}";
testDiagnosticsFor(template, d(0, 6, 0, 9, QuteErrorCode.UndefinedNamespace, "No namespace resolver found for: `foo`.",
DiagnosticSeverity.Warning));
}

@Test
public void badNamespace() throws Exception {
Expand Down