Skip to content

Commit

Permalink
Fix set_by_lua_block syntax error
Browse files Browse the repository at this point in the history
Lua contexts were assumed to not have any variables and so the parser
would expect and opening brace right after the context declaration.
This of course does work with set_by_lua_block, so the fix will parse
any variables before starting the Lua block.

This fix is a small hack to not change NginxVariableReferece@L41
(https://github.com/Deadleg/idea-nginx/blob/6e0239cec3360de241c088660d1185b9c7821711/src/net/ishchenko/idea/nginx/psi/NginxVariableReference.java#L41)

Fixes #55
  • Loading branch information
Deadleg committed Sep 9, 2017
1 parent 6e0239c commit 056598d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/net/ishchenko/idea/nginx/parser/NginxParser.java
Expand Up @@ -210,13 +210,20 @@ private void parseLuaContext(PsiBuilder builder) {
PsiBuilder.Marker contextMarker = builder.mark();
int braceCount = 0;
boolean closingBraceFound = false;
builder.advanceLexer();

// for set_by_lua
while (builder.getTokenType() == NginxElementTypes.VALUE_WHITE_SPACE) {
builder.advanceLexer();
}

parseDirectiveValues(builder);

// If the next token is not a opening brace there will be an infinite loop.
while ((token = builder.getTokenType()) == NginxElementTypes.WHITE_SPACE) {
while (builder.getTokenType() == NginxElementTypes.WHITE_SPACE) {
builder.advanceLexer();
}

builder.advanceLexer();
token = builder.getTokenType();

if (token != NginxElementTypes.OPENING_BRACE) {
Expand Down
2 changes: 1 addition & 1 deletion src/net/ishchenko/idea/nginx/psi/NginxLuaContext.java
Expand Up @@ -2,5 +2,5 @@

import com.intellij.psi.PsiLanguageInjectionHost;

public interface NginxLuaContext extends NginxPsiElement, PsiLanguageInjectionHost {
public interface NginxLuaContext extends NginxPsiElement, NginxDirective, PsiLanguageInjectionHost {
}
Expand Up @@ -40,7 +40,7 @@
*/
public class NginxDirectiveImpl extends NginxElementImpl implements NginxDirective {

private static final TokenSet DIRECTIVE_VALUE_TOKENS = TokenSet.create(NginxElementTypes.COMPLEX_VALUE);
public static final TokenSet DIRECTIVE_VALUE_TOKENS = TokenSet.create(NginxElementTypes.COMPLEX_VALUE);

public NginxDirectiveImpl(ASTNode node) {
super(node);
Expand Down
62 changes: 62 additions & 0 deletions src/net/ishchenko/idea/nginx/psi/impl/NginxLuaContextImpl.java
Expand Up @@ -3,9 +3,20 @@
import com.intellij.lang.ASTNode;
import com.intellij.psi.LiteralTextEscaper;
import com.intellij.psi.PsiLanguageInjectionHost;
import net.ishchenko.idea.nginx.NginxKeywordsManager;
import net.ishchenko.idea.nginx.injection.LuaTextEscaper;
import net.ishchenko.idea.nginx.lexer.NginxElementTypes;
import net.ishchenko.idea.nginx.psi.NginxComplexValue;
import net.ishchenko.idea.nginx.psi.NginxContext;
import net.ishchenko.idea.nginx.psi.NginxDirectiveName;
import net.ishchenko.idea.nginx.psi.NginxLuaContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import static net.ishchenko.idea.nginx.psi.impl.NginxDirectiveImpl.DIRECTIVE_VALUE_TOKENS;

public class NginxLuaContextImpl extends NginxElementImpl implements NginxLuaContext {
public NginxLuaContextImpl(@NotNull ASTNode node) {
Expand All @@ -27,4 +38,55 @@ public PsiLanguageInjectionHost updateText(@NotNull String text) {
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper() {
return new LuaTextEscaper(this);
}

@NotNull
@Override
public String getNameString() {
return getText();
}

@NotNull
@Override
public NginxDirectiveName getDirectiveName() {
// TODO don't extend NginxDirectiveName
return (NginxDirectiveName) this;
}

@Nullable
@Override
public NginxContext getDirectiveContext() {
ASTNode contextNode = getNode().findChildByType(NginxElementTypes.CONTEXT);
return contextNode != null ? (NginxContext) contextNode.getPsi() : null;
}

@Nullable
@Override
public NginxContext getParentContext() {
ASTNode parentNode = getNode().getTreeParent();
if (parentNode.getPsi() instanceof NginxContext) {
return (NginxContext) parentNode.getPsi();
} else {
return null;
}
}

@NotNull
@Override
public List<NginxComplexValue> getValues() {
ArrayList<NginxComplexValue> result = new ArrayList<>();
for (ASTNode value : getNode().getChildren(DIRECTIVE_VALUE_TOKENS)) {
result.add((NginxComplexValue) value.getPsi());
}
return result;
}

@Override
public boolean isInChaosContext() {
return getParentContext() != null && NginxKeywordsManager.CHAOS_DIRECTIVES.contains(getParentContext().getDirective().getNameString());
}

@Override
public boolean hasContext() {
return getDirectiveContext() != null;
}
}

0 comments on commit 056598d

Please sign in to comment.