Skip to content

Conversation

@Munksgaard
Copy link
Contributor

@Munksgaard Munksgaard commented Nov 12, 2025

Removing double newlines breaks formatting in e.g. the generated browser docs,
and breaks doctests, since they expect the example code sections to be separated
by the rest of the comments by double newlines.

Instead, we should focus on collapsing triple newlines, which can occur for
legitimate reasons, e.g. a section of the generated documentation doesn't return
any text, but which shouldn't be showed in the resulting documentation.

Fixes: #2433

I tried adding a regression test to code_interface_test.exs by adding the following, but it didn't work because Code.fetch_docs returned {:error, :module_not_found}:

  defmodule CalculationDocumentation do
    use Ash.Resource,
      data_layer: Ash.DataLayer.Ets,
      domain: Ash.Test.Domain

    attributes do
      attribute :foo, :string do
        public? true
      end
    end

    actions do
      defaults create: :*
    end

    calculations do
      calculate :foo_calculation, :boolean, expr(true) do
        description """
        Calculate a foo.

        This is hard to do.
        """
      end
    end

    code_interface do
      define_calculation :foo_calculation
    end
  end

  describe "documentation" do
    test "calculation code interface doc string doesn't strip double newlines" do
      generated_doc =
        Code.fetch_docs(CalculationDocumentation)
        |> dbg
        |> elem(6)
        |> Enum.find(&(elem(&1, 0) == {:function, :foo, 1}))
        |> elem(3)
        |> Map.fetch!("en")
        |> dbg

      assert String.contains?(generated_doc, """
             Calculate a foo.

             This is hard to do.
             """)
    end
  end

Any suggestions for a better way to test this would be greatly appreciated.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

Edit: Updated to reflect this comment: #2433 (comment)

end

@doc false
def trim_double_newlines(str) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't remove this right now because AshPhoenix is using it. Also I see a formatting error, is that from these changes or in main? Either way, will merge once this is added back in 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got to the same realization when I went to sleep last night, sorry about that!

The formatting changes are caused by my changes, though there was also a formatting error in actions/destroy/bulk.ex which I've fixed in a separate commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also my comment in ash-project/ash_phoenix#439 (comment)

@Munksgaard
Copy link
Contributor Author

Huh, the formatter in the CI behaves differently from my local install, with regards to that bulk destroy change. I'll remove that commit again.

Removing double newlines breaks formatting in e.g. the generated browser docs,
and breaks doctests, since they expect the example code sections to be separated
by the rest of the comments by double newlines.

Instead, we should focus on collapsing triple newlines, which can occur for
legitimate reasons, e.g. a section of the generated documentation doesn't return
any text, but which shouldn't be showed in the resulting documentation.

Fixes: ash-project#2433
@Munksgaard Munksgaard changed the title fix: Don't remove double newlines from generated docs fix: Trim triple newlines instead of double newlines Nov 15, 2025
@zachdaniel
Copy link
Contributor

Thanks for pointing out the issues here! I'm just going to update the trim_double_newlines function to trim triple newlines. Its not a public function (@doc false) so it really doesn't matter how accurate the name is 😄

@zachdaniel zachdaniel closed this Nov 15, 2025
@zachdaniel
Copy link
Contributor

This will then automatically resolve the issue in AshPhoenix as well 😄

@Munksgaard
Copy link
Contributor Author

Thanks!

@Munksgaard Munksgaard deleted the fix-2433 branch November 15, 2025 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code interface documentation trims double newlines

2 participants