The first Greens MP to serve as an attorney-general anywhere in Australia has announced his retirement from politics after almost two decades.