package com.karateca.jstoolbox.generatemethod;

import com.intellij.openapi.editor.Document;

public class ClassFinder {

  protected final Document document;

  public ClassFinder(Document document) {
    this.document = document;
  }

  protected String findNamespaceForCurrentFile() {
    String text = document.getText();

    // Find the @constructor and then go to the first function.
    int constructorOffset = text.indexOf("@constructor");
    if (constructorOffset < 0) {
      return null;
    }

    // Find the closing jsdoc.
    int closingJsDocOffset = text.indexOf("*/", constructorOffset);
    if (closingJsDocOffset < 0) {
      return null;
    }

    closingJsDocOffset += 2;

    // Get the string until the first "=".
    int indexOfEqual = text.indexOf("=", closingJsDocOffset);
    if (indexOfEqual < 0) {
      return null;
    }

    // And remove empty spaces.
    return text.substring(closingJsDocOffset, indexOfEqual)
        .replaceAll("\\s+", "");
  }
}

@@ -85,16 +85,60 @@
 ocument;
+%0Aimport com.intellij.openapi.util.TextRange;
 %0A%0Apublic
@@ -195,16 +195,53 @@
 ocument;
+%0A  private final String documentText;
 %0A%0A  publ
@@ -312,100 +312,82 @@
 ;%0A  
-%7D%0A%0A  protected String findNamespaceForCurrentFile() %7B%0A    String text = document.getText();%0A
+  documentText = document.getText();%0A  %7D%0A%0A  public String getClassName() %7B
 %0A   
@@ -467,33 +467,41 @@
 tructorOffset = 
-t
+documentT
 ext.indexOf(%22@co
@@ -632,17 +632,25 @@
 ffset = 
-t
+documentT
 ext.inde
@@ -835,17 +835,25 @@
 Equal = 
-t
+documentT
 ext.inde
@@ -979,17 +979,25 @@
  return 
-t
+documentT
 ext.subs
@@ -1072,11 +1072,761 @@
 %22%22);%0A  %7D
+%0A%0A  public String getParentClassName() %7B%0A    // Find @extends.%0A    int extendsOffset = documentText.indexOf(%22@extends%22);%0A    if (extendsOffset %3C 0) %7B%0A      return null;%0A    %7D%0A%0A    // Find the next %22*%22.%0A    int lineNumber = document.getLineNumber(extendsOffset);%0A    String line = document.getText(getTextRange(lineNumber));%0A%0A    // Remove the beginning of the line.%0A    line = line.replaceAll(%22%5B%5C%5Cs%5C%5C*%5D*@extends%5C%5Cs*%5C%5C%7B?%22, %22%22);%0A%0A    // Remove the end of the line.%0A    line = line.replaceAll(%22%5C%5C%7D?%5C%5Cs*$%22, %22%22);%0A%0A    return line;%0A  %7D%0A%0A  private TextRange getTextRange(int lineNumber) %7B%0A    int lineStart = document.getLineStartOffset(lineNumber);%0A    int lineEnd = document.getLineEndOffset(lineNumber);%0A%0A    return new TextRange(lineStart, lineEnd);%0A  %7D
 %0A%7D%0A

add methods to find class name and parent