package com.jantvrdik.intellij.latte.editor;

import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.ide.structureView.impl.common.PsiTreeElementBase;
import com.intellij.psi.PsiElement;
import com.jantvrdik.intellij.latte.psi.LatteFile;
import com.jantvrdik.intellij.latte.psi.LatteMacroClassic;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by matej21 on 28.3.16.
 */
public class LatteStructureViewTreeElement extends PsiTreeElementBase<PsiElement> {

	public LatteStructureViewTreeElement(PsiElement psiElement) {
		super(psiElement);
	}

	@NotNull
	@Override
	public Collection<StructureViewTreeElement> getChildrenBase() {
		Collection<StructureViewTreeElement> elements = new ArrayList<StructureViewTreeElement>();
		for (PsiElement el : getElement().getChildren()) {
			if (el instanceof LatteMacroClassic) {
				elements.add(new LatteStructureViewTreeElement(el));
			}
		}
		return elements;
	}

	@Nullable
	@Override
	public String getPresentableText() {
		if (getElement() instanceof LatteMacroClassic) {
			return ((LatteMacroClassic) getElement()).getOpenTag().getMacroName();
		} else if (getElement() instanceof LatteAutoClosedBlock) {
			return ((LatteAutoClosedBlock) getElement()).getMacroOpenTag().getMacroName();
		} else if (getElement() instanceof LatteFile) {
			return ((LatteFile) getElement()).getName();
		}
		return "";
	}
}

@@ -466,50 +466,72 @@
 on;%0A
-%0A/**%0A * Created by matej21 on 28.3.16.%0A */
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A
 %0Apub
@@ -1129,272 +1129,589 @@
 %7B%0A%09%09
-if (getElement() instanceof LatteMacroClassic) %7B%0A%09%09%09return ((LatteMacroClassic) getElement()).getOpenTag().getMacroName();%0A%09%09%7D else if (getElement() instanceof LatteAutoClosedBlock
+PsiElement element = getElement();%0A%09%09if (element instanceof LatteMacroClassic) %7B%0A%09%09%09LatteMacroClassic macroClassic = (LatteMacroClassic) element;%0A%09%09%09String presentableText = macroClassic.getOpenTag().getMacroName();%0A%09%09%09if (macroClassic.getOpenTag().getMacroContent() != null
 ) %7B%0A%09%09%09
+%09
-return ((LatteAutoClosedBlock) getElement()).getMacroOpenTag().getMacroName()
+String macroText = macroClassic.getOpenTag().getMacroContent().getText().trim();%0A%09%09%09%09Pattern pattern  = Pattern.compile(%22(%5B%5C%5CS%5D+).*%22);%0A%09%09%09%09Matcher matcher = pattern.matcher(macroText);%0A%09%09%09%09if (matcher.matches()) %7B%0A%09%09%09%09%09presentableText += %22 %22 + matcher.group(1);%0A%09%09%09%09%7D%0A%0A%09%09%09%7D%0A%09%09%09return presentableText
 ;%0A%09%09
@@ -1721,28 +1721,23 @@
 lse if (
-getE
+e
 lement
-()
  instanc
@@ -1776,28 +1776,23 @@
 teFile) 
-getE
+e
 lement
-()
 ).getNam

structure review: show a first word of a macro argument