code: public static int getLineAtPosition( JTextComponent editor, int position )
    {
      if( position <= 0 )
      {
        return 1;
      }
  
      String s = editor.getText();
      if( position > s.length() )
      {
        position = s.length();
      }
  
      try
      {
        return GosuStringUtil.countMatches( editor.getText( 0, position ), "\n" ) + 1;
      }
      catch( BadLocationException e )
      {
        throw new RuntimeException( e );
      }
    }
docs: Gets the line at a given position in the editor