package org.nextras.orm.intellij.parser;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocParser;
import com.jetbrains.php.lang.documentation.phpdoc.parser.tags.PhpDocTagParserRegistry;
import org.jetbrains.annotations.NotNull;


public class PhpParserDefinition extends com.jetbrains.php.lang.parser.PhpParserDefinition
{
	public PhpParserDefinition()
	{
		super();
		new PhpDocParser(); //initialize phpdoc parser registry
		PhpDocTagParserRegistry.register("@property", new PhpDocPropertyTagParser());
		PhpDocTagParserRegistry.register("@property-read", new PhpDocPropertyTagParser());
	}


	@NotNull
	@Override
	public PsiElement createElement(ASTNode node)
	{
		IElementType type = node.getElementType();
		if (type == PhpDocTypes.phpDocTagModifier) {
			return new PhpDocTagModifier(node);
		} else if (type == PhpDocTypes.phpDocTagModifierName) {
			return new PhpDocTagModifierName(node);
		} else if (type == PhpDocTypes.phpDocTagModifierIdentifier) {
			if (node.getTreeParent().getFirstChildNode().getTreeNext().getText().equals("container")) {
				return new PhpDocTagModifierClassType(node);
			} else {
				return new PhpDocTagModifierIdentifier(node);
			}
		}
		return super.createElement(node);
	}
}

@@ -350,16 +350,59 @@
 otNull;%0A
+import org.jetbrains.annotations.Nullable;%0A
 %0A%0Apublic
@@ -760,16 +760,422 @@
 );%0A%09%7D%0A%0A%0A
+%09@Nullable%0A%09private ASTNode getFirstArgument(ASTNode tagNode)%0A%09%7B%0A%09%09boolean tagNameFound = false;%0A%09%09ASTNode token = tagNode.getFirstChildNode();%0A%09%09token = token.getTreeNext(); // skip opening bracket%0A%09%09while (token != null) %7B%0A%09%09%09if (!token.getText().trim().equals(%22%22)) %7B%0A%09%09%09%09if (tagNameFound) %7B%0A%09%09%09%09%09return token;%0A%09%09%09%09%7D%0A%09%09%09%09tagNameFound = true;%0A%09%09%09%7D%0A%0A%09%09%09token = token.getTreeNext();%0A%09%09%7D%0A%09%09return null;%0A%09%7D%0A%0A%0A
 %09@NotNul
@@ -1540,16 +1540,92 @@
 %0A%09%09%09if (
+node.equals(getFirstArgument(node.getTreeParent()))) %7B%0A%09%09%09%09String tagName = 
 node.get
@@ -1681,16 +1681,40 @@
 tText().
+trim();%0A%09%09%09%09if (tagName.
 equals(%22
@@ -1728,24 +1728,25 @@
 er%22)) %7B%0A%09%09%09%09
+%09
 return new P
@@ -1785,18 +1785,180 @@
 %0A%09%09%09
+%09
 %7D else 
-%7B%0A%09
+if (3 %3C= tagName.length() && tagName.length() %3C= 4 && tagName.indexOf(':') == 1) %7B // m:n, 1:x, 1:1d %E2%80%A6%0A%09%09%09%09%09return new PhpDocTagModifierClassType(node);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%09r
@@ -2002,21 +2002,16 @@
 (node);%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%09re

add class type to relations